From aae5f0789d60a5406eeb5c8f119e136ac519ea20 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 28 Nov 2024 00:00:18 -0600 Subject: [PATCH] fix: Make content script `matches` optional (#1220) --- docs/guide/essentials/scripting.md | 13 ++++++++----- packages/wxt/src/core/utils/content-scripts.ts | 2 +- packages/wxt/src/core/utils/manifest.ts | 6 +++--- packages/wxt/src/types.ts | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/guide/essentials/scripting.md b/docs/guide/essentials/scripting.md index 53b8a304..468e2c77 100644 --- a/docs/guide/essentials/scripting.md +++ b/docs/guide/essentials/scripting.md @@ -12,15 +12,18 @@ When using `browser.scripting.executeScript`, you can execute content scripts or // entrypoints/background.ts const res = await browser.scripting.executeScript({ target: { tabId }, - files: ['injected.js'], + files: ['content-scripts/example.js'], }); console.log(res); // "Hello John!" ``` ```ts -// entrypoints/injected.js -export default defineUnlistedScript(() => { - console.log('Script was injected!'); - return 'Hello John!'; +// entrypoints/example.content.ts +export default defineContentScript({ + registration: 'runtime', + main(ctx) { + console.log('Script was executed!'); + return 'Hello John!'; + }, }); ``` diff --git a/packages/wxt/src/core/utils/content-scripts.ts b/packages/wxt/src/core/utils/content-scripts.ts index f1c7dabf..3ba1eb18 100644 --- a/packages/wxt/src/core/utils/content-scripts.ts +++ b/packages/wxt/src/core/utils/content-scripts.ts @@ -51,7 +51,7 @@ export function mapWxtOptionsToContentScript( css: string[] | undefined, ): Manifest.ContentScript { return { - matches: options.matches, + matches: options.matches ?? [], all_frames: options.allFrames, match_about_blank: options.matchAboutBlank, exclude_globs: options.excludeGlobs, diff --git a/packages/wxt/src/core/utils/manifest.ts b/packages/wxt/src/core/utils/manifest.ts index 98ca5da5..9f785cd8 100644 --- a/packages/wxt/src/core/utils/manifest.ts +++ b/packages/wxt/src/core/utils/manifest.ts @@ -363,7 +363,7 @@ function addEntrypoints( // at runtime if (wxt.config.command === 'serve' && wxt.config.manifestVersion === 3) { contentScripts.forEach((script) => { - script.options.matches.forEach((matchPattern) => { + script.options.matches?.forEach((matchPattern) => { addHostPermission(manifest, matchPattern); }); }); @@ -406,7 +406,7 @@ function addEntrypoints( ); } runtimeContentScripts.forEach((script) => { - script.options.matches.forEach((matchPattern) => { + script.options.matches?.forEach((matchPattern) => { addHostPermission(manifest, matchPattern); }); }); @@ -547,7 +547,7 @@ export function getContentScriptCssWebAccessibleResources( resources.push({ resources: [cssFile], - matches: script.options.matches.map((matchPattern) => + matches: script.options.matches?.map((matchPattern) => stripPathFromMatchPattern(matchPattern), ), }); diff --git a/packages/wxt/src/types.ts b/packages/wxt/src/types.ts index 0582f81e..3205559c 100644 --- a/packages/wxt/src/types.ts +++ b/packages/wxt/src/types.ts @@ -588,7 +588,7 @@ export interface BackgroundEntrypointOptions extends BaseEntrypointOptions { export interface BaseContentScriptEntrypointOptions extends BaseEntrypointOptions { - matches: PerBrowserOption; + matches?: PerBrowserOption; /** * See https://developer.chrome.com/docs/extensions/mv3/content_scripts/ * @default "documentIdle"