onInstalled doesnt get called when the browser extension is toggle off then on.

149 views
Skip to first unread message

Moe Bazzi

unread,
Aug 24, 2022, 4:30:02 PM8/24/22
to Chromium Extensions
Hey everyone,

If you go to chrome://extensions and click the refresh button on your extension, the onInstalled event will get called in the service worker with the reason set to 'update', but if you toggle the extension off then on (via the toggle) then this event listener doesnt get triggered. I need to know if this happens so that I can reinject the content scripts into the browser tabs because the previous one will get 'orphaned' (ie. Extension context invalidated). Is this a bug with MV3 service workers, or is this expected behavior? I cant remember if this happened with background scripts in MV2. Are there any workarounds for my case ?

Thanks

Mike-O

unread,
Aug 24, 2022, 7:28:35 PM8/24/22
to Chromium Extensions, bazz...@gmail.com
That is expected behavior. It has existed even in MV2 (when I started coding extensions). If you want to detect when your Chrome Extension is enabled or disabled, then look for the chrome.management API, which requires the management permission in your manifest.


Note by adding this, your users will get the permissions warning: "Manage your apps, extensions, and themes" that they'll have to approve.

I believe the call you'll be wanting to intercept is chrome.management.onEnabled.addListener().

Uladzimir Yankovich

unread,
Aug 25, 2022, 5:05:40 AM8/25/22
to Chromium Extensions, Mike-O, bazz...@gmail.com
When your extension is enabled, the service worker will start. Perhaps you can add some checks when it starts, which will allow you to restore the connection. 
Reply all
Reply to author
Forward
0 new messages