Firefox extension. How to catch onload event?

So..this script extension for Firefox. this is somelika webspider written in javascript. what i want to do:

I want it to load the page, they do some work, then go to another page (using the URL from the loaded page). After loading a new page, the spider does the same job.

The algorithm is similar to this:

  • wait for the page to load
  • do some work
  • choose another URL
  • go to this url
  • go over 1.

in my main function, I am doing this code:

gBrowser.addEventListener("DOMContentLoaded",haXahv8, false); 

and everything works fine until I go to another page ... how can I reuse the DOMContentLoaded event in my Firefox extension? So the question is: is it possible to reuse the load / DOMContentLoaded event in the Firefox extension for different pages? if so, how?

ps \ I used to solve this problem, em with window forms and webbrowser + C ++ ... oh, what time it was ... a dream! because everything works fine =)

+6
javascript firefox
source share
2 answers

You only need to register for the event once, and you should receive it every time the browser tab lights up.

Your code should work fine if you expect the XUL window to fire its load event before adding an event listener to the gBrowser element. If you add an event listener before this, gBrowser is not initialized yet, and the behavior is undefined.

Here is an example:

 window.addEventListener('load', function () { gBrowser.addEventListener('DOMContentLoaded', function () { // stuff that happens for each web page load goes here }, false); }, false); 

Please note that in practice, I can only make sure that this works with Firefox 3.5 and higher. I wrote an extension (internal for my company) that reliably does such things for Firefox from 1.5 to 3.5, but, unfortunately, is a bit more complicated (and, fortunately) much more reliable than the solution above. If you need support more than Firefox 3.5+, let me know and I will provide more information.

Here are some additional indications:

+11
source share

The last parameter of the addEventListener () method, which in your case is false, should be set to true, this tells the browser to start the event handler every time an event occurs.

The current false value only indicates that the handler will be launched on the first event.

I hope I take care of some of you a bit, it took me quite a while to look through the event pages of the document object model on W3C.org!

+2
source share

All Articles