feat: Support returning values from scripts executed with the scripting API

This commit is contained in:
Aaron
2024-04-19 16:25:17 -05:00
parent e47519fb76
commit 82b769e939
4 changed files with 39 additions and 9 deletions
+15 -3
View File
@@ -737,16 +737,24 @@ export interface IsolatedWorldContentScriptDefinition
extends IsolatedWorldContentScriptEntrypointOptions {
/**
* Main function executed when the content script is loaded.
*
* When running a content script with `browser.scripting.executeScript`,
* values returned from this function will be returned in the `executeScript`
* result as well. Otherwise returning a value does nothing.
*/
main(ctx: ContentScriptContext): void | Promise<void>;
main(ctx: ContentScriptContext): any | Promise<any>;
}
export interface MainWorldContentScriptDefinition
extends MainWorldContentScriptEntrypointOptions {
/**
* Main function executed when the content script is loaded.
*
* When running a content script with `browser.scripting.executeScript`,
* values returned from this function will be returned in the `executeScript`
* result as well. Otherwise returning a value does nothing.
*/
main(): void | Promise<void>;
main(): any | Promise<any>;
}
export type ContentScriptDefinition =
@@ -763,8 +771,12 @@ export interface BackgroundDefinition extends BackgroundEntrypointOptions {
export interface UnlistedScriptDefinition extends BaseEntrypointOptions {
/**
* Main function executed when the unlisted script is ran.
*
* When running a content script with `browser.scripting.executeScript`,
* values returned from this function will be returned in the `executeScript`
* result as well. Otherwise returning a value does nothing.
*/
main(): void | Promise<void>;
main(): any | Promise<any>;
}
/**
@@ -2,16 +2,22 @@ import definition from 'virtual:user-content-script-isolated-world-entrypoint';
import { logger } from '../sandbox/utils/logger';
import { ContentScriptContext } from 'wxt/client';
(async () => {
const result = (async () => {
try {
const { main, ...options } = definition;
const ctx = new ContentScriptContext(import.meta.env.ENTRYPOINT, options);
await main(ctx);
return await main(ctx);
} catch (err) {
logger.error(
`The content script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
err,
);
throw err;
}
})();
// Return the main function's result to the background when executed via the scripting API.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
// Tested on both Chrome and Firefox
result;
@@ -1,14 +1,20 @@
import definition from 'virtual:user-content-script-main-world-entrypoint';
import { logger } from '../sandbox/utils/logger';
(async () => {
const result = (async () => {
try {
const { main } = definition;
await main();
return await main();
} catch (err) {
logger.error(
`The content script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
err,
);
throw err;
}
})();
// Return the main function's result to the background when executed via the scripting API.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
// Tested on both Chrome and Firefox
result;
+8 -2
View File
@@ -1,13 +1,19 @@
import definition from 'virtual:user-unlisted-script-entrypoint';
import { logger } from '../sandbox/utils/logger';
(async () => {
const result = (async () => {
try {
await definition.main();
return await definition.main();
} catch (err) {
logger.error(
`The unlisted script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
err,
);
throw err;
}
})();
// Return the main function's result to the background when executed via the scripting API.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
// Tested on both Chrome and Firefox
result;