How to update add-on without breaking content-scripts

I’m working on a new add-on that has some UI that can be in injected in any page. When I reload add-on, I can see the UI is not responsive anymore, because the javascript part is gone but the DOM persists. This is very bad user experience.

If I’m right, this will happen for all users every time I release a new version.

As a possible solution I could:

  1. delay the update using runtime.onUpdateAvailable API
  2. save list of tabs with injected scripts
  3. remove all DOM nodes
  4. use browser.runtime.reload() to install update
  5. use browser.runtime.onInstalled.addListener event to re-inject scripts and recreate DOM

This is a lot of work, maybe there is an easier way?

Firefox injects content scripts automatically after an update (or install); Chrome does not. For that reason alone you need to do some work to handle the update procedure in order to not break functionality when an update randomly arrives.

So you could try doing something a bit like this:

Then, when your content script loads, it could check the current page for the existence of the DOM you injected and either hook up the necessary events again or delete and recreate it from scratch.

1 Like

Thank you for the info, that’s very good to know.
However in this add-on I’m injecting my content script dynamically using browser.tabs.executeScript so here Firefox won’t re-inject them, right? At least it will behave same way in all browsers :slight_smile:.

I’ve not used that API before but that’s my understanding of it, yes.