As far as I am aware of, there is no explicit mechanism to unload/destroy content scripts, i.e. no event that would allow one to remove event listeners and stuff.
What I do in Chrome to get such an event is to create a port with chrome.runtime.connect()
and listen for it’s onDisconnect
event. Which works perfectly in Chrome (or all other Chromium based browsers).
Unfortunately in Firefox, that event usually isn’t fired. The best I managed here is to catch errors during the port.postMessage()
in the tabs, which throws after the extension backend was un-/reloaded.
The result is that after an extension unload, changed UI in tabs won’t change until the user triggers some kind of action that sends a message, at which point I can unload in that tab - which obviously doesn’t look good.
This could probably be improved a bit by pinging, either periodically or on tab focus or something. But that still isn’t a sound solution.
Is this a bug that just wasn’t fixed or am I missing something?