[Project_owners] Cancelling Web page selection

Neil neil at parkwaycc.co.uk
Sat Dec 10 19:29:06 EST 2005


Alex Eng wrote:

> Neil wrote:
>
>> Alex Eng wrote:
>>
>>> What I want to do is extract all text inside a rich edit box (i.e., 
>>> an HTML document inside an <iframe> with designMode = "on"), while 
>>> still keeping line breaks.
>>>
>>> There are other approaches I explored, but they didn't work out:
>>> - Using the DOM Range methods.  Calling rangeObject.toString() 
>>> retrieves all the text, but line breaks are lost and the extracted 
>>> plain text is a garbled mess.
>>
>> Well, this javascript bookmarklet worked for me on a random web page:
>> javascript: var range = document.createRange(); 
>> range.selectNode(document.body); alert(range.toString());
>
> That gives you back text with line breaks from the original HTML 
> source, not the line breaks the user sees when the HTML is rendered in 
> the browser.  Try doing a Select All on a Web page, then execute the 
> following:
>
>   javascript:alert(window.getSelection());
>
> Compare that with the output from your bookmarklet code.  The result 
> from window.getSelection() is what I want.

I'm sorry, I see now that my code is the same as 
document.body.textContent whereas what you want is the same effect as 
saving the page as text. In that case, you need to get hold of the 
nsIEditor object for the rich editor, you can then call its 
outputToString method. This is easier if you can use an <editor> rather 
than an <iframe>, but I think it should be possible for an <iframe> too.


More information about the Project_owners mailing list