From 82b769e9393edc0e7ebca856d4bc5ae031427e79 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 19 Apr 2024 16:25:17 -0500 Subject: [PATCH] feat: Support returning values from scripts executed with the scripting API --- src/types/index.ts | 18 +++++++++++++++--- ...content-script-isolated-world-entrypoint.ts | 10 ++++++++-- .../content-script-main-world-entrypoint.ts | 10 ++++++++-- src/virtual/unlisted-script-entrypoint.ts | 10 ++++++++-- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/types/index.ts b/src/types/index.ts index 44bfa062..83bbc459 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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; + main(ctx: ContentScriptContext): any | Promise; } 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; + main(): any | Promise; } 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; + main(): any | Promise; } /** diff --git a/src/virtual/content-script-isolated-world-entrypoint.ts b/src/virtual/content-script-isolated-world-entrypoint.ts index c2caa200..efa66e12 100644 --- a/src/virtual/content-script-isolated-world-entrypoint.ts +++ b/src/virtual/content-script-isolated-world-entrypoint.ts @@ -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; diff --git a/src/virtual/content-script-main-world-entrypoint.ts b/src/virtual/content-script-main-world-entrypoint.ts index 52616afb..a4578682 100644 --- a/src/virtual/content-script-main-world-entrypoint.ts +++ b/src/virtual/content-script-main-world-entrypoint.ts @@ -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; diff --git a/src/virtual/unlisted-script-entrypoint.ts b/src/virtual/unlisted-script-entrypoint.ts index 035b7e38..fa0ba550 100644 --- a/src/virtual/unlisted-script-entrypoint.ts +++ b/src/virtual/unlisted-script-entrypoint.ts @@ -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;