/    Sign up×
Community /Pin to ProfileBookmark

Newbie: Getting displayed text from Firefox toolbar

Newbie question:

I’m trying to get the web page displayed contents (or all contents) from a toolbar I’ve made for Firefox.

Basic functionality I want is that when I click the toolbar button, my script ‘reads’ the page the user currently sees.

So I want to get the page contents into a Text variable so I can parse that.

I’ve tried:

document.getElementsByTagName(“html”)[0].innerHTML;
and also
document.createElement(“undefinedNode”).appendchild(document.documentElement.cloneNode(true)).parentNode.innerHTML
but I just can’t seem to get the text.

Any ideas?

Many thanks,

Pauwl

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisJan 09.2008 — If the page you are trying to "read" is not on the same domain as the "toolbar", browser security features will block it from doing so.
Copy linkTweet thisAlerts:
@Pauwl_LunowauthorJan 09.2008 — thanks. I don't quite understand though, but let me explain what I'm do that does work.

If I try:

alert(document.title);

it correctly gives me the title of the page I'm looking for. So I think this means the document object exists and is OK.

I've tried a HTML page launched form my desktop - page title = OK, but I can't seem to get the body text by any means.

?
Copy linkTweet thisAlerts:
@gil_davisJan 10.2008 — Post some code
Copy linkTweet thisAlerts:
@Pauwl_LunowauthorJan 10.2008 — This is what I've tried. None work.

// now parse the web content if we can...
// if we use : document.title, it's clear that firefox can see the corect document (try alert(document.title) and it will display
// the title of the document correctly.

// the following things DONT work
// mHTML = document.getElementsByTagName("html")[0].innerHTML;
// alert(window.document.createElement("undefinedNode").appendchild(window.document.documentElement.cloneNode(true)).parentNode.innerHTML);
// document.body.innerHTML
// document.getElementsByTagName("html")[0].text
// document.getElementsByTagName("body")[0].text

// The latest attempt:
alert(document.anchors[0].innerHTML);
Copy linkTweet thisAlerts:
@gil_davisJan 10.2008 — It looks like you are on a fishing expedition.

Is the "toolbar" a separate window? If you are trying to get information from a different window or frame, you need to add a pointer to the window in front of "document".
Copy linkTweet thisAlerts:
@Pauwl_LunowauthorJan 10.2008 — fishing expidition :-)

I've made a toolbar in Firefox, so yes, it's separate from the page itself.

If I use :

alert(document.title);

I do get the correct title of the browser when that page is displayed.

I guess I'm presuming "document" points to the current open page.

I've tried: window.document...etc. as well, but not joy either. Am I mis-understanding.

Many thanks in any case for advice up til now.
Copy linkTweet thisAlerts:
@gil_davisJan 10.2008 — I am not familiar enough with FF. Is the toolbar a special thing that FF invented?
Copy linkTweet thisAlerts:
@Pauwl_LunowauthorJan 11.2008 — I think you're previous post about a pointer to the window is where the answer lies.

If I make a seperate page:

<html>

<body>

HEllo there !!!

</body>

<script type="text/javascript">

alert(document.body.innerHTML);

</script>

</html>


Then it gives an alert box with the following content:
------------------


HEllo there !!!

<script type="text/javascript">

alert(document.body.innerHTML);

</script>
-----------------------


(curious, I thought BODY would point to only the body text, but it sees the rest is shown in any case - but this is fine for my purposes).

If I use the script in the toolbar, I get the error UNDEFINED

How can I find a pointer to the actual window?

thanks again.
Copy linkTweet thisAlerts:
@gil_davisJan 11.2008 — Name the window. If you name the window "slave" then you can create a link by using:
<i>
</i>var slave = window.top["slave"];
alert(slave.document.getElementsByTagName("body")[0].innerHTML);
Copy linkTweet thisAlerts:
@Pauwl_LunowauthorJan 11.2008 — Thanks a lot, will have a go.

Is there a simple way to 'cycle' through the windows though?
Copy linkTweet thisAlerts:
@gil_davisJan 11.2008 — If you don't know its name, I don't think you are allowed to access it.

There are some names that are always valid:
<i>
</i>window.top
window.parent
window.opener
Copy linkTweet thisAlerts:
@Pauwl_LunowauthorJan 11.2008 — Gil,

thanks for all the pointers. Turned out the code I need was:

alert(window._content.document.body.innerHTML);

"window" was clearly my toolbar (i.e. FireFox itself).

_content gets details from the dispalyed current page.

You people here rock!

thanks,

Pauwl
×

Success!

Help @Pauwl_Lunow spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...