Skip to content

Commit

Permalink
Move legacy userScripts API documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rebloor committed Feb 4, 2025
1 parent 0a4d5b4 commit 48edbb7
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 30 deletions.
20 changes: 8 additions & 12 deletions files/en-us/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -14430,18 +14430,6 @@
"modified": "2020-10-15T22:22:39.367Z",
"contributors": ["rebloor", "hellosct1", "bershanskiy"]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts/UserScriptOptions": {
"modified": "2020-06-22T16:28:43.349Z",
"contributors": ["hellosct1", "MelchiorIm3Tal", "irenesmith"]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts/Working_with_userScripts": {
"modified": "2020-06-22T16:28:43.959Z",
"contributors": ["irenesmith", "hellosct1"]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts/onBeforeScript": {
"modified": "2020-10-15T22:23:14.808Z",
"contributors": ["chrisdavidmills", "rebloor", "bershanskiy"]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts/register": {
"modified": "2020-10-15T22:21:40.204Z",
"contributors": [
Expand All @@ -14454,6 +14442,14 @@
"stoyanster"
]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/Working_with_userScripts": {
"modified": "2020-06-22T16:28:43.959Z",
"contributors": ["irenesmith", "hellosct1"]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/onBeforeScript": {
"modified": "2020-10-15T22:23:14.808Z",
"contributors": ["chrisdavidmills", "rebloor", "bershanskiy"]
},
"Mozilla/Add-ons/WebExtensions/API/webNavigation": {
"modified": "2020-10-15T21:38:48.088Z",
"contributors": ["fscholz", "wbamberg", "abbycar"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: userScripts (Legacy)
slug: Mozilla/Add-ons/WebExtensions/API/userScripts_legacy
page-type: webextension-api
browser-compat: webextensions.api.userScripts_legacy
---

{{AddonSidebar}}

> [!WARNING]
> This is documentation for the legacy `userScripts` API. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new {{WebExtAPIRef("userScripts")}} API.
Use this API to register user scripts, third-party scripts designed to manipulate webpages or provide new features. Registering a user script instructs the browser to attach the script to pages that match the URL patterns specified during registration.

This API offers similar capabilities to {{WebExtAPIRef("contentScripts")}} but with features suited to handling third-party scripts:

- execution is in an isolated sandbox: each user script is run in an isolated sandbox within the web content processes, preventing accidental or deliberate interference among scripts.
- access to the `window` and `document` global values related to the webpage the user script is attached to.
- no access to WebExtension APIs or associated permissions granted to the extension: the API script, which inherits the extension's permissions, can provide packaged WebExtension APIs to registered user scripts. An API script is declared in the extension's manifest file using the "user_scripts" manifest key.

> [!WARNING]
> This API requires the presence of the [`user_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/user_scripts) key in the manifest.json, even if no API script is specified. For example. `user_scripts: {}`.
To use the API, call `{{WebExtAPIRef("userScripts_legacy.register","register()")}}` passing in an object defining the scripts to register. The method returns a Promise that is resolved with a `{{WebExtAPIRef("userScripts_legacy.RegisteredUserScript","RegisteredUserScript")}}` object.

> [!NOTE]
> User scripts are unregistered when the related extension page (from which the user scripts were registered) is unloaded, so you should register a user script from an extension page that persists at least as long as you want the user scripts to stay registered.
## Types

- {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript","userScripts.RegisteredUserScript")}}
- : The `object` returned by the {{WebExtAPIRef("userScripts_legacy.register","register()")}} method. It represents the registered user scripts and is used to deregister the user scripts.

## Methods

- {{WebExtAPIRef("userScripts_legacy.register()","userScripts.register()")}}
- : Registers user scripts.

## Events

- {{WebExtAPIRef("userScripts_legacy.onBeforeScript","userScripts.onBeforeScript")}}
- : An event available to the API script, registered in[`"user_scripts"`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/user_scripts), that execute before a user script executes. Use it to trigger the export of the additional APIs provided by the API script, so they are available to the user script.

## Browser compatibility

{{Compat}}

## See also

- [Working with `userScripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/Working_with_userScripts)
- {{WebExtAPIRef("contentScripts","browser.contentScripts")}}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
---
title: userScripts.onBeforeScript
slug: Mozilla/Add-ons/WebExtensions/API/userScripts/onBeforeScript
title: userScripts.onBeforeScript (Legacy)
slug: Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/onBeforeScript
page-type: webextension-api-event
browser-compat: webextensions.api.userScripts.onBeforeScript
browser-compat: webextensions.api.userScripts_legacy.onBeforeScript
---

{{AddonSidebar}}

The `onBeforeScript` event of the {{WebExtAPIRef("userScripts","browser.userScripts")}} is fired before a user script is executed. It can only be included in the API script, the script registered in [`"user_scripts"`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/user_scripts), where it is used to detect that the custom API methods should be exported to the user script.
> [!WARNING]
> This is documentation for the legacy `userScripts` API. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new {{WebExtAPIRef("userScripts")}} API.
The `onBeforeScript` event of the {{WebExtAPIRef("userScripts_legacy","browser.userScripts")}} is fired before a user script is executed. It can only be included in the API script, the script registered in [`"user_scripts"`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/user_scripts), where it is used to detect that the custom API methods should be exported to the user script.

## Syntax

Expand Down Expand Up @@ -102,4 +105,4 @@ browser.userScripts.onBeforeScript.addListener((script) => {

## See also

- {{WebExtAPIRef("contentScripts")}}
- {{WebExtAPIRef("contentScripts_legacy")}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: userScripts.register() (Legacy)
slug: Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/register
page-type: webextension-api-function
browser-compat: webextensions.api.userScripts_legacy.register
---

{{AddonSidebar}}

> [!WARNING]
> This is documentation for the legacy `userScripts` API. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new {{WebExtAPIRef("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 {{WebExtAPIRef("contentScripts.register","contentScripts.register()")}} API method (for example, they both return a promise that resolves to an API object with an {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript.unregister","unregister()")}} method for unregistering the script). There are, however, differences in the options supported.

This is an asynchronous method that returns a {{JSxRef("Promise")}}.

## Syntax

```js-nolint
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 {{WebExtAPIRef("contentScripts.register","contentScripts.register()")}}.

The `UserScriptOptions` object has the following properties:

- `scriptMetadata` {{Optional_Inline}}
- : A `JSON` object 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.](/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) This metadata is used to pass details from the script to the [API script](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/user_scripts). For example, providing details of a subset of the APIs that need to be injected by the [API script](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/user_scripts). The API does not use this metadata,
- `allFrames` {{Optional_Inline}}
- : Same as `all_frames` in the [`content_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) key.
- `cookieStoreId` {{optional_inline}}
- : 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](https://support.mozilla.org/en-US/kb/extensions-private-browsing)), the tabs of a [contextual identity](/en-US/docs/Mozilla/Add-ons/WebExtensions/Work_with_contextual_identities), or a combination of these.
- `excludeGlobs` {{Optional_Inline}}
- : Same as `exclude_globs` in the [`content_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) key.
- `excludeMatches` {{Optional_Inline}}
- : Same as `exclude_matches` in the [`content_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) key.
- `includeGlobs` {{Optional_Inline}}
- : Same as `include_globs` in the [`content_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) key.
- `js`
- : An array of objects. Each object has either a property named `file`, which is a URL starting at the extension's manifest.json and pointing to a JavaScript file to register, or a property named `code`, which contains JavaScript code to register.
- `matchAboutBlank` {{Optional_Inline}}
- : Same as `match_about_blank` in the [`content_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) key.
- `matches`
- : Same as `matches` in the [`content_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) key.
The URL patterns provided in `matches` must be enabled by the host permissions defined in the manifest [`permission`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions) property or enabled by the user from the [`optional_permissions`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/optional_permissions) list. For example, if matches includes `https://mozilla.org/a` a 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` {{Optional_Inline}}
- : Same as `run_at` in the [`content_scripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) key.

Unlike content script options, the userScriptOptions object does not have a CSS property. Use {{WebExtAPIRef("contentScripts.register","contentScripts.register()")}} to dynamically register and unregister stylesheets.

### Return value

A {{JSxRef("Promise")}} that is fulfilled with a {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript","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

{{Compat}}

## See also

- {{WebExtAPIRef("contentScripts.register","contentScripts.register()")}}
- {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript.unregister","RegisteredUserScript.unregister()")}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: userScripts.RegisteredUserScript (Legacy)
slug: Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/RegisteredUserScript
page-type: webextension-api-type
browser-compat: webextensions.api.userScripts_legacy.RegisteredUserScript
---

{{AddonSidebar}}

> [!WARNING]
> This is documentation for the legacy `userScripts` API. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new {{WebExtAPIRef("userScripts")}} API.
A `RegisteredUserScript` object is returned by a call to {{WebExtAPIRef("userScripts_legacy.register","userScripts.register()")}} and represents the user scripts registered in that call.

The object defines a single method, {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript.unregister","unregister()")}}, which is used to unregister the user scripts.

> [!NOTE]
> If this object is destroyed (for example because it goes out of scope) then the associated scripts will be unregistered automatically, so you should keep a reference to this object for as long as you want the user scripts to stay registered.
## Methods

- {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript.unregister","unregister()")}}
- : Unregisters the user scripts represented by this object.

## Browser compatibility

{{Compat}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: RegisteredUserScript.unregister() (Legacy)
slug: Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript_legacy/unregister
page-type: webextension-api-function
browser-compat: webextensions.api.userScripts_legacy.RegisteredUserScript.unregister
---

{{AddonSidebar}}

> [!WARNING]
> This is documentation for the legacy `userScripts` API. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new {{WebExtAPIRef("userScripts")}} API.
The `unregister()` method of the {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript","RegisteredUserScript")}} object unregisters the user scripts represented by the object, user scripts that were registered using {{WebExtAPIRef("userScripts_legacy.register","userScripts.register()")}}.

> [!NOTE]
> User Scripts are automatically unregistered when the related extension page (from which the user scripts were registered) is unloaded, so you should register a user script from an extension page that persists at least as long as you want the user scripts to stay registered.
## Syntax

```js-nolint
const registeredUserScript = await browser.userScripts.register(
userScriptOptions // object
);
// …
await registeredUserScript.unregister()
```

### Parameters

None.

### Return value

A {{JSxRef("Promise")}} that is resolved once the user scripts are unregistered. The promise does not return a value.

## Browser compatibility

{{Compat}}

## See also

- {{WebExtAPIRef("userScripts_legacy.register","userScripts.register()")}}
- {{WebExtAPIRef("userScripts_legacy.RegisteredUserScript","RegisteredUserScript")}}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
---
title: UserScripts.UserScriptOptions
slug: Mozilla/Add-ons/WebExtensions/API/userScripts/UserScriptOptions
title: UserScripts.UserScriptOptions (Legacy)
slug: Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/UserScriptOptions
page-type: webextension-api-type
---

{{AddonSidebar}}

> [!WARNING]
> This is documentation for the legacy `userScripts` API. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new {{WebExtAPIRef("userScripts")}} API.
The UserScriptOptions object represents the content scripts to register. It has similar syntax to the contentScript options supported by browser.contentScripts.register. The differences are:

- it does not support a CSS property (use browser.contentScripts.register to dynamically register/unregister stylesheets)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
---
title: Working with userScripts
slug: Mozilla/Add-ons/WebExtensions/API/userScripts/Working_with_userScripts
title: Working with userScripts (Legacy)
slug: Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/Working_with_userScripts
page-type: guide
---

{{AddonSidebar}}

> [!WARNING]
> This is documentation for the legacy `userScripts` API. It's available in Firefox for Manifest V2. For functionality to work with user scripts in Manifest V3 see the new {{WebExtAPIRef("userScripts")}} API.
By implementing userScripts, extension developers can modify how sites look and/or work to better meet user needs.

Implement userScripts in your extension using the following steps:
Expand Down Expand Up @@ -103,6 +106,6 @@ Once the script has been registered, navigate to a page whose domain name ends i

## See also

- {{WebExtAPIRef("userScripts")}}
- {{WebExtAPIRef("userScripts.register()", "userScripts.register()")}}
- {{WebExtAPIRef("userScripts.onBeforeScript")}}
- {{WebExtAPIRef("userScripts_legacy","userScripts")}}
- {{WebExtAPIRef("userScripts_legacy.register()", "userScripts.register()")}}
- {{WebExtAPIRef("userScripts_legacy.onBeforeScript","userScripts.onBeforeScript" )}}
Loading

0 comments on commit 48edbb7

Please sign in to comment.