最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension: How to remove content script after injection? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Aug 28, 2013 at 1:17 AzmisovAzmisov 7,2538 gold badges57 silver badges79 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

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.

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

发布评论

评论列表(0)

  1. 暂无评论