Firefox扩展。如何捕获onload事件? [英] firefox extension. How to catch onload event?
问题描述
它是一个用JavaScript编写的webspider。
我想要做什么:
我想要加载一个页面,他们做一些工作,然后转到另一个页面(使用从加载页面)。在新的页面加载后,蜘蛛做同样的工作。
Algoritm有点像这个:
- 等到页面加载完毕
- 做一些工作
- 选择一个不同的url
- 转至此网址
- 转到1.
- https://developer.mozilla .org / en / Code_snippets / On_page_load
- https://developer.mozilla.org/en/Code_snippets/Tabbed_browser
- https://developer.mozilla.org/zh/Listening_to_events_on_all_tabs
- wait till the page is loaded
- do some job
- choose a different url
- go to this url
- goto 1.
- https://developer.mozilla.org/en/Code_snippets/On_page_load
- https://developer.mozilla.org/en/Code_snippets/Tabbed_browser
- https://developer.mozilla.org/en/Listening_to_events_on_all_tabs
在我的主要功能,我做这个代码:
pre $ gBrowser.addEventListener(DOMContentLoaded,haXahv8,false);
一切工作正常,直到我去另一个页面...我如何重用DOMContentLoaded事件在我的Firefox扩展?
所以,问题是:
是否可以在firefox扩展中为不同页面重新使用load / DOMContentLoaded事件?如果是的话,怎么样?
ps \earlier我是解决这个问题,与Windows窗体和Web浏览器+ C ++的时间...哦,现在是什么时间.. 。一个梦!导致一切工作正常=)
您应该只需注册一次的事件,你应该每次收到它浏览器选项卡会触发它。
如果您正在等待XUL窗口触发 load $ c $在将事件侦听器添加到gBrowser元素之前,如果您在此之前添加事件侦听器,gBrowser尚未初始化,并且行为未定义。
以下是一个示例:
window.addEventListener('load',function(){
gBrowser.addEventListener('DOMContentLoaded',function(){
//每个网页加载发生的事情都在这里
},false);
},false);
请注意,在实践中,我只能确定这适用于Firefox 3.5及更高版本。我写了一个扩展(在我的公司内部)为Firefox 1.5至3.5可靠地执行此类事情,但不幸的是,比上述解决方案更复杂一点(幸运的是)更加复杂。如果您需要Firefox 3.5以上的支持,请告诉我,我会提供更多信息。
以下是一些额外的阅读:
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:
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=)
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:
这篇关于Firefox扩展。如何捕获onload事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!