开发者

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:

  1. webpage is loaded.
  2. all anchors on the web page are assigned attribute onmouseover.
  3. user points to a link.
  4. the event is triggered 开发者_如何学Cand the link's url is extracted and displayed on a alert box.


A few things:

  1. For a script inserted into a browser overlay, document is the document of the browser, not of a webpage, so you do want content.document.
  2. The function is getElementsByTagName. Use the error console -- it should tell you that getElementsByTag is not a function.
  3. You can also just use content.document.links
  4. 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.
  5. 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") ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜