Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

selecting all text

43 views
Skip to first unread message

Tom

unread,
Oct 8, 2012, 4:24:53 PM10/8/12
to
Hi All,

Is there a simple way to select all text on the content document? For
example...the same as if I would issue the hot key CTRL+A.

What I am trying to do is save all text on the page.

Any suggestions would be appreciated.

Thanks,
Tom


Neil

unread,
Oct 9, 2012, 4:36:17 AM10/9/12
to
Tom wrote:

>What I am trying to do is save all text on the page.
>
>
Save in a variable? In that case, you have a number of options:

1. Extract the textContent of the document's body.
2. Create a range, select the document's body, and convert to string.
3. Select all text and convert the selection to string.
4. Create a document serialiser and convert the document to text.

Since you asked, for 3, you could use
content.getSelection().selectAllChildren(content.document.body); to
select all.

--
Warning: May contain traces of nuts.

Tom

unread,
Oct 9, 2012, 9:55:14 AM10/9/12
to

"Neil" <ne...@parkwaycc.co.uk> wrote in message
news:iYidnRTaV9uff-7N...@mozilla.org...
Thanks Neil! We will check that out. Will this include all IFrames?


Neil

unread,
Oct 9, 2012, 11:08:02 AM10/9/12
to
Tom wrote:

>Neil wrote:
>
>
>>Tom wrote:
>>
>>>What I am trying to do is save all text on the page.
>>>
>>Save in a variable? In that case, you have a number of options:
>>
>> 1. Extract the textContent of the document's body.
>> 2. Create a range, select the document's body, and convert to string.
>> 3. Select all text and convert the selection to string.
>> 4. Create a document serialiser and convert the document to text.
>>
>>Since you asked, for 3, you could use content.getSelection().selectAllChildren(content.document.body); to select all.
>>
>>
>Thanks Neil! We will check that out. Will this include all IFrames?
>
No DOM methods* include (i)frames. In fact, I don't know any way of
serialising the content of an iframe within the appropriate point in its
parent document.

*window.frames doesn't count as a DOM method.

Tom

unread,
Oct 10, 2012, 12:46:58 PM10/10/12
to

"Neil" <ne...@parkwaycc.co.uk> wrote in message
news:pKudnVTop7FPoOnN...@mozilla.org...
Hi Neil,

I have a bit of an issue yet. I am selecting all of the text in the document
and creating a range. However when we get the text it contains CSS and
SCRIPT nodes. I am interested in strictly the readable text....something
like doing a ctrl+a followed by a ctrl+c.

Here is my code. Is there a way to select the readable/viewable text only?

var contentDoc = gBrowser.selectedBrowser.contentDocument;
contentDoc.getSelection().selectAllChildren(contentDoc);
var win = gBrowser.getBrowserForDocument(contentDoc).contentWindow;
var selObj = win.getSelection();
var range = selObj.getRangeAt(0);
var docText = range.toString();

I am thinking that I will have to iterate through the selection and remove
nodes like STYLE and SCRIPT.

I also looked at copy the selection to the clipboard, but I could not find a
method that would copy the current user selected text.

Any thoughts?

Thanks,
Tom


Neil

unread,
Oct 11, 2012, 5:04:44 AM10/11/12
to
Tom wrote:

>Is there a way to select the readable/viewable text only?
>
>
Have you tried selObj.toString() ?

>var contentDoc = gBrowser.selectedBrowser.contentDocument;
>contentDoc.getSelection().selectAllChildren(contentDoc);
>var win = gBrowser.getBrowserForDocument(contentDoc).contentWindow;
>var selObj = win.getSelection();
>
>
Note that (these days, at least) contentDoc.getSelection() is the same
as contentWindow.getSelection().

Tom

unread,
Oct 11, 2012, 12:55:23 PM10/11/12
to

"Neil" <ne...@parkwaycc.co.uk> wrote in message
news:bLedndMReOMwFuvN...@mozilla.org...
Hi Neil,

Unfortionately that gives me an error. See below


Tom

unread,
Oct 11, 2012, 12:56:42 PM10/11/12
to

"Tom" <t...@gigasoftdevelopment.com> wrote in message
news:Z_GdnWpWDfkdZ-vN...@mozilla.org...
Sorry fat fingered the last message. :-)

Hi Neil,

Unfortionately that gives me an error. See below

var contentDoc = gBrowser.selectedBrowser.contentDocument;
contentDoc.getSelection().selectAllChildren(contentDoc);
var win = gBrowser.getBrowserForDocument(contentDoc).contentWindow;
var selObj = win.getSelection();
alert(selObj.toString()) <<< ERRROR

extension: getTextFromDoc: [Exception... "Component returned failure code:
0x80004005 (NS_ERROR_FAILURE) [nsISelection.toString]" nsresult:
"0x80004005 (NS_ERROR_FAILURE)" location: "JS frame ::
chrome://extension/content/tb.js :: <TOP_LEVEL> :: line 3868" data: no]
Error line: 3868



Neil

unread,
Oct 12, 2012, 5:54:47 AM10/12/12
to
Tom wrote:

>var contentDoc = gBrowser.selectedBrowser.contentDocument;
>contentDoc.getSelection().selectAllChildren(contentDoc);
>var win = gBrowser.getBrowserForDocument(contentDoc).contentWindow;
>var selObj = win.getSelection();
>alert(selObj.toString()) <<< ERRROR
>
>
Try

var contentDoc = gBrowser.selectedBrowser.contentDocument;
contentDoc.getSelection().selectAllChildren(contentDoc.body);
alert(contentDoc.getSelection().toString());

Tom

unread,
Oct 12, 2012, 2:36:53 PM10/12/12
to

"Neil" <ne...@parkwaycc.co.uk> wrote in message
news:gvmdne9GTLx1derN...@mozilla.org...
Okay that worked,

Thanks for your help Neil.


0 new messages