How to detect my extension is disabled

505 views
Skip to first unread message

Lu Xu

unread,
Feb 10, 2023, 6:45:25 PM2/10/23
to Chromium Extensions
Hi, this seems an old topic, but seems still has no good answer today.

The "management" permission can detect the onDisable event correctly, but it is from another extension.

Is it feasible if I want to detect disable from my own extension? 
I tried this code
```
chrome.management.onDisabled.addListener(function(info){
  chrome.tabs.create({
  });
});
chrome.management.onEnabled.addListener(function(info){
  chrome.tabs.create({
  });
});
```
When I disable the extension, the tab is not created, but when I re-enable the extension, the tab is created. So I guess when the extension is disabled, the entire service worker is disabled immediately, and all its tasks get abandoned as well?

On the other hand, the Chrome WebStore can detect the extension disabled or enabled. How does CWS achieve it? Is it a CWS special privilege or my own website can do the same thing?

Thank you!


wOxxOm

unread,
Feb 11, 2023, 9:56:21 AM2/11/23
to Chromium Extensions, Lu Xu
Indeed, there's no good solution.

> when the extension is disabled, the entire service worker is disabled immediately, and all its tasks get abandoned as well?

Yes.

> Chrome WebStore can detect the extension disabled or enabled

In Chromium source code this special site is explicitly granted access to the privileged API.

> my own website can do the same thing

No, but your site can either use 1) external messaging to communicate with the extension's background script directly or 2) send a DOM message to the content script (e.g. using CustomEvent), then the content script will send a message to the service worker and see if there's no response, then send a new CustomEvent message to the site with the result.

Lu Xu

unread,
Feb 11, 2023, 6:00:41 PM2/11/23
to wOxxOm, Chromium Extensions
Thanks wOxxOm.

I tried your suggestions, it works in some cases, I think the main idea is that content-script still remains on the web page after extension is disabled.
So after I make the external messaging or content script messaging to the service worker, though the service worker returns some error, I can tell the extension is disabled.

However, after I refresh the webpage, since extension is already disabled, we can not make external messaging any more (chrome.runtime is undefined), and no content script will be loaded.
In this case, I suppose we can no longer tell if the extension is "not installed" or "disabled".

Simeon Vincent

unread,
Feb 12, 2023, 4:07:11 AM2/12/23
to Chromium Extensions, thus...@gmail.com, Chromium Extensions, wOxxOm
Let's take a step back. First, where are you trying to detect whether or not the extension is installed? Second, why? What problem are you trying to solve? Depending on your answer we may be able to better advise you on how to proceed.

Simeon - @dotproto

Lu Xu

unread,
Feb 12, 2023, 1:30:44 PM2/12/23
to Simeon Vincent, Chromium Extensions, wOxxOm
Thanks Simeon. Basically I would like to detect it every time a user visits my home page, and I don't want to rely on another extension to achieve that.

The initiative is I would like to give better instructions to users, for example:
If the user hasn't installed my extension, a notification would show up on my home page, offering the CWS link.
If the user already installed the extension, but disabled it, another notification would show up asking the user to re-enable it
 

Cuyler Stuwe

unread,
Feb 12, 2023, 5:11:02 PM2/12/23
to Chromium Extensions, Lu Xu, Chromium Extensions, wOxxOm, Simeon Vincent
If the user disabled your extension, they're probably aware of it.

You can just have one page that takes the user to the CWS page as though they never installed it in the first place. If they disabled it, it will say so there.

Cuyler Stuwe

unread,
Feb 12, 2023, 5:28:28 PM2/12/23
to Chromium Extensions, Cuyler Stuwe, Lu Xu, Chromium Extensions, wOxxOm, Simeon Vincent
When you use Chrome to visit the CWS for an extension that the user has disabled, the user will see this banner:

Screenshot 2023-02-12 at 2.24.04 PM.png

Seems that should solve your problem implicitly.

Lu Xu

unread,
Feb 13, 2023, 9:28:01 PM2/13/23
to Cuyler Stuwe, Chromium Extensions, wOxxOm, Simeon Vincent
Thanks Cuyler, yeah, relying on CWS might be the best workaround at present
Reply all
Reply to author
Forward
0 new messages