0

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?

stayhere
  • 41
  • 6
  • You can try using this [listener](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/management/onDisabled). However I am not sure whether it works with content scripts or not. – Prerak Sola Jan 03 '20 at 16:33
  • It doesn't seem to, it throws an error as well @PrerakSola – stayhere Jan 03 '20 at 17:22
  • @PrerakSola Given that Firefox doesn't support that event, it's not an option, even in a background script. – Makyen Jan 03 '20 at 23:10
  • Possible duplicate of [Firefox WebExtension: How Do I Run Code Prior to Disable/Uninstall?](https://stackoverflow.com/questions/38645274/firefox-webextension-how-do-i-run-code-prior-to-disable-uninstall) – Makyen Jan 03 '20 at 23:10
  • BTW: Given what your content script does, change `visibility`, why are you not just adding equivalent CSS, using `css`, in your [`content_scripts`](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) directive within your manifest.json? Such added CSS should be automatically removed from the page when your extension is disabled/uninstalled, which solves your problem (even if you also have to use JavaScript to add an additional class to just the elements you want to affect). – Makyen Jan 03 '20 at 23:39
  • @Makyen Thanks for pointing me to a more relevant question, unfortunately, as for the use of a CSS content script, that's not really doable, this is just a minimal working example of the issue I tried to write, in reality, I have a good reason for using Javascript for this (namely I have to access a storage.sync variable to determine what to actually do with the comments which can't be done with CSS as far as I know) – stayhere Jan 04 '20 at 08:42

0 Answers0