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 - new userScripts API content #38073

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 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
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ It also provides messaging APIs enabling you to:
- {{WebExtAPIRef("runtime.connectNative()")}}
- : Connects the extension to a native application on the user's computer.
- {{WebExtAPIRef("runtime.sendMessage()")}}
- : Sends a single message to event listeners within your extension or a different extension. Similar to {{WebExtAPIRef('runtime.connect')}} but only sends a single message, with an optional response.
- : Sends a message to event listeners within your extension or a different extension. Similar to {{WebExtAPIRef('runtime.connect')}} but only sends a single message, with an optional response.
- {{WebExtAPIRef("runtime.sendNativeMessage()")}}
- : Sends a single message from an extension to a native application.
- : Sends a message from an extension to a native application.
- {{WebExtAPIRef("runtime.getPlatformInfo()")}}
- : Returns information about the current platform.
- {{WebExtAPIRef("runtime.getBrowserInfo()")}}
Expand All @@ -98,10 +98,14 @@ It also provides messaging APIs enabling you to:
- : Fired when a connection is made with either an extension process or a content script.
- {{WebExtAPIRef("runtime.onConnectExternal")}}
- : Fired when a connection is made with another extension.
- {{WebExtAPIRef("runtime.onserScriptConnect")}}
- : Fired when a connection is made with a user script registered by the extension.
- {{WebExtAPIRef("runtime.onMessage")}}
- : Fired when a message is sent from either an extension process or a content script.
- {{WebExtAPIRef("runtime.onMessageExternal")}}
- : Fired when a message is sent from another extension. Cannot be used in a content script.
- {{WebExtAPIRef("runtime.onUserScriptMessage")}}
- : Fired when a message is sent from a user script registered by the extension.
- {{WebExtAPIRef("runtime.onPerformanceWarning")}}
- : Fired when a runtime performance issue is detected for the extension.
- {{WebExtAPIRef("runtime.onRestartRequired")}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Values of this type are objects. They contain the following properties:

If the sender is a script running in a web page (including content and normal page scripts), then `url` is the web page URL. If the script is running in an iframe, `url` is the iframe's URL.

- `userScriptWorldId` {{optional_inline}}
- : `string`. The `worldId` of the `USER_SCRIPT` world that sent the message. Only present in {{WebExtAPIRef("runtime.onUserScriptMessage")}} and in {{WebExtAPIRef("port.sender")}} for {{WebExtAPIRef("runtime.onUserScriptConnect")}}.

{{WebExtExamples}}

## Browser compatibility
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: runtime.onUserScriptConnect
slug: Mozilla/Add-ons/WebExtensions/API/runtime/onUserScriptConnect
page-type: webextension-api-event
browser-compat: webextensions.api.runtime.onUserScriptConnect
---

{{AddonSidebar}}

Fired when a connection is made with a user script from one of the extension's [`USER_SCRIPT` worlds](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/ExecutionWorld).

In Firefox, this event requires the [`userScripts` permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts#permissions). In Chrome, the event is always present, including in extensions that don't declare the `userScripts` permission.

A user script can only connect and then sent messages from a `USER_SCRIPT` world that is configured by {{WebExtAPIRef('userScripts.configureWorld()')}} with `messaging` set to `true`.

## Syntax

```js-nolint
browser.runtime.onUserScriptConnect.addListener(listener)
browser.runtime.onUserScriptConnect.removeListener(listener)
browser.runtime.onUserScriptConnect.hasListener(listener)
```

Events have three functions:

- `addListener(listener)`
- : Adds a listener to this event.
- `removeListener(listener)`
- : Stop listening to this event. The `listener` argument is the listener to remove.
- `hasListener(listener)`
- : Checks whether a `listener` is registered for this event. Returns `true` if it is listening, `false` otherwise.

## addListener syntax

### Parameters

- `function`

- : The function called when this event occurs. The function is passed this argument:

- `port`
- : {{WebExtAPIRef('runtime.Port')}}. An object connecting the current script to the other context.

{{WebExtExamples("h2")}}

## Browser compatibility

{{Compat}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: runtime.onUserScriptMessage
slug: Mozilla/Add-ons/WebExtensions/API/runtime/onUserScriptMessage
page-type: webextension-api-event
browser-compat: webextensions.api.runtime.onUserScriptMessage
---

{{AddonSidebar}}

Use this event to listen for messages sent from one of the extension's [`USER_SCRIPT` worlds](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts/ExecutionWorld).

In Firefox, this event requires the [`userScripts` permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts#permissions). In Chrome, the event is always present, including in extensions that don't declare the `userScripts` permission.

A user script can only send messages using {{WebExtAPIRef('runtime.sendMessage')}} from a `USER_SCRIPT` world that is configured by {{WebExtAPIRef('userScripts.configureWorld()')}} with `messaging` set to `true`.

Along with the message, the listener is passed:

- a `sender` object with details about the message sender.
- a `sendResponse` function the listener can use to send a response back to the sender.

## Syntax

```js-nolint
browser.runtime.onUserScriptMessage.addListener(listener)
browser.runtime.onUserScriptMessage.removeListener(listener)
browser.runtime.onUserScriptMessage.hasListener(listener)
```

Events have three functions:

- `addListener(listener)`
- : Adds a listener to this event.
- `removeListener(listener)`
- : Stop listening to this event. The `listener` argument is the listener to remove.
- `hasListener(listener)`
- : Checks whether a `listener` is registered for this event. Returns `true` if it is listening, `false` otherwise.

## addListener syntax

### Parameters

- `listener`

- : The function called when this event occurs. The function is passed these arguments:

- `message`
- : `object`. The message. This is a JSON-ifiable object.
- `sender`
- : A {{WebExtAPIRef('runtime.MessageSender')}} object representing the sender of the message.
- `sendResponse`

- : A function to call, at most once, to send a response to the message. The function takes one argument, which is any JSON-ifiable object. This argument is passed back to the message sender.

If you have more than one `onUserScriptMessage` listener in the same document, then only one can send a response.

To send a response synchronously, call `sendResponse` before the listener function returns. To send a response asynchronously, do one of these:

- keep a reference to the `sendResponse` argument and return `true` from the listener function. You can then call `sendResponse` after the listener function has returned.
- return a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) from the listener function and resolve the promise when the response is ready.

## Examples

In this example, a user script in a `USER_SCRIPT` world with the ID `myScriptWorld` sends a message to the extension that registered it:

```js
// The user script
// Send a message to the extension that registered the user script
browser.runtime.sendMessage("my message");
```

```js
// The extension that registered the user script

function handleMessage(message, sender) {
// check that the message originated from "myScriptWorld" world
if (sender.userScriptWorldId === "myScriptWorld") {
// process message
console.log(message);
}
}

browser.runtime.onUserScriptMessage.addListener(handleMessage);
```

{{WebExtExamples}}

## Browser compatibility

{{Compat}}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Values of this type are objects. They contain the following properties:
- `postMessage`
- : `function`. Send a message to the other end. This takes one argument, which is a serializable value (see [Data cloning algorithm](/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities#data_cloning_algorithm)) representing the message to send. It will be delivered to any script listening to the port's `onMessage` event, or to the native application if this port is connected to a native application.
- `sender` {{optional_inline}}
- : {{WebExtAPIRef('runtime.MessageSender')}}. Contains information about the sender of the message. This property will only be present on ports passed to `onConnect`/`onConnectExternal` listeners.
- : {{WebExtAPIRef('runtime.MessageSender')}}. Contains information about the sender of the message. Only present on ports passed to the {{WebExtAPIRef('runtime.onConnect')}}, {{WebExtAPIRef('runtime.onConnectExternal')}}, or {{WebExtAPIRef("runtime.onUserScriptConnect")}} listeners.

## Lifecycle

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: userScripts.configureWorld()
slug: Mozilla/Add-ons/WebExtensions/API/userScripts/configureWorld
page-type: webextension-api-function
browser-compat: webextensions.api.userScripts.configureWorld
---

{{AddonSidebar}}

Configures `USER_SCRIPT` execution environments for the extension.

Changes to world configurations only apply to new instances of the world: A configuration won't apply to a world initialized by the execution of a user script in a document until the document is reloaded. However, the browser may revoke certain privileges when a configuration is updated. For example, message calls from a `USER_SCRIPT` world may fail if the extension sets `messaging` to `false`.

World configurations persist until the extension is updated or the configuration is reset by {{WebExtAPIRef("userScripts.resetWorldConfiguration()")}}.

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

## Syntax

```js-nolint
let configuredWorld = browser.userScripts.configureWorld(
properties // object
);
```

### Parameters

- `properties`

- : {{WebExtAPIRef("userScripts.WorldProperties")}}. Details of the configuration for a `USER_SCRIPT` world.

When `worldId` is omitted or the string is empty, the update is applied to the default world and all worlds without an explicit configuration. When `worldId` is specified only that world is configured.

When updating the default world and worlds without an explicit configuration, when properties are omitted the {{WebExtAPIRef("userScripts.WorldProperties")}} defaults are used.

### Return value

A {{JSxRef("Promise")}} fulfilled with no arguments if the request is successful. If the request fails, the promise is rejected with an error message.

{{WebExtExamples("h2")}}

## Browser compatibility

{{Compat}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: userScripts.ExecutionWorld
slug: Mozilla/Add-ons/WebExtensions/API/userScripts/ExecutionWorld
page-type: webextension-api-type
browser-compat: webextensions.api.userScripts.ExecutionWorld
---

{{AddonSidebar}}

The execution environment for a script injected with {{WebExtAPIRef("userScripts.register()")}}
or {{WebExtAPIRef("userScripts.update()")}}.

## Type

Values of this type are a string. Possible values are:

- `MAIN`

The web page execution environment. This environment is shared with the web page without isolation. Scripts in this environment don't have access to the APIs that are only available to content scripts.

> [!WARNING]
> Web pages can detect and interfere with the executed code due to the lack of isolation. Therefore, don't use the `MAIN` world unless it's acceptable for web pages to read, access, or modify the logic or data that flows through the executed code.

- `USER_SCRIPT`

The default execution environment for user scripts. This environment is isolated from the page's context and other `USER_SCRIPT` worlds. Extension APIs are unavailable, unlike [`ISOLATED` worlds of content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/ExecutionWorld). Several `USER_SCRIPT` worlds can exist when scripts are registered with `worldId`. {WebExtAPIRef("userScripts.configureWorld()")}} is used to change the configuration of a `USER_SCRIPT` world.

{{WebExtExamples("h2")}}

## Browser compatibility

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

{{AddonSidebar}}

Returns user scripts registered by the extension.

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

## Syntax

```js-nolint
const gettingUserScripts = await browser.userScripts.getScripts(
filter // object
);
```

### Parameters

- `filter` {{optional_inline}}
- : {{WebExtAPIRef("userScripts.UserScriptFilter")}}. A list of user script IDs to return.

### Return value

A {{JSxRef("Promise")}} fulfilled with an array of {{WebExtAPIRef("userScripts.RegisteredUserScript")}} objects. If no matching user scripts are found, the array is empty. If the request fails, the promise is rejected with an error message.

{{WebExtExamples("h2")}}

## Browser compatibility

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

{{AddonSidebar}}

Returns all the `USER_SCRIPT` world configurations registered by the extension with {{WebExtAPIRef("userScripts.configureWorld()")}}.

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

## Syntax

```js-nolint
const gettingWorldConfigurations = await browser.userScripts.getWorldConfigurations();
```

### Parameters

This function takes no parameters.

### Return value

A {{JSxRef("Promise")}} fulfilled with an array of {{WebExtAPIRef("userScripts.WorldProperties")}} objects. If the request fails, the promise is rejected with an error message.

{{WebExtExamples("h2")}}

## Browser compatibility

{{Compat}}
Loading
Loading