Movatterモバイル変換


[0]ホーム

URL:


  1. Mozilla
  2. Add-ons
  3. Browser extensions
  4. JavaScript APIs
  5. userScripts
  6. userScripts.execute()

userScripts.execute()

Injects a user script into a target context (e.g., tab or frame).

Note:By default, the injected script is executed atdocument_idle, or immediately if the page is loaded. If theinjectImmediately property is set totrue, the script injects without waiting, even if the page is loading.

Syntax

js
let executeUserScript = browser.userScripts.execute(  injection, // array of objects);

Parameters

injection

An array of objects specifying what user scripts to inject, where, and how.

injectImmediatelyOptional

boolean. If set totrue, the injection is triggered as soon as possible in the target context. This doesn't guarantee that the injection occurs before the page finishes loading, as the page may load before the script has reached the target.

js

array ofuserScripts.ScriptSource. The scripts to inject into matching pages.

target

An object that defines the target context scripts are injected into.

allFramesOptional

boolean. If set totrue, the script is injected into all available frames. Defaults tofalse, in which the script is only injected into the top frame.

documentIdsOptional

array ofstring. The IDs of the documents to inject into. Must not be specified ifframeIds is set.

frameIdsOptional

array ofinteger. The IDs of the frames to inject into. Must not be specified ifdocumentIds is set.

tabId

integer. The ID of a tab to inject into.

worldOptional

userScripts.ExecutionWorld. The execution environment to use to run the scripts. Defaults to"USER_SCRIPT".

worldIdOptional

string. ID of a user script world the script executes in. Only valid ifworld isUSER_SCRIPT or omitted. IfworldId is omitted, the script is executed in the defaultUSER_SCRIPT world (""). Values with leading underscores (_) are reserved. The maximum length is 256 characters. A world can be used by several scripts as their execution environment. To configure the behavior of a world, pass itsworldId touserScripts.configureWorld before the first script executes in that world.

Return Value

APromise fulfilled with an array of objects describing the outcome of the injection with these properties:

documentId

string. Document ID associated with the injection.

errorOptional

string. Error message if any. This is mutually exclusive withresult

frameId

integer. Frame ID associated with the injection.

resultOptional

string. Result of the script injection if any. This is mutually exclusive witherror

Examples

js
await browser.userScripts.execute([  {    js: [{ code: "console.log('Hello world!');" }],    target: { tabId: 1 },  },]);

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp