contentScripts

翻译不完整。 请帮助我们翻译这篇文章!

使用此 API 以注册内容脚本。“注册内容脚本”意味着浏览器会将给定的内容脚本插入到每个与给定的 URL 模式相匹配的页面中。

此 API 与 manifest.json 的  "content_scripts" 键非常相似,但  "content_scripts" 键所能注册的内容脚本是固定的,自拓展安装后便不可更改。但通过 contentScripts API,拓展可以在运行时动态地注册或取消注册脚本。

To use the API, call contentScripts.register() passing in an object defining the scripts to register, the URL patterns, and other options. This returns a Promise that is resolved with a contentScripts.RegisteredContentScript object.

The RegisteredContentScript object represents the scripts that were registered in the register() call. It defines an unregister() method that you can use to unregister the content scripts. Content scripts are also unregistered automatically when the page that created them is destroyed. For example, if they are registered from the background page they will be unregistered automatically when the background page is destroyed, and if they are registered from a sidebar or a popup, they will be unregistered automatically when the sidebar or popup is closed.

没有与 contentScripts API 相关联的权限,但是拓展必须拥有与其试图通过 register() 注册的脚本的匹配模式相对应的主机权限,才能实现注入。

类型

contentScripts.RegisteredContentScript

contentScripts.register() 函数会返回一个持有此类型的对象。它表示被通过调用此函数注册的内容脚本,可被用于取消注册对应的内容脚本。

函数

contentScripts.register()
注册给定的内容脚本。

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxOperaFirefox for Android
RegisteredContentScriptChrome No support No
Notes
No support No
Notes
Notes There is a polyfill available.
Edge No support NoFirefox Full support 59Opera No support NoFirefox Android Full support 59
RegisteredContentScript.unregisterChrome No support No
Notes
No support No
Notes
Notes There is a polyfill available.
Edge No support NoFirefox Full support 59Opera No support NoFirefox Android Full support 59
registerChrome No support No
Notes
No support No
Notes
Notes There is a polyfill available.
Edge No support NoFirefox Full support 59Opera No support NoFirefox Android Full support 59

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

Example extensions