Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: let content scripts listen for event chrome.runtime.onUninstalled #138

Open
bershanskiy opened this issue Nov 12, 2021 · 5 comments
Labels
proposal Proposal for a change or new feature supportive: chrome Supportive from Chrome supportive: safari Supportive from Safari

Comments

@bershanskiy
Copy link
Member

Problem

Context scripts continue running even after their extension is uninstalled or disabled, and they have no convenient way to be notified of these events to start their cleanup. I contribute to multiple extensions which need this functionality to prevent old context scripts from interfering with new context scripts after extension update.

Current workarounds

Right now, an extension content script can detect extension uninstall/shutdown by:

  • registering a persistent port and waiting for its shutdown (port can be closed by either crash, extension uninstall or periodic shutdown)
  • try to send a message from content script to background and get error that receiving end (background context) does not exist
  • listen for chrome.runtime.onUpdateAvailable event (extension listens for this event, asks content scripts to clean themselves up, then installs update)

Workarounds 1 and 3 can be combined into one to achieve very precise notification timings if needed.

Proposal

Let content pages listen for a new event, chrome.runtime.onUninstalled. If we want to be very helpful, we can even supply a reason attribute, which can be disabled (extension was disabled), uninstalled (user completely uninstalled extension), or update (old version was uninstalled and a new version was installed in its place).

Response to security concerns

This problem was raised multiple times in the past and received responses like "notifying content scripts about extension shutdown is dangerous because the content script can retaliate against uninstalling and do something bad". I don't understand this argument, since clearly continuing to run an orphaned content script might be even worse.

@bershanskiy bershanskiy changed the title Proposal: event to notify content scripts of extension uninstall/disabling/finished extension update Proposal: let content scripts listen for event chrome.runtime.onUninstalled Nov 12, 2021
@tophf
Copy link

tophf commented Mar 10, 2022

Another workaround that doesn't depend on timing and seems reliable in my practice is to send a synchronous DOM message from the newly injected content script to the old orphaned one so it performs the cleanup, and then the new content script initializes. It's used in Stylus extension (search for orphan, there's just a few lines).

@bershanskiy
Copy link
Member Author

Another workaround that doesn't depend on timing and seems reliable in my practice is to send a synchronous DOM message from the newly injected content script to the old orphaned one so it performs the cleanup, and then the new content script initializes. It's used in Stylus extension (search for orphan, there's just a few lines).

Yes, that is yet another workaround. I believe some closed-source extensions like Grammarly use it too.

@bershanskiy
Copy link
Member Author

Relevant Chromium bug: 414213 created in 2014.

@xeenon xeenon added the proposal Proposal for a change or new feature label Aug 31, 2022
@carlosjeurissen carlosjeurissen added the agenda Discuss in future meetings label Oct 17, 2022
@dotproto dotproto removed the agenda Discuss in future meetings label Mar 16, 2023
@xeenon xeenon added the supportive: safari Supportive from Safari label Mar 20, 2024
@rdcronin
Copy link
Contributor

We discussed this at the WECG Meet-Up.

We agree this makes sense from a functionality point of view and are interested in exploring it more. The next steps here will be to write a concrete proposal, following the new steps outlined in the proposal process.

One note we highlighted during discussion is that we probably want to call this something other than runtime.onUninstalled, since the extension will likely want to know about other cases in which the extension is no longer active (disable, reload, etc).

@fregante
Copy link

we probably want to call this something other than runtime.onUninstalled

Linking a related proposal, the name might be onDisabled:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Proposal for a change or new feature supportive: chrome Supportive from Chrome supportive: safari Supportive from Safari
Projects
None yet
Development

No branches or pull requests

7 participants