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