API for removing injected js file using tabs.executeScript()?


(Sai Karthik) #1

Is there any API to remove script inserted with tabs.executeScript() just like the tabs.removeCSS() ? In case if it doesn’t exist, Any workaround is there?


(Martin Giger) #2

If you want to be able to remove JS code you insert, consider using https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contentScripts over executeScript, executeScript is more like a remote eval.

Another alternative is to build the “kill switch” into your executed script that you can communicate with.

(Niklas Gollenstede) #3

No, there isn’t. But its not a lack in the APIs:

CSS has an effect while it is added, JS only when it is first added.

So CSS can be removed to restore the previous state, but the JS itself is gone immediately.
Only what the scripts do may have lasting effects (e.g. event listeners). But those are specific to your scripts and you will have to revert them manually.

Firefox does some IMHO rather annoying things with (all) content scripts when their extensions are unloaded. (I think technically the sandboxes the scripts are executed in get nuked, which effectively removes all event listeners.) This severely limits the ability to respond to unloads and remove for example added elements, which works perfectly well in Chrome. I’d actually be interested in a solution for that myself …

(Sai Karthik) #4

Thank you @freaktechnik & @NilkasG for the suggestions!