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 call this API, you must have the "scripting" permission. To run the injected script, the extension must have permission for the page's URL, either explicitly as a host permission or using the activeTab permission.

Syntax

js
await browser.scripting.registerContentScripts(
  scripts         // array
)

Parameters

scripts

array of scripting.RegisteredContentScript. A list of scripts to register.

Return value

A Promise fulfilled with no arguments or rejected when there are errors. Errors can occur during script parsing and file validation or if the IDs specified exist. When an error occurs, no scripts are registered.

Examples

This example registers a content script that injects the file "script.js":

js
const script = {
  id: "a-script",
  js: ["script.js"],
  matches: ["https://example.com/*"],
};

try {
  await browser.scripting.registerContentScripts([script]);
} catch (err) {
  console.error(`failed to register content scripts: ${err}`);
}

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
registerContentScripts
Chrome – Full support
Chrome 96 (Release date: 2021-11-15)
footnote Full support
Edge – Full support
Edge 96 (Release date: 2021-11-19)
footnote Full support
Firefox – Full support
Firefox 102 (Release date: 2022-06-28)
footnote Full support
Opera – Full support
Opera 82 (Release date: 2021-12-02)
footnote Full support
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 102 (Release date: 2022-06-28)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Note: This API is based on Chromium's chrome.scripting API.