Chrome extension: how to remove script contents after injection?

With the Google Chrome extension: is it possible to delete the contents of the script after it has already been entered on the page?

There are no API methods for reloading content scripts (as far as I know), so I would like to re-enter the script and delete, if possible, the old one.

+6
source share
2 answers

No. You cannot β€œdelete” it. Running the contents of a script can have side effects, such as declaring variables and functions on a window object, connecting to a background page, or listening to DOM events. If your script content has no side effects, it is identical to the fact that it is not entered at all.

If you want to re-enter it, just call executeScript using the code or source parameter. It may just be simple to define your scripts as functions, then call the .toString () functions for the functions and insert them as raw strings with the "code" argument in executeScript. Arguments of these functions can be inserted as JSON strings, which is even more convenient.

+4
source

I suggest that if you dynamically load content scripts to force page refresh using javascript (window.location.reload ()), this time without loading content scripts

+1
source

Source: https://habr.com/ru/post/952629/


All Articles