So, I've made a small Firefox extension, with a content script and no background scripts, I've been trying to make the scripts do some cleanup if the extension is disabled or uninstalled. Which, with a background script, would be done by adding a listener function using browser.runtime.onSuspend.addListener(<name of function>);
, but a content script has no access to that part of the API (as far as I can tell), so having each individual running content script do it's own cleanup can't be done this way. What would be a good workaround for this issue.
Here's a minimal example of what I'm trying to do, say I have an extension that's meant to make youtube comments invisible on a page when it's running, and bring them back if the extension is disabled or uninstalled. Here is my (obviously not-working) attempt at achieving this.
manifest.json
:
{
"name": "Minimal Example",
"version": "0.9",
"description": "unimportant",
"content_scripts": [
{
"matches": ["https://www.youtube.com/*"],
"js": ["styles.js"]
}
],
"manifest_version": 2
}
and styles.js
:
document.getElementsByTagName("ytd-comments")[0].style.visibility = "hidden";
function handleSuspend() {
console.log("Suspending extension...");
document.getElementsByTagName("ytd-comments")[0].style.visibility = "visible";
}
browser.runtime.onSuspend.addListener(handleSuspend);
This will indeed make the comment section disappear, but it will not reappear with the disabling, or uninstalling of the extension. Because the script seems to be unable to use this specific listener.
How could the desired result be achieved, would I have to set up a background script to listen on this event, and then somehow message the content script to do what it's supposed to (i'm not too familiar with the WebExtensions API as a whole to be honest)? Is there a simpler way than this?