Skip to content

Commit aae5f07

Browse files
authored
fix: Make content script matches optional (#1220)
1 parent 586fe39 commit aae5f07

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

docs/guide/essentials/scripting.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ When using `browser.scripting.executeScript`, you can execute content scripts or
1212
// entrypoints/background.ts
1313
const res = await browser.scripting.executeScript({
1414
target: { tabId },
15-
files: ['injected.js'],
15+
files: ['content-scripts/example.js'],
1616
});
1717
console.log(res); // "Hello John!"
1818
```
1919

2020
```ts
21-
// entrypoints/injected.js
22-
export default defineUnlistedScript(() => {
23-
console.log('Script was injected!');
24-
return 'Hello John!';
21+
// entrypoints/example.content.ts
22+
export default defineContentScript({
23+
registration: 'runtime',
24+
main(ctx) {
25+
console.log('Script was executed!');
26+
return 'Hello John!';
27+
},
2528
});
2629
```

packages/wxt/src/core/utils/content-scripts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function mapWxtOptionsToContentScript(
5151
css: string[] | undefined,
5252
): Manifest.ContentScript {
5353
return {
54-
matches: options.matches,
54+
matches: options.matches ?? [],
5555
all_frames: options.allFrames,
5656
match_about_blank: options.matchAboutBlank,
5757
exclude_globs: options.excludeGlobs,

packages/wxt/src/core/utils/manifest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ function addEntrypoints(
363363
// at runtime
364364
if (wxt.config.command === 'serve' && wxt.config.manifestVersion === 3) {
365365
contentScripts.forEach((script) => {
366-
script.options.matches.forEach((matchPattern) => {
366+
script.options.matches?.forEach((matchPattern) => {
367367
addHostPermission(manifest, matchPattern);
368368
});
369369
});
@@ -406,7 +406,7 @@ function addEntrypoints(
406406
);
407407
}
408408
runtimeContentScripts.forEach((script) => {
409-
script.options.matches.forEach((matchPattern) => {
409+
script.options.matches?.forEach((matchPattern) => {
410410
addHostPermission(manifest, matchPattern);
411411
});
412412
});
@@ -547,7 +547,7 @@ export function getContentScriptCssWebAccessibleResources(
547547

548548
resources.push({
549549
resources: [cssFile],
550-
matches: script.options.matches.map((matchPattern) =>
550+
matches: script.options.matches?.map((matchPattern) =>
551551
stripPathFromMatchPattern(matchPattern),
552552
),
553553
});

packages/wxt/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ export interface BackgroundEntrypointOptions extends BaseEntrypointOptions {
588588

589589
export interface BaseContentScriptEntrypointOptions
590590
extends BaseEntrypointOptions {
591-
matches: PerBrowserOption<Manifest.ContentScript['matches']>;
591+
matches?: PerBrowserOption<Manifest.ContentScript['matches']>;
592592
/**
593593
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
594594
* @default "documentIdle"

0 commit comments

Comments
 (0)