Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions docs/guide/essentials/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!';
},
});
```
2 changes: 1 addition & 1 deletion packages/wxt/src/core/utils/content-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions packages/wxt/src/core/utils/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function generateManifest(
}

if (wxt.config.manifestVersion === 3) {
validateMv3WebAccessbileResources(manifest);
validateMv3WebAccessibleResources(manifest);
}

stripKeys(manifest);
Expand All @@ -143,7 +143,7 @@ export async function generateManifest(
}

/**
* Removes suffixes from the version, like X.Y.Z-alpha1 (which brosers don't allow), so it's a
* Removes suffixes from the version, like X.Y.Z-alpha1 (which browsers don't allow), so it's a
* simple version number, like X or X.Y or X.Y.Z, which browsers allow.
*/
function simplifyVersion(versionName: string): string {
Expand Down Expand Up @@ -362,7 +362,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);
});
});
Expand Down Expand Up @@ -405,7 +405,7 @@ function addEntrypoints(
);
}
runtimeContentScripts.forEach((script) => {
script.options.matches.forEach((matchPattern) => {
script.options.matches?.forEach((matchPattern) => {
addHostPermission(manifest, matchPattern);
});
});
Expand Down Expand Up @@ -552,7 +552,7 @@ export function getContentScriptCssWebAccessibleResources(

resources.push({
resources: [cssFile],
matches: script.options.matches.map((matchPattern) =>
matches: script.options.matches?.map((matchPattern) =>
stripPathFromMatchPattern(matchPattern),
),
});
Expand Down Expand Up @@ -613,7 +613,7 @@ export function stripPathFromMatchPattern(pattern: string) {
/**
* Converts all MV3 web accessible resources to their MV2 forms. MV3 web accessible resources are
* generated in this file, and may be defined by the user in their manifest. In both cases, when
* targetting MV2, automatically convert their definitions down to the basic MV2 array.
* targeting MV2, automatically convert their definitions down to the basic MV2 array.
*/
export function convertWebAccessibleResourcesToMv2(
manifest: Manifest.WebExtensionManifest,
Expand Down Expand Up @@ -655,7 +655,7 @@ function convertActionToMv2(manifest: Manifest.WebExtensionManifest): void {
/**
* Make sure all resources are in MV3 format. If not, add a wanring
*/
export function validateMv3WebAccessbileResources(
export function validateMv3WebAccessibleResources(
manifest: Manifest.WebExtensionManifest,
): void {
if (manifest.web_accessible_resources == null) return;
Expand Down
2 changes: 1 addition & 1 deletion packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export interface BackgroundEntrypointOptions extends BaseEntrypointOptions {

export interface BaseContentScriptEntrypointOptions
extends BaseEntrypointOptions {
matches: PerBrowserOption<Manifest.ContentScript['matches']>;
matches?: PerBrowserOption<Manifest.ContentScript['matches']>;
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default "documentIdle"
Expand Down