fix(types): Add type safety to browser.runtime.executeScript files option (#2142)
Co-authored-by: nickbar01234 <nickbar01234gmail.com> Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
@@ -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',
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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<typeof _browser, 'runtime' | 'i18n'> & {
|
||||
type ScriptInjection<Args extends any[], Result> =
|
||||
Browser.scripting.ScriptInjection<Args, Result> extends infer T
|
||||
? T extends { files: string[] }
|
||||
? Omit<T, 'files'> & { files: ScriptPublicPath[] }
|
||||
: T
|
||||
: never;
|
||||
type InjectionResult<Result> = Array<
|
||||
Browser.scripting.InjectionResult<Awaited<Result>>
|
||||
>;
|
||||
|
||||
export interface WxtScripting {
|
||||
executeScript: {
|
||||
/**
|
||||
* @see {@link Browser.scripting.executeScript}
|
||||
*/
|
||||
<Args extends any[], Result>(
|
||||
injection: ScriptInjection<Args, Result>,
|
||||
): Promise<InjectionResult<Result>>;
|
||||
<Args extends any[], Result>(
|
||||
injection: ScriptInjection<Args, Result>,
|
||||
callback: (results: InjectionResult<Result>) => 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;
|
||||
|
||||
Reference in New Issue
Block a user