Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#71867 [Chrome] Add getWorldConfigurations …
Browse files Browse the repository at this point in the history
…and resetWorldConfiguration for userScripts since Chrome 133 by @erwanjugand
  • Loading branch information
erwanjugand authored Feb 7, 2025
1 parent 60e3fe6 commit 99f6642
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
34 changes: 25 additions & 9 deletions types/chrome/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14236,26 +14236,22 @@ declare namespace chrome {
*/
export type ExecutionWorld = "MAIN" | "USER_SCRIPT";

/**
* Properties for configuring the user script world.
*/
export interface WorldProperties {
/** Specifies the world csp. The default is the `ISOLATED` world csp. */
csp?: string;
/** Specifies whether messaging APIs are exposed. The default is false.*/
messaging?: boolean;
/**
* Specifies the ID of the specific user script world to update. If not provided, updates the properties of the default user script world. Values with leading underscores (`_`) are reserved.
* @since Chrome 133
*/
worldId?: string;
}

/**
* Properties for filtering user scripts.
*/
export interface UserScriptFilter {
ids?: string[];
}

/**
* Properties for a registered user script.
*/
export interface RegisteredUserScript {
/** 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. */
allFrames?: boolean;
Expand All @@ -14275,6 +14271,11 @@ declare namespace chrome {
runAt?: RunAt;
/** The JavaScript execution environment to run the script in. The default is `USER_SCRIPT` */
world?: ExecutionWorld;
/**
* 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.
* @since Chrome 133
*/
worldId?: string;
}

/**
Expand Down Expand Up @@ -14322,6 +14323,13 @@ declare namespace chrome {
*/
export function getScripts(filter: UserScriptFilter, callback: (scripts: RegisteredUserScript[]) => void): void;

/**
* Retrieves all registered world configurations.
* @since Chrome 133
*/
export function getWorldConfigurations(): Promise<WorldProperties[]>;
export function getWorldConfigurations(callback: (worlds: WorldProperties[]) => void): void;

/**
* Registers one or more user scripts for this extension.
*
Expand All @@ -14337,6 +14345,14 @@ declare namespace chrome {
*/
export function register(scripts: RegisteredUserScript[], callback: () => void): void;

/**
* Resets the configuration for a user script world. Any scripts that inject into the world with the specified ID will use the default world configuration.
* @param worldId The ID of the user script world to reset. If omitted, resets the default world's configuration.
*/
export function resetWorldConfiguration(worldId?: string): Promise<void>;
export function resetWorldConfiguration(worldId: string, callback: () => void): void;
export function resetWorldConfiguration(callback: () => void): void;

/**
* Unregisters all dynamically-registered user scripts for this extension.
*
Expand Down
22 changes: 21 additions & 1 deletion types/chrome/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3134,7 +3134,11 @@ function testInstanceID() {
}

function testUserScripts() {
const worldProperties = { csp: "script-src 'self'", messaging: true };
const worldProperties: chrome.userScripts.WorldProperties = {
csp: "script-src 'self'",
messaging: true,
worldId: "customId",
};
chrome.userScripts.configureWorld(worldProperties); // $ExpectType Promise<void>
chrome.userScripts.configureWorld(worldProperties, () => void 0); // $ExpectType void

Expand Down Expand Up @@ -3162,11 +3166,27 @@ function testUserScripts() {
},
];

chrome.userScripts.getWorldConfigurations(); // $ExpectType Promise<WorldProperties[]>
chrome.userScripts.getWorldConfigurations(([world]) => { // $ExpectType void
world.csp; // $ExpectType string | undefined
world.messaging; // $ExpectType boolean | undefined
world.worldId; // $ExpectType string | undefined
});
// @ts-expect-error
chrome.userScripts.getWorldConfigurations(() => {}).then(() => {});

chrome.userScripts.register(scripts); // $ExpectType Promise<void>
chrome.userScripts.register(scripts, () => void 0); // $ExpectType void
// @ts-expect-error Missing required property 'js'.
chrome.userScripts.register(badScripts);

chrome.userScripts.resetWorldConfiguration(); // $ExpectType Promise<void>
chrome.userScripts.resetWorldConfiguration("scriptId1"); // $ExpectType Promise<void>
chrome.userScripts.resetWorldConfiguration(() => {}); // $ExpectType void
chrome.userScripts.resetWorldConfiguration("scriptId1", () => {}); // $ExpectType void
// @ts-expect-error
chrome.userScripts.resetWorldConfiguration(() => {}).then(() => {});

chrome.userScripts.unregister(userScriptFilter); // $ExpectType Promise<void>
chrome.userScripts.unregister(userScriptFilter, () => void 0); // $ExpectType void

Expand Down

0 comments on commit 99f6642

Please sign in to comment.