Chrome Extension Cleanup on Update/Uninstall for mv3 extensions?

671 views
Skip to first unread message

Cem Toker

unread,
Jun 13, 2023, 7:13:47 PM6/13/23
to Chromium Extensions
Is it necessary to cleanup the Chrome extension during an update or uninstall of the extension?

```chrome.webRequest.onBeforeRequest.addListener(func); chrome.runtime.onMessage.addListener(func); chrome.runtime.onInstalled.addListener(func); ```

As an example I add these listeners at the start of the extension, should I remove them during an extension update occurs? Or Google Chrome handles their removal by itself automatically?

Cuyler Stuwe

unread,
Jun 13, 2023, 8:19:38 PM6/13/23
to Chromium Extensions, Cem Toker
You'll get a new instance of the background script or service worker, but your content scripts continue running (in Chrome, at least).

Any side-effects you caused (e.g., mutating the DOM of tabs, registering "normal" non-extension service workers against your extension's domain, etc.) will also persist.

Ron Astle Lobo

unread,
Dec 29, 2023, 6:26:09 AM12/29/23
to Chromium Extensions, Cuyler Stuwe, Cem Toker, Ronak Bhandari
So what are the alternatives to cleanup the content script on Disabling/Uninstalling the Chrome Extension ?

wOxxOm

unread,
Dec 29, 2023, 10:39:01 AM12/29/23
to Chromium Extensions, Ron Astle Lobo, Cuyler Stuwe, Cem Toker, Ronak Bhandari
There's no need to remove the listeners, because any JavaScript code runs only until its environment is terminated (the background script in your case), which happens automatically when the extension is updated either due to a browser restart or when you click the update button or the reload icon in chrome://extensions.

There are some things, like context menus or dynamically registered content scripts, that should be recreated after an update in chrome.runtime.onInstalled listener when the `reason` parameter is "update" or "install".

wOxxOm

unread,
Dec 29, 2023, 10:42:12 AM12/29/23
to Chromium Extensions, wOxxOm, Ron Astle Lobo, Cuyler Stuwe, Cem Toker, Ronak Bhandari
As for the content scripts, the listeners in `chrome` don't need to be unregistered as it happens automatically e.g. chrome.runtime.onMessage. All other listeners (e.g. for mousedown) need to be unregistered in Chrome, here's an example: https://stackoverflow.com/a/57471345

Test Chat GPT Writer

unread,
Apr 24, 2024, 4:42:09 AM4/24/24
to Chromium Extensions, wOxxOm, Ron Astle Lobo, Cuyler Stuwe, Cem Toker, Ronak Bhandari
Hi

I was facing a similar issue, extension context is invalidated in the old content script when extension is updated
can we use https://developer.chrome.com/docs/extensions/reference/api/scripting#method-updateContentScripts or have to follow steps mentioned in https://stackoverflow.com/a/57471345

wOxxOm

unread,
Apr 24, 2024, 5:40:48 AM4/24/24
to Chromium Extensions, Test Chat GPT Writer, wOxxOm, Ron Astle Lobo, Cuyler Stuwe, Cem Toker, Ronak Bhandari
updateContentScripts is for an entirely different task: to update the registration info, it doesn't actually update the already injected content scripts, so you'll have to use https://stackoverflow.com/a/57471345

Test Chat GPT Writer

unread,
Jun 4, 2024, 12:00:33 PM6/4/24
to Chromium Extensions, wOxxOm
Thanks for the response @wOxxOm
I was reading your answers on Stackoverflow.com and it helped me a lot. Thank you!
Can you help with the below query
1. If there is some state in the old content script, how can we transfer the state to the new content script. I tried sending the custom event with state in the document/window event detail after disconnecting all the listeners in the old content script but when the event was received by the new content script the event detail was undefined

woxxom

unread,
Jun 4, 2024, 12:02:22 PM6/4/24
to Chromium Extensions, Test Chat GPT Writer, wOxxOm
Show your code.

Test Chat GPT Writer

unread,
Jun 5, 2024, 1:27:14 AM6/5/24
to Chromium Extensions, woxxom
Hi

I was preparing a sample code for you and it worked.
I was storing one of the HTML elements in the state and once I removed the element from the state I was able to receive the state in the document/window event detail in the new content script
Thank you for all the support!
Reply all
Reply to author
Forward
0 new messages