Skip to content

Commit 220dafe

Browse files
authored
🤖 Merge PR DefinitelyTyped#72573 [chrome]: add userScripts.execute() by @nicolas377
1 parent 81d0317 commit 220dafe

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

types/chrome/index.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14860,6 +14860,18 @@ declare namespace chrome {
1486014860
*/
1486114861
export type ExecutionWorld = "MAIN" | "USER_SCRIPT";
1486214862

14863+
/** @since Chrome 135 */
14864+
export interface InjectionResult {
14865+
/** The document associated with the injection. */
14866+
documentId: string;
14867+
/** The error, if any. `error` and `result` are mutually exclusive. */
14868+
error?: string;
14869+
/** The frame associated with the injection. */
14870+
frameId: number;
14871+
/** The result of the script execution. */
14872+
result: any;
14873+
}
14874+
1486314875
export interface WorldProperties {
1486414876
/** Specifies the world csp. The default is the `ISOLATED` world csp. */
1486514877
csp?: string;
@@ -14876,6 +14888,18 @@ declare namespace chrome {
1487614888
ids?: string[];
1487714889
}
1487814890

14891+
/** @since Chrome 135 */
14892+
export interface InjectionTarget {
14893+
/** Whether the script should inject into all frames within the tab. Defaults to false. This must not be true if `frameIds` is specified. */
14894+
allFrames?: boolean;
14895+
/** The IDs of specific documentIds to inject into. This must not be set if `frameIds` is set. */
14896+
documentIds?: string[];
14897+
/** The IDs of specific frames to inject into. */
14898+
frameIds?: number[];
14899+
/** The ID of the tab into which to inject. */
14900+
tabId: number;
14901+
}
14902+
1487914903
export interface RegisteredUserScript {
1488014904
/** If true, it will inject into all frames, even if the frame is not the top-most frame in the tab. Each frame is checked independently for URL requirements; it will not inject into child frames if the URL requirements are not met. Defaults to false, meaning that only the top frame is matched. */
1488114905
allFrames?: boolean;
@@ -14902,6 +14926,20 @@ declare namespace chrome {
1490214926
worldId?: string;
1490314927
}
1490414928

14929+
/** @since Chrome 135 */
14930+
export interface UserScriptInjection {
14931+
/** Whether the injection should be triggered in the target as soon as possible. Note that this is not a guarantee that injection will occur prior to page load, as the page may have already loaded by the time the script reaches the target. */
14932+
injectImmediately?: boolean;
14933+
/** The list of ScriptSource objects defining sources of scripts to be injected into the target. */
14934+
js: ScriptSource[];
14935+
/** Details specifying the target into which to inject the script. */
14936+
target: InjectionTarget;
14937+
/** The JavaScript "world" to run the script in. The default is `USER_SCRIPT`. */
14938+
world?: ExecutionWorld;
14939+
/** Specifies the user script world ID to execute in. If omitted, the script will execute in the default user script world. Only valid if `world` is omitted or is `USER_SCRIPT`. Values with leading underscores (`_`) are reserved. */
14940+
worldId?: string;
14941+
}
14942+
1490514943
/**
1490614944
* Properties for a script source.
1490714945
*/
@@ -14954,6 +14992,13 @@ declare namespace chrome {
1495414992
export function getWorldConfigurations(): Promise<WorldProperties[]>;
1495514993
export function getWorldConfigurations(callback: (worlds: WorldProperties[]) => void): void;
1495614994

14995+
/**
14996+
* Injects a script into a target context. By default, the script will be run at `document_idle`, or immediately if the page has already loaded. If the `injectImmediately` property is set, the script will inject without waiting, even if the page has not finished loading. If the script evaluates to a promise, the browser will wait for the promise to settle and return the resulting value.
14997+
* @since Chrome 135
14998+
*/
14999+
export function execute(injection: UserScriptInjection): Promise<InjectionResult[]>;
15000+
export function execute(injection: UserScriptInjection, callback: (result: InjectionResult[]) => void): void;
15001+
1495715002
/**
1495815003
* Registers one or more user scripts for this extension.
1495915004
*

types/chrome/test/index.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,6 +4404,12 @@ function testUserScripts() {
44044404
chrome.userScripts.getScripts(userScriptFilter); // $ExpectType Promise<RegisteredUserScript[]>
44054405
chrome.userScripts.getScripts(userScriptFilter, (scripts: chrome.userScripts.RegisteredUserScript[]) => void 0); // $ExpectType void
44064406

4407+
const badScripts = [
4408+
{
4409+
id: "badScriptId",
4410+
matches: ["*://example.com/*"],
4411+
},
4412+
];
44074413
const scripts = [
44084414
{
44094415
id: "scriptId1",
@@ -4416,13 +4422,26 @@ function testUserScripts() {
44164422
matches: ["*://example.org/*"],
44174423
},
44184424
];
4419-
4420-
const badScripts = [
4425+
const jsInjections: chrome.userScripts.ScriptSource[] = [
44214426
{
4422-
id: "badScriptId",
4423-
matches: ["*://example.com/*"],
4427+
file: "./the/script.js",
4428+
},
4429+
{
4430+
code: "console.log(\"Wow the script works!\");",
44244431
},
44254432
];
4433+
const injectionTarget: chrome.userScripts.InjectionTarget = {
4434+
tabId: 46,
4435+
allFrames: true,
4436+
};
4437+
4438+
const badExeOptions = {};
4439+
const exeOptions: chrome.userScripts.UserScriptInjection = {
4440+
injectImmediately: true,
4441+
js: jsInjections,
4442+
target: injectionTarget,
4443+
worldId: "USER_SCRIPT",
4444+
};
44264445

44274446
chrome.userScripts.getWorldConfigurations(); // $ExpectType Promise<WorldProperties[]>
44284447
chrome.userScripts.getWorldConfigurations(([world]) => { // $ExpectType void
@@ -4433,6 +4452,13 @@ function testUserScripts() {
44334452
// @ts-expect-error
44344453
chrome.userScripts.getWorldConfigurations(() => {}).then(() => {});
44354454

4455+
// @ts-expect-error
4456+
chrome.userScripts.execute(badExeOptions);
4457+
chrome.userScripts.execute(exeOptions); // $ExpectType Promise<InjectionResult[]>
4458+
chrome.userScripts.execute(exeOptions, (result) => { // $ExpectType void
4459+
result; // $ExpectType InjectionResult[]
4460+
});
4461+
44364462
chrome.userScripts.register(scripts); // $ExpectType Promise<void>
44374463
chrome.userScripts.register(scripts, () => void 0); // $ExpectType void
44384464
// @ts-expect-error Missing required property 'js'.

0 commit comments

Comments
 (0)