Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

javascript - firefox extension. How to catch onload event?

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=)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...