正在检测Chrome浏览器退出? [英] Detecting Chrome browser exit?

查看:202
本文介绍了正在检测Chrome浏览器退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一切,但似乎无济于事.我试图通过检测窗口关闭来检测浏览器的关闭,如下面的代码所示,但这对我不起作用.我在做什么错,还有其他解决方案可以检测到Chrome关闭吗?

I tried everything but nothing seems to work for me. I tried to detect the closing of the browsing by detecting window closing like the code below but that does not work for me. What am I doing wrong and is there another solution to detect Chrome closing?

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

推荐答案

您的一般问题是,退出浏览器意味着浏览器无法再运行您的脚本来检查脚本是否已经退出-就像试图问一个人如果他们已经死了:答案(如果有的话)只会是否".

Your general problem is that quitting the browser means that browser can no longer run your script to check if it has quit yet -- it's like trying to ask a person if they've died: the answer (if you get one) is only ever going to be "no".

您可以尝试的一种解决方案是使用 background权限:

One solution you might try is a long-running background page with the background permission:

使Chrome提前启动,然后延迟关闭,以便使应用和扩展程序的使用寿命更长.

Makes Chrome start up early and and shut down late, so that apps and extensions can have a longer life.

当任何已安装的托管应用,打包应用或扩展程序具有背景"字样时,权限,则在用户启动Chrome之前,Chrome就会(无形地)在用户登录到计算机后立即运行. 背景"指的是背景".权限还会使Chrome继续运行(即使在关闭最后一个窗口之后),直到用户明确退出Chrome.

When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.

此权限使Chrome即使在关闭所有浏览器窗口后仍可以继续运行您的扩展程序.通常,Chrome仅在打开窗口后才能执行操作,因此我们遇到了Chrome无法检测到所有窗口何时关闭的问题(如上所述).有了后台许可,即使所有窗口都已关闭,Chrome仍可以运行扩展程序代码.

This permission allows the Chrome to keep running your extension even after all browser windows have closed. Normally, Chrome can only do things when it has windows open, so we run into the problem of Chrome being unable to detect when all windows have closed (as explained above). With the background permission, Chrome retains the ability to run extension code even when all windows have closed.

这篇关于正在检测Chrome浏览器退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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