mcp-browser-dev-tools 0.2.1 → 0.3.0
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 +17 -15
- package/package.json +9 -2
- package/src/action-changes.mjs +340 -0
- package/src/browser-adapter.mjs +40 -4
- package/src/cdp-client.mjs +1006 -50
- package/src/config.mjs +9 -0
- package/src/firefox-bidi-client.mjs +834 -51
- package/src/input-delivery.mjs +82 -0
- package/src/json-schema.mjs +14 -1
- package/src/mcp-server.mjs +59 -8
- package/src/network-control.mjs +156 -0
- package/src/page-context.mjs +315 -3
- package/src/page-script.mjs +1317 -14
- package/src/run-steps.mjs +75 -23
- package/src/run-tabs.mjs +11 -16
- package/src/screenshot-output.mjs +92 -1
- package/src/session-events.mjs +57 -0
- package/src/tool-schemas.mjs +88 -83
- package/src/tool-sets.mjs +73 -0
- package/src/tools/action-tools.mjs +217 -77
- package/src/tools/browser-tools.mjs +12 -19
- package/src/tools/inspection-tools.mjs +88 -54
- package/src/tools/more-tools.mjs +108 -0
- package/src/tools/performance-tools.mjs +116 -0
- package/src/tools/state-tools.mjs +62 -16
- package/src/upload-files.mjs +57 -0
package/src/page-script.mjs
CHANGED
|
@@ -182,6 +182,7 @@ export function pageScript(payload) {
|
|
|
182
182
|
if (tag === "button") return "button";
|
|
183
183
|
if (tag === "dialog") return "dialog";
|
|
184
184
|
if (tag === "img") return "img";
|
|
185
|
+
if (/^h[1-6]$/.test(tag)) return "heading";
|
|
185
186
|
if (tag === "select") return "combobox";
|
|
186
187
|
if (tag === "textarea") return "textbox";
|
|
187
188
|
if (tag === "option") return "option";
|
|
@@ -194,6 +195,9 @@ export function pageScript(payload) {
|
|
|
194
195
|
if (type === "checkbox") return "checkbox";
|
|
195
196
|
if (type === "radio") return "radio";
|
|
196
197
|
if (type === "range") return "slider";
|
|
198
|
+
// Not an ARIA role: it tells an agent to use upload_file, since a
|
|
199
|
+
// click opens a native file dialog.
|
|
200
|
+
if (type === "file") return "file";
|
|
197
201
|
return "textbox";
|
|
198
202
|
}
|
|
199
203
|
|
|
@@ -371,6 +375,77 @@ export function pageScript(payload) {
|
|
|
371
375
|
};
|
|
372
376
|
}
|
|
373
377
|
|
|
378
|
+
// Refs (e12) name elements across calls: get_snapshot hands them out and
|
|
379
|
+
// ref= locators look them up. The registry lives on the window, so it
|
|
380
|
+
// lasts until the page navigates, and an element keeps its ref between
|
|
381
|
+
// snapshots. Numbering continues from payload.refStart, which the server
|
|
382
|
+
// carries across navigations, so a ref from an earlier page never names
|
|
383
|
+
// an element on a later one. Elements that left the document are dropped
|
|
384
|
+
// at each snapshot, so the registry does not keep an SPA's old trees alive.
|
|
385
|
+
const REF_REGISTRY_KEY = "__mcpBrowserDevToolsRefs";
|
|
386
|
+
|
|
387
|
+
function refRegistry() {
|
|
388
|
+
let registry = window[REF_REGISTRY_KEY];
|
|
389
|
+
if (!registry) {
|
|
390
|
+
registry = { next: 1, byRef: new Map(), byElement: new WeakMap() };
|
|
391
|
+
Object.defineProperty(window, REF_REGISTRY_KEY, {
|
|
392
|
+
value: registry,
|
|
393
|
+
enumerable: false,
|
|
394
|
+
configurable: true,
|
|
395
|
+
writable: false,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
const start = Number(payload.refStart);
|
|
399
|
+
if (start > registry.next) {
|
|
400
|
+
registry.next = start;
|
|
401
|
+
}
|
|
402
|
+
return registry;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function pruneRefs(registry) {
|
|
406
|
+
for (const [ref, element] of registry.byRef) {
|
|
407
|
+
if (!element.isConnected) {
|
|
408
|
+
registry.byRef.delete(ref);
|
|
409
|
+
registry.byElement.delete(element);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function refFor(element) {
|
|
415
|
+
const registry = refRegistry();
|
|
416
|
+
let ref = registry.byElement.get(element);
|
|
417
|
+
if (!ref) {
|
|
418
|
+
ref = "e" + registry.next++;
|
|
419
|
+
registry.byElement.set(element, ref);
|
|
420
|
+
registry.byRef.set(ref, element);
|
|
421
|
+
}
|
|
422
|
+
return ref;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function resolveRef(query) {
|
|
426
|
+
const registry = window[REF_REGISTRY_KEY];
|
|
427
|
+
const element = registry?.byRef.get(query) ?? null;
|
|
428
|
+
if (!element) {
|
|
429
|
+
return {
|
|
430
|
+
element: null,
|
|
431
|
+
error:
|
|
432
|
+
"Unknown ref " +
|
|
433
|
+
query +
|
|
434
|
+
": refs come from get_snapshot on this page and a navigation invalidates them; call get_snapshot again",
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
if (!element.isConnected) {
|
|
438
|
+
return {
|
|
439
|
+
element: null,
|
|
440
|
+
error:
|
|
441
|
+
"Stale ref " +
|
|
442
|
+
query +
|
|
443
|
+
": its element left the document; call get_snapshot again",
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
return { element };
|
|
447
|
+
}
|
|
448
|
+
|
|
374
449
|
function parseRoleLocator(value) {
|
|
375
450
|
const match = value.match(
|
|
376
451
|
/^([^[]+?)(?:\[name=(?:"([^"]*)"|'([^']*)')\])?$/,
|
|
@@ -408,6 +483,7 @@ export function pageScript(payload) {
|
|
|
408
483
|
"tab",
|
|
409
484
|
"menuitem",
|
|
410
485
|
"option",
|
|
486
|
+
"file",
|
|
411
487
|
];
|
|
412
488
|
|
|
413
489
|
function resolvesTo(locator, element) {
|
|
@@ -456,12 +532,15 @@ export function pageScript(payload) {
|
|
|
456
532
|
return null;
|
|
457
533
|
}
|
|
458
534
|
|
|
459
|
-
|
|
535
|
+
// Finding a locator resolves candidates against the whole page, so the
|
|
536
|
+
// line-based snapshots, which do not print it, skip it.
|
|
537
|
+
function describeControl(element, role, options = {}) {
|
|
460
538
|
const name = normalizeText(getAccessibleName(element));
|
|
461
|
-
const control = {
|
|
462
|
-
|
|
463
|
-
role,
|
|
464
|
-
}
|
|
539
|
+
const control = { ref: refFor(element) };
|
|
540
|
+
if (options.locators !== false) {
|
|
541
|
+
control.locator = suggestLocator(element, role, name);
|
|
542
|
+
}
|
|
543
|
+
control.role = role;
|
|
465
544
|
if (name) {
|
|
466
545
|
control.name = clipText(name, SNAPSHOT_TEXT_LIMIT);
|
|
467
546
|
}
|
|
@@ -479,6 +558,17 @@ export function pageScript(payload) {
|
|
|
479
558
|
delete control.name;
|
|
480
559
|
}
|
|
481
560
|
}
|
|
561
|
+
if (role === "file" && element.files) {
|
|
562
|
+
control.value = clipText(
|
|
563
|
+
Array.from(element.files, (file) => file.name).join(", "),
|
|
564
|
+
SNAPSHOT_TEXT_LIMIT,
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
// A password's value stays out, but whether it is filled is shown, so
|
|
568
|
+
// typing one is visible in the snapshot and in an action's changes.
|
|
569
|
+
if (isPassword(element) && typeof element.value === "string") {
|
|
570
|
+
control.filled = element.value.length > 0;
|
|
571
|
+
}
|
|
482
572
|
if (role === "checkbox" || role === "radio" || role === "switch") {
|
|
483
573
|
control.checked =
|
|
484
574
|
"checked" in element
|
|
@@ -491,25 +581,624 @@ export function pageScript(payload) {
|
|
|
491
581
|
return control;
|
|
492
582
|
}
|
|
493
583
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
584
|
+
function describeHeading(element) {
|
|
585
|
+
const tag = element.tagName.toLowerCase();
|
|
586
|
+
const level = /^h[1-6]$/.test(tag)
|
|
587
|
+
? Number(tag.slice(1))
|
|
588
|
+
: Number(element.getAttribute("aria-level")) || 2;
|
|
589
|
+
return {
|
|
590
|
+
ref: refFor(element),
|
|
591
|
+
role: "heading",
|
|
592
|
+
level,
|
|
593
|
+
name: clipText(
|
|
594
|
+
normalizeText(getAccessibleName(element)),
|
|
595
|
+
SNAPSHOT_TEXT_LIMIT,
|
|
596
|
+
),
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// Lists the visible controls on the page (and its headings, when asked),
|
|
601
|
+
// in document order, so an agent can decide what to do next, or fix a
|
|
602
|
+
// failed step, without another round trip to look around.
|
|
603
|
+
function snapshotControls(options = {}) {
|
|
604
|
+
pruneRefs(refRegistry());
|
|
605
|
+
const limit = options.limit ?? SNAPSHOT_LIMIT;
|
|
606
|
+
const root = options.root ?? document;
|
|
607
|
+
const candidates =
|
|
608
|
+
root === document
|
|
609
|
+
? collectCandidates()
|
|
610
|
+
: Array.from(root.querySelectorAll("*"));
|
|
611
|
+
if (root !== document && root !== document.body) {
|
|
612
|
+
candidates.unshift(root);
|
|
613
|
+
}
|
|
497
614
|
const controls = [];
|
|
498
615
|
let more = 0;
|
|
499
|
-
for (const element of
|
|
616
|
+
for (const element of candidates) {
|
|
500
617
|
const role = inferRole(element);
|
|
501
618
|
const editable =
|
|
502
619
|
element.hasAttribute("contenteditable") && element.isContentEditable;
|
|
503
|
-
|
|
620
|
+
const heading = options.headings && role === "heading";
|
|
621
|
+
if (
|
|
622
|
+
!(CONTROL_ROLES.includes(role) || editable || heading) ||
|
|
623
|
+
!isVisible(element)
|
|
624
|
+
) {
|
|
504
625
|
continue;
|
|
505
626
|
}
|
|
506
|
-
if (controls.length >=
|
|
627
|
+
if (controls.length >= limit) {
|
|
507
628
|
more += 1;
|
|
629
|
+
options.beyond?.add(element);
|
|
508
630
|
continue;
|
|
509
631
|
}
|
|
510
|
-
controls.push(
|
|
632
|
+
controls.push(
|
|
633
|
+
heading
|
|
634
|
+
? describeHeading(element)
|
|
635
|
+
: describeControl(element, role ?? "textbox", options),
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
return { controls, moreControls: more, nextRef: refRegistry().next };
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// One line per node, compact enough to send on every turn:
|
|
642
|
+
// e3 textbox "Email" value="ada@example.com"
|
|
643
|
+
// e4 checkbox "Accept terms" checked
|
|
644
|
+
function formatSnapshotLine(node) {
|
|
645
|
+
const parts = [
|
|
646
|
+
node.ref,
|
|
647
|
+
node.role === "heading" ? "h" + node.level : node.role,
|
|
648
|
+
];
|
|
649
|
+
if (node.name) {
|
|
650
|
+
parts.push(JSON.stringify(node.name));
|
|
651
|
+
}
|
|
652
|
+
if (node.value !== undefined) {
|
|
653
|
+
parts.push("value=" + JSON.stringify(node.value));
|
|
654
|
+
}
|
|
655
|
+
if (node.filled) {
|
|
656
|
+
parts.push("filled");
|
|
657
|
+
}
|
|
658
|
+
if (node.checked !== undefined) {
|
|
659
|
+
parts.push(node.checked ? "checked" : "unchecked");
|
|
660
|
+
}
|
|
661
|
+
if (node.disabled) {
|
|
662
|
+
parts.push("disabled");
|
|
663
|
+
}
|
|
664
|
+
return parts.join(" ");
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
function snapshotPage() {
|
|
668
|
+
const result = {
|
|
669
|
+
browserFamily: payload.browserFamily,
|
|
670
|
+
url: location.href,
|
|
671
|
+
title: document.title,
|
|
672
|
+
};
|
|
673
|
+
let root = document;
|
|
674
|
+
if (payload.selector) {
|
|
675
|
+
const resolved = ensureResolved(payload.selector);
|
|
676
|
+
if (!resolved.element) {
|
|
677
|
+
return { ...result, ...resolved };
|
|
678
|
+
}
|
|
679
|
+
root = resolved.element;
|
|
511
680
|
}
|
|
512
|
-
|
|
681
|
+
const { controls, moreControls, nextRef } = snapshotControls({
|
|
682
|
+
root,
|
|
683
|
+
limit: payload.limit,
|
|
684
|
+
headings: payload.headings !== false,
|
|
685
|
+
locators: false,
|
|
686
|
+
});
|
|
687
|
+
return {
|
|
688
|
+
...result,
|
|
689
|
+
found: true,
|
|
690
|
+
nodes: controls.map(formatSnapshotLine),
|
|
691
|
+
more: moreControls,
|
|
692
|
+
nextRef,
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// Actions report what they changed. change_baseline records the visible
|
|
697
|
+
// headings and controls, and the text of live regions, before the action
|
|
698
|
+
// and watches the DOM; change_status tells the server how long the DOM has
|
|
699
|
+
// been quiet; change_report diffs against the baseline and lists the text
|
|
700
|
+
// the action added or revealed. Each action keeps its own state, keyed by
|
|
701
|
+
// its change id, so overlapping actions do not disturb each other; each
|
|
702
|
+
// baseline drops the states its server no longer tracks.
|
|
703
|
+
// A navigation brings a new window with a new document id; on it,
|
|
704
|
+
// change_status watches the new document for quiet and change_report
|
|
705
|
+
// returns its snapshot.
|
|
706
|
+
const CHANGE_KEY = "__mcpBrowserDevToolsChanges";
|
|
707
|
+
const DOCUMENT_KEY = "__mcpBrowserDevToolsDocument";
|
|
708
|
+
const CHANGE_CONTROL_LIMIT = 500;
|
|
709
|
+
const CHANGE_LINE_LIMIT = 15;
|
|
710
|
+
const CHANGE_TEXT_LIMIT = 5;
|
|
711
|
+
const CHANGE_TEXT_CHARS = 160;
|
|
712
|
+
const CHANGE_WATCH_LIMIT = 200;
|
|
713
|
+
const CHANGE_MESSAGE_LIMIT = 200;
|
|
714
|
+
// States of another server (another MCP client on the tab) are dropped
|
|
715
|
+
// when this old, in case that server went away mid-action.
|
|
716
|
+
const CHANGE_STALE_MS = 3_600_000;
|
|
717
|
+
const MESSAGE_SELECTOR =
|
|
718
|
+
'[role="alert"], [role="status"], [role="alertdialog"], [aria-live], output';
|
|
719
|
+
// Attributes whose change can reveal an element without adding nodes.
|
|
720
|
+
const REVEAL_ATTRIBUTES = ["hidden", "aria-hidden", "open"];
|
|
721
|
+
|
|
722
|
+
function defineHidden(key, value) {
|
|
723
|
+
Object.defineProperty(window, key, {
|
|
724
|
+
value,
|
|
725
|
+
enumerable: false,
|
|
726
|
+
configurable: true,
|
|
727
|
+
writable: false,
|
|
728
|
+
});
|
|
729
|
+
return value;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
function documentId() {
|
|
733
|
+
return (
|
|
734
|
+
window[DOCUMENT_KEY] ??
|
|
735
|
+
defineHidden(
|
|
736
|
+
DOCUMENT_KEY,
|
|
737
|
+
Math.random().toString(36).slice(2) + Date.now().toString(36),
|
|
738
|
+
)
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function changeStates() {
|
|
743
|
+
return window[CHANGE_KEY] ?? defineHidden(CHANGE_KEY, new Map());
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// Keeps the distinct elements that gained nodes or text, and apart from
|
|
747
|
+
// them those whose hiding attribute changed, to read their text later.
|
|
748
|
+
// Each set is capped, so a page that rebuilds itself stays cheap.
|
|
749
|
+
function remember(set, element) {
|
|
750
|
+
if (element && set.size < CHANGE_WATCH_LIMIT) {
|
|
751
|
+
set.add(element);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function recordMutations(state, records) {
|
|
756
|
+
state.lastMutationAt = Date.now();
|
|
757
|
+
for (const record of records) {
|
|
758
|
+
if (record.type === "attributes") {
|
|
759
|
+
if (REVEAL_ATTRIBUTES.includes(record.attributeName)) {
|
|
760
|
+
remember(state.revealed, record.target);
|
|
761
|
+
}
|
|
762
|
+
} else if (record.type === "characterData") {
|
|
763
|
+
remember(state.touched, record.target.parentElement);
|
|
764
|
+
} else if (record.type === "childList") {
|
|
765
|
+
for (const node of record.addedNodes) {
|
|
766
|
+
remember(
|
|
767
|
+
state.touched,
|
|
768
|
+
node.nodeType === 1 ? node : node.parentElement,
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// Changes often come a moment after the action, from a timer: a redirect
|
|
776
|
+
// 300 ms after "Signed in", a search debounced until typing pauses. While
|
|
777
|
+
// an action is tracked, setTimeout is wrapped to count the timers set with
|
|
778
|
+
// delays from TIMER_MIN_MS to TIMER_MAX_MS, so the server waits for them
|
|
779
|
+
// as it does for requests. Loops (polling, animation) would keep a page
|
|
780
|
+
// from settling, so a timer is not counted when it is set from inside
|
|
781
|
+
// another timer's callback, or when its handler already ran while the
|
|
782
|
+
// action was tracked. The wrappers are put over whatever the page has at
|
|
783
|
+
// the time and put back when no action is tracked, unless the page has
|
|
784
|
+
// since wrapped them in turn; they then stay, passing calls through.
|
|
785
|
+
const TIMER_KEY = "__mcpBrowserDevToolsTimers";
|
|
786
|
+
const TIMER_MIN_MS = 50;
|
|
787
|
+
const TIMER_MAX_MS = 1000;
|
|
788
|
+
|
|
789
|
+
// A delay as the browser would read it, without calling a page object's
|
|
790
|
+
// valueOf, which could differ between calls.
|
|
791
|
+
function timerDelay(delay) {
|
|
792
|
+
if (delay === undefined) {
|
|
793
|
+
return 0;
|
|
794
|
+
}
|
|
795
|
+
if (typeof delay === "number" || typeof delay === "string") {
|
|
796
|
+
return Number(delay);
|
|
797
|
+
}
|
|
798
|
+
return Number.NaN;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
function timerWatch() {
|
|
802
|
+
return (
|
|
803
|
+
window[TIMER_KEY] ??
|
|
804
|
+
defineHidden(TIMER_KEY, {
|
|
805
|
+
timers: new Map(),
|
|
806
|
+
ran: new WeakSet(),
|
|
807
|
+
callbacks: new WeakSet(),
|
|
808
|
+
users: 0,
|
|
809
|
+
inTimer: false,
|
|
810
|
+
installed: {},
|
|
811
|
+
})
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// Fresh wrappers for each period of tracking, each calling the function
|
|
816
|
+
// it was put over. One a page saved earlier may still be in its chain;
|
|
817
|
+
// a new one cannot be, so calls never loop back into it.
|
|
818
|
+
function timerWrappers(watch, natives) {
|
|
819
|
+
const call = (name, args) => natives[name].apply(window, args);
|
|
820
|
+
const wrappers = {
|
|
821
|
+
setTimeout(handler, delay, ...args) {
|
|
822
|
+
// A callback of ours, passed on by a newer wrapper through a page's
|
|
823
|
+
// chain, was counted (or not) there already.
|
|
824
|
+
if (typeof handler !== "function" || watch.callbacks.has(handler)) {
|
|
825
|
+
return call("setTimeout", [handler, delay, ...args]);
|
|
826
|
+
}
|
|
827
|
+
const ms = timerDelay(delay);
|
|
828
|
+
const counted =
|
|
829
|
+
watch.users > 0 &&
|
|
830
|
+
!watch.inTimer &&
|
|
831
|
+
!watch.ran.has(handler) &&
|
|
832
|
+
ms >= TIMER_MIN_MS &&
|
|
833
|
+
ms <= TIMER_MAX_MS;
|
|
834
|
+
let id;
|
|
835
|
+
const callback = function (...callbackArgs) {
|
|
836
|
+
watch.timers.delete(id);
|
|
837
|
+
if (watch.users > 0) {
|
|
838
|
+
watch.ran.add(handler);
|
|
839
|
+
}
|
|
840
|
+
const outer = watch.inTimer;
|
|
841
|
+
watch.inTimer = true;
|
|
842
|
+
try {
|
|
843
|
+
return handler.apply(this, callbackArgs);
|
|
844
|
+
} finally {
|
|
845
|
+
watch.inTimer = outer;
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
watch.callbacks.add(callback);
|
|
849
|
+
id = call("setTimeout", [callback, delay, ...args]);
|
|
850
|
+
if (counted) {
|
|
851
|
+
watch.timers.set(id, Date.now());
|
|
852
|
+
}
|
|
853
|
+
return id;
|
|
854
|
+
},
|
|
855
|
+
};
|
|
856
|
+
// Timeouts and intervals share ids, so either can cancel a timeout.
|
|
857
|
+
for (const name of ["clearTimeout", "clearInterval"]) {
|
|
858
|
+
wrappers[name] = function (id) {
|
|
859
|
+
watch.timers.delete(id);
|
|
860
|
+
return call(name, [id]);
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
return wrappers;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
function watchTimers() {
|
|
867
|
+
const watch = timerWatch();
|
|
868
|
+
watch.users += 1;
|
|
869
|
+
if (watch.users > 1) {
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const names = ["setTimeout", "clearTimeout", "clearInterval"];
|
|
873
|
+
const natives = Object.fromEntries(
|
|
874
|
+
names.map((name) => [name, window[name]]),
|
|
875
|
+
);
|
|
876
|
+
const wrappers = timerWrappers(watch, natives);
|
|
877
|
+
for (const name of names) {
|
|
878
|
+
window[name] = wrappers[name];
|
|
879
|
+
watch.installed[name] = {
|
|
880
|
+
native: natives[name],
|
|
881
|
+
wrapper: wrappers[name],
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
function unwatchTimers() {
|
|
887
|
+
const watch = window[TIMER_KEY];
|
|
888
|
+
if (!watch || watch.users === 0) {
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
watch.users -= 1;
|
|
892
|
+
if (watch.users > 0) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
watch.timers.clear();
|
|
896
|
+
watch.ran = new WeakSet();
|
|
897
|
+
for (const [name, { native, wrapper }] of Object.entries(watch.installed)) {
|
|
898
|
+
if (window[name] === wrapper) {
|
|
899
|
+
window[name] = native;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
watch.installed = {};
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// Timers counted since the state's baseline that have not fired.
|
|
906
|
+
function pendingTimers(state) {
|
|
907
|
+
let pending = 0;
|
|
908
|
+
for (const createdAt of window[TIMER_KEY]?.timers.values() ?? []) {
|
|
909
|
+
if (createdAt >= state.createdAt) {
|
|
910
|
+
pending += 1;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return pending;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
function startChangeTracking(state) {
|
|
917
|
+
watchTimers();
|
|
918
|
+
if (typeof MutationObserver === "function") {
|
|
919
|
+
state.observer = new MutationObserver((records) =>
|
|
920
|
+
recordMutations(state, records),
|
|
921
|
+
);
|
|
922
|
+
state.observer.observe(document, {
|
|
923
|
+
subtree: true,
|
|
924
|
+
childList: true,
|
|
925
|
+
attributes: true,
|
|
926
|
+
characterData: true,
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
changeStates().set(state.id, state);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
function stopChangeTracking(state) {
|
|
933
|
+
state.observer?.disconnect();
|
|
934
|
+
const states = window[CHANGE_KEY];
|
|
935
|
+
if (states?.get(state.id) === state) {
|
|
936
|
+
states.delete(state.id);
|
|
937
|
+
unwatchTimers();
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// Lines for the first CHANGE_CONTROL_LIMIT visible headings and controls,
|
|
942
|
+
// and the set of visible ones past it, so the diff does not mistake an
|
|
943
|
+
// element crossing the limit for one that appeared or disappeared.
|
|
944
|
+
function changeSnapshot() {
|
|
945
|
+
const beyond = new Set();
|
|
946
|
+
const { controls } = snapshotControls({
|
|
947
|
+
headings: true,
|
|
948
|
+
limit: CHANGE_CONTROL_LIMIT,
|
|
949
|
+
locators: false,
|
|
950
|
+
beyond,
|
|
951
|
+
});
|
|
952
|
+
return {
|
|
953
|
+
lines: new Map(
|
|
954
|
+
controls.map((control) => [control.ref, formatSnapshotLine(control)]),
|
|
955
|
+
),
|
|
956
|
+
beyond,
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// The 1px, clipped live regions that announce to screen readers, which
|
|
961
|
+
// extensions such as password managers add as the page is used.
|
|
962
|
+
function isScreenReaderOnly(element) {
|
|
963
|
+
const rect = element.getBoundingClientRect();
|
|
964
|
+
return rect.width < 2 || rect.height < 2;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// isShown, unlike isVisible, keeps text under pointer-events: none, such
|
|
968
|
+
// as a toast.
|
|
969
|
+
function shownText(element) {
|
|
970
|
+
return isShown(element) && !isScreenReaderOnly(element)
|
|
971
|
+
? normalizeText(getVisibleText(element))
|
|
972
|
+
: "";
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
// A live region or alert can be revealed by a class or style change alone,
|
|
976
|
+
// which adds no nodes, so its text is compared with the baseline.
|
|
977
|
+
function messageTexts() {
|
|
978
|
+
const texts = new Map();
|
|
979
|
+
const elements = Array.from(document.querySelectorAll(MESSAGE_SELECTOR));
|
|
980
|
+
for (const element of elements.slice(0, CHANGE_MESSAGE_LIMIT)) {
|
|
981
|
+
texts.set(element, shownText(element));
|
|
982
|
+
}
|
|
983
|
+
return texts;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
function dropAbandonedStates() {
|
|
987
|
+
const active = Array.isArray(payload.active) ? payload.active : [];
|
|
988
|
+
for (const state of Array.from(changeStates().values())) {
|
|
989
|
+
const abandoned =
|
|
990
|
+
state.owner === payload.owner
|
|
991
|
+
? !active.includes(state.id)
|
|
992
|
+
: Date.now() - state.createdAt > CHANGE_STALE_MS;
|
|
993
|
+
if (abandoned) {
|
|
994
|
+
stopChangeTracking(state);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
function changeBaseline() {
|
|
1000
|
+
dropAbandonedStates();
|
|
1001
|
+
pruneRefs(refRegistry());
|
|
1002
|
+
const { lines, beyond } = changeSnapshot();
|
|
1003
|
+
startChangeTracking({
|
|
1004
|
+
id: payload.changeId,
|
|
1005
|
+
owner: payload.owner,
|
|
1006
|
+
createdAt: Date.now(),
|
|
1007
|
+
url: location.href,
|
|
1008
|
+
title: document.title,
|
|
1009
|
+
lines,
|
|
1010
|
+
beyond,
|
|
1011
|
+
messages: messageTexts(),
|
|
1012
|
+
touched: new Set(),
|
|
1013
|
+
revealed: new Set(),
|
|
1014
|
+
lastMutationAt: Date.now(),
|
|
1015
|
+
observer: null,
|
|
1016
|
+
});
|
|
1017
|
+
return { documentId: documentId(), nextRef: refRegistry().next };
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
function changeStatus() {
|
|
1021
|
+
const states = changeStates();
|
|
1022
|
+
if (documentId() !== payload.documentId) {
|
|
1023
|
+
let watcher = states.get(payload.changeId);
|
|
1024
|
+
if (!watcher) {
|
|
1025
|
+
watcher = {
|
|
1026
|
+
id: payload.changeId,
|
|
1027
|
+
owner: payload.owner,
|
|
1028
|
+
createdAt: Date.now(),
|
|
1029
|
+
touched: new Set(),
|
|
1030
|
+
revealed: new Set(),
|
|
1031
|
+
lastMutationAt: Date.now(),
|
|
1032
|
+
observer: null,
|
|
1033
|
+
};
|
|
1034
|
+
startChangeTracking(watcher);
|
|
1035
|
+
}
|
|
1036
|
+
return {
|
|
1037
|
+
document: "new",
|
|
1038
|
+
readyState: document.readyState,
|
|
1039
|
+
quietMs: Date.now() - watcher.lastMutationAt,
|
|
1040
|
+
timers: pendingTimers(watcher),
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
const state = states.get(payload.changeId);
|
|
1044
|
+
if (!state) {
|
|
1045
|
+
return { document: "same", lost: true };
|
|
1046
|
+
}
|
|
1047
|
+
return {
|
|
1048
|
+
document: "same",
|
|
1049
|
+
quietMs: Date.now() - state.lastMutationAt,
|
|
1050
|
+
timers: pendingTimers(state),
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
function changeStop() {
|
|
1055
|
+
const state = changeStates().get(payload.changeId);
|
|
1056
|
+
if (state) {
|
|
1057
|
+
stopChangeTracking(state);
|
|
1058
|
+
}
|
|
1059
|
+
return { stopped: Boolean(state) };
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
// A list item wrapping a link says nothing the link's line does not.
|
|
1063
|
+
function onlyControlText(element) {
|
|
1064
|
+
if (typeof element.querySelectorAll !== "function") {
|
|
1065
|
+
return false;
|
|
1066
|
+
}
|
|
1067
|
+
const controls = Array.from(element.querySelectorAll("*")).filter((node) =>
|
|
1068
|
+
CONTROL_ROLES.includes(inferRole(node)),
|
|
1069
|
+
);
|
|
1070
|
+
if (controls.length === 0) {
|
|
1071
|
+
return false;
|
|
1072
|
+
}
|
|
1073
|
+
const controlText = controls
|
|
1074
|
+
.map((control) => normalizeText(getVisibleText(control)))
|
|
1075
|
+
.join(" ");
|
|
1076
|
+
return normalizeText(controlText) === shownText(element);
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
function insideControl(element) {
|
|
1080
|
+
for (let node = element; node; node = node.parentElement) {
|
|
1081
|
+
if (CONTROL_ROLES.includes(inferRole(node))) {
|
|
1082
|
+
return true;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
return false;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
// The visible text of the outermost elements the action added, changed
|
|
1089
|
+
// the text of, or revealed, skipping controls, which the diff lists.
|
|
1090
|
+
function addedText(state) {
|
|
1091
|
+
const candidates = new Set(
|
|
1092
|
+
[...state.touched, ...state.revealed].filter(
|
|
1093
|
+
(element) => element.isConnected,
|
|
1094
|
+
),
|
|
1095
|
+
);
|
|
1096
|
+
for (const [element, before] of state.messages) {
|
|
1097
|
+
const now = element.isConnected ? shownText(element) : "";
|
|
1098
|
+
if (now && now !== before) {
|
|
1099
|
+
candidates.add(element);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
const elements = Array.from(candidates);
|
|
1103
|
+
const outermost = elements.filter(
|
|
1104
|
+
(element) =>
|
|
1105
|
+
!elements.some(
|
|
1106
|
+
(other) =>
|
|
1107
|
+
other !== element &&
|
|
1108
|
+
typeof other.contains === "function" &&
|
|
1109
|
+
other.contains(element),
|
|
1110
|
+
),
|
|
1111
|
+
);
|
|
1112
|
+
const items = [];
|
|
1113
|
+
let more = 0;
|
|
1114
|
+
for (const element of outermost) {
|
|
1115
|
+
if (insideControl(element) || onlyControlText(element)) {
|
|
1116
|
+
continue;
|
|
1117
|
+
}
|
|
1118
|
+
const text = clipText(shownText(element), CHANGE_TEXT_CHARS);
|
|
1119
|
+
if (!text || items.includes(text)) {
|
|
1120
|
+
continue;
|
|
1121
|
+
}
|
|
1122
|
+
if (items.length >= CHANGE_TEXT_LIMIT) {
|
|
1123
|
+
more += 1;
|
|
1124
|
+
continue;
|
|
1125
|
+
}
|
|
1126
|
+
items.push(text);
|
|
1127
|
+
}
|
|
1128
|
+
return { items, more };
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
function capLines(lines) {
|
|
1132
|
+
return {
|
|
1133
|
+
lines: lines.slice(0, CHANGE_LINE_LIMIT),
|
|
1134
|
+
more: Math.max(0, lines.length - CHANGE_LINE_LIMIT),
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
function changeReport() {
|
|
1139
|
+
const page = { url: location.href, title: document.title };
|
|
1140
|
+
const state = changeStates().get(payload.changeId);
|
|
1141
|
+
if (documentId() !== payload.documentId) {
|
|
1142
|
+
if (state) {
|
|
1143
|
+
stopChangeTracking(state);
|
|
1144
|
+
}
|
|
1145
|
+
const { controls, moreControls, nextRef } = snapshotControls({
|
|
1146
|
+
headings: true,
|
|
1147
|
+
limit: payload.limit,
|
|
1148
|
+
locators: false,
|
|
1149
|
+
});
|
|
1150
|
+
return {
|
|
1151
|
+
newDocument: true,
|
|
1152
|
+
...page,
|
|
1153
|
+
nodes: controls.map(formatSnapshotLine),
|
|
1154
|
+
more: moreControls,
|
|
1155
|
+
nextRef,
|
|
1156
|
+
};
|
|
1157
|
+
}
|
|
1158
|
+
if (!state) {
|
|
1159
|
+
return { lost: true, ...page };
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
if (state.observer) {
|
|
1163
|
+
const records = state.observer.takeRecords();
|
|
1164
|
+
if (records.length > 0) {
|
|
1165
|
+
recordMutations(state, records);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
stopChangeTracking(state);
|
|
1169
|
+
const registry = refRegistry();
|
|
1170
|
+
pruneRefs(registry);
|
|
1171
|
+
const after = changeSnapshot();
|
|
1172
|
+
const added = [];
|
|
1173
|
+
const updated = [];
|
|
1174
|
+
const removed = [];
|
|
1175
|
+
for (const [ref, line] of after.lines) {
|
|
1176
|
+
const before = state.lines.get(ref);
|
|
1177
|
+
if (before === undefined) {
|
|
1178
|
+
if (!state.beyond.has(registry.byRef.get(ref))) {
|
|
1179
|
+
added.push(line);
|
|
1180
|
+
}
|
|
1181
|
+
} else if (before !== line) {
|
|
1182
|
+
updated.push(line);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
for (const [ref, line] of state.lines) {
|
|
1186
|
+
const element = registry.byRef.get(ref);
|
|
1187
|
+
if (!after.lines.has(ref) && !(element && after.beyond.has(element))) {
|
|
1188
|
+
removed.push(line);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
return {
|
|
1192
|
+
newDocument: false,
|
|
1193
|
+
...page,
|
|
1194
|
+
urlChanged: page.url !== state.url,
|
|
1195
|
+
titleChanged: page.title !== state.title,
|
|
1196
|
+
added: capLines(added),
|
|
1197
|
+
removed: capLines(removed),
|
|
1198
|
+
updated: capLines(updated),
|
|
1199
|
+
text: addedText(state),
|
|
1200
|
+
nextRef: registry.next,
|
|
1201
|
+
};
|
|
513
1202
|
}
|
|
514
1203
|
|
|
515
1204
|
function describePage() {
|
|
@@ -652,6 +1341,16 @@ export function pageScript(payload) {
|
|
|
652
1341
|
throw new Error("selector must be a non-empty string");
|
|
653
1342
|
}
|
|
654
1343
|
|
|
1344
|
+
if (locator.startsWith("ref=")) {
|
|
1345
|
+
const query = locator.slice(4).trim();
|
|
1346
|
+
const { element, error } = resolveRef(query);
|
|
1347
|
+
const resolved = { locator, strategy: "ref", query, element };
|
|
1348
|
+
if (error) {
|
|
1349
|
+
resolved.error = error;
|
|
1350
|
+
}
|
|
1351
|
+
return resolved;
|
|
1352
|
+
}
|
|
1353
|
+
|
|
655
1354
|
if (locator.startsWith("text=")) {
|
|
656
1355
|
const query = normalizeText(locator.slice(5));
|
|
657
1356
|
const candidates = collectCandidates();
|
|
@@ -811,6 +1510,393 @@ export function pageScript(payload) {
|
|
|
811
1510
|
});
|
|
812
1511
|
}
|
|
813
1512
|
|
|
1513
|
+
// The element under a point, short: a ref to act on it again, and its
|
|
1514
|
+
// role and name when it has a role (a plain element's name would be all
|
|
1515
|
+
// of its text).
|
|
1516
|
+
function describePointTarget(element) {
|
|
1517
|
+
const target = { ref: refFor(element), ...summarizeTarget(element) };
|
|
1518
|
+
const role = inferRole(element);
|
|
1519
|
+
if (role) {
|
|
1520
|
+
target.role = role;
|
|
1521
|
+
const name = normalizeText(getAccessibleName(element));
|
|
1522
|
+
if (name) {
|
|
1523
|
+
target.name = clipText(name, SNAPSHOT_TEXT_LIMIT);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
if (isDisabled(element)) {
|
|
1527
|
+
target.disabled = true;
|
|
1528
|
+
}
|
|
1529
|
+
return target;
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
// Firefox starts a native HTML5 drag from WebDriver input but does not
|
|
1533
|
+
// finish it, so there an HTML5 drag runs as drag events dispatched here,
|
|
1534
|
+
// sharing one DataTransfer as a real drag would. The events are untrusted.
|
|
1535
|
+
// check only says whether the element under from is HTML5-draggable.
|
|
1536
|
+
function allowedOperations(allowed) {
|
|
1537
|
+
if (allowed === "all" || allowed === "uninitialized") {
|
|
1538
|
+
return ["copy", "link", "move"];
|
|
1539
|
+
}
|
|
1540
|
+
return ["copy", "link", "move"].filter((operation) =>
|
|
1541
|
+
allowed.toLowerCase().includes(operation),
|
|
1542
|
+
);
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
// Load timing, the Web Vitals the browser exposes, long tasks, and the
|
|
1546
|
+
// heaviest resources. LCP, layout shifts, interactions, and long tasks
|
|
1547
|
+
// are only readable through buffered observers, which deliver after a
|
|
1548
|
+
// task, hence the short wait. Ratings follow the Web Vitals thresholds.
|
|
1549
|
+
const VITAL_THRESHOLDS = {
|
|
1550
|
+
ttfb: [800, 1800],
|
|
1551
|
+
fcp: [1800, 3000],
|
|
1552
|
+
lcp: [2500, 4000],
|
|
1553
|
+
cls: [0.1, 0.25],
|
|
1554
|
+
inp: [200, 500],
|
|
1555
|
+
};
|
|
1556
|
+
|
|
1557
|
+
function rated(name, value) {
|
|
1558
|
+
if (value === null || value === undefined) {
|
|
1559
|
+
return null;
|
|
1560
|
+
}
|
|
1561
|
+
const [good, poor] = VITAL_THRESHOLDS[name];
|
|
1562
|
+
const rating =
|
|
1563
|
+
value <= good ? "good" : value <= poor ? "needs-improvement" : "poor";
|
|
1564
|
+
return {
|
|
1565
|
+
value:
|
|
1566
|
+
name === "cls" ? Math.round(value * 1000) / 1000 : Math.round(value),
|
|
1567
|
+
rating,
|
|
1568
|
+
};
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
function observeBuffered(type) {
|
|
1572
|
+
return new Promise((resolve) => {
|
|
1573
|
+
if (
|
|
1574
|
+
typeof PerformanceObserver !== "function" ||
|
|
1575
|
+
!PerformanceObserver.supportedEntryTypes?.includes(type)
|
|
1576
|
+
) {
|
|
1577
|
+
resolve(null);
|
|
1578
|
+
return;
|
|
1579
|
+
}
|
|
1580
|
+
const entries = [];
|
|
1581
|
+
let observer;
|
|
1582
|
+
try {
|
|
1583
|
+
observer = new PerformanceObserver((list) =>
|
|
1584
|
+
entries.push(...list.getEntries()),
|
|
1585
|
+
);
|
|
1586
|
+
observer.observe({
|
|
1587
|
+
type,
|
|
1588
|
+
buffered: true,
|
|
1589
|
+
...(type === "event" ? { durationThreshold: 16 } : {}),
|
|
1590
|
+
});
|
|
1591
|
+
} catch {
|
|
1592
|
+
resolve(null);
|
|
1593
|
+
return;
|
|
1594
|
+
}
|
|
1595
|
+
setTimeout(() => {
|
|
1596
|
+
entries.push(...observer.takeRecords());
|
|
1597
|
+
observer.disconnect();
|
|
1598
|
+
resolve(entries);
|
|
1599
|
+
}, 50);
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
// The largest sum of layout shifts in a session window, as the Web
|
|
1604
|
+
// Vitals library computes it: a shift joins the window when it is less
|
|
1605
|
+
// than 1 s after the previous one and less than 5 s after the first;
|
|
1606
|
+
// shifts right after input do not count.
|
|
1607
|
+
function cumulativeLayoutShift(shifts) {
|
|
1608
|
+
let worst = 0;
|
|
1609
|
+
let windowValue = 0;
|
|
1610
|
+
let first = null;
|
|
1611
|
+
let previous = null;
|
|
1612
|
+
for (const shift of shifts) {
|
|
1613
|
+
if (shift.hadRecentInput) {
|
|
1614
|
+
continue;
|
|
1615
|
+
}
|
|
1616
|
+
if (
|
|
1617
|
+
windowValue &&
|
|
1618
|
+
shift.startTime - previous.startTime < 1000 &&
|
|
1619
|
+
shift.startTime - first.startTime < 5000
|
|
1620
|
+
) {
|
|
1621
|
+
windowValue += shift.value;
|
|
1622
|
+
} else {
|
|
1623
|
+
windowValue = shift.value;
|
|
1624
|
+
first = shift;
|
|
1625
|
+
}
|
|
1626
|
+
previous = shift;
|
|
1627
|
+
worst = Math.max(worst, windowValue);
|
|
1628
|
+
}
|
|
1629
|
+
return worst;
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
function describeEntryElement(element) {
|
|
1633
|
+
if (!element) {
|
|
1634
|
+
return null;
|
|
1635
|
+
}
|
|
1636
|
+
const id = element.id ? "#" + element.id : "";
|
|
1637
|
+
const text = clipText(normalizeText(getVisibleText(element)), 60);
|
|
1638
|
+
return element.tagName.toLowerCase() + id + (text ? ' "' + text + '"' : "");
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
async function performanceReport() {
|
|
1642
|
+
const round = (value) =>
|
|
1643
|
+
value === null || value === undefined ? null : Math.round(value);
|
|
1644
|
+
const nav = performance.getEntriesByType("navigation")[0] ?? null;
|
|
1645
|
+
const fcp = performance.getEntriesByName("first-contentful-paint")[0];
|
|
1646
|
+
// After a back/forward-cache restore, only what came after it counts,
|
|
1647
|
+
// and the load metrics of the earlier visit do not apply.
|
|
1648
|
+
const recorder = window[Symbol.for("mcp-browser-dev-tools.inputRecorder")];
|
|
1649
|
+
const restoredAt = recorder?.restoredAt ?? null;
|
|
1650
|
+
const since = (entries) =>
|
|
1651
|
+
entries && restoredAt !== null
|
|
1652
|
+
? entries.filter((entry) => (entry.startTime ?? 0) >= restoredAt)
|
|
1653
|
+
: entries;
|
|
1654
|
+
const [lcps, shifts, events, longTasks] = (
|
|
1655
|
+
await Promise.all(
|
|
1656
|
+
["largest-contentful-paint", "layout-shift", "event", "longtask"].map(
|
|
1657
|
+
observeBuffered,
|
|
1658
|
+
),
|
|
1659
|
+
)
|
|
1660
|
+
).map(since);
|
|
1661
|
+
// A prerendered page's times count from when it was shown.
|
|
1662
|
+
const activation = nav?.activationStart || 0;
|
|
1663
|
+
const fromActivation = (time) =>
|
|
1664
|
+
time === undefined || time === null || restoredAt !== null
|
|
1665
|
+
? null
|
|
1666
|
+
: Math.max(0, time - activation);
|
|
1667
|
+
const lcp = lcps?.at(-1) ?? null;
|
|
1668
|
+
|
|
1669
|
+
// INP as the Web Vitals library estimates it: each interaction (a click,
|
|
1670
|
+
// tap, or key press) by its longest event, then the one at the 98th
|
|
1671
|
+
// percentile of the page's interactions, which drops one outlier per 50.
|
|
1672
|
+
// Browsers buffer only events of 104 ms or more from before the
|
|
1673
|
+
// observer, so faster interactions are not seen.
|
|
1674
|
+
const interactions = new Map();
|
|
1675
|
+
for (const event of events ?? []) {
|
|
1676
|
+
if (event.interactionId) {
|
|
1677
|
+
interactions.set(
|
|
1678
|
+
event.interactionId,
|
|
1679
|
+
Math.max(interactions.get(event.interactionId) ?? 0, event.duration),
|
|
1680
|
+
);
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
const durations = Array.from(interactions.values()).sort((x, y) => y - x);
|
|
1684
|
+
// Counted from the restore, after one.
|
|
1685
|
+
const interactionCount = Math.max(
|
|
1686
|
+
(performance.interactionCount ?? 0) -
|
|
1687
|
+
(restoredAt !== null ? (recorder.interactionsBeforeRestore ?? 0) : 0),
|
|
1688
|
+
durations.length,
|
|
1689
|
+
);
|
|
1690
|
+
const inp = durations.length
|
|
1691
|
+
? durations[
|
|
1692
|
+
Math.min(durations.length - 1, Math.floor(interactionCount / 50))
|
|
1693
|
+
]
|
|
1694
|
+
: null;
|
|
1695
|
+
|
|
1696
|
+
const resources = performance.getEntriesByType("resource");
|
|
1697
|
+
const byType = {};
|
|
1698
|
+
for (const entry of resources) {
|
|
1699
|
+
const type = entry.initiatorType || "other";
|
|
1700
|
+
byType[type] ??= { count: 0, transferBytes: 0 };
|
|
1701
|
+
byType[type].count += 1;
|
|
1702
|
+
byType[type].transferBytes += entry.transferSize || 0;
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
return {
|
|
1706
|
+
browserFamily: payload.browserFamily,
|
|
1707
|
+
url: location.href,
|
|
1708
|
+
navigation: nav
|
|
1709
|
+
? {
|
|
1710
|
+
type: nav.type,
|
|
1711
|
+
protocol: nav.nextHopProtocol || null,
|
|
1712
|
+
redirects: nav.redirectCount,
|
|
1713
|
+
domContentLoadedMs: round(nav.domContentLoadedEventEnd),
|
|
1714
|
+
loadMs: round(nav.loadEventEnd) || null,
|
|
1715
|
+
transferBytes: nav.transferSize ?? null,
|
|
1716
|
+
}
|
|
1717
|
+
: null,
|
|
1718
|
+
vitals: {
|
|
1719
|
+
ttfb: rated("ttfb", fromActivation(nav?.responseStart)),
|
|
1720
|
+
fcp: rated("fcp", fromActivation(fcp?.startTime)),
|
|
1721
|
+
lcp: lcp
|
|
1722
|
+
? {
|
|
1723
|
+
...rated("lcp", Math.max(0, lcp.startTime - activation)),
|
|
1724
|
+
element: describeEntryElement(lcp.element),
|
|
1725
|
+
url: lcp.url ? clipText(lcp.url, 120) : null,
|
|
1726
|
+
}
|
|
1727
|
+
: null,
|
|
1728
|
+
cls: shifts ? rated("cls", cumulativeLayoutShift(shifts)) : null,
|
|
1729
|
+
inp:
|
|
1730
|
+
inp === null
|
|
1731
|
+
? null
|
|
1732
|
+
: {
|
|
1733
|
+
...rated("inp", inp),
|
|
1734
|
+
slowestMs: Math.round(durations[0]),
|
|
1735
|
+
interactions: interactionCount,
|
|
1736
|
+
},
|
|
1737
|
+
},
|
|
1738
|
+
...(restoredAt !== null ? { restoredFromCache: true } : {}),
|
|
1739
|
+
longTasks: longTasks
|
|
1740
|
+
? {
|
|
1741
|
+
count: longTasks.length,
|
|
1742
|
+
totalMs: round(
|
|
1743
|
+
longTasks.reduce((sum, task) => sum + task.duration, 0),
|
|
1744
|
+
),
|
|
1745
|
+
longestMs: round(
|
|
1746
|
+
Math.max(0, ...longTasks.map((task) => task.duration)),
|
|
1747
|
+
),
|
|
1748
|
+
}
|
|
1749
|
+
: null,
|
|
1750
|
+
resources: {
|
|
1751
|
+
count: resources.length,
|
|
1752
|
+
transferBytes: resources.reduce(
|
|
1753
|
+
(sum, entry) => sum + (entry.transferSize || 0),
|
|
1754
|
+
0,
|
|
1755
|
+
),
|
|
1756
|
+
byType,
|
|
1757
|
+
slowest: resources
|
|
1758
|
+
.slice()
|
|
1759
|
+
.sort((a, b) => b.duration - a.duration)
|
|
1760
|
+
.slice(0, 5)
|
|
1761
|
+
.map((entry) => ({
|
|
1762
|
+
url: clipText(entry.name, 120),
|
|
1763
|
+
type: entry.initiatorType || "other",
|
|
1764
|
+
durationMs: round(entry.duration),
|
|
1765
|
+
transferBytes: entry.transferSize ?? null,
|
|
1766
|
+
})),
|
|
1767
|
+
},
|
|
1768
|
+
};
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
// upload_file takes the file input itself, a <label> for one, or an
|
|
1772
|
+
// element with one inside (a styled upload area). The input is left on
|
|
1773
|
+
// the window under the upload's token for the server to take a handle
|
|
1774
|
+
// to, since only the browser protocol can set files.
|
|
1775
|
+
const FILE_TARGET_KEY = "__mcpBrowserDevToolsFileTargets";
|
|
1776
|
+
|
|
1777
|
+
function fileInput() {
|
|
1778
|
+
const resolved = ensureResolved(payload.selector);
|
|
1779
|
+
if (!resolved.element) {
|
|
1780
|
+
return resolved;
|
|
1781
|
+
}
|
|
1782
|
+
const element = resolved.element;
|
|
1783
|
+
const isFileInput = (node) =>
|
|
1784
|
+
node?.tagName === "INPUT" && node.type === "file";
|
|
1785
|
+
const input = isFileInput(element)
|
|
1786
|
+
? element
|
|
1787
|
+
: isFileInput(element.control)
|
|
1788
|
+
? element.control
|
|
1789
|
+
: (element.querySelector?.('input[type="file"]') ?? null);
|
|
1790
|
+
const result = {
|
|
1791
|
+
browserFamily: payload.browserFamily,
|
|
1792
|
+
selector: payload.selector,
|
|
1793
|
+
found: true,
|
|
1794
|
+
fileInput: Boolean(input),
|
|
1795
|
+
};
|
|
1796
|
+
if (!input) {
|
|
1797
|
+
return result;
|
|
1798
|
+
}
|
|
1799
|
+
// Clearing is done here, so both browsers fire input and change.
|
|
1800
|
+
if (payload.clear) {
|
|
1801
|
+
input.value = "";
|
|
1802
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
1803
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
1804
|
+
return { ...result, cleared: true };
|
|
1805
|
+
}
|
|
1806
|
+
(window[FILE_TARGET_KEY] ?? defineHidden(FILE_TARGET_KEY, new Map())).set(
|
|
1807
|
+
payload.token,
|
|
1808
|
+
input,
|
|
1809
|
+
);
|
|
1810
|
+
return {
|
|
1811
|
+
...result,
|
|
1812
|
+
multiple: Boolean(input.multiple),
|
|
1813
|
+
accept: input.accept || null,
|
|
1814
|
+
disabled: isDisabled(input),
|
|
1815
|
+
};
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
// The operation the browser proposes on dragenter and dragover for the
|
|
1819
|
+
// operations the source allows, per the HTML drag-and-drop model.
|
|
1820
|
+
function defaultDropEffect(allowed, isLink) {
|
|
1821
|
+
if (allowed === "none") {
|
|
1822
|
+
return "none";
|
|
1823
|
+
}
|
|
1824
|
+
if (allowed === "link" || allowed === "linkMove") {
|
|
1825
|
+
return "link";
|
|
1826
|
+
}
|
|
1827
|
+
if (allowed === "move") {
|
|
1828
|
+
return "move";
|
|
1829
|
+
}
|
|
1830
|
+
if (allowed === "uninitialized" && isLink) {
|
|
1831
|
+
return "link";
|
|
1832
|
+
}
|
|
1833
|
+
return "copy";
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
function html5Drag() {
|
|
1837
|
+
const draggable = hitTest(payload.from)?.closest?.(
|
|
1838
|
+
'[draggable="true"], a[href]:not([draggable="false"]), img:not([draggable="false"])',
|
|
1839
|
+
);
|
|
1840
|
+
if (!draggable || payload.check) {
|
|
1841
|
+
return { html5: Boolean(draggable) };
|
|
1842
|
+
}
|
|
1843
|
+
const dataTransfer = new DataTransfer();
|
|
1844
|
+
// As in a real drag, dragstart handlers may say which operations they
|
|
1845
|
+
// allow, and the browser fills in a link's or image's URL first.
|
|
1846
|
+
dataTransfer.effectAllowed = "uninitialized";
|
|
1847
|
+
const url =
|
|
1848
|
+
draggable.tagName === "A"
|
|
1849
|
+
? draggable.href
|
|
1850
|
+
: draggable.tagName === "IMG"
|
|
1851
|
+
? draggable.src
|
|
1852
|
+
: null;
|
|
1853
|
+
if (url) {
|
|
1854
|
+
dataTransfer.setData("text/uri-list", url);
|
|
1855
|
+
dataTransfer.setData("text/plain", url);
|
|
1856
|
+
}
|
|
1857
|
+
const fire = (element, type, point) =>
|
|
1858
|
+
element.dispatchEvent(
|
|
1859
|
+
new DragEvent(type, {
|
|
1860
|
+
bubbles: true,
|
|
1861
|
+
cancelable: true,
|
|
1862
|
+
composed: true,
|
|
1863
|
+
clientX: point.x,
|
|
1864
|
+
clientY: point.y,
|
|
1865
|
+
dataTransfer,
|
|
1866
|
+
}),
|
|
1867
|
+
);
|
|
1868
|
+
if (!fire(draggable, "dragstart", payload.from)) {
|
|
1869
|
+
return { html5: true, dropped: false, canceled: true };
|
|
1870
|
+
}
|
|
1871
|
+
fire(draggable, "drag", payload.from);
|
|
1872
|
+
// Read after dragstart, whose handlers may have changed what is there.
|
|
1873
|
+
const target = hitTest(payload.to) ?? document.body;
|
|
1874
|
+
const effect = defaultDropEffect(
|
|
1875
|
+
dataTransfer.effectAllowed,
|
|
1876
|
+
draggable.tagName === "A",
|
|
1877
|
+
);
|
|
1878
|
+
dataTransfer.dropEffect = effect;
|
|
1879
|
+
fire(target, "dragenter", payload.to);
|
|
1880
|
+
dataTransfer.dropEffect = effect;
|
|
1881
|
+
// A target accepts by canceling dragover with an operation the source
|
|
1882
|
+
// allows, and takes the drop by canceling drop; otherwise the source
|
|
1883
|
+
// sees dropEffect none, as after a real drop nobody took.
|
|
1884
|
+
const accepted =
|
|
1885
|
+
!fire(target, "dragover", payload.to) &&
|
|
1886
|
+
allowedOperations(dataTransfer.effectAllowed).includes(
|
|
1887
|
+
dataTransfer.dropEffect,
|
|
1888
|
+
);
|
|
1889
|
+
const dropped = accepted && !fire(target, "drop", payload.to);
|
|
1890
|
+
if (!accepted) {
|
|
1891
|
+
fire(target, "dragleave", payload.to);
|
|
1892
|
+
}
|
|
1893
|
+
if (!dropped) {
|
|
1894
|
+
dataTransfer.dropEffect = "none";
|
|
1895
|
+
}
|
|
1896
|
+
fire(draggable, "dragend", payload.to);
|
|
1897
|
+
return { html5: true, dropped };
|
|
1898
|
+
}
|
|
1899
|
+
|
|
814
1900
|
function summarizeTarget(element) {
|
|
815
1901
|
return element
|
|
816
1902
|
? {
|
|
@@ -892,6 +1978,86 @@ export function pageScript(payload) {
|
|
|
892
1978
|
return Boolean(label && label.control === element);
|
|
893
1979
|
}
|
|
894
1980
|
|
|
1981
|
+
// Tells input the browser silently dropped apart from input the page
|
|
1982
|
+
// received, from the trusted events inputRecorder counted between arming
|
|
1983
|
+
// the probe and reading it. A recorder installed when the document started
|
|
1984
|
+
// runs before every page listener, so no count means nothing arrived. One
|
|
1985
|
+
// installed later can miss events that earlier page listeners stopped, so
|
|
1986
|
+
// there a click also counts as delivered when the element under the pointer
|
|
1987
|
+
// becomes hovered, and typing when the field's content changes: the
|
|
1988
|
+
// browser does both before any page listener runs.
|
|
1989
|
+
const INPUT_PROBE_KEY = Symbol.for("mcp-browser-dev-tools.inputProbe");
|
|
1990
|
+
const INPUT_RECORDER_KEY = Symbol.for("mcp-browser-dev-tools.inputRecorder");
|
|
1991
|
+
|
|
1992
|
+
// Input aimed at a frame goes to the frame's own document, whose events
|
|
1993
|
+
// this window does not see.
|
|
1994
|
+
function hostsDocument(element) {
|
|
1995
|
+
return ["IFRAME", "FRAME", "OBJECT", "EMBED"].includes(element?.tagName);
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
function isHovered(element) {
|
|
1999
|
+
try {
|
|
2000
|
+
return element.matches(":hover");
|
|
2001
|
+
} catch {
|
|
2002
|
+
return false;
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
function editedContent(element) {
|
|
2007
|
+
if (!element) {
|
|
2008
|
+
return null;
|
|
2009
|
+
}
|
|
2010
|
+
return typeof element.value === "string"
|
|
2011
|
+
? element.value
|
|
2012
|
+
: element.textContent;
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
function armInputProbe(token, kind, options = {}) {
|
|
2016
|
+
const recorder = window[INPUT_RECORDER_KEY];
|
|
2017
|
+
if (!recorder) {
|
|
2018
|
+
delete window[INPUT_PROBE_KEY];
|
|
2019
|
+
return;
|
|
2020
|
+
}
|
|
2021
|
+
const hovered = options.pointerAt ?? null;
|
|
2022
|
+
window[INPUT_PROBE_KEY] = {
|
|
2023
|
+
token,
|
|
2024
|
+
kind,
|
|
2025
|
+
count: recorder[kind],
|
|
2026
|
+
early: recorder.early,
|
|
2027
|
+
element: options.element ?? null,
|
|
2028
|
+
locator: options.locator ?? null,
|
|
2029
|
+
hovered: hovered && !isHovered(hovered) ? hovered : null,
|
|
2030
|
+
edited: options.edited ?? null,
|
|
2031
|
+
content: editedContent(options.edited),
|
|
2032
|
+
};
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
// Unarmed means delivery cannot be checked, for example after the page
|
|
2036
|
+
// navigated. conclusive says whether "not delivered" can be trusted.
|
|
2037
|
+
function readInputProbe(token, disarm) {
|
|
2038
|
+
const probe = window[INPUT_PROBE_KEY];
|
|
2039
|
+
const recorder = window[INPUT_RECORDER_KEY];
|
|
2040
|
+
if (!probe || probe.token !== token || !recorder) {
|
|
2041
|
+
return { armed: false, delivered: false, conclusive: false };
|
|
2042
|
+
}
|
|
2043
|
+
const delivered =
|
|
2044
|
+
recorder[probe.kind] > probe.count ||
|
|
2045
|
+
Boolean(probe.hovered && isHovered(probe.hovered)) ||
|
|
2046
|
+
(probe.edited !== null && editedContent(probe.edited) !== probe.content);
|
|
2047
|
+
if (disarm || delivered) {
|
|
2048
|
+
delete window[INPUT_PROBE_KEY];
|
|
2049
|
+
}
|
|
2050
|
+
return {
|
|
2051
|
+
armed: true,
|
|
2052
|
+
delivered,
|
|
2053
|
+
conclusive: probe.early,
|
|
2054
|
+
node:
|
|
2055
|
+
delivered && probe.element?.isConnected
|
|
2056
|
+
? describeElement(probe.element, probe.locator)
|
|
2057
|
+
: null,
|
|
2058
|
+
};
|
|
2059
|
+
}
|
|
2060
|
+
|
|
895
2061
|
// Whether the page renders the element at all. innerText falls back to the
|
|
896
2062
|
// raw text content for an element inside display: none, which the page
|
|
897
2063
|
// does not show; unlike isVisible, size and pointer events do not matter.
|
|
@@ -999,6 +2165,16 @@ export function pageScript(payload) {
|
|
|
999
2165
|
switch (payload.action) {
|
|
1000
2166
|
case "read_text":
|
|
1001
2167
|
return readText();
|
|
2168
|
+
case "snapshot":
|
|
2169
|
+
return snapshotPage();
|
|
2170
|
+
case "change_baseline":
|
|
2171
|
+
return changeBaseline();
|
|
2172
|
+
case "change_status":
|
|
2173
|
+
return changeStatus();
|
|
2174
|
+
case "change_report":
|
|
2175
|
+
return changeReport();
|
|
2176
|
+
case "change_stop":
|
|
2177
|
+
return changeStop();
|
|
1002
2178
|
case "controls_snapshot":
|
|
1003
2179
|
return {
|
|
1004
2180
|
browserFamily: payload.browserFamily,
|
|
@@ -1046,6 +2222,18 @@ export function pageScript(payload) {
|
|
|
1046
2222
|
const receivesEvents = Boolean(
|
|
1047
2223
|
hit && receivesPointerAt(resolved.element, hit),
|
|
1048
2224
|
);
|
|
2225
|
+
if (
|
|
2226
|
+
receivesEvents &&
|
|
2227
|
+
typeof payload.inputProbe === "string" &&
|
|
2228
|
+
!hostsDocument(hit) &&
|
|
2229
|
+
!isDisabled(resolved.element)
|
|
2230
|
+
) {
|
|
2231
|
+
armInputProbe(payload.inputProbe, "pointer", {
|
|
2232
|
+
element: resolved.element,
|
|
2233
|
+
locator: resolved,
|
|
2234
|
+
pointerAt: hit,
|
|
2235
|
+
});
|
|
2236
|
+
}
|
|
1049
2237
|
|
|
1050
2238
|
return {
|
|
1051
2239
|
browserFamily: payload.browserFamily,
|
|
@@ -1057,6 +2245,61 @@ export function pageScript(payload) {
|
|
|
1057
2245
|
node: describeElement(resolved.element, resolved),
|
|
1058
2246
|
};
|
|
1059
2247
|
}
|
|
2248
|
+
case "point_target": {
|
|
2249
|
+
// Coordinates are CSS pixels from the viewport's top-left corner, as
|
|
2250
|
+
// the browser's input takes them.
|
|
2251
|
+
const point = { x: Number(payload.x), y: Number(payload.y) };
|
|
2252
|
+
const result = {
|
|
2253
|
+
browserFamily: payload.browserFamily,
|
|
2254
|
+
point,
|
|
2255
|
+
viewport: { width: window.innerWidth, height: window.innerHeight },
|
|
2256
|
+
};
|
|
2257
|
+
if (
|
|
2258
|
+
!(point.x >= 0 && point.x < window.innerWidth) ||
|
|
2259
|
+
!(point.y >= 0 && point.y < window.innerHeight)
|
|
2260
|
+
) {
|
|
2261
|
+
return {
|
|
2262
|
+
...result,
|
|
2263
|
+
found: false,
|
|
2264
|
+
error:
|
|
2265
|
+
"Point (" +
|
|
2266
|
+
point.x +
|
|
2267
|
+
", " +
|
|
2268
|
+
point.y +
|
|
2269
|
+
") is outside the viewport (" +
|
|
2270
|
+
window.innerWidth +
|
|
2271
|
+
"x" +
|
|
2272
|
+
window.innerHeight +
|
|
2273
|
+
"); scroll first, or use a selector",
|
|
2274
|
+
};
|
|
2275
|
+
}
|
|
2276
|
+
const hit = hitTest(point);
|
|
2277
|
+
if (
|
|
2278
|
+
hit &&
|
|
2279
|
+
typeof payload.inputProbe === "string" &&
|
|
2280
|
+
!hostsDocument(hit) &&
|
|
2281
|
+
!isDisabled(hit)
|
|
2282
|
+
) {
|
|
2283
|
+
armInputProbe(payload.inputProbe, "pointer", {
|
|
2284
|
+
element: hit,
|
|
2285
|
+
pointerAt: hit,
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
return {
|
|
2289
|
+
...result,
|
|
2290
|
+
found: true,
|
|
2291
|
+
target: hit ? describePointTarget(hit) : null,
|
|
2292
|
+
nextRef: refRegistry().next,
|
|
2293
|
+
};
|
|
2294
|
+
}
|
|
2295
|
+
case "html5_drag":
|
|
2296
|
+
return html5Drag();
|
|
2297
|
+
case "file_input":
|
|
2298
|
+
return fileInput();
|
|
2299
|
+
case "performance":
|
|
2300
|
+
return performanceReport();
|
|
2301
|
+
case "input_probe":
|
|
2302
|
+
return readInputProbe(payload.token, payload.disarm === true);
|
|
1060
2303
|
case "prepare_type": {
|
|
1061
2304
|
const resolved = ensureResolved(payload.selector);
|
|
1062
2305
|
if (!resolved.element) {
|
|
@@ -1122,6 +2365,14 @@ export function pageScript(payload) {
|
|
|
1122
2365
|
selection.addRange(range);
|
|
1123
2366
|
}
|
|
1124
2367
|
|
|
2368
|
+
if (typeof payload.inputProbe === "string") {
|
|
2369
|
+
armInputProbe(payload.inputProbe, "keyboard", {
|
|
2370
|
+
element,
|
|
2371
|
+
locator: resolved,
|
|
2372
|
+
edited: element,
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2375
|
+
|
|
1125
2376
|
return {
|
|
1126
2377
|
browserFamily: payload.browserFamily,
|
|
1127
2378
|
selector: payload.selector,
|
|
@@ -1181,11 +2432,18 @@ export function pageScript(payload) {
|
|
|
1181
2432
|
}
|
|
1182
2433
|
}
|
|
1183
2434
|
|
|
2435
|
+
const focused = getDeepActiveElement() ?? document.body;
|
|
2436
|
+
if (typeof payload.inputProbe === "string" && !hostsDocument(focused)) {
|
|
2437
|
+
armInputProbe(payload.inputProbe, "keyboard", {
|
|
2438
|
+
edited: isEditable(focused) ? focused : null,
|
|
2439
|
+
});
|
|
2440
|
+
}
|
|
2441
|
+
|
|
1184
2442
|
return {
|
|
1185
2443
|
browserFamily: payload.browserFamily,
|
|
1186
2444
|
selector: payload.selector ?? null,
|
|
1187
2445
|
found: true,
|
|
1188
|
-
target: summarizeTarget(
|
|
2446
|
+
target: summarizeTarget(focused),
|
|
1189
2447
|
};
|
|
1190
2448
|
}
|
|
1191
2449
|
case "scroll": {
|
|
@@ -1370,3 +2628,48 @@ export function pageScript(payload) {
|
|
|
1370
2628
|
|
|
1371
2629
|
return runAction();
|
|
1372
2630
|
}
|
|
2631
|
+
|
|
2632
|
+
// Counts trusted pointer and keyboard events in the page from the moment it
|
|
2633
|
+
// runs, for the input probe in pageScript. Registered to run when each new
|
|
2634
|
+
// document starts (early), it sees every event before page listeners can
|
|
2635
|
+
// stop it; run on a document that is already loaded, it misses what
|
|
2636
|
+
// listeners added before it stop. Like pageScript, it is serialized with
|
|
2637
|
+
// toString() and must not reference anything outside itself.
|
|
2638
|
+
export function inputRecorder(early) {
|
|
2639
|
+
const key = Symbol.for("mcp-browser-dev-tools.inputRecorder");
|
|
2640
|
+
if (window[key]) {
|
|
2641
|
+
return;
|
|
2642
|
+
}
|
|
2643
|
+
const counts = { pointer: 0, keyboard: 0, early };
|
|
2644
|
+
const kinds = {
|
|
2645
|
+
pointer: ["pointerdown", "mousedown", "pointerup", "mouseup", "click"],
|
|
2646
|
+
keyboard: ["keydown", "keyup", "beforeinput", "input"],
|
|
2647
|
+
};
|
|
2648
|
+
for (const [kind, types] of Object.entries(kinds)) {
|
|
2649
|
+
for (const type of types) {
|
|
2650
|
+
window.addEventListener(
|
|
2651
|
+
type,
|
|
2652
|
+
(event) => {
|
|
2653
|
+
if (event.isTrusted) {
|
|
2654
|
+
counts[kind] += 1;
|
|
2655
|
+
}
|
|
2656
|
+
},
|
|
2657
|
+
true,
|
|
2658
|
+
);
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
// A page restored from the back/forward cache keeps its window, and its
|
|
2662
|
+
// performance entries, from the earlier visit; the report measures from
|
|
2663
|
+
// the restore.
|
|
2664
|
+
window.addEventListener(
|
|
2665
|
+
"pageshow",
|
|
2666
|
+
(event) => {
|
|
2667
|
+
if (event.persisted) {
|
|
2668
|
+
counts.restoredAt = performance.now();
|
|
2669
|
+
counts.interactionsBeforeRestore = performance.interactionCount ?? 0;
|
|
2670
|
+
}
|
|
2671
|
+
},
|
|
2672
|
+
true,
|
|
2673
|
+
);
|
|
2674
|
+
Object.defineProperty(window, key, { value: counts });
|
|
2675
|
+
}
|