Chrome扩展程序:如何在注入后删除内容脚本? [英] Chrome Extension: How to remove content script after injection?

查看:633
本文介绍了Chrome扩展程序:如何在注入后删除内容脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Chrome浏览器扩展程序:是否可以在内容脚本已被注入页面后删除内容脚本?



没有用于重新加载内容的API方法脚本(据我所知),所以我想重新注入脚本并删除旧脚本。如果可能的话。 解决方案

没有。你不能移除它。运行内容脚本会产生副作用,例如在窗口对象上声明变量和函数,连接到后台页面或监听DOM事件。如果您的内容脚本没有副作用,则完全相同于根本不被注入。



如果您想重新注入它,只需使用代码调用executeScript或源参数。简单地将注入脚本定义为函数,然后在函数上调用.toString(),并将它们作为原始字符串以及executeScript的code参数注入是很好的。这些函数的参数可以插入为JSON字符串,这更加方便。


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.

解决方案

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.

这篇关于Chrome扩展程序:如何在注入后删除内容脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆