userScripts.register() (Legacy)
This method enables user scripts to be registered from an extension's pages (such as the background page).
[!WARNING] This is documentation for the legacy
userScriptsAPI. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new userScripts API.
This method enables user scripts to be registered from an extension's pages (such as the background page).
This method is very similar to the contentScripts.register() API method (for example, they both return a promise that resolves to an API object with an unregister() method for unregistering the script). There are, however, differences in the options supported.
This is an asynchronous method that returns a Promise.
Syntax
const registeredUserScript = await browser.userScripts.register(
userScriptOptions // object
);
// …
await registeredUserScript.unregister();
Parameters
userScriptOptions-
:
object. Represents the user scripts to register. It has similar syntax to contentScripts.register().The
UserScriptOptionsobject has the following properties: -scriptMetadata- : AJSONobject containing arbitrary metadata properties associated with the registered user scripts. However, while arbitrary, the object must be serializable, so it is compatible with the structured clone algorithm. This metadata is used to pass details from the script to the API script. For example, providing details of a subset of the APIs that need to be injected by the API script. The API does not use this metadata, -allFrames- : Same asall_framesin thecontent_scriptskey. -cookieStoreId- : An array of cookie store ID strings or a string containing a cookie store ID. Registers the user script in the tabs that belong to the cookie store IDs. This enables scripts to be registered for all default or non-contextual identity tabs, private browsing tabs (if the extension is enabled in private browsing), the tabs of a contextual identity, or a combination of these. -excludeGlobs- : Same asexclude_globsin thecontent_scriptskey. -excludeMatches- : Same asexclude_matchesin thecontent_scriptskey. -includeGlobs- : Same asinclude_globsin thecontent_scriptskey. -js- : An array of objects. Each object has either a property namedfile, which is a URL starting at the extension's manifest.json and pointing to a JavaScript file to register, or a property namedcode, which contains JavaScript code to register. -matchAboutBlank- : Same asmatch_about_blankin thecontent_scriptskey. -matches- : Same asmatchesin thecontent_scriptskey. The URL patterns provided inmatchesmust be enabled by the host permissions defined in the manifestpermissionproperty or enabled by the user from theoptional_permissionslist. For example, if matches includeshttps://mozilla.org/aa script is only registered if host permissions include, for example,https://mozilla.org/*. If the URL pattern isn't enabled, the call to register fails with the error "Permission denied to register a user script for ORIGIN". -runAt- : Same asrun_atin thecontent_scriptskey.
Unlike content script options, the userScriptOptions object does not have a CSS property. Use contentScripts.register() to dynamically register and unregister stylesheets.
Return value
A Promise that is fulfilled with a RegisteredUserScript object that is use to unregister the user scripts.
[!NOTE] User scripts are unregistered when the related extension page (from which the user scripts were registered) is unloaded, so you should register user scripts from an extension page that persists at least as long as you want the user scripts to stay registered.
Browser compatibility
See also
- contentScripts.register()
- RegisteredUserScript.unregister()