contentScripts.register()
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.
Syntax
var registering = browser.contentScripts.register( contentScriptOptions // object )
Parameters
contentScriptOptions-
object. ARegisteredContentScriptOptionsobject representing the content scripts to register. It has similar syntax to the objects in thecontent_scriptsmanifest key array. The differences are:- property names use camelCase, rather than underscores (for example,
excludeMatches, notexclude_matches - the
jsandcssproperties allow you to register strings as well as URLs, so their syntax has to distinguish these types.
The
RegisteredContentScriptOptionsobject has the following properties:-
allFramesOptional - Same as
all_framesin thecontent_scriptskey. -
cssOptional - 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 namedcode, which is some CSS code to register. -
excludeGlobsOptional - Same as
exclude_globsin thecontent_scriptskey. -
excludeMatchesOptional - Same as
exclude_matchesin thecontent_scriptskey. -
includeGlobsOptional - Same as
include_globsin thecontent_scriptskey. -
jsOptional - 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 namedcode, which is some JavaScript code to register. -
matchAboutBlankOptional - Same as
match_about_blankin thecontent_scriptskey. matches- Same as
matchesin thecontent_scriptskey. -
runAtOptional - Same as
run_atin thecontent_scriptskey.
- property names use camelCase, rather than underscores (for example,
Return value
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.
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
register |
No
There is a polyfill available.
|
No
There is a polyfill available.
|
59 |
? |
No |
No
There is a polyfill available.
|
? |
? |
59 |
? |
? |
? |
Examples
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" });
Example extensions
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/contentScripts/register