Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1943050 user scripts API - move legacy content #37975

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion files/en-us/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6123,7 +6123,11 @@
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/proxy/registerProxyScript /en-US/docs/Mozilla/Add-ons/WebExtensions/API/proxy
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/proxy/unregister /en-US/docs/Mozilla/Add-ons/WebExtensions/API/proxy
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/moveInSuccession() /en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/moveInSuccession
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript/RegisteredUserScript.unregister() /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript/unregister
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript/RegisteredUserScript.unregister() /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/RegisteredUserScript/unregister
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript/RegisteredUserScript.unregister() /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/RegisteredUserScript/unregister
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript/unregister /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/RegisteredUserScript/unregister

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dotproto redirects are set automatically and we're specifically told not to edit them manually, see https://github.com/mdn/content/blob/main/CONTRIBUTING.md#moving-documents

/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript/unregister /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/RegisteredUserScript/unregister
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/UserScriptOptions /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/UserScriptOptions
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/Working_with_userScripts /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/Working_with_userScripts
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/onBeforeScript /en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/onBeforeScript
/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows/update() /en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows/update
/en-US/docs/Mozilla/Add-ons/WebExtensions/Accessible_guidelines https://extensionworkshop.com/documentation/develop/build-an-accessible-extension/
/en-US/docs/Mozilla/Add-ons/WebExtensions/Add-on_ID https://extensionworkshop.com/documentation/develop/extensions-and-the-add-on-id/
Expand Down
32 changes: 16 additions & 16 deletions files/en-us/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -14426,22 +14426,6 @@
"modified": "2020-10-15T22:20:45.849Z",
"contributors": ["rebloor", "irenesmith", "bershanskiy", "pesar81"]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts/RegisteredUserScript/unregister": {
"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 +14438,22 @@
"stoyanster"
]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/RegisteredUserScript/unregister": {
"modified": "2020-10-15T22:22:39.367Z",
"contributors": ["rebloor", "hellosct1", "bershanskiy"]
},
"Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/UserScriptOptions": {
"modified": "2020-06-22T16:28:43.349Z",
"contributors": ["hellosct1", "MelchiorIm3Tal", "irenesmith"]
},
"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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 42 should be updated to add a space before the user_scripts link.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will go when the page is updated to add the new API docs.

Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ To use the API, call `{{WebExtAPIRef("userScripts.register","register()")}}` pas

## See also

- [Working with `userScripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/Working_with_userScripts)
- [Working with `userScripts`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts_legacy/Working_with_userScripts)
- {{WebExtAPIRef("contentScripts","browser.contentScripts")}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

This file was deleted.

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.
Comment on lines +10 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible (and a good idea) to move this block into a reusable fragment that we can include in each page? (I may have asked something like this before on a previous issue. If so, apologies for the repetition.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I'm aware, banners have to be coded as a macro (https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Page_structures/Banners_and_notices). If there is another option, it's not obviously documented.

Even if the process was simple, as the banners here are static and will not be used ad hoc, the benefit of including a reusable fragment seemed limited.


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")}}
- : 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()")}}
- : Registers user scripts.

## Events

- {{WebExtAPIRef("userScripts_legacy.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_legacy/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
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_legacy/RegisteredUserScript/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
Loading