Chrome Extension: How to remove content script after injection?

旧街凉风 提交于 2020-02-14 05:43:25

问题


With a Google Chrome Extension: Is it possible to remove a content script after it has already been injected into the page?

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


回答1:


No. You can't "remove" it. Running a content script can have side effects, like declaring variables and functions on the window object, connecting to the background page, or listening to DOM events. If your content script has no side effects, it is identical to not being injected at all.

If you want to re-inject it, simply call executeScript with either a code or source parameter. It can be nice to simply define your injected scripts as functions, then calling .toString() on the functions, and injecting them as raw strings with the "code" argument to executeScript. Arguments to these functions can be inserted as JSON strings, which is even more convenient.




回答2:


I suppose if you dynamically load the content scripts it's possible to force a page refresh using javascript (window.location.reload()) this time not loading the content scripts



来源:https://stackoverflow.com/questions/18477910/chrome-extension-how-to-remove-content-script-after-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!