开发者

How to detect Chrome extension uninstall

I am trying to detect whether my extension was uninstalled.

I can't use chrome.management.onUninstalled because it wil开发者_StackOverflow社区l be fired on other extension.


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.


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.


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.


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


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.


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);
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜