mcp-browser-dev-tools 0.0.1-beta.1 → 0.0.1-beta.11
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/README.md +260 -13
- package/package.json +4 -1
- package/src/browser-adapter.mjs +275 -0
- package/src/browser-launcher.mjs +35 -0
- package/src/cdp-client.mjs +526 -85
- package/src/cli.mjs +212 -9
- package/src/config.mjs +24 -9
- package/src/doctor.mjs +25 -5
- package/src/firefox-bidi-client.mjs +509 -100
- package/src/index.mjs +13 -1
- package/src/json-rpc-stdio.mjs +130 -21
- package/src/mcp-server.mjs +360 -5
- package/src/page-context.mjs +734 -0
- package/src/session-events.mjs +16 -2
- package/src/tcp-relay.mjs +194 -0
package/src/cdp-client.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
filterConsoleMessages,
|
|
3
3
|
summarizeNetworkRequests,
|
|
4
4
|
} from "./session-events.mjs";
|
|
5
|
+
import { buildPageContextExpression } from "./page-context.mjs";
|
|
5
6
|
|
|
6
7
|
function toErrorMessage(error) {
|
|
7
8
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -18,6 +19,10 @@ function normalizeTarget(target) {
|
|
|
18
19
|
};
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
function cdpBrowserFamily(config) {
|
|
23
|
+
return config.browserFamily === "edge" ? "edge" : "chromium";
|
|
24
|
+
}
|
|
25
|
+
|
|
21
26
|
function summarizeRemoteObject(object) {
|
|
22
27
|
if (!object) {
|
|
23
28
|
return null;
|
|
@@ -42,26 +47,100 @@ function summarizeRemoteObject(object) {
|
|
|
42
47
|
};
|
|
43
48
|
}
|
|
44
49
|
|
|
45
|
-
function
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
function summarizeCallFrames(callFrames = []) {
|
|
51
|
+
return callFrames.map((frame) => ({
|
|
52
|
+
functionName: frame.functionName || null,
|
|
53
|
+
url: frame.url || null,
|
|
54
|
+
lineNumber: frame.lineNumber ?? null,
|
|
55
|
+
columnNumber: frame.columnNumber ?? null,
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function firstCallFrame(stackTrace) {
|
|
60
|
+
const frame = stackTrace?.callFrames?.[0];
|
|
61
|
+
if (!frame) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
functionName: frame.functionName || null,
|
|
67
|
+
url: frame.url || null,
|
|
68
|
+
lineNumber: frame.lineNumber ?? null,
|
|
69
|
+
columnNumber: frame.columnNumber ?? null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function normalizeConsoleText(values = []) {
|
|
74
|
+
const text = values
|
|
75
|
+
.filter((value) => value !== null && value !== undefined)
|
|
76
|
+
.map((value) =>
|
|
77
|
+
typeof value === "string" ? value : JSON.stringify(value, null, 2),
|
|
78
|
+
)
|
|
79
|
+
.join(" ");
|
|
80
|
+
|
|
81
|
+
return text || null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function screenshotMimeType(format) {
|
|
85
|
+
if (format === "jpeg") {
|
|
86
|
+
return "image/jpeg";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (format === "webp") {
|
|
90
|
+
return "image/webp";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return "image/png";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function formatScreenshotResult(data, format, extras = {}) {
|
|
97
|
+
return {
|
|
98
|
+
format,
|
|
99
|
+
mimeType: screenshotMimeType(format),
|
|
100
|
+
encoding: "base64",
|
|
101
|
+
byteLength: Buffer.byteLength(data, "base64"),
|
|
102
|
+
data,
|
|
103
|
+
...extras,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function normalizeWaitUntil(value) {
|
|
108
|
+
if (value === "none" || value === "interactive") {
|
|
109
|
+
return value;
|
|
49
110
|
}
|
|
50
111
|
|
|
51
|
-
return
|
|
112
|
+
return "complete";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function cdpLifecycleEventFor(waitUntil) {
|
|
116
|
+
if (waitUntil === "interactive") {
|
|
117
|
+
return "Page.domContentEventFired";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (waitUntil === "none") {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return "Page.loadEventFired";
|
|
52
125
|
}
|
|
53
126
|
|
|
54
127
|
function normalizeEvent(method, params) {
|
|
55
128
|
if (method === "Runtime.consoleAPICalled") {
|
|
129
|
+
const values = (params.args || []).map(summarizeRemoteObject);
|
|
130
|
+
const source = firstCallFrame(params.stackTrace);
|
|
56
131
|
return {
|
|
57
132
|
kind: "console",
|
|
58
133
|
level: params.type,
|
|
59
134
|
timestamp: params.timestamp,
|
|
60
|
-
|
|
135
|
+
text: normalizeConsoleText(values),
|
|
136
|
+
values,
|
|
137
|
+
source,
|
|
138
|
+
stackTrace: summarizeCallFrames(params.stackTrace?.callFrames),
|
|
61
139
|
};
|
|
62
140
|
}
|
|
63
141
|
|
|
64
142
|
if (method === "Runtime.exceptionThrown") {
|
|
143
|
+
const exception = params.exceptionDetails?.exception ?? null;
|
|
65
144
|
return {
|
|
66
145
|
kind: "exception",
|
|
67
146
|
timestamp: params.timestamp,
|
|
@@ -69,16 +148,28 @@ function normalizeEvent(method, params) {
|
|
|
69
148
|
url: params.exceptionDetails?.url,
|
|
70
149
|
lineNumber: params.exceptionDetails?.lineNumber,
|
|
71
150
|
columnNumber: params.exceptionDetails?.columnNumber,
|
|
151
|
+
stackTrace: summarizeCallFrames(
|
|
152
|
+
params.exceptionDetails?.stackTrace?.callFrames,
|
|
153
|
+
),
|
|
154
|
+
exception:
|
|
155
|
+
exception && typeof exception === "object"
|
|
156
|
+
? summarizeRemoteObject(exception)
|
|
157
|
+
: null,
|
|
72
158
|
};
|
|
73
159
|
}
|
|
74
160
|
|
|
75
161
|
if (method === "Log.entryAdded") {
|
|
162
|
+
const source = firstCallFrame(params.entry?.stackTrace);
|
|
76
163
|
return {
|
|
77
164
|
kind: "log",
|
|
78
165
|
level: params.entry?.level,
|
|
79
166
|
text: params.entry?.text,
|
|
80
167
|
url: params.entry?.url,
|
|
81
168
|
timestamp: params.entry?.timestamp,
|
|
169
|
+
lineNumber: source?.lineNumber ?? null,
|
|
170
|
+
columnNumber: source?.columnNumber ?? null,
|
|
171
|
+
source,
|
|
172
|
+
stackTrace: summarizeCallFrames(params.entry?.stackTrace?.callFrames),
|
|
82
173
|
};
|
|
83
174
|
}
|
|
84
175
|
|
|
@@ -92,6 +183,7 @@ function normalizeEvent(method, params) {
|
|
|
92
183
|
method: params.request?.method,
|
|
93
184
|
url: params.request?.url,
|
|
94
185
|
resourceType: params.type,
|
|
186
|
+
source: "protocol",
|
|
95
187
|
};
|
|
96
188
|
}
|
|
97
189
|
|
|
@@ -107,6 +199,7 @@ function normalizeEvent(method, params) {
|
|
|
107
199
|
status: params.response?.status,
|
|
108
200
|
statusText: params.response?.statusText,
|
|
109
201
|
mimeType: params.response?.mimeType,
|
|
202
|
+
source: "protocol",
|
|
110
203
|
};
|
|
111
204
|
}
|
|
112
205
|
|
|
@@ -118,6 +211,8 @@ function normalizeEvent(method, params) {
|
|
|
118
211
|
failed: false,
|
|
119
212
|
timestamp: params.timestamp,
|
|
120
213
|
requestId: params.requestId,
|
|
214
|
+
encodedBodySize: params.encodedDataLength ?? null,
|
|
215
|
+
source: "protocol",
|
|
121
216
|
};
|
|
122
217
|
}
|
|
123
218
|
|
|
@@ -131,6 +226,33 @@ function normalizeEvent(method, params) {
|
|
|
131
226
|
requestId: params.requestId,
|
|
132
227
|
errorText: params.errorText,
|
|
133
228
|
canceled: params.canceled ?? false,
|
|
229
|
+
source: "protocol",
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (
|
|
234
|
+
method === "Page.frameNavigated" ||
|
|
235
|
+
method === "Page.navigatedWithinDocument"
|
|
236
|
+
) {
|
|
237
|
+
return {
|
|
238
|
+
kind: "page",
|
|
239
|
+
phase: "navigated",
|
|
240
|
+
timestamp: new Date().toISOString(),
|
|
241
|
+
url: params.frame?.url ?? params.url ?? null,
|
|
242
|
+
frameId: params.frame?.id ?? params.frameId ?? null,
|
|
243
|
+
loaderId: params.frame?.loaderId ?? params.loaderId ?? null,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (
|
|
248
|
+
method === "Page.loadEventFired" ||
|
|
249
|
+
method === "Page.domContentEventFired" ||
|
|
250
|
+
method === "Page.frameStoppedLoading"
|
|
251
|
+
) {
|
|
252
|
+
return {
|
|
253
|
+
kind: "page",
|
|
254
|
+
phase: method,
|
|
255
|
+
timestamp: params.timestamp ?? new Date().toISOString(),
|
|
134
256
|
};
|
|
135
257
|
}
|
|
136
258
|
|
|
@@ -176,6 +298,7 @@ async function readWebSocketData(data) {
|
|
|
176
298
|
export class CdpSession {
|
|
177
299
|
constructor(target, options = {}) {
|
|
178
300
|
this.id = crypto.randomUUID();
|
|
301
|
+
this.config = options.config ?? { browserFamily: "chromium" };
|
|
179
302
|
this.target = target;
|
|
180
303
|
this.eventBufferSize = options.eventBufferSize ?? 200;
|
|
181
304
|
this.onClosed = options.onClosed ?? null;
|
|
@@ -185,6 +308,10 @@ export class CdpSession {
|
|
|
185
308
|
this.connectedAt = new Date().toISOString();
|
|
186
309
|
this.websocket = null;
|
|
187
310
|
this.closed = false;
|
|
311
|
+
this.eventWaiters = new Set();
|
|
312
|
+
this.lastNavigationAt = null;
|
|
313
|
+
this.lastReloadAt = null;
|
|
314
|
+
this.viewportOverride = null;
|
|
188
315
|
}
|
|
189
316
|
|
|
190
317
|
markClosed() {
|
|
@@ -194,6 +321,7 @@ export class CdpSession {
|
|
|
194
321
|
|
|
195
322
|
this.closed = true;
|
|
196
323
|
this.rejectPending(new Error("CDP session closed"));
|
|
324
|
+
this.rejectEventWaiters(new Error("CDP session closed"));
|
|
197
325
|
this.onClosed?.(this);
|
|
198
326
|
}
|
|
199
327
|
|
|
@@ -258,6 +386,8 @@ export class CdpSession {
|
|
|
258
386
|
this.send("Network.enable"),
|
|
259
387
|
]);
|
|
260
388
|
|
|
389
|
+
await this.seedBufferedState();
|
|
390
|
+
|
|
261
391
|
return this.getSummary();
|
|
262
392
|
}
|
|
263
393
|
|
|
@@ -291,17 +421,96 @@ export class CdpSession {
|
|
|
291
421
|
}
|
|
292
422
|
|
|
293
423
|
bufferEvent(method, params) {
|
|
294
|
-
|
|
424
|
+
const capturedAt = new Date().toISOString();
|
|
425
|
+
if (
|
|
426
|
+
method === "Page.frameNavigated" &&
|
|
427
|
+
params.frame?.url &&
|
|
428
|
+
!params.frame?.parentId
|
|
429
|
+
) {
|
|
430
|
+
this.target.url = params.frame.url;
|
|
431
|
+
this.lastNavigationAt = capturedAt;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (method === "Page.navigatedWithinDocument" && params.url) {
|
|
435
|
+
this.target.url = params.url;
|
|
436
|
+
this.lastNavigationAt = capturedAt;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (method === "Page.loadEventFired") {
|
|
440
|
+
this.lastNavigationAt = capturedAt;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
this.pushEvent({
|
|
295
444
|
method,
|
|
296
|
-
capturedAt
|
|
445
|
+
capturedAt,
|
|
297
446
|
...normalizeEvent(method, params),
|
|
298
447
|
});
|
|
448
|
+
this.resolveEventWaiters(method, params);
|
|
449
|
+
}
|
|
299
450
|
|
|
451
|
+
pushEvent(event) {
|
|
452
|
+
this.bufferedEvents.push(event);
|
|
300
453
|
if (this.bufferedEvents.length > this.eventBufferSize) {
|
|
301
454
|
this.bufferedEvents.shift();
|
|
302
455
|
}
|
|
303
456
|
}
|
|
304
457
|
|
|
458
|
+
resolveEventWaiters(method, params) {
|
|
459
|
+
for (const waiter of Array.from(this.eventWaiters)) {
|
|
460
|
+
if (waiter.method !== method) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
try {
|
|
465
|
+
if (!waiter.predicate(params)) {
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
} catch {
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
clearTimeout(waiter.timer);
|
|
473
|
+
this.eventWaiters.delete(waiter);
|
|
474
|
+
waiter.resolve(params);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
rejectEventWaiters(error) {
|
|
479
|
+
for (const waiter of this.eventWaiters) {
|
|
480
|
+
clearTimeout(waiter.timer);
|
|
481
|
+
waiter.reject(error);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
this.eventWaiters.clear();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
createEventWaiter(method, predicate = () => true, timeoutMs = 10_000) {
|
|
488
|
+
let waiter = null;
|
|
489
|
+
const promise = new Promise((resolve, reject) => {
|
|
490
|
+
const timer = setTimeout(() => {
|
|
491
|
+
this.eventWaiters.delete(waiter);
|
|
492
|
+
reject(new Error(`Timed out waiting for ${method}`));
|
|
493
|
+
}, timeoutMs);
|
|
494
|
+
|
|
495
|
+
waiter = { method, predicate, resolve, reject, timer };
|
|
496
|
+
this.eventWaiters.add(waiter);
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
return {
|
|
500
|
+
promise,
|
|
501
|
+
cancel: (error = new Error(`Stopped waiting for ${method}`)) => {
|
|
502
|
+
if (waiter && this.eventWaiters.delete(waiter)) {
|
|
503
|
+
clearTimeout(waiter.timer);
|
|
504
|
+
waiter.reject(error);
|
|
505
|
+
}
|
|
506
|
+
},
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
waitForEvent(method, predicate = () => true, timeoutMs = 10_000) {
|
|
511
|
+
return this.createEventWaiter(method, predicate, timeoutMs).promise;
|
|
512
|
+
}
|
|
513
|
+
|
|
305
514
|
rejectPending(error) {
|
|
306
515
|
for (const pending of this.pending.values()) {
|
|
307
516
|
pending.reject(error);
|
|
@@ -328,13 +537,18 @@ export class CdpSession {
|
|
|
328
537
|
});
|
|
329
538
|
}
|
|
330
539
|
|
|
331
|
-
async
|
|
332
|
-
|
|
540
|
+
async evaluateRuntime(expression, options = {}) {
|
|
541
|
+
return this.send("Runtime.evaluate", {
|
|
333
542
|
expression,
|
|
334
543
|
awaitPromise: options.awaitPromise ?? true,
|
|
335
544
|
returnByValue: options.returnByValue ?? true,
|
|
336
|
-
replMode: true,
|
|
545
|
+
replMode: options.replMode ?? true,
|
|
546
|
+
userGesture: options.userGesture ?? false,
|
|
337
547
|
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
async evaluate(expression, options = {}) {
|
|
551
|
+
const result = await this.evaluateRuntime(expression, options);
|
|
338
552
|
|
|
339
553
|
return {
|
|
340
554
|
result: summarizeRemoteObject(result.result),
|
|
@@ -349,8 +563,88 @@ export class CdpSession {
|
|
|
349
563
|
});
|
|
350
564
|
}
|
|
351
565
|
|
|
352
|
-
async
|
|
353
|
-
|
|
566
|
+
async runPageAction(payload, options = {}) {
|
|
567
|
+
const result = await this.evaluateRuntime(
|
|
568
|
+
buildPageContextExpression(
|
|
569
|
+
{
|
|
570
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
571
|
+
...payload,
|
|
572
|
+
},
|
|
573
|
+
{ serialize: true },
|
|
574
|
+
),
|
|
575
|
+
{
|
|
576
|
+
awaitPromise: true,
|
|
577
|
+
replMode: false,
|
|
578
|
+
userGesture: options.userGesture ?? false,
|
|
579
|
+
},
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
if (result.exceptionDetails) {
|
|
583
|
+
throw new Error(result.exceptionDetails.text || "Page action failed");
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const value = result.result?.value ?? summarizeRemoteObject(result.result);
|
|
587
|
+
return typeof value === "string" ? JSON.parse(value) : value;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
async getPageState() {
|
|
591
|
+
const result = await this.runPageAction({
|
|
592
|
+
action: "page_state",
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
return {
|
|
596
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
597
|
+
...result.page,
|
|
598
|
+
lastNavigationAt: this.lastNavigationAt,
|
|
599
|
+
lastReloadAt: this.lastReloadAt,
|
|
600
|
+
viewportOverride: this.viewportOverride,
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
async takeScreenshot(format = "png", options = {}) {
|
|
605
|
+
if (options.selector) {
|
|
606
|
+
const inspected = await this.runPageAction({
|
|
607
|
+
action: "inspect",
|
|
608
|
+
selector: options.selector,
|
|
609
|
+
scrollIntoView: true,
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
if (!inspected.found) {
|
|
613
|
+
return {
|
|
614
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
615
|
+
format,
|
|
616
|
+
scope: "element",
|
|
617
|
+
selector: options.selector,
|
|
618
|
+
found: false,
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const clip = {
|
|
623
|
+
x: Math.max(inspected.node.box?.x ?? 0, 0),
|
|
624
|
+
y: Math.max(inspected.node.box?.y ?? 0, 0),
|
|
625
|
+
width: Math.max(inspected.node.box?.width ?? 0, 1),
|
|
626
|
+
height: Math.max(inspected.node.box?.height ?? 0, 1),
|
|
627
|
+
scale: 1,
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
const screenshot = await this.send("Page.captureScreenshot", {
|
|
631
|
+
format,
|
|
632
|
+
clip,
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
return formatScreenshotResult(screenshot.data, format, {
|
|
636
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
637
|
+
scope: "element",
|
|
638
|
+
selector: options.selector,
|
|
639
|
+
clip,
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const screenshot = await this.send("Page.captureScreenshot", { format });
|
|
644
|
+
return formatScreenshotResult(screenshot.data, format, {
|
|
645
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
646
|
+
scope: "page",
|
|
647
|
+
});
|
|
354
648
|
}
|
|
355
649
|
|
|
356
650
|
getConsoleMessages(limit = 50) {
|
|
@@ -362,93 +656,196 @@ export class CdpSession {
|
|
|
362
656
|
}
|
|
363
657
|
|
|
364
658
|
async inspectElement(selector) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (!rootNodeId) {
|
|
368
|
-
throw new Error("Unable to resolve the DOM root node");
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
const { nodeId } = await this.send("DOM.querySelector", {
|
|
372
|
-
nodeId: rootNodeId,
|
|
659
|
+
return this.runPageAction({
|
|
660
|
+
action: "inspect",
|
|
373
661
|
selector,
|
|
374
662
|
});
|
|
663
|
+
}
|
|
375
664
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
665
|
+
async click(selector) {
|
|
666
|
+
return this.runPageAction(
|
|
667
|
+
{
|
|
668
|
+
action: "click",
|
|
379
669
|
selector,
|
|
380
|
-
|
|
381
|
-
}
|
|
382
|
-
|
|
670
|
+
},
|
|
671
|
+
{ userGesture: true },
|
|
672
|
+
);
|
|
673
|
+
}
|
|
383
674
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
this.send("DOM.getAttributes", { nodeId }),
|
|
392
|
-
this.send("DOM.getOuterHTML", { nodeId }),
|
|
393
|
-
this.readNodeText(nodeId),
|
|
394
|
-
],
|
|
675
|
+
async hover(selector) {
|
|
676
|
+
return this.runPageAction(
|
|
677
|
+
{
|
|
678
|
+
action: "hover",
|
|
679
|
+
selector,
|
|
680
|
+
},
|
|
681
|
+
{ userGesture: true },
|
|
395
682
|
);
|
|
683
|
+
}
|
|
396
684
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
nodeName: description.node?.nodeName ?? null,
|
|
405
|
-
localName: description.node?.localName ?? null,
|
|
406
|
-
nodeType: description.node?.nodeType ?? null,
|
|
407
|
-
childNodeCount: description.node?.childNodeCount ?? 0,
|
|
408
|
-
attributes: toAttributeObject(attributes.attributes),
|
|
409
|
-
textContent: textContent.textContent,
|
|
410
|
-
innerText: textContent.innerText,
|
|
411
|
-
outerHTML: outerHtml.outerHTML ?? null,
|
|
685
|
+
async type(selector, text, options = {}) {
|
|
686
|
+
return this.runPageAction(
|
|
687
|
+
{
|
|
688
|
+
action: "type",
|
|
689
|
+
selector,
|
|
690
|
+
text,
|
|
691
|
+
clear: options.clear,
|
|
412
692
|
},
|
|
413
|
-
|
|
693
|
+
{ userGesture: true },
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
async select(selector, options = {}) {
|
|
698
|
+
return this.runPageAction(
|
|
699
|
+
{
|
|
700
|
+
action: "select",
|
|
701
|
+
selector,
|
|
702
|
+
value: options.value,
|
|
703
|
+
label: options.label,
|
|
704
|
+
},
|
|
705
|
+
{ userGesture: true },
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
async pressKey(key, selector = null) {
|
|
710
|
+
return this.runPageAction(
|
|
711
|
+
{
|
|
712
|
+
action: "press_key",
|
|
713
|
+
key,
|
|
714
|
+
selector,
|
|
715
|
+
},
|
|
716
|
+
{ userGesture: true },
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
async scroll(options = {}) {
|
|
721
|
+
return this.runPageAction(
|
|
722
|
+
{
|
|
723
|
+
action: "scroll",
|
|
724
|
+
selector: options.selector,
|
|
725
|
+
deltaX: options.deltaX,
|
|
726
|
+
deltaY: options.deltaY,
|
|
727
|
+
block: options.block,
|
|
728
|
+
},
|
|
729
|
+
{ userGesture: true },
|
|
730
|
+
);
|
|
414
731
|
}
|
|
415
732
|
|
|
416
|
-
async
|
|
417
|
-
|
|
733
|
+
async navigate(url, options = {}) {
|
|
734
|
+
const waitUntil = normalizeWaitUntil(options.waitUntil);
|
|
735
|
+
const initiatedAt = new Date().toISOString();
|
|
736
|
+
const lifecycleEvent = cdpLifecycleEventFor(waitUntil);
|
|
737
|
+
const lifecycleWaiter = lifecycleEvent
|
|
738
|
+
? this.createEventWaiter(lifecycleEvent, () => true, options.timeoutMs)
|
|
739
|
+
: null;
|
|
418
740
|
|
|
419
741
|
try {
|
|
420
|
-
const
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
return {
|
|
424
|
-
textContent: null,
|
|
425
|
-
innerText: null,
|
|
426
|
-
};
|
|
742
|
+
const result = await this.send("Page.navigate", { url });
|
|
743
|
+
if (result.errorText) {
|
|
744
|
+
throw new Error(result.errorText);
|
|
427
745
|
}
|
|
428
746
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
returnByValue: true,
|
|
434
|
-
silent: true,
|
|
435
|
-
});
|
|
747
|
+
this.target.url = url;
|
|
748
|
+
if (lifecycleWaiter) {
|
|
749
|
+
await lifecycleWaiter.promise;
|
|
750
|
+
}
|
|
436
751
|
|
|
437
|
-
|
|
438
|
-
result.result?.value ?? {
|
|
439
|
-
textContent: null,
|
|
440
|
-
innerText: null,
|
|
441
|
-
}
|
|
442
|
-
);
|
|
443
|
-
} catch {
|
|
752
|
+
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
444
753
|
return {
|
|
445
|
-
|
|
446
|
-
|
|
754
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
755
|
+
url,
|
|
756
|
+
frameId: result.frameId ?? null,
|
|
757
|
+
loaderId: result.loaderId ?? null,
|
|
758
|
+
waitUntil,
|
|
759
|
+
initiatedAt,
|
|
760
|
+
page,
|
|
447
761
|
};
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
|
|
762
|
+
} catch (error) {
|
|
763
|
+
lifecycleWaiter?.cancel(error);
|
|
764
|
+
throw error;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
async reload(options = {}) {
|
|
769
|
+
const waitUntil = normalizeWaitUntil(options.waitUntil);
|
|
770
|
+
const reloadedAt = new Date().toISOString();
|
|
771
|
+
const lifecycleEvent = cdpLifecycleEventFor(waitUntil);
|
|
772
|
+
const lifecycleWaiter = lifecycleEvent
|
|
773
|
+
? this.createEventWaiter(lifecycleEvent, () => true, options.timeoutMs)
|
|
774
|
+
: null;
|
|
775
|
+
|
|
776
|
+
try {
|
|
777
|
+
await this.send("Page.reload", {
|
|
778
|
+
ignoreCache: options.ignoreCache ?? false,
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
if (lifecycleWaiter) {
|
|
782
|
+
await lifecycleWaiter.promise;
|
|
451
783
|
}
|
|
784
|
+
} catch (error) {
|
|
785
|
+
lifecycleWaiter?.cancel(error);
|
|
786
|
+
throw error;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
this.lastReloadAt = reloadedAt;
|
|
790
|
+
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
791
|
+
return {
|
|
792
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
793
|
+
url: this.target.url,
|
|
794
|
+
waitUntil,
|
|
795
|
+
ignoreCache: options.ignoreCache ?? false,
|
|
796
|
+
reloadedAt,
|
|
797
|
+
page,
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
async setViewport(options) {
|
|
802
|
+
const viewport = {
|
|
803
|
+
width: options.width,
|
|
804
|
+
height: options.height,
|
|
805
|
+
deviceScaleFactor:
|
|
806
|
+
typeof options.deviceScaleFactor === "number" &&
|
|
807
|
+
options.deviceScaleFactor > 0
|
|
808
|
+
? options.deviceScaleFactor
|
|
809
|
+
: 1,
|
|
810
|
+
mobile: options.mobile ?? false,
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
await this.send("Emulation.setDeviceMetricsOverride", viewport);
|
|
814
|
+
this.viewportOverride = {
|
|
815
|
+
...viewport,
|
|
816
|
+
appliedAt: new Date().toISOString(),
|
|
817
|
+
};
|
|
818
|
+
|
|
819
|
+
return {
|
|
820
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
821
|
+
applied: true,
|
|
822
|
+
viewport: this.viewportOverride,
|
|
823
|
+
page: await this.getPageState(),
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
async seedBufferedState() {
|
|
828
|
+
try {
|
|
829
|
+
const snapshot = await this.runPageAction({
|
|
830
|
+
action: "network_snapshot",
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
for (const entry of snapshot?.entries ?? []) {
|
|
834
|
+
this.pushEvent({
|
|
835
|
+
method: "Network.snapshotCaptured",
|
|
836
|
+
capturedAt: new Date().toISOString(),
|
|
837
|
+
kind: "network",
|
|
838
|
+
phase: "snapshot",
|
|
839
|
+
completed: true,
|
|
840
|
+
finished: true,
|
|
841
|
+
failed: false,
|
|
842
|
+
canceled: false,
|
|
843
|
+
source: "performance",
|
|
844
|
+
...entry,
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
} catch {
|
|
848
|
+
// Ignore snapshot failures so attach still succeeds against restrictive pages.
|
|
452
849
|
}
|
|
453
850
|
}
|
|
454
851
|
|
|
@@ -463,9 +860,12 @@ export class CdpSession {
|
|
|
463
860
|
targetId: this.target.targetId,
|
|
464
861
|
title: this.target.title,
|
|
465
862
|
url: this.target.url,
|
|
466
|
-
browserFamily:
|
|
863
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
467
864
|
connectedAt: this.connectedAt,
|
|
468
865
|
bufferedEvents: this.bufferedEvents.length,
|
|
866
|
+
lastNavigationAt: this.lastNavigationAt,
|
|
867
|
+
lastReloadAt: this.lastReloadAt,
|
|
868
|
+
viewportOverride: this.viewportOverride,
|
|
469
869
|
};
|
|
470
870
|
}
|
|
471
871
|
|
|
@@ -533,6 +933,7 @@ export class CdpSessionManager {
|
|
|
533
933
|
}
|
|
534
934
|
|
|
535
935
|
const session = new CdpSession(target, {
|
|
936
|
+
config: this.config,
|
|
536
937
|
eventBufferSize: this.config.eventBufferSize,
|
|
537
938
|
onClosed: (closedSession) => {
|
|
538
939
|
this.sessions.delete(closedSession.id);
|
|
@@ -570,8 +971,48 @@ export class CdpSessionManager {
|
|
|
570
971
|
return this.getSession(sessionId).getDocument(depth);
|
|
571
972
|
}
|
|
572
973
|
|
|
573
|
-
async
|
|
574
|
-
return this.getSession(sessionId).
|
|
974
|
+
async getPageState(sessionId) {
|
|
975
|
+
return this.getSession(sessionId).getPageState();
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
async navigate(sessionId, url, options) {
|
|
979
|
+
return this.getSession(sessionId).navigate(url, options);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
async reload(sessionId, options) {
|
|
983
|
+
return this.getSession(sessionId).reload(options);
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
async click(sessionId, selector) {
|
|
987
|
+
return this.getSession(sessionId).click(selector);
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
async hover(sessionId, selector) {
|
|
991
|
+
return this.getSession(sessionId).hover(selector);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
async type(sessionId, selector, text, options) {
|
|
995
|
+
return this.getSession(sessionId).type(selector, text, options);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
async select(sessionId, selector, options) {
|
|
999
|
+
return this.getSession(sessionId).select(selector, options);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
async pressKey(sessionId, key, selector) {
|
|
1003
|
+
return this.getSession(sessionId).pressKey(key, selector);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
async scroll(sessionId, options) {
|
|
1007
|
+
return this.getSession(sessionId).scroll(options);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
async setViewport(sessionId, options) {
|
|
1011
|
+
return this.getSession(sessionId).setViewport(options);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
async takeScreenshot(sessionId, format, options = {}) {
|
|
1015
|
+
return this.getSession(sessionId).takeScreenshot(format, options);
|
|
575
1016
|
}
|
|
576
1017
|
|
|
577
1018
|
getConsoleMessages(sessionId, limit) {
|