diff --git a/packages/wxt-demo/src/entrypoints/background.ts b/packages/wxt-demo/src/entrypoints/background.ts index ab38f0c3..3243b0bc 100644 --- a/packages/wxt-demo/src/entrypoints/background.ts +++ b/packages/wxt-demo/src/entrypoints/background.ts @@ -43,3 +43,14 @@ export default defineBackground({ storage.setItem('session:startTime', Date.now()); }, }); + +function _otherTypeChecksNotEvaluated() { + browser.scripting.executeScript({ + target: { tabId: 1 }, + files: [ + '/background.js', + // @ts-expect-error: Should error for non-existing paths + '/other.js', + ], + }); +} diff --git a/packages/wxt/src/browser.ts b/packages/wxt/src/browser.ts index 0019efed..f205e148 100644 --- a/packages/wxt/src/browser.ts +++ b/packages/wxt/src/browser.ts @@ -10,6 +10,7 @@ * @module wxt/browser */ import { browser as _browser, type Browser } from '@wxt-dev/browser'; +import type { ScriptPublicPath } from './utils/inject-script'; /** * This interface is empty because it is generated per-project when running `wxt prepare`. See: @@ -23,9 +24,39 @@ export interface WxtRuntime {} */ export interface WxtI18n {} -export type WxtBrowser = Omit & { +type ScriptInjection = + Browser.scripting.ScriptInjection extends infer T + ? T extends { files: string[] } + ? Omit & { files: ScriptPublicPath[] } + : T + : never; +type InjectionResult = Array< + Browser.scripting.InjectionResult> +>; + +export interface WxtScripting { + executeScript: { + /** + * @see {@link Browser.scripting.executeScript} + */ + ( + injection: ScriptInjection, + ): Promise>; + ( + injection: ScriptInjection, + callback: (results: InjectionResult) => void, + ): void; + }; +} + +export type WxtBrowser = Omit< + typeof _browser, + 'runtime' | 'i18n' | 'scripting' +> & { runtime: WxtRuntime & Omit<(typeof _browser)['runtime'], 'getURL'>; i18n: WxtI18n & Omit<(typeof _browser)['i18n'], 'getMessage'>; + scripting: WxtScripting & + Omit<(typeof _browser)['scripting'], 'executeScript'>; }; export const browser: WxtBrowser = _browser;