contentScripts.RegisteredContentScript

A RegisteredContentScript is returned by a call to contentScripts.register() and represents the content scripts registered in that call.

It defines a single function unregister(), which can be used to unregister the content scripts.

Note: 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.

Methods

unregister()
Unregisters the content scripts represented by this object.

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
RegisteredContentScript
No
There is a polyfill available.
No
There is a polyfill available.
59
?
No
No
There is a polyfill available.
?
?
59
?
?
?
unregister
No
There is a polyfill available.
No
There is a polyfill available.
59
?
No
No
There is a polyfill available.
?
?
59
?
?
?

Examples

This code toggles a registered content script on a browser action click:

var registered = null;

async function register() {

  registered = await browser.contentScripts.register({
    matches: ["*://*.org/*"],
    js: [{
      code: "document.body.innerHTML = '<h1>This page has been eaten<h1>'"
    }],
    runAt: "document_idle"
  });

}

function toggle() {
  if (registered) {
    registered.unregister();
    registered = null;
  } else {
    register();
  }
}

browser.browserAction.onClicked.addListener(toggle);

© 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/RegisteredContentScript