如何检测 Chrome 扩展卸载
我正在尝试检测我的扩展程序是否已卸载。
我无法使用 chrome.management.onUninstalled ,因为它将在其他扩展上触发。
I am trying to detect whether my extension was uninstalled.
I can't use chrome.management.onUninstalled
because it will be fired on other extension.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 Chrome 41 开始,您现在可以卸载扩展程序时打开网址。这可能包含退出调查或跟踪卸载事件作为某种分析。
As of Chrome 41, you can now open a URL when the extension is uninstalled. That could contain an exit survey or track the uninstall event as some sort of analytics.
与 Firefox 不同,Google Chrome 不允许检测用户何时卸载扩展程序,这对于了解用户行为非常有用。
crbug.com 上有一个功能请求,其中讨论了以下内容该功能但尚未实现。
Google Chrome, unlike Firefox, doesn’t allow to detect when the user uninstalls the extension, which is quite useful to understand user behaviour.
There is a feature request on crbug.com with a discussion of this feature but it has not been implemented yet.
您可以调用
chrome.runtime.setUninstallURL("www.example.com/survey" )
并将用户重定向到某个网址。不幸的是,一旦删除扩展程序,后台脚本也会被删除,并且您无法执行诸如记录事件或将命中发送到谷歌分析之类的任何操作。我所做的是将重定向网址设置为我的服务器端点,并执行一些任务,例如将事件记录到我自己的数据库,或将命中发送到谷歌分析(ga 热门构建器)。然后调用
res.status(301).redirect("www.example.com/survey")
访问某个调查网址。最后我可以将卸载事件发送到谷歌分析。You can call
chrome.runtime.setUninstallURL("www.example.com/survey")
and redirect user to a url. Unfortunately, as soon as the extension is removed, the background script is removed too, and you can't do anything like log event or send hit to google analytics.What I did is to set the redirect url to my server endpoint, and do some tasks like logging event to my own db, or sending hit to google analytics (ga hit builder). Then call
res.status(301).redirect("www.example.com/survey")
to some survey url. Finally I can send the uninstall event to google analysis.如果您使用的是 Manifest V3,则可以将其添加到 onInstalled Listener 中。如果您还想捕获现有用户的卸载,则还需要将其添加到“更新”。
将此代码放入您的后台页面:
在此处查找更多信息:https: //developer.chrome.com/docs/extensions/reference/runtime/#method-setUninstallURL
If you're on Manifest V3, you can add it on your onInstalled Listener. If you want to capture uninstall for existing users as well, you need to add it to 'update' as well.
Place this code in your background page:
Find more information here: https://developer.chrome.com/docs/extensions/reference/runtime/#method-setUninstallURL
内容脚本可以检测卸载
只需检查
chrome.runtime
的值,当扩展被卸载时,该值将变为undefined
。端口断开连接是检查这一点的一个很好的触发器:
Content Script can Detect an Uninstall
Simply check the value of
chrome.runtime
, which becomesundefined
when an extension is uninstalled.A good trigger to check this is port disconnect:
对于 mv3:一个简单的方法是将
其放置在背景中。
For mv3: An easy way would be to have
Place it in your background.