简体   繁体   English

如何从XUL访问窗口对象?

[英]How to access window object from XUL?

I'm trying to set an onLoad event to the current web page from a firefox extension. 我正在尝试从firefox扩展onLoad事件设置为当前网页。 I'm using the gBrowser object but I'm not sure if this is the best way. 我正在使用gBrowser对象,但不确定是否这是最好的方法。 I would like to set an onLoad event to the web page window to execute some actions of the plugin as soon as the page is loaded. 我想在网页窗口上设置一个onLoad事件,以在页面加载后立即执行插件的某些操作。

Thanks in advance. 提前致谢。

After several attempts I found a solution for this. 经过几次尝试,我找到了解决方案。 Lets say we have a function called pageLoaded , which is the one we want to call when the website has been totally loaded. 可以说我们有一个名为pageLoadedfunction ,当网站完全加载后,我们要调用该function Now, we have to add an event listener to the gBrowser object to catch the load event. 现在,我们必须向gBrowser对象添加一个事件侦听器以捕获load事件。

Here the code: 这里的代码:

function pageLoaded(){
    alert("the page has been loaded")
}

gBrowser.addEventListener("load", pageLoaded, true);

I recommend to use add an event listener to the extension document before to add it to the gBrowser. 我建议先使用事件监听器添加到扩展文档中,然后再将其添加到gBrowser中。 The result would be something like this: 结果将是这样的:

window.addEventListener("load", function () {
    gBrowser.addEventListener("load", pageLoaded, true);
}, false);

I hope this solution will be useful for someone. 我希望此解决方案对某人有用。

Thanks for reading. 谢谢阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM