Firefox扩展。如何捕获onload事件? [英] firefox extension. How to catch onload event?

查看:140
本文介绍了Firefox扩展。如何捕获onload事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,为Firefox提供可脚本化的扩展。
它是一个用JavaScript编写的webspider。
我想要做什么:

我想要加载一个页面,他们做一些工作,然后转到另一个页面(使用从加载页面)。在新的页面加载后,蜘蛛做同样的工作。



Algoritm有点像这个:


  1. 等到页面加载完毕

  2. 做一些工作

  3. 选择一个不同的url

  4. 转至此网址

  5. 转到1.



  6. 在我的主要功能,我做这个代码:

    pre $ gBrowser.addEventListener(DOMContentLoaded,haXahv8,false);

    一切工作正常,直到我去另一个页面...我如何重用DOMContentLoaded事件在我的Firefox扩展?
    所以,问题是:
    是否可以在firefox扩展中为不同页面重新使用load / DOMContentLoaded事件?如果是的话,怎么样?

    ps \earlier我是解决这个问题,与Windows窗体和Web浏览器+ C ++的时间...哦,现在是什么时间.. 。一个梦!导致一切工作正常=)

    解决方案

    您应该只需注册一次的事件,你应该每次收到它浏览器选项卡会触发它。



    如果您正在等待XUL窗口触发 load

    以下是一个示例:

      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:

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

    解决方案

    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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆