mcp-browser-dev-tools 0.0.5 → 0.0.6-beta.1
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 +10 -0
- package/package.json +2 -1
- package/src/browser-adapter.mjs +5 -2
- package/src/browser-launch-service.mjs +146 -9
- package/src/cdp-client.mjs +120 -20
- package/src/cli.mjs +3 -1
- package/src/config.mjs +1 -0
- package/src/firefox-bidi-client.mjs +127 -22
- package/src/keyboard.mjs +278 -0
- package/src/mcp-server.mjs +312 -109
- package/src/page-context.mjs +210 -88
package/src/page-context.mjs
CHANGED
|
@@ -9,6 +9,15 @@ export function buildPageContextExpression(
|
|
|
9
9
|
const body = `(() => {
|
|
10
10
|
const payload = ${buildPayloadLiteral(payload)};
|
|
11
11
|
const TEXT_LIMIT = 400;
|
|
12
|
+
const VALUE_ONLY_INPUT_TYPES = [
|
|
13
|
+
"date",
|
|
14
|
+
"time",
|
|
15
|
+
"datetime-local",
|
|
16
|
+
"month",
|
|
17
|
+
"week",
|
|
18
|
+
"color",
|
|
19
|
+
"range",
|
|
20
|
+
];
|
|
12
21
|
|
|
13
22
|
function clipText(value, limit = TEXT_LIMIT) {
|
|
14
23
|
if (typeof value !== "string") {
|
|
@@ -566,24 +575,112 @@ export function buildPageContextExpression(
|
|
|
566
575
|
element.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
|
|
567
576
|
}
|
|
568
577
|
|
|
578
|
+
// Assigning element.value directly also updates the value tracker React
|
|
579
|
+
// keeps on the instance, so React would treat the next input event as a
|
|
580
|
+
// no-op. The prototype setter bypasses that tracker.
|
|
581
|
+
function setNativeValue(element, value) {
|
|
582
|
+
const prototype = Object.getPrototypeOf(element);
|
|
583
|
+
const descriptor = prototype
|
|
584
|
+
? Object.getOwnPropertyDescriptor(prototype, "value")
|
|
585
|
+
: null;
|
|
586
|
+
if (descriptor?.set) {
|
|
587
|
+
descriptor.set.call(element, value);
|
|
588
|
+
} else {
|
|
589
|
+
element.value = value;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
569
593
|
function maybeScrollIntoView(element) {
|
|
570
594
|
if (!element || payload.scrollIntoView === false) {
|
|
571
595
|
return;
|
|
572
596
|
}
|
|
573
597
|
|
|
574
|
-
element.scrollIntoView({
|
|
598
|
+
element.scrollIntoView({
|
|
599
|
+
block: "center",
|
|
600
|
+
inline: "center",
|
|
601
|
+
behavior: "instant",
|
|
602
|
+
});
|
|
575
603
|
}
|
|
576
604
|
|
|
577
|
-
function
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
605
|
+
function summarizeTarget(element) {
|
|
606
|
+
return element
|
|
607
|
+
? {
|
|
608
|
+
tagName: element.tagName,
|
|
609
|
+
id: element.getAttribute("id"),
|
|
610
|
+
className: element.getAttribute("class"),
|
|
611
|
+
}
|
|
612
|
+
: null;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function getDeepActiveElement() {
|
|
616
|
+
let active = document.activeElement;
|
|
617
|
+
while (active?.shadowRoot?.activeElement) {
|
|
618
|
+
active = active.shadowRoot.activeElement;
|
|
619
|
+
}
|
|
620
|
+
return active;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function focusElement(element) {
|
|
624
|
+
if (typeof element.focus === "function") {
|
|
625
|
+
element.focus();
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const active = getDeepActiveElement();
|
|
629
|
+
return Boolean(
|
|
630
|
+
active &&
|
|
631
|
+
(active === element ||
|
|
632
|
+
(element.isContentEditable &&
|
|
633
|
+
typeof active.contains === "function" &&
|
|
634
|
+
active.contains(element))),
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function findClickablePoint(element) {
|
|
639
|
+
const rects =
|
|
640
|
+
typeof element.getClientRects === "function"
|
|
641
|
+
? Array.from(element.getClientRects())
|
|
642
|
+
: [];
|
|
643
|
+
if (rects.length === 0) {
|
|
644
|
+
rects.push(element.getBoundingClientRect());
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
for (const rect of rects) {
|
|
648
|
+
const left = Math.max(rect.left, 0);
|
|
649
|
+
const top = Math.max(rect.top, 0);
|
|
650
|
+
const right = Math.min(rect.right, window.innerWidth);
|
|
651
|
+
const bottom = Math.min(rect.bottom, window.innerHeight);
|
|
652
|
+
if (right - left >= 1 && bottom - top >= 1) {
|
|
653
|
+
return { x: (left + right) / 2, y: (top + bottom) / 2 };
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
return null;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function hitTest(point) {
|
|
661
|
+
let hit = document.elementFromPoint(point.x, point.y);
|
|
662
|
+
while (hit?.shadowRoot) {
|
|
663
|
+
const inner = hit.shadowRoot.elementFromPoint(point.x, point.y);
|
|
664
|
+
if (!inner || inner === hit) {
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
hit = inner;
|
|
668
|
+
}
|
|
669
|
+
return hit;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function receivesPointerAt(element, hit) {
|
|
673
|
+
for (let node = hit; node; node = node.parentNode ?? node.host ?? null) {
|
|
674
|
+
if (node === element) {
|
|
675
|
+
return true;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// Styled checkboxes often hide the input behind its <label>, which
|
|
680
|
+
// forwards the click to the control.
|
|
681
|
+
const label =
|
|
682
|
+
typeof hit?.closest === "function" ? hit.closest("label") : null;
|
|
683
|
+
return Boolean(label && label.control === element);
|
|
587
684
|
}
|
|
588
685
|
|
|
589
686
|
function runAction() {
|
|
@@ -608,96 +705,100 @@ export function buildPageContextExpression(
|
|
|
608
705
|
node: describeElement(resolved.element, resolved),
|
|
609
706
|
};
|
|
610
707
|
}
|
|
611
|
-
case "
|
|
708
|
+
case "pointer_target": {
|
|
612
709
|
const resolved = ensureResolved(payload.selector);
|
|
613
710
|
if (!resolved.element) {
|
|
614
711
|
return resolved;
|
|
615
712
|
}
|
|
616
713
|
|
|
617
714
|
maybeScrollIntoView(resolved.element);
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
resolved.element.click();
|
|
624
|
-
} else {
|
|
625
|
-
resolved.element.dispatchEvent(
|
|
626
|
-
new MouseEvent("click", {
|
|
627
|
-
bubbles: true,
|
|
628
|
-
cancelable: true,
|
|
629
|
-
composed: true,
|
|
630
|
-
}),
|
|
631
|
-
);
|
|
632
|
-
}
|
|
715
|
+
const point = findClickablePoint(resolved.element);
|
|
716
|
+
const hit = point ? hitTest(point) : null;
|
|
717
|
+
const receivesEvents = Boolean(
|
|
718
|
+
hit && receivesPointerAt(resolved.element, hit),
|
|
719
|
+
);
|
|
633
720
|
|
|
634
721
|
return {
|
|
635
722
|
browserFamily: payload.browserFamily,
|
|
636
723
|
selector: payload.selector,
|
|
637
724
|
found: true,
|
|
638
|
-
|
|
725
|
+
point,
|
|
726
|
+
receivesEvents,
|
|
727
|
+
obscuredBy: receivesEvents ? null : summarizeTarget(hit),
|
|
639
728
|
node: describeElement(resolved.element, resolved),
|
|
640
729
|
};
|
|
641
730
|
}
|
|
642
|
-
case "
|
|
731
|
+
case "prepare_type": {
|
|
643
732
|
const resolved = ensureResolved(payload.selector);
|
|
644
733
|
if (!resolved.element) {
|
|
645
734
|
return resolved;
|
|
646
735
|
}
|
|
647
736
|
|
|
648
|
-
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
cancelable: true,
|
|
652
|
-
composed: true,
|
|
653
|
-
};
|
|
654
|
-
resolved.element.dispatchEvent(new MouseEvent("mouseover", eventInit));
|
|
655
|
-
resolved.element.dispatchEvent(new MouseEvent("mouseenter", eventInit));
|
|
656
|
-
resolved.element.dispatchEvent(new MouseEvent("mousemove", eventInit));
|
|
737
|
+
const element = resolved.element;
|
|
738
|
+
const clear = payload.clear !== false;
|
|
739
|
+
maybeScrollIntoView(element);
|
|
657
740
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
node: describeElement(resolved.element, resolved),
|
|
664
|
-
};
|
|
665
|
-
}
|
|
666
|
-
case "type": {
|
|
667
|
-
const resolved = ensureResolved(payload.selector);
|
|
668
|
-
if (!resolved.element) {
|
|
669
|
-
return resolved;
|
|
741
|
+
const isTextControl =
|
|
742
|
+
element instanceof HTMLInputElement ||
|
|
743
|
+
element instanceof HTMLTextAreaElement;
|
|
744
|
+
if (!isEditable(element) || isDisabled(element)) {
|
|
745
|
+
throw new Error("Resolved element is not editable");
|
|
670
746
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
maybeScrollIntoView(resolved.element);
|
|
674
|
-
if (typeof resolved.element.focus === "function") {
|
|
675
|
-
resolved.element.focus();
|
|
747
|
+
if (element.readOnly === true) {
|
|
748
|
+
throw new Error("Resolved element is read-only");
|
|
676
749
|
}
|
|
677
750
|
|
|
751
|
+
// Pickers such as date or color inputs do not accept typed text, so
|
|
752
|
+
// their value is set directly.
|
|
678
753
|
if (
|
|
679
|
-
|
|
680
|
-
|
|
754
|
+
element instanceof HTMLInputElement &&
|
|
755
|
+
VALUE_ONLY_INPUT_TYPES.includes((element.type || "").toLowerCase())
|
|
681
756
|
) {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
:
|
|
690
|
-
|
|
757
|
+
focusElement(element);
|
|
758
|
+
setNativeValue(element, payload.text);
|
|
759
|
+
dispatchInputEvents(element);
|
|
760
|
+
return {
|
|
761
|
+
browserFamily: payload.browserFamily,
|
|
762
|
+
selector: payload.selector,
|
|
763
|
+
found: true,
|
|
764
|
+
method: "value",
|
|
765
|
+
typedText: payload.text,
|
|
766
|
+
node: describeElement(element, resolved),
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
if (!focusElement(element)) {
|
|
771
|
+
throw new Error("Could not focus the resolved element");
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
if (isTextControl) {
|
|
775
|
+
if (clear) {
|
|
776
|
+
element.select();
|
|
777
|
+
} else {
|
|
778
|
+
const end = element.value.length;
|
|
779
|
+
try {
|
|
780
|
+
element.setSelectionRange(end, end);
|
|
781
|
+
} catch {
|
|
782
|
+
// email and number inputs do not support selection ranges.
|
|
783
|
+
}
|
|
784
|
+
}
|
|
691
785
|
} else {
|
|
692
|
-
|
|
786
|
+
const range = document.createRange();
|
|
787
|
+
range.selectNodeContents(element);
|
|
788
|
+
if (!clear) {
|
|
789
|
+
range.collapse(false);
|
|
790
|
+
}
|
|
791
|
+
const selection = window.getSelection();
|
|
792
|
+
selection.removeAllRanges();
|
|
793
|
+
selection.addRange(range);
|
|
693
794
|
}
|
|
694
795
|
|
|
695
796
|
return {
|
|
696
797
|
browserFamily: payload.browserFamily,
|
|
697
798
|
selector: payload.selector,
|
|
698
799
|
found: true,
|
|
699
|
-
|
|
700
|
-
node: describeElement(
|
|
800
|
+
method: "native",
|
|
801
|
+
node: describeElement(element, resolved),
|
|
701
802
|
};
|
|
702
803
|
}
|
|
703
804
|
case "select": {
|
|
@@ -738,11 +839,7 @@ export function buildPageContextExpression(
|
|
|
738
839
|
node: describeElement(resolved.element, resolved),
|
|
739
840
|
};
|
|
740
841
|
}
|
|
741
|
-
case "
|
|
742
|
-
let target = document.activeElement instanceof HTMLElement
|
|
743
|
-
? document.activeElement
|
|
744
|
-
: document.body;
|
|
745
|
-
|
|
842
|
+
case "focus": {
|
|
746
843
|
if (payload.selector) {
|
|
747
844
|
const resolved = ensureResolved(payload.selector);
|
|
748
845
|
if (!resolved.element) {
|
|
@@ -750,25 +847,16 @@ export function buildPageContextExpression(
|
|
|
750
847
|
}
|
|
751
848
|
|
|
752
849
|
maybeScrollIntoView(resolved.element);
|
|
753
|
-
if (
|
|
754
|
-
resolved
|
|
850
|
+
if (!focusElement(resolved.element)) {
|
|
851
|
+
throw new Error("Could not focus the resolved element");
|
|
755
852
|
}
|
|
756
|
-
target = resolved.element;
|
|
757
853
|
}
|
|
758
854
|
|
|
759
|
-
pressKeyOnTarget(target, payload.key);
|
|
760
|
-
|
|
761
855
|
return {
|
|
762
856
|
browserFamily: payload.browserFamily,
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
target:
|
|
766
|
-
? {
|
|
767
|
-
tagName: target.tagName,
|
|
768
|
-
id: target.getAttribute("id"),
|
|
769
|
-
className: target.getAttribute("class"),
|
|
770
|
-
}
|
|
771
|
-
: null,
|
|
857
|
+
selector: payload.selector ?? null,
|
|
858
|
+
found: true,
|
|
859
|
+
target: summarizeTarget(getDeepActiveElement() ?? document.body),
|
|
772
860
|
};
|
|
773
861
|
}
|
|
774
862
|
case "scroll": {
|
|
@@ -949,3 +1037,37 @@ export function buildPageContextExpression(
|
|
|
949
1037
|
|
|
950
1038
|
return serialize ? `JSON.stringify(${body})` : body;
|
|
951
1039
|
}
|
|
1040
|
+
|
|
1041
|
+
function describeTarget(target) {
|
|
1042
|
+
if (!target) {
|
|
1043
|
+
return "another element";
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
const id = target.id ? `#${target.id}` : "";
|
|
1047
|
+
const className =
|
|
1048
|
+
typeof target.className === "string" && target.className.trim()
|
|
1049
|
+
? `.${target.className.trim().split(/\s+/).join(".")}`
|
|
1050
|
+
: "";
|
|
1051
|
+
return `<${target.tagName.toLowerCase()}${id}${className}>`;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// Real pointer input lands on whatever is painted at the point, so refuse to
|
|
1055
|
+
// send it when the resolved element is off-screen or covered.
|
|
1056
|
+
export function assertPointerTarget(target) {
|
|
1057
|
+
if (!target.found) {
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
if (!target.point) {
|
|
1062
|
+
throw new Error(
|
|
1063
|
+
`Element "${target.selector}" has no visible area inside the viewport`,
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
if (!target.receivesEvents) {
|
|
1068
|
+
const { x, y } = target.point;
|
|
1069
|
+
throw new Error(
|
|
1070
|
+
`Element "${target.selector}" is covered by ${describeTarget(target.obscuredBy)} at (${Math.round(x)}, ${Math.round(y)}); close or scroll past the covering element first`,
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
}
|