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

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

问题描述

我正在编写一个带有弹出窗口和背景页面的 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" 不会 为浏览器操作弹出窗口触发.相信我,我试过了.但是,卸载"应该正确触发,所以请注意.

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

实际上,我有一个类似的系统,它允许您使用 addEventListener 附加事件,并在卸载事件触发时自动清理它们.

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

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

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

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

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