views:

779

answers:

2

So..got scriptable extention for firefox. it's somelika a webspider, written in javascript. what i want to do:

i want it load a page, them do some job, then go to another page (using an url from the loaded page). After the new page is loaded - the spider do the same job.

Algoritm is somelike this one:

  1. wait till the page is loaded
  2. do some job
  3. choose a different url
  4. go to this url
  5. goto 1.

in my main function i do this code:

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

and everything works fine till i go to another page...how can i reuse DOMContentLoaded event in my firefox extension? So, the question is: is it possible to reuse load/DOMContentLoaded event in firefox extention for different pages? if yes then how?

p.s.\earlier i was solving this prob,em with windows forms and webbrowser + c++...ooh, what the time it was...a dream! cause everything worked fine=)

+2  A: 

You should only have to register for the event once, and you should receive it every time a browser tab fires it.

Your code should work just fine, provided you are waiting for the XUL window to fire its load event before adding an event listener to the gBrowser element. If you are adding an event listener before that, gBrowser is not yet initialized, 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);

Note that in practice I can only be sure this works with Firefox 3.5 and up. I wrote an extension (internal to my company) that does this sort of thing reliably for Firefox 1.5 through 3.5, but it is unfortunately a bit more complicated (and fortunately) much more robust than the solution above. If you need support for more than Firefox 3.5+, let me know and I'll provide more info.

Here is some additional reading:

Mike
A: 

yeh,thanx. i've discovered this. but we got some more problems.cause DOMContentLoaded fires more than ones on some pages, so we found maybe not the best way to solve the problem, but we've solved it=)

lazybob
You should post the answer then, please
Dmitri Farkov