How to get plain text of currently displayed text

I need to get text of currently displayed page from extension.

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                  .getService(Components.interfaces.nsIWindowMediator);
       var mainWindow = wm.getMostRecentWindow("navigator:browser");
       
alert(mainWindow.getBrowser().selectedBrowser.contentDocument.getSelection().toString()) 


This returns only selected text. I need to get all text content but has 
no glue how to do this.
Please help.


Alex
0
dzen
12/16/2008 9:15:49 PM
mozilla.dev.extensions 5694 articles. 0 followers. Post Follow

3 Replies
978 Views

Similar Articles

[PageSpeed] 17

On Dec 17, 5:15=A0am, dzen <dze...@inbox.ru> wrote:
> I need to get text of currently displayed page from extension.
>
> var wm =3D Components.classes["@mozilla.org/appshell/window-mediator;1"]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .getService(Components.interfaces.nsI=
WindowMediator);
> =A0 =A0 =A0 =A0var mainWindow =3D wm.getMostRecentWindow("navigator:brows=
er");
>
> alert(mainWindow.getBrowser().selectedBrowser.contentDocument.getSelectio=
n().toString())
>
> This returns only selected text. I need to get all text content but has
> no glue how to do this.
> Please help.
>
> Alex

mainWindow.getBrowser().selectedBrowser.contentDocument.innerHTML/
innerText ?

or, if you want the text in some element with id=3Deleid:

gBrowser.selectedTab.contentDocument.getElementById('eleid').innerHTML/
innerText
0
marffin
12/17/2008 1:25:55 AM
>> I need to get text of currently displayed page from extension.
>>
>> var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
>>                   .getService(Components.interfaces.nsIWindowMediator);
>>        var mainWindow = wm.getMostRecentWindow("navigator:browser");
>>
>> alert(mainWindow.getBrowser().selectedBrowser.contentDocument.getSelection().toString())
>>
>> This returns only selected text. I need to get all text content but has
>> no glue how to do this.
>> Please help.
>>
>> Alex
>>     
>
> mainWindow.getBrowser().selectedBrowser.contentDocument.innerHTML/
> innerText ?
>
> or, if you want the text in some element with id=eleid:
>
> gBrowser.selectedTab.contentDocument.getElementById('eleid').innerHTML/
> innerText
>   

 mainWindow.getBrowser().selectedBrowser.contentDocument.innerHTML

does not work 

mainWindow.getBrowser().selectedBrowser.contentDocument.innerText

In error console I see 

Warning: reference to undefined property mainWindow.getBrowser().selectedBrowser.contentDocument.innerText




0
dzen
12/17/2008 1:43:58 AM
On Dec 17, 9:43=A0am, dzen <dze...@inbox.ru> wrote:
> >> I need to get text of currently displayed page from extension.
>
> >> var wm =3D Components.classes["@mozilla.org/appshell/window-mediator;1=
"]
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .getService(Components.interfaces.=
nsIWindowMediator);
> >> =A0 =A0 =A0 =A0var mainWindow =3D wm.getMostRecentWindow("navigator:br=
owser");
>
> >> alert(mainWindow.getBrowser().selectedBrowser.contentDocument.getSelec=
tion().toString())
>
> >> This returns only selected text. I need to get all text content but ha=
s
> >> no glue how to do this.
> >> Please help.
>
> >> Alex
>
> > mainWindow.getBrowser().selectedBrowser.contentDocument.innerHTML/
> > innerText ?
>
> > or, if you want the text in some element with id=3Deleid:
>
> > gBrowser.selectedTab.contentDocument.getElementById('eleid').innerHTML/
> > innerText
>
> =A0mainWindow.getBrowser().selectedBrowser.contentDocument.innerHTML
>
> does not work
>
> mainWindow.getBrowser().selectedBrowser.contentDocument.innerText
>
> In error console I see
>
> Warning: reference to undefined property mainWindow.getBrowser().selected=
Browser.contentDocument.innerText

no idea why it doesn't work

I think there is a simpler way to get the tabbrowser element of the
firefox interface: just use 'gBrowser', check out the code snippet
section of MDC
0
marffin
12/17/2008 3:57:32 AM
Reply: