Scripting
Refer to the browser docs above for basics on how the API works.
Execute Script Return Values
When using browser.scripting.executeScript, you can execute content scripts or unlisted scripts. To return a value, just return a value from the script's main function.
ts
// entrypoints/background.ts
const res = await browser.scripting.executeScript({
target: { tabId },
files: ['content-scripts/example.js'],
});
console.log(res); // "Hello John!"ts
// entrypoints/example.content.ts
export default defineContentScript({
registration: 'runtime',
main(ctx) {
console.log('Script was executed!');
return 'Hello John!';
},
});Optional Host Registration
When using registration: 'optional', WXT adds the script's matches to optional_host_permissions instead of host_permissions. You must request host access before registering/executing the script at runtime.