Add-ons

contentScripts.register()

Use this function to register one or more content scripts.

It accepts one parameter, which is an object with the same properties as 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. A RegisteredContentScriptOptions object representing the content scripts to register.
This object has the same syntax as the objects in the content_scripts manifest key array.

Return value

A Promise that will be fulfilled with a contentScripts.RegisteredContentScript object that you can use to unregister the content scripts.

Note that if this object is destroyed (for example because it goes out of scope) then the content scripts will be unregistered automatically, so you should keep a reference to this object for as long as you want the content scripts to stay registered.

Browser compatibility

ChromeEdgeFirefoxFirefox for AndroidOpera
Basic support No No5959 No

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);

 

Document Tags and Contributors

 Contributors to this page: wbamberg
 Last updated by: wbamberg,