scripting.executeScript()

Injects a script into a target context. The script is run at document_idle by default.

Note: This method is available in Manifest V3 or higher in Chrome and Firefox 101. In Safari and Firefox 102+, this method is also available in Manifest V2.

To use this API you must have the "scripting" permission and permission for the target's URL, either explicitly as a host permission or using the activeTab permission.

In Firefox and Safari, partial lack of host permissions can result in a successful execution (with the partial results in the resolved promise). In Chrome, any missing permission prevents any execution from happening, see (Issue 1325114)).

The scripts you inject are called content scripts.

This is an asynchronous function that returns a Promise.

Syntax

let results = await browser.scripting.executeScript(
  details             // object
)

Parameters

details

An object describing the script to inject. It contains these properties:

args

An array of arguments to carry into the function. This is only valid if the func parameter is specified. The arguments must be JSON-serializable.

files

array of string. An array of path of the JS files to inject, relative to the extension's root directory. Exactly one of files and func must be specified.

func

function. A JavaScript function to inject. This function is serialized and then deserialized for injection. This means that any bound parameters and execution context are lost. Exactly one of files and func must be specified.

injectImmediately Optional

boolean. Whether the injection into the target is triggered as soon as possible, but not necessarily prior to page load.

target

scripting.InjectionTarget. Details specifying the target to inject the script into.

Return value

A Promise that fulfills with an array of InjectionResult objects, which represent the result of the injected script in every injected frame.

The promise is rejected if the injection fails, such as when the injection target is invalid. When script execution has started, its result is included in the result, whether successful (as result) or unsuccessfully (as error).

Each InjectionResult object has these properties:

frameId

number. The frame ID associated with the injection.

result Optional

any. The result of the script execution.

error Optional

any. If an error occurs, contains the value the script threw or rejected with. Typically this is an error object with a message property but it could be any value (including primitives and undefined).

The result of the script is the last evaluated statement, which is similar to the results seen if you executed the script in the Web Console (not any console.log() output). For example, consider a script like this:

let foo='my result'; foo;

Here the results array contains the string "my result" as an element.

The result values must be structured clonable (see Data cloning algorithm).

Examples

This example executes a one-line code snippet in the active tab:

browser.action.onClicked.addListener(async (tab) => {
  try {
    await browser.scripting.executeScript({
      target: {
        tabId: tab.id,
      },
      func: () => {
        document.body.style.border = "5px solid green";
      },
    });
  } catch (err) {
    console.error(`failed to execute script: ${err}`);
  }
});

This example executes a script from a file (packaged with the extension) called "content-script.js". The script is executed in the active tab. The script is executed in subframes and the main document:

browser.action.onClicked.addListener(async (tab) => {
  try {
    await browser.scripting.executeScript({
      target: {
        tabId: tab.id,
        allFrames: true,
      },
      files: ["content-script.js"],
    });
  } catch (err) {
    console.error(`failed to execute script: ${err}`);
  }
});

Browser compatibility

BCD tables only load in the browser

Note: This API is based on Chromium's chrome.scripting API.