onbforeunload事件是否触发google.hrome扩展中的popup.html? [英] Does onbeforeunload event trigger for popup.html in a google chrome extension?

查看:174
本文介绍了onbforeunload事件是否触发google.hrome扩展中的popup.html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有弹出窗口和背景页面的Google Chrome扩展程序。弹出窗口订阅了背景生成的某些事件,并且我想在弹出窗口消失时取消订阅这些事件。但是,我没有看到从弹出窗口生成onbeforeunload或onunload事件。这些事件是否被解雇?如果没有,关于如何捕捉弹出关闭的任何想法?

I'm writing a google chrome extension with a popup and a background page. The popup subscribes to certain events that the background generates, and I would like to unsubscribe from those events when the popup goes away. However, I don't see either of onbeforeunload or onunload events being generated from the popup. Are these events fired? If not, any ideas on how to capture popup close?

推荐答案

beforeunload not 用于浏览器动作弹出窗口。相信我,我试过了。然后,unload应该被正确触发,所以请仔细阅读。

"beforeunload" is not fired for browser action popups. Believe me, I tried. "unload" should be fired correctly, however, so listen for that.

实际上,我有一个类似的系统,可以使用addEventListener附加事件,它们会在unload事件触发时被自动清理。

Actually, I have a similar system in place, that allows you to attach events using addEventListener, and they are automagically cleaned up when the unload event fires.

这里有一点小技巧:显然你不能使用 console.log ,因为在卸载事件触发时,已经太晚了。但是,您可以这样做:

Here's a little tip: obviously you can't use console.log, because by the time the unload event fires, it's already too late. But, you can do this:

var background = chrome.extension.getBackgroundPage();

addEventListener("unload", function (event) {
    background.console.log(event.type);
}, true);

通过使用上面的代码,你可以打开后台页面的控制台,它应该显示卸载事件被正确触发。

By using the above code, you can open up the background page's console, which should show that the unload event is fired correctly.

这篇关于onbforeunload事件是否触发google.hrome扩展中的popup.html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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