Use this function to register one or more content scripts.
It accepts one parameter, which is an object with similar properties to the objects given in the
content_scripts
manifest key (but note that
content_scripts
is an array of objects, while the argument to
register()
is a single object).
This is an asynchronous function that returns a
Promise
.
var registering = browser.contentScripts.register(
contentScriptOptions
// object
)
contentScriptOptions
对象
。
RegisteredContentScriptOptions
object representing the content scripts to register. It has similar syntax to the objects in the
content_scripts
manifest key array. The differences are:
excludeMatches
, not
exclude_matches
js
and
css
properties allow you to register strings as well as URLs, so their syntax has to distinguish these types.
RegisteredContentScriptOptions
object has the following properties:
allFrames
可选
如同
all_frames
在
content_scripts
key.
cookieStoreId
可选
A string or array of strings. Registers the content script in the tabs that belong to one or more cookie store IDs. This enables scripts to be registered for all default or non-contextual identity tabs, private browsing tabs (if extensions are enabled in private browsing), the tabs of a contextual identity , or a combination of these.
css
可选
An array of objects. Each object has either a property named
file
, which is a URL starting at the extension's manifest.json and pointing to a CSS file to register, or a property named
code
, which is some CSS code to register.
excludeGlobs
可选
如同
exclude_globs
在
content_scripts
key.
excludeMatches
可选
如同
exclude_matches
在
content_scripts
key.
includeGlobs
可选
如同
include_globs
在
content_scripts
key.
js
可选
An array of objects. Each object has either a property named
file
, which is a URL starting at the extension's manifest.json and pointing to a JavaScript file to register, or a property named
code
, which is some JavaScript code to register.
matchAboutBlank
可选
如同
match_about_blank
在
content_scripts
key.
匹配
如同
匹配
在
content_scripts
key.
runAt
可选
如同
run_at
在
content_scripts
key.
A
Promise
that will be fulfilled with a
contentScripts.RegisteredContentScript
object that you can use to unregister the content scripts.
Currently, content scripts are unregistered when the related extension page (from which the content scripts were registered) is unloaded, so you should register a content script from an extension page that persists at least as long as you want the content scripts to stay registered.
BCD tables only load in the browser
This example registers the
defaultCode
content script for all
.org
URLs:
const defaultHosts = "*://*.org/*";
const defaultCode = "document.body.innerHTML = '<h1>This page has been eaten<h1>'";
async function register(hosts, code) {
return await browser.contentScripts.register({
matches: [hosts],
js: [{code}],
runAt: "document_idle"
});
}
var registered = register(defaultHosts, defaultCode);
This code registers the JS file at content_scripts/example.js:
const scriptObj = await browser.contentScripts.register({
"js": [{file: "/content_scripts/example.js"}],
"matches": ["<all_urls>"],
"allFrames": true,
"runAt": "document_start"
});
最后修改: , 由 MDN 贡献者