onbeforeunload 事件会触发 google chrome 扩展中的 popup.html 吗?

发布于 2024-08-22 18:11:43 字数 146 浏览 12 评论 0原文

我正在编写一个带有弹出窗口和背景页面的谷歌浏览器扩展。弹出窗口订阅后台生成的某些事件,我想在弹出窗口消失时取消订阅这些事件。但是,我没有看到弹出窗口生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

作业与我同在 2024-08-29 18:11:43

浏览器操作弹出窗口不会触发“beforeunload”。相信我,我试过了。然而,“unload”应该被正确触发,所以请听听。

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

这里有一个小提示:显然你不能使用console.log,因为当unload事件触发时,已经太晚了。但是,您可以这样做:

var background = chrome.extension.getBackgroundPage();

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

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

"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:

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.

他是夢罘是命 2024-08-29 18:11:43

我也遇到过类似的问题。
就我而言,我已将焦点设置在弹出页面中的元素上。
因此,当弹出窗口关闭时,元素失去焦点,我可以捕获模糊事件。

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.

纸伞微斜 2024-08-29 18:11:43

如果您已在扩展程序中转而使用事件页面而不是背景页面,推荐的方式访问它的方法是使用一个函数调用 chrome.runtime.getBackgroundPage() ,该函数会通过后台页面引用进行回调。

但是,如果您尝试在 onbeforeunload 处理程序中获取后台页面,如下所示:

document.addEventListener("beforeunload", function() {
    chrome.runtime.getBackgroundPage(function(backgroundPage) {
        backgroundPage.log("unloading");
    });
});

回调似乎不会在页面卸载之前触发,因此 log()从未拨打过电话。

在上面的评论中,Pauan 建议使用 chrome.extension.getBackgroundPage() 来获取页面。我可以让它工作的唯一方法是不使用 addEventListener() 创建处理程序。相反,我不得不走老路,直接在 window 上设置处理程序:

window.onbeforeunload = function() {
    chrome.extension.getBackgroundPage().log("unloading");
};

这最终到达了后台页面。

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:

document.addEventListener("beforeunload", function() {
    chrome.runtime.getBackgroundPage(function(backgroundPage) {
        backgroundPage.log("unloading");
    });
});

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 with addEventListener(). Instead I had to go old school and set the handler directly on window:

window.onbeforeunload = function() {
    chrome.extension.getBackgroundPage().log("unloading");
};

That finally got through to the background page.

墨落画卷 2024-08-29 18:11:43

使用消息传递 API 在弹出页面和后台页面之间设置端口。当弹出窗口消失时,您应该在后台页面中收到一个 onDisconnect 事件。

// popup.js
chrome.runtime.connect({ name: "popup" });
// background.js
chrome.runtime.onConnect.addListener(function (port) {
    if (port.name === "popup") {
        port.onDisconnect.addListener(function () {
        console.log("popup has been closed");
    });
  }
});

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.

// popup.js
chrome.runtime.connect({ name: "popup" });
// background.js
chrome.runtime.onConnect.addListener(function (port) {
    if (port.name === "popup") {
        port.onDisconnect.addListener(function () {
        console.log("popup has been closed");
    });
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文