@narumitw/pi-chrome-devtools 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +83 -0
- package/package.json +39 -0
- package/src/chrome-devtools.ts +337 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 narumiruna
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# pi-chrome-devtools
|
|
2
|
+
|
|
3
|
+
A public [pi](https://pi.dev) extension package that exposes Chrome DevTools Protocol (CDP) tools to the agent.
|
|
4
|
+
|
|
5
|
+
It is inspired by [`chrome-devtools-mcp`](https://github.com/ChromeDevTools/chrome-devtools-mcp), but implemented as native pi tools instead of an MCP server.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pi install npm:@narumitw/pi-chrome-devtools
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Try without installing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pi -e npm:@narumitw/pi-chrome-devtools
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Try this package locally from the repository root:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pi -e ./extensions/pi-chrome-devtools
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Start Chrome with CDP enabled
|
|
26
|
+
|
|
27
|
+
The extension connects to `127.0.0.1:9222` by default.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
google-chrome --remote-debugging-port=9222 --user-data-dir=/tmp/pi-chrome-devtools
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
On macOS:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
|
|
37
|
+
--remote-debugging-port=9222 \
|
|
38
|
+
--user-data-dir=/tmp/pi-chrome-devtools
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Override the endpoint if needed:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
PI_CHROME_DEVTOOLS_HOST=127.0.0.1 PI_CHROME_DEVTOOLS_PORT=9223 pi -e ./extensions/pi-chrome-devtools
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Tools
|
|
48
|
+
|
|
49
|
+
- `chrome_devtools_list_pages` — list inspectable Chrome tabs/pages.
|
|
50
|
+
- `chrome_devtools_select_page` — select the active page for later tool calls.
|
|
51
|
+
- `chrome_devtools_navigate` — navigate a page to a URL.
|
|
52
|
+
- `chrome_devtools_evaluate` — evaluate JavaScript in the selected page.
|
|
53
|
+
- `chrome_devtools_screenshot` — capture a PNG screenshot.
|
|
54
|
+
|
|
55
|
+
## Command
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
/chrome-devtools
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Shows the configured CDP endpoint and quick start hint.
|
|
62
|
+
|
|
63
|
+
## Package layout
|
|
64
|
+
|
|
65
|
+
```txt
|
|
66
|
+
extensions/pi-chrome-devtools/
|
|
67
|
+
├── src/
|
|
68
|
+
│ └── chrome-devtools.ts
|
|
69
|
+
├── README.md
|
|
70
|
+
├── LICENSE
|
|
71
|
+
├── tsconfig.json
|
|
72
|
+
└── package.json
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The package exposes its extension through `package.json`:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"pi": {
|
|
80
|
+
"extensions": ["./src/chrome-devtools.ts"]
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@narumitw/pi-chrome-devtools",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Pi extension that exposes Chrome DevTools Protocol tools.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"private": false,
|
|
8
|
+
"keywords": [
|
|
9
|
+
"pi-package",
|
|
10
|
+
"pi-extension",
|
|
11
|
+
"pi",
|
|
12
|
+
"chrome",
|
|
13
|
+
"devtools",
|
|
14
|
+
"cdp"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"pi": {
|
|
22
|
+
"extensions": [
|
|
23
|
+
"./src/chrome-devtools.ts"
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"check": "biome check . && npm run typecheck",
|
|
28
|
+
"format": "biome check --write .",
|
|
29
|
+
"typecheck": "tsc --noEmit"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"typebox": "^1.1.37"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@biomejs/biome": "2.4.14",
|
|
36
|
+
"@mariozechner/pi-coding-agent": "0.73.0",
|
|
37
|
+
"typescript": "6.0.3"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { defineTool, type ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
|
|
4
|
+
const DEFAULT_HOST = "127.0.0.1";
|
|
5
|
+
const DEFAULT_PORT = 9222;
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
7
|
+
|
|
8
|
+
interface DevToolsPage {
|
|
9
|
+
id: string;
|
|
10
|
+
type: string;
|
|
11
|
+
title: string;
|
|
12
|
+
url: string;
|
|
13
|
+
webSocketDebuggerUrl?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface ChromeDevToolsState {
|
|
17
|
+
host: string;
|
|
18
|
+
port: number;
|
|
19
|
+
activePageId?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface CdpResponse<T = unknown> {
|
|
23
|
+
id: number;
|
|
24
|
+
result?: T;
|
|
25
|
+
error?: {
|
|
26
|
+
code: number;
|
|
27
|
+
message: string;
|
|
28
|
+
data?: unknown;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const state: ChromeDevToolsState = {
|
|
33
|
+
host: process.env.PI_CHROME_DEVTOOLS_HOST ?? DEFAULT_HOST,
|
|
34
|
+
port: Number(process.env.PI_CHROME_DEVTOOLS_PORT ?? DEFAULT_PORT),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const listPagesTool = defineTool({
|
|
38
|
+
name: "chrome_devtools_list_pages",
|
|
39
|
+
label: "Chrome DevTools: List Pages",
|
|
40
|
+
description: "List Chrome tabs/pages from a running Chrome DevTools Protocol endpoint.",
|
|
41
|
+
promptSnippet: "List Chrome tabs/pages available over Chrome DevTools Protocol",
|
|
42
|
+
parameters: Type.Object({}),
|
|
43
|
+
async execute() {
|
|
44
|
+
const pages = await listPages();
|
|
45
|
+
return textResult(JSON.stringify(pages.map(formatPage), null, 2), { pages });
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const selectPageTool = defineTool({
|
|
50
|
+
name: "chrome_devtools_select_page",
|
|
51
|
+
label: "Chrome DevTools: Select Page",
|
|
52
|
+
description: "Select the active Chrome page for later chrome_devtools_* tool calls.",
|
|
53
|
+
promptSnippet: "Select the Chrome tab/page to inspect or control",
|
|
54
|
+
parameters: Type.Object({
|
|
55
|
+
pageId: Type.String({ description: "Page id from chrome_devtools_list_pages." }),
|
|
56
|
+
}),
|
|
57
|
+
async execute(_toolCallId, params) {
|
|
58
|
+
const page = await getPage(params.pageId);
|
|
59
|
+
state.activePageId = page.id;
|
|
60
|
+
return textResult(`Selected page ${page.id}: ${page.title}\n${page.url}`, {
|
|
61
|
+
page: formatPage(page),
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const navigateTool = defineTool({
|
|
67
|
+
name: "chrome_devtools_navigate",
|
|
68
|
+
label: "Chrome DevTools: Navigate",
|
|
69
|
+
description: "Navigate a Chrome page to a URL through Chrome DevTools Protocol.",
|
|
70
|
+
promptSnippet: "Navigate the selected Chrome tab to a URL",
|
|
71
|
+
parameters: Type.Object({
|
|
72
|
+
url: Type.String({ description: "URL to navigate to." }),
|
|
73
|
+
pageId: Type.Optional(
|
|
74
|
+
Type.String({ description: "Optional page id. Defaults to selected or first page." }),
|
|
75
|
+
),
|
|
76
|
+
}),
|
|
77
|
+
async execute(_toolCallId, params) {
|
|
78
|
+
const page = await resolvePage(params.pageId);
|
|
79
|
+
const result = await withCdp(page, async (client) => {
|
|
80
|
+
await client.send("Page.enable");
|
|
81
|
+
return client.send("Page.navigate", { url: params.url });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
state.activePageId = page.id;
|
|
85
|
+
return textResult(`Navigated ${page.id} to ${params.url}`, { page: formatPage(page), result });
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const evaluateTool = defineTool({
|
|
90
|
+
name: "chrome_devtools_evaluate",
|
|
91
|
+
label: "Chrome DevTools: Evaluate",
|
|
92
|
+
description: "Evaluate JavaScript in a Chrome page through Chrome DevTools Protocol.",
|
|
93
|
+
promptSnippet: "Evaluate JavaScript in the selected Chrome tab",
|
|
94
|
+
parameters: Type.Object({
|
|
95
|
+
expression: Type.String({ description: "JavaScript expression to evaluate." }),
|
|
96
|
+
pageId: Type.Optional(
|
|
97
|
+
Type.String({ description: "Optional page id. Defaults to selected or first page." }),
|
|
98
|
+
),
|
|
99
|
+
awaitPromise: Type.Optional(
|
|
100
|
+
Type.Boolean({ description: "Whether to await a returned Promise. Defaults to true." }),
|
|
101
|
+
),
|
|
102
|
+
}),
|
|
103
|
+
async execute(_toolCallId, params) {
|
|
104
|
+
const page = await resolvePage(params.pageId);
|
|
105
|
+
const result = await withCdp(page, (client) =>
|
|
106
|
+
client.send("Runtime.evaluate", {
|
|
107
|
+
expression: params.expression,
|
|
108
|
+
awaitPromise: params.awaitPromise ?? true,
|
|
109
|
+
returnByValue: true,
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
state.activePageId = page.id;
|
|
114
|
+
return textResult(JSON.stringify(result, null, 2), { page: formatPage(page), result });
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const screenshotTool = defineTool({
|
|
119
|
+
name: "chrome_devtools_screenshot",
|
|
120
|
+
label: "Chrome DevTools: Screenshot",
|
|
121
|
+
description: "Capture a PNG screenshot from a Chrome page through Chrome DevTools Protocol.",
|
|
122
|
+
promptSnippet: "Capture a screenshot from the selected Chrome tab",
|
|
123
|
+
parameters: Type.Object({
|
|
124
|
+
pageId: Type.Optional(
|
|
125
|
+
Type.String({ description: "Optional page id. Defaults to selected or first page." }),
|
|
126
|
+
),
|
|
127
|
+
fullPage: Type.Optional(
|
|
128
|
+
Type.Boolean({ description: "Capture the full document, not just the viewport." }),
|
|
129
|
+
),
|
|
130
|
+
}),
|
|
131
|
+
async execute(_toolCallId, params) {
|
|
132
|
+
const page = await resolvePage(params.pageId);
|
|
133
|
+
const result = await withCdp(page, async (client) => {
|
|
134
|
+
await client.send("Page.enable");
|
|
135
|
+
|
|
136
|
+
if (!params.fullPage) {
|
|
137
|
+
return client.send<{ data: string }>("Page.captureScreenshot", { format: "png" });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const metrics = await client.send<{
|
|
141
|
+
contentSize: { x: number; y: number; width: number; height: number };
|
|
142
|
+
}>("Page.getLayoutMetrics");
|
|
143
|
+
|
|
144
|
+
return client.send<{ data: string }>("Page.captureScreenshot", {
|
|
145
|
+
captureBeyondViewport: true,
|
|
146
|
+
format: "png",
|
|
147
|
+
clip: {
|
|
148
|
+
x: metrics.contentSize.x,
|
|
149
|
+
y: metrics.contentSize.y,
|
|
150
|
+
width: metrics.contentSize.width,
|
|
151
|
+
height: metrics.contentSize.height,
|
|
152
|
+
scale: 1,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
state.activePageId = page.id;
|
|
158
|
+
return {
|
|
159
|
+
content: [
|
|
160
|
+
{ type: "text", text: `Captured PNG screenshot from ${page.title || page.url}` },
|
|
161
|
+
{ type: "image", data: result.data, mimeType: "image/png" },
|
|
162
|
+
],
|
|
163
|
+
details: { page: formatPage(page), bytes: Buffer.byteLength(result.data, "base64") },
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
export default function chromeDevtools(pi: ExtensionAPI) {
|
|
169
|
+
pi.registerTool(listPagesTool);
|
|
170
|
+
pi.registerTool(selectPageTool);
|
|
171
|
+
pi.registerTool(navigateTool);
|
|
172
|
+
pi.registerTool(evaluateTool);
|
|
173
|
+
pi.registerTool(screenshotTool);
|
|
174
|
+
|
|
175
|
+
pi.registerCommand("chrome-devtools", {
|
|
176
|
+
description: "Show Chrome DevTools endpoint and quick start help",
|
|
177
|
+
handler: async (_args, ctx) => {
|
|
178
|
+
ctx.ui.notify(
|
|
179
|
+
`Chrome DevTools endpoint: http://${state.host}:${state.port}. Start Chrome with --remote-debugging-port=${state.port}.`,
|
|
180
|
+
"info",
|
|
181
|
+
);
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
pi.on("session_start", (_event, ctx) => {
|
|
186
|
+
ctx.ui.setStatus("chrome-devtools", `cdp: ${state.host}:${state.port}`);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
190
|
+
ctx.ui.setStatus("chrome-devtools", undefined);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function listPages() {
|
|
195
|
+
const response = await fetch(`http://${state.host}:${state.port}/json/list`);
|
|
196
|
+
if (!response.ok) {
|
|
197
|
+
throw new Error(`Chrome DevTools endpoint returned ${response.status} ${response.statusText}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const pages = (await response.json()) as DevToolsPage[];
|
|
201
|
+
return pages.filter((page) => page.type === "page" && page.webSocketDebuggerUrl);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function getPage(pageId: string) {
|
|
205
|
+
const pages = await listPages();
|
|
206
|
+
const page = pages.find((candidate) => candidate.id === pageId);
|
|
207
|
+
if (!page) throw new Error(`Chrome DevTools page not found: ${pageId}`);
|
|
208
|
+
return page;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function resolvePage(pageId?: string) {
|
|
212
|
+
if (pageId) return getPage(pageId);
|
|
213
|
+
if (state.activePageId) return getPage(state.activePageId);
|
|
214
|
+
|
|
215
|
+
const pages = await listPages();
|
|
216
|
+
const page = pages[0];
|
|
217
|
+
if (!page) {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`No Chrome pages found at http://${state.host}:${state.port}. Start Chrome with --remote-debugging-port=${state.port}.`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return page;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function formatPage(page: DevToolsPage) {
|
|
227
|
+
return {
|
|
228
|
+
id: page.id,
|
|
229
|
+
type: page.type,
|
|
230
|
+
title: page.title,
|
|
231
|
+
url: page.url,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function textResult(text: string, details: unknown) {
|
|
236
|
+
return {
|
|
237
|
+
content: [{ type: "text" as const, text }],
|
|
238
|
+
details,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async function withCdp<T>(page: DevToolsPage, callback: (client: CdpClient) => Promise<T>) {
|
|
243
|
+
if (!page.webSocketDebuggerUrl) throw new Error(`Page has no webSocketDebuggerUrl: ${page.id}`);
|
|
244
|
+
|
|
245
|
+
const client = await CdpClient.connect(page.webSocketDebuggerUrl);
|
|
246
|
+
try {
|
|
247
|
+
return await callback(client);
|
|
248
|
+
} finally {
|
|
249
|
+
client.close();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
class CdpClient {
|
|
254
|
+
#nextId = 1;
|
|
255
|
+
#pending = new Map<
|
|
256
|
+
number,
|
|
257
|
+
{
|
|
258
|
+
resolve: (value: unknown) => void;
|
|
259
|
+
reject: (reason: unknown) => void;
|
|
260
|
+
timeout: NodeJS.Timeout;
|
|
261
|
+
}
|
|
262
|
+
>();
|
|
263
|
+
|
|
264
|
+
private constructor(private readonly socket: WebSocket) {
|
|
265
|
+
socket.addEventListener("message", (event) => {
|
|
266
|
+
const response = JSON.parse(String(event.data)) as CdpResponse;
|
|
267
|
+
if (typeof response.id !== "number") return;
|
|
268
|
+
|
|
269
|
+
const pending = this.#pending.get(response.id);
|
|
270
|
+
if (!pending) return;
|
|
271
|
+
|
|
272
|
+
clearTimeout(pending.timeout);
|
|
273
|
+
this.#pending.delete(response.id);
|
|
274
|
+
|
|
275
|
+
if (response.error) {
|
|
276
|
+
pending.reject(new Error(`CDP error ${response.error.code}: ${response.error.message}`));
|
|
277
|
+
} else {
|
|
278
|
+
pending.resolve(response.result);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
socket.addEventListener("close", () => {
|
|
283
|
+
this.rejectAll(new Error("Chrome DevTools WebSocket closed"));
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
socket.addEventListener("error", () => {
|
|
287
|
+
this.rejectAll(new Error("Chrome DevTools WebSocket error"));
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
static connect(url: string) {
|
|
292
|
+
return new Promise<CdpClient>((resolve, reject) => {
|
|
293
|
+
const socket = new WebSocket(url);
|
|
294
|
+
const timeout = setTimeout(() => {
|
|
295
|
+
socket.close();
|
|
296
|
+
reject(new Error(`Timed out connecting to Chrome DevTools WebSocket: ${url}`));
|
|
297
|
+
}, DEFAULT_TIMEOUT_MS);
|
|
298
|
+
|
|
299
|
+
socket.addEventListener("open", () => {
|
|
300
|
+
clearTimeout(timeout);
|
|
301
|
+
resolve(new CdpClient(socket));
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
socket.addEventListener("error", () => {
|
|
305
|
+
clearTimeout(timeout);
|
|
306
|
+
reject(new Error(`Failed to connect to Chrome DevTools WebSocket: ${url}`));
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
send<T = unknown>(method: string, params?: Record<string, unknown>) {
|
|
312
|
+
const id = this.#nextId;
|
|
313
|
+
this.#nextId += 1;
|
|
314
|
+
|
|
315
|
+
return new Promise<T>((resolve, reject) => {
|
|
316
|
+
const timeout = setTimeout(() => {
|
|
317
|
+
this.#pending.delete(id);
|
|
318
|
+
reject(new Error(`Timed out waiting for CDP response: ${method}`));
|
|
319
|
+
}, DEFAULT_TIMEOUT_MS);
|
|
320
|
+
|
|
321
|
+
this.#pending.set(id, { resolve: resolve as (value: unknown) => void, reject, timeout });
|
|
322
|
+
this.socket.send(JSON.stringify({ id, method, params: params ?? {} }));
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
close() {
|
|
327
|
+
this.socket.close();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private rejectAll(error: Error) {
|
|
331
|
+
for (const [id, pending] of this.#pending) {
|
|
332
|
+
clearTimeout(pending.timeout);
|
|
333
|
+
pending.reject(error);
|
|
334
|
+
this.#pending.delete(id);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|