Firefox Extension link extractor
I want to create an extension in Firefox that extracts links embedded on a web page from users moving the mouse on the link. This would be very similar to link target display on the status bar at the bottom of the browser.
The problem I am having is getting access to document object of the loaded webpage. I tried document.getElementsByTag("a")
it didn't work. I tried content.document.getElementByTag("a")
it didn't work.
Ideally I want it to behave like this:
- webpage is loaded.
- all anchors on the web page are assigned attribute onmouseover.
- user points to a link.
- the event is triggered 开发者_如何学Cand the link's url is extracted and displayed on a alert box.
A few things:
- For a script inserted into a browser overlay,
document
is the document of the browser, not of a webpage, so you do wantcontent.document
. - The function is
getElementsByTagName
. Use the error console -- it should tell you thatgetElementsByTag
is not a function. - You can also just use
content.document.links
- It might be simpler to just add an event listener to the entire document when it loads, and then in the event listener, just do
if (event.target instanceof HTMLAnchorElement)
or something to see if it's a link. - Either way you do it, you should probably remove the event listeners when you're done with them (i.e. on the page's
unload
event).
Read this tutorial. Don't forget to change maxVersion in install.rdf (in LinkTargetFinder.xpi)
In linkTargetFinder.js in function run() you can see how to find links:
... allLinks = content.document.getElementsByTagName("a") ...
精彩评论