Javascript Google Chrome 扩展程序的 onBrowserClose 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3390470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:25:12  来源:igfitidea点击:

Event onBrowserClose for Google Chrome Extension?

javascriptgoogle-chromegoogle-chrome-extension

提问by simple

I am developing an extension for Google Chrome. My background script, everytime, authorizes on a server that uses the XMPP API, and subscribes for a PubSub node. I need to unsubscribe on the exit, otherwise the dummy subscriptions will remain on the server. Is There any onBrowserCloseevent in the Google Chrome Extension APIs?

我正在为 Google Chrome 开发一个扩展程序。我的后台脚本每次都在使用 XMPP API 的服务器上授权,并订阅 PubSub 节点。我需要在退出时取消订阅,否则虚拟订阅将保留在服务器上。onBrowserCloseGoogle Chrome 扩展 API 中是否有任何事件?

采纳答案by aolde

There is no such event in the Chrome Extension API.

Chrome 扩展 API 中没有此类事件。

There is however a chrome.windows.onRemovedevent that fires each time a window closes. I figured you could check in this event if you closed the last window, but unfortunately due to the asynchronous nature of Chrome this doesn't work.

然而chrome.windows.onRemoved,每次窗口关闭时都会触发一个事件。我想如果您关闭最后一个窗口,您可以检查此事件,但不幸的是,由于 Chrome 的异步性质,这不起作用。

What I tried was running a simple AJAX request in the onRemovedevent handler. The AJAX request never got to the server, as Chrome had already closed before running the event (or just disregarded it).

我尝试在onRemoved事件处理程序中运行一个简单的 AJAX 请求。AJAX 请求从未到达服务器,因为 Chrome 在运行事件之前已经关闭(或者只是忽略它)。

Making the final answer be: No, currently you can't, as far as I know. You might want to star the following bug report at http://crbug.com/30885to get noticed on updates.

最后的答案是:不,目前你不能,据我所知。您可能希望在http://crbug.com/30885 上为以下错误报告加星标,以便在更新时引起注意。

回答by hondaman

If you catch the case when the number of open tabs is 0, you can treat that as a Chrome onClose event. In my case, I have to cancel a desktop notification before Chrome closes because it was crashing otherwise. This is how I did it:

如果您发现打开的标签页数为 0 的情况,您可以将其视为 Chrome onClose 事件。就我而言,我必须在 Chrome 关闭之前取消桌面通知,否则它会崩溃。我是这样做的:

1. Initialize a variable num_tabs by using the following:1. 使用以下命令初始化变量 num_tabs:
chrome.tabs.getAllInWindow( null, function( tabs ){
    console.log("Initial tab count: " + tabs.length);
    num_tabs = tabs.length;
});
2. Increment num_tabs when a tab is created:2. 创建选项卡时增加 num_tabs:
chrome.tabs.onCreated.addListener(function(tab){
    num_tabs++;
    console.log("Tab created event caught. Open tabs #: " + num_tabs);
});
3. Decrement num_tabs when a tab is removed and run your browser onclose event handler if num_tabs = 03. 删除选项卡时减少 num_tabs 并在 num_tabs = 0 时运行浏览器 onclose 事件处理程序
chrome.tabs.onRemoved.addListener(function(tabId){
    num_tabs--;
    console.log("Tab removed event caught. Open tabs #: " + num_tabs);
    if( num_tabs == 0 )
        notification.cancel();
});

回答by Shabeer V P K

This one works for me:

这个对我有用:

chrome.windows.onRemoved.addListener(function(windowId){
  alert("!! Exiting the Browser !!");
});

It takes chrome.windowsrather than chrome.tabs.

它需要chrome.windows而不是chrome.tabs

回答by oldestlivingboy

Adding a browser close event is a pretty frequent request. Star http://crbug.com/30885for updates. And read the bug report for a clever hack to detect when the browser is shut down via a key press.

添加浏览器关闭事件是一个非常频繁的请求。星http://crbug.com/30885更新。并阅读错误报告,了解一个聪明的黑客来检测浏览器何时通过按键关闭。

回答by Valentin Shergin

TL;DR:Try window.onunloadevent, it works for some cases.

TL;DR:尝试window.onunload事件,它适用于某些情况。

As it was mentioned before we generally can't handle something like onBrowserCloseevent and prevent browser from closing. But for some cases we can use window.onunloadevent for doing something synchronously, and it does work if it really synchronously.

正如之前提到的,我们通常无法处理诸如onBrowserClose事件之类的事情并阻止浏览器关闭。但对于一些情况下,我们可以使用window.onunload事件做一些同步,并且它的工作是否真的同步

From my experience you can at least:

根据我的经验,您至少可以:

  • Save some information in (for example logs) in HTML5 localStorage(which is synchronous).
  • Call some asynchronouschrome extension API functions but you can't get a result. (It works for me! Yay!)
  • Perform synchronousXMLHTTPRequest (yeah, sometimes it works).
  • 在 HTML5 localStorage同步)中保存一些信息(例如日志)。
  • 调用一些异步chrome扩展API函数却得不到结果。(它对我有用!耶!)
  • 执行同步XMLHTTPRequest(是的,有时它有效)。