如何检测 Chrome 扩展卸载

发布于 2024-12-10 11:19:23 字数 87 浏览 1 评论 0原文

我正在尝试检测我的扩展程序是否已卸载。

我无法使用 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 技术交流群。

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

发布评论

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

评论(6

等风来 2024-12-17 11:19:23

从 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.

紫罗兰の梦幻 2024-12-17 11:19:23

与 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.

情魔剑神 2024-12-17 11:19:23

您可以调用 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.

情话墙 2024-12-17 11:19:23

如果您使用的是 Manifest V3,则可以将其添加到 onInstalled Listener 中。如果您还想捕获现有用户的卸载,则还需要将其添加到“更新”。

将此代码放入您的后台页面:

chrome.runtime.onInstalled.addListener(function (details) {
    if (details.reason == 'install') {
        ... can add things like sending a user to a tutorial page on your website
        chrome.runtime.setUninstallURL('https://www.yourwebsite.com/uninstall');
    } else if (details.reason == 'update') {
        ... can add things like sending user to a update page on your website
        chrome.runtime.setUninstallURL('https://www.yourwebsite.com/uninstall');
    }
});

在此处查找更多信息: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:

chrome.runtime.onInstalled.addListener(function (details) {
    if (details.reason == 'install') {
        ... can add things like sending a user to a tutorial page on your website
        chrome.runtime.setUninstallURL('https://www.yourwebsite.com/uninstall');
    } else if (details.reason == 'update') {
        ... can add things like sending user to a update page on your website
        chrome.runtime.setUninstallURL('https://www.yourwebsite.com/uninstall');
    }
});

Find more information here: https://developer.chrome.com/docs/extensions/reference/runtime/#method-setUninstallURL

幻想少年梦 2024-12-17 11:19:23

内容脚本可以检测卸载

只需检查 chrome.runtime 的值,当扩展被卸载时,该值将变为 undefined

端口断开连接是检查这一点的一个很好的触发器:

// content_script.js

const port = chrome.runtime.connect();

port.onDisconnect.addListener(onPortDisconnect);

function onPortDisconnect() {
  // After the extension is disabled/uninstalled, `chrome.runtime` may take 
  // a few milliseconds to get cleared, so use a delay before checking.
  setTimeout(() => {
    if (!chrome.runtime?.id) {
      console.log('Extension disabled!');
    }
  }, 1000);
};

Content Script can Detect an Uninstall

Simply check the value of chrome.runtime, which becomes undefined when an extension is uninstalled.

A good trigger to check this is port disconnect:

// content_script.js

const port = chrome.runtime.connect();

port.onDisconnect.addListener(onPortDisconnect);

function onPortDisconnect() {
  // After the extension is disabled/uninstalled, `chrome.runtime` may take 
  // a few milliseconds to get cleared, so use a delay before checking.
  setTimeout(() => {
    if (!chrome.runtime?.id) {
      console.log('Extension disabled!');
    }
  }, 1000);
};
水染的天色ゝ 2024-12-17 11:19:23

对于 mv3:一个简单的方法是将

// Redirect users to a form when the extension is uninstalled.
const uninstallListener = (details) => {
  if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
    chrome.runtime.setUninstallURL('https://forms.gle/...');
  }

  if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
    // TODO: show changelog
  }
};
chrome.runtime.onInstalled.addListener(uninstallListener);

其放置在背景中。

For mv3: An easy way would be to have

// Redirect users to a form when the extension is uninstalled.
const uninstallListener = (details) => {
  if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
    chrome.runtime.setUninstallURL('https://forms.gle/...');
  }

  if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
    // TODO: show changelog
  }
};
chrome.runtime.onInstalled.addListener(uninstallListener);

Place it in your background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文