files/en-us/mozilla/add-ons/webextensions/api/userscripts/register/index.md
Registers user scripts for the extension.
let registeredUserScript = browser.userScripts.register(
scripts // array of objects
)
scripts
: array of {{WebExtAPIRef("userScripts.RegisteredUserScript")}}. Details of user scripts to register.
Each {{WebExtAPIRef("userScripts.RegisteredUserScript")}} object must contain the js property as a non-empty array and a non-empty array in either matches or includeGlobs.
A {{JSxRef("Promise")}} fulfilled with no arguments if all the requested user scripts are registered. If any user scripts fail to register or the request fails for another reason, none of the scripts are registered, and the promise is rejected with an error message.
This snippet registers hello world code into the "myScriptId" execution world to run in all websites matching "*://example.com/*".
await browser.userScripts.register([
{
worldId: "myScriptId",
js: [{ code: "console.log('Hello world!');" }],
matches: ["*://example.com/*"],
},
]);
{{WebExtExamples}}
{{Compat}}