scripting.registerContentScripts()
Registers one or more content scripts.
Note: This method is available in Manifest V3 or higher in Chrome and Firefox 101. In Firefox 102+, this method is also available in Manifest V2.
To use this API you must have the "scripting" permission and permission for the page's URL, either explicitly as a host permission or using the activeTab permission.
This is an asynchronous function that returns a Promise.
Syntax
await browser.scripting.registerContentScripts(
scripts
)
Return value
A Promise that fulfills with an array of scripting.RegisteredContentScript. If there are errors during script parsing and file validation, or if the IDs specified do not exist, no scripts are registered and the promise is rejected.
Examples
This example registers a content script that injects the file "script.js":
const aScript = {
id: "a-script",
js: ["script.js"],
matches: ["https://example.com/*"],
};
try {
await browser.scripting.registerContentScripts([aScript]);
} catch (err) {
console.error(`failed to register content scripts: ${err}`);
}
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 |
registerContentScripts |
88 |
88 |
102 |
? |
74 |
16.4Available for use in Manifest V2 or later.
|
? |
? |
102 |
? |
16.4Available for use in Manifest V2 or later.
|
? |