Skip to content

Commit 0f04238

Browse files
authored
🤖 Merge PR DefinitelyTyped#72139 [Chrome] update debugger namespace by @erwanjugand
1 parent 770a63a commit 0f04238

File tree

2 files changed

+135
-96
lines changed

2 files changed

+135
-96
lines changed

types/chrome/index.d.ts

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,133 +2298,122 @@ declare namespace chrome {
22982298
* Permissions: "debugger"
22992299
*/
23002300
export namespace _debugger {
2301-
/** Debuggee identifier. Either tabId or extensionId must be specified */
2301+
/** Debuggee identifier. Either tabId, extensionId or targetId must be specified */
23022302
export interface Debuggee {
2303-
/** Optional. The id of the tab which you intend to debug. */
2304-
tabId?: number | undefined;
2305-
/**
2306-
* Optional.
2307-
* @since Chrome 27
2308-
* The id of the extension which you intend to debug. Attaching to an extension background page is only possible when 'silent-debugger-extension-api' flag is enabled on the target browser.
2309-
*/
2310-
extensionId?: string | undefined;
2311-
/**
2312-
* Optional.
2313-
* @since Chrome 28
2314-
* The opaque id of the debug target.
2315-
*/
2316-
targetId?: string | undefined;
2303+
/** The id of the tab which you intend to debug. */
2304+
tabId?: number;
2305+
/** The id of the extension which you intend to debug. Attaching to an extension background page is only possible when the `--silent-debugger-extension-api` command-line switch is used. */
2306+
extensionId?: string;
2307+
/** The opaque id of the debug target. */
2308+
targetId?: string;
23172309
}
23182310

23192311
/**
2320-
* @since Chrome 28
2321-
* Debug target information
2312+
* Debugger session identifier. One of tabId, extensionId or targetId must be specified. Additionally, an optional sessionId can be provided. If sessionId is specified for arguments sent from {@link onEvent}, it means the event is coming from a child protocol session within the root debuggee session. If sessionId is specified when passed to {@link sendCommand}, it targets a child protocol session within the root debuggee session.
2313+
* @since Chrome 125
23222314
*/
2315+
export interface DebuggerSession {
2316+
/** The id of the extension which you intend to debug. Attaching to an extension background page is only possible when the `--silent-debugger-extension-api` command-line switch is used.*/
2317+
extensionId?: string;
2318+
/** The opaque id of the Chrome DevTools Protocol session. Identifies a child session within the root session identified by tabId, extensionId or targetId. */
2319+
sessionId?: string;
2320+
/** The id of the tab which you intend to debug. */
2321+
tabId?: number;
2322+
/** The opaque id of the debug target. */
2323+
targetId?: string;
2324+
}
2325+
2326+
/**
2327+
* Connection termination reason.
2328+
* @since Chrome 44
2329+
*/
2330+
export enum DetachReason {
2331+
CANCELED_BY_USER = "canceled_by_user",
2332+
TARGET_CLOSED = "target_closed",
2333+
}
2334+
2335+
/** Debug target information */
23232336
export interface TargetInfo {
23242337
/** Target type. */
2325-
type: string;
2338+
type: `${TargetInfoType}`;
23262339
/** Target id. */
23272340
id: string;
2328-
/**
2329-
* Optional.
2330-
* @since Chrome 30
2331-
* The tab id, defined if type == 'page'.
2332-
*/
2333-
tabId?: number | undefined;
2334-
/**
2335-
* Optional.
2336-
* @since Chrome 30
2337-
* The extension id, defined if type = 'background_page'.
2338-
*/
2339-
extensionId?: string | undefined;
2341+
/** The tab id, defined if type == 'page'. */
2342+
tabId?: number;
2343+
/** The extension id, defined if type = 'background_page'. */
2344+
extensionId?: string;
23402345
/** True if debugger is already attached. */
23412346
attached: boolean;
23422347
/** Target page title. */
23432348
title: string;
23442349
/** Target URL. */
23452350
url: string;
2346-
/** Optional. Target favicon URL. */
2347-
faviconUrl?: string | undefined;
2351+
/** Target favicon URL. */
2352+
faviconUrl?: string;
23482353
}
23492354

2350-
export interface DebuggerDetachedEvent
2351-
extends chrome.events.Event<(source: Debuggee, reason: string) => void>
2352-
{}
2353-
2354-
export interface DebuggerEventEvent
2355-
extends chrome.events.Event<(source: Debuggee, method: string, params?: Object) => void>
2356-
{}
2357-
23582355
/**
2359-
* Attaches debugger to the given target.
2360-
* @param target Debugging target to which you want to attach.
2361-
* @param requiredVersion Required debugging protocol version ("0.1"). One can only attach to the debuggee with matching major version and greater or equal minor version. List of the protocol versions can be obtained in the documentation pages.
2362-
* @return The `attach` method provides its result via callback or returned as a `Promise` (MV3 only). It has no parameters.
2356+
* Target type.
2357+
* @since Chrome 44
23632358
*/
2364-
export function attach(target: Debuggee, requiredVersion: string): Promise<void>;
2359+
export enum TargetInfoType {
2360+
BACKGROUND_PAGE = "background_page",
2361+
OTHER = "other",
2362+
PAGE = "page",
2363+
WORKER = "worker",
2364+
}
2365+
23652366
/**
23662367
* Attaches debugger to the given target.
23672368
* @param target Debugging target to which you want to attach.
23682369
* @param requiredVersion Required debugging protocol version ("0.1"). One can only attach to the debuggee with matching major version and greater or equal minor version. List of the protocol versions can be obtained in the documentation pages.
2369-
* @param callback Called once the attach operation succeeds or fails. If the attach fails, runtime.lastError will be set to the error message.
2370+
*
2371+
* Can return its result via Promise since Chrome 96.
23702372
*/
2373+
export function attach(target: Debuggee, requiredVersion: string): Promise<void>;
23712374
export function attach(target: Debuggee, requiredVersion: string, callback: () => void): void;
2375+
23722376
/**
23732377
* Detaches debugger from the given target.
23742378
* @param target Debugging target from which you want to detach.
2375-
* @return The `detach` method provides its result via callback or returned as a `Promise` (MV3 only). It has no parameters.
2379+
*
2380+
* Can return its result via Promise since Chrome 96.
23762381
*/
23772382
export function detach(target: Debuggee): Promise<void>;
2378-
/**
2379-
* Detaches debugger from the given target.
2380-
* @param target Debugging target from which you want to detach.
2381-
* @param callback Called once the detach operation succeeds or fails. If the detach fails, runtime.lastError will be set to the error message.
2382-
*/
23832383
export function detach(target: Debuggee, callback: () => void): void;
2384+
23842385
/**
23852386
* Sends given command to the debugging target.
23862387
* @param target Debugging target to which you want to send the command.
23872388
* @param method Method name. Should be one of the methods defined by the remote debugging protocol.
2388-
* @param commandParams Since Chrome 22.
2389-
* JSON object with request parameters. This object must conform to the remote debugging params scheme for given method.
2390-
* @return The `sendCommand` method provides its result via callback or returned as a `Promise` (MV3 only).
2389+
* @param commandParams JSON object with request parameters. This object must conform to the remote debugging params scheme for given method.
2390+
*
2391+
* Can return its result via Promise since Chrome 96.
23912392
*/
23922393
export function sendCommand(
2393-
target: Debuggee,
2394+
target: DebuggerSession,
23942395
method: string,
23952396
commandParams?: Object,
2396-
): Promise<Object>;
2397-
/**
2398-
* Sends given command to the debugging target.
2399-
* @param target Debugging target to which you want to send the command.
2400-
* @param method Method name. Should be one of the methods defined by the remote debugging protocol.
2401-
* @param commandParams Since Chrome 22.
2402-
* JSON object with request parameters. This object must conform to the remote debugging params scheme for given method.
2403-
* @param callback Response body. If an error occurs while posting the message, the callback will be called with no arguments and runtime.lastError will be set to the error message.
2404-
*/
2397+
): Promise<Object | undefined>;
24052398
export function sendCommand(
2406-
target: Debuggee,
2399+
target: DebuggerSession,
24072400
method: string,
24082401
commandParams?: Object,
24092402
callback?: (result?: Object) => void,
24102403
): void;
2404+
24112405
/**
2412-
* @since Chrome 28
24132406
* Returns the list of available debug targets.
2414-
* @return The `getTargets` method provides its result via callback or returned as a `Promise` (MV3 only).
2407+
*
2408+
* Can return its result via Promise since Chrome 96.
24152409
*/
24162410
export function getTargets(): Promise<TargetInfo[]>;
2417-
/**
2418-
* @since Chrome 28
2419-
* Returns the list of available debug targets.
2420-
* Parameter result: Array of TargetInfo objects corresponding to the available debug targets.
2421-
*/
24222411
export function getTargets(callback: (result: TargetInfo[]) => void): void;
24232412

24242413
/** Fired when browser terminates debugging session for the tab. This happens when either the tab is being closed or Chrome DevTools is being invoked for the attached tab. */
2425-
export var onDetach: DebuggerDetachedEvent;
2414+
export const onDetach: chrome.events.Event<(source: Debuggee, reason: `${DetachReason}`) => void>;
24262415
/** Fired whenever debugging target issues instrumentation event. */
2427-
export var onEvent: DebuggerEventEvent;
2416+
export const onEvent: chrome.events.Event<(source: DebuggerSession, method: string, params?: Object) => void>;
24282417
}
24292418

24302419
export { _debugger as debugger };

types/chrome/test/index.ts

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -620,27 +620,85 @@ function testTabCaptureOptions() {
620620
constraints2 = constraints;
621621
}
622622

623-
// https://developer.chrome.com/extensions/debugger
623+
// https://developer.chrome.com/docs/extensions/reference/api/debugger
624624
function testDebugger() {
625-
chrome.debugger.attach({ tabId: 123 }, "1.23", () => {
626-
console.log("This is a callback!");
625+
chrome.debugger.DetachReason.CANCELED_BY_USER === "canceled_by_user";
626+
chrome.debugger.DetachReason.TARGET_CLOSED === "target_closed";
627+
628+
chrome.debugger.TargetInfoType.BACKGROUND_PAGE === "background_page";
629+
chrome.debugger.TargetInfoType.OTHER === "other";
630+
chrome.debugger.TargetInfoType.PAGE === "page";
631+
chrome.debugger.TargetInfoType.WORKER === "worker";
632+
633+
const debuggee: chrome.debugger.Debuggee = {
634+
tabId: 123,
635+
};
636+
637+
chrome.debugger.attach(debuggee, "0.1"); // $ExpectType Promise<void>
638+
chrome.debugger.attach(debuggee, "0.1", () => {}); // $ExpectType void
639+
// @ts-expect-error
640+
chrome.debugger.attach(debuggee, "0.1", () => {}).then(() => {});
641+
642+
chrome.debugger.detach(debuggee); // $ExpectType Promise<void>
643+
chrome.debugger.detach(debuggee, () => {}); // $ExpectType void
644+
// @ts-expect-error
645+
chrome.debugger.detach(debuggee, () => {}).then(() => {});
646+
647+
chrome.debugger.getTargets(); // $ExpectType Promise<TargetInfo[]>
648+
chrome.debugger.getTargets(([result]) => { // $ExpectType void
649+
result.type; // $ExpectType "background_page" | "other" | "page" | "worker";
650+
result.id; // $ExpectType string;
651+
result.tabId; // $ExpectType number | undefined;
652+
result.extensionId; // $ExpectType string | undefined;
653+
result.attached; // $ExpectType boolean
654+
result.title; // $ExpectType string
655+
result.url; // $ExpectType string
656+
result.faviconUrl; // $ExpectType string | undefined
627657
});
658+
// @ts-expect-error
659+
chrome.debugger.getTargets(() => {}).then(() => {});
628660

629-
chrome.debugger.detach({ tabId: 123 }, () => {
630-
console.log("This is a callback!");
661+
const debuggerSession: chrome.debugger.DebuggerSession = {
662+
sessionId: "123",
663+
};
664+
665+
chrome.debugger.sendCommand(debuggerSession, "Debugger.Cmd", {}); // $ExpectType Promise<Object | undefined>
666+
chrome.debugger.sendCommand(debuggerSession, "Debugger.Cmd", {}, (result) => { // $ExpectType void
667+
result; // $ExpectType Object | undefined
631668
});
669+
// @ts-expect-error
670+
chrome.debugger.sendCommand(debuggerSession, "Debugger.Cmd", {}, () => {}).then(() => {});
632671

633-
chrome.debugger.sendCommand({ targetId: "abc" }, "Debugger.Cmd", { param1: "x" }, result => {
634-
console.log("Do something with the result." + result);
672+
chrome.debugger.onEvent.addListener((source, methodName, params) => { // $ExpectType void
673+
source; // $ExpectType DebuggerSession
674+
methodName; // $ExpectType string
675+
params; // $ExpectType Object | undefined
635676
});
677+
chrome.debugger.onEvent.removeListener((source, methodName, params) => { // $ExpectType void
678+
source; // $ExpectType DebuggerSession
679+
methodName; // $ExpectType string
680+
params; // $ExpectType Object | undefined
681+
});
682+
chrome.debugger.onEvent.hasListener((source, methodName, params) => { // $ExpectType boolean
683+
source; // $ExpectType DebuggerSession
684+
methodName; // $ExpectType string
685+
params; // $ExpectType Object | undefined
686+
});
687+
chrome.debugger.onEvent.hasListeners(); // $ExpectType boolean
636688

637-
chrome.debugger.getTargets(results => {
638-
for (let result of results) {
639-
if (result.tabId == 123) {
640-
// Do Something.
641-
}
642-
}
689+
chrome.debugger.onDetach.addListener((source, reason) => { // $ExpectType void
690+
source; // $ExpectType Debuggee
691+
reason; // $ExpectType "canceled_by_user" | "target_closed"
692+
});
693+
chrome.debugger.onDetach.removeListener((source, reason) => { // $ExpectType void
694+
source; // $ExpectType Debuggee
695+
reason; // $ExpectType "canceled_by_user" | "target_closed"
696+
});
697+
chrome.debugger.onDetach.hasListener((source, reason) => { // $ExpectType boolean
698+
source; // $ExpectType Debuggee
699+
reason; // $ExpectType "canceled_by_user" | "target_closed"
643700
});
701+
chrome.debugger.onDetach.hasListeners(); // $ExpectType boolean
644702

645703
chrome.debugger.onEvent.addListener((source, methodName, params) => {
646704
if (source.tabId == 123) {
@@ -655,14 +713,6 @@ function testDebugger() {
655713
});
656714
}
657715

658-
// https://developer.chrome.com/extensions/debugger
659-
async function testDebuggerForPromise() {
660-
await chrome.debugger.attach({ tabId: 123 }, "1.23");
661-
await chrome.debugger.detach({ tabId: 123 });
662-
await chrome.debugger.sendCommand({ targetId: "abc" }, "Debugger.Cmd", { param1: "x" });
663-
await chrome.debugger.getTargets();
664-
}
665-
666716
// https://developer.chrome.com/extensions/declarativeContent
667717
function testDeclarativeContent() {
668718
const activeIcon: ImageData = new ImageData(32, 32);

0 commit comments

Comments
 (0)