onbeforeunload 事件会触发 google chrome 扩展中的 popup.html 吗?
我正在编写一个带有弹出窗口和背景页面的谷歌浏览器扩展。弹出窗口订阅后台生成的某些事件,我想在弹出窗口消失时取消订阅这些事件。但是,我没有看到弹出窗口生成 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
浏览器操作弹出窗口不会触发“beforeunload”。相信我,我试过了。然而,“unload”应该被正确触发,所以请听听。
实际上,我有一个类似的系统,它允许您使用 addEventListener 附加事件,并且当卸载事件触发时它们会自动清除。
这里有一个小提示:显然你不能使用console.log,因为当unload事件触发时,已经太晚了。但是,您可以这样做:
通过使用上面的代码,您可以打开后台页面的控制台,这应该显示卸载事件已正确触发。
"beforeunload" is not fired for browser action popups. Believe me, I tried. "unload" should be fired correctly, however, so listen for that.
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.
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:By using the above code, you can open up the background page's console, which should show that the unload event is fired correctly.
我也遇到过类似的问题。
就我而言,我已将焦点设置在弹出页面中的元素上。
因此,当弹出窗口关闭时,元素失去焦点,我可以捕获模糊事件。
I've had a kind of similar issue.
In my case, I've set the focus on an element which was in the popup page.
So when the popup is closed, the element lose the focus and I can catch the blur event.
如果您已在扩展程序中转而使用事件页面而不是背景页面,推荐的方式访问它的方法是使用一个函数调用 chrome.runtime.getBackgroundPage() ,该函数会通过后台页面引用进行回调。
但是,如果您尝试在
onbeforeunload
处理程序中获取后台页面,如下所示:回调似乎不会在页面卸载之前触发,因此
log()
从未拨打过电话。在上面的评论中,Pauan 建议使用 chrome.extension.getBackgroundPage() 来获取页面。我可以让它工作的唯一方法是不使用
addEventListener()
创建处理程序。相反,我不得不走老路,直接在window
上设置处理程序:这最终到达了后台页面。
If you've switched to using event pages instead of background pages in your extension, the recommended way of getting access to it is calling
chrome.runtime.getBackgroundPage()
with a function that gets called back with the background page reference.However, if you try to get the background page in an
onbeforeunload
handler, like this:The callback doesn't seem to fire before the page unloads, so that
log()
call is never made.In a comment above, Pauan suggested using
chrome.extension.getBackgroundPage()
to get the page. The only way I could get that to work was not to create the handler withaddEventListener()
. Instead I had to go old school and set the handler directly onwindow
:That finally got through to the background page.
使用消息传递 API 在弹出页面和后台页面之间设置端口。当弹出窗口消失时,您应该在后台页面中收到一个 onDisconnect 事件。
Use the messaging APIs to set up a Port between the popup and background page. You then should get a onDisconnect event in the background page when the popup goes away.