mcp-browser-dev-tools 0.2.1 → 0.2.2
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/package.json +2 -1
- package/src/cdp-client.mjs +105 -29
- package/src/firefox-bidi-client.mjs +97 -24
- package/src/input-delivery.mjs +82 -0
- package/src/page-context.mjs +9 -1
- package/src/page-script.mjs +142 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-browser-dev-tools",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Browser MCP server for AI agents: batch actions, waits, and checks into one call on Chrome, Edge, or Firefox (CDP and WebDriver BiDi)",
|
|
5
5
|
"author": "Hinaser <rindo.hinase@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"src/doctor.mjs",
|
|
32
32
|
"src/firefox-bidi-client.mjs",
|
|
33
33
|
"src/index.mjs",
|
|
34
|
+
"src/input-delivery.mjs",
|
|
34
35
|
"src/json-rpc-stdio.mjs",
|
|
35
36
|
"src/keyboard.mjs",
|
|
36
37
|
"src/logger.mjs",
|
package/src/cdp-client.mjs
CHANGED
|
@@ -8,9 +8,11 @@ import { waitForPageCondition } from "./wait-for.mjs";
|
|
|
8
8
|
import {
|
|
9
9
|
assertEnabled,
|
|
10
10
|
assertPointerTarget,
|
|
11
|
+
buildInputRecorderExpression,
|
|
11
12
|
buildPageContextExpression,
|
|
12
13
|
} from "./page-context.mjs";
|
|
13
14
|
import { buildCdpKeyEvents } from "./keyboard.mjs";
|
|
15
|
+
import { sendCheckedInput } from "./input-delivery.mjs";
|
|
14
16
|
|
|
15
17
|
function toErrorMessage(error) {
|
|
16
18
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -466,6 +468,7 @@ export class CdpSession {
|
|
|
466
468
|
this.send("Network.enable"),
|
|
467
469
|
]);
|
|
468
470
|
|
|
471
|
+
await this.installInputRecorder();
|
|
469
472
|
await this.seedBufferedState();
|
|
470
473
|
|
|
471
474
|
return this.getSummary();
|
|
@@ -894,15 +897,36 @@ export class CdpSession {
|
|
|
894
897
|
});
|
|
895
898
|
}
|
|
896
899
|
|
|
897
|
-
async resolvePointerTarget(selector) {
|
|
900
|
+
async resolvePointerTarget(selector, options = {}) {
|
|
898
901
|
const target = await this.runPageAction({
|
|
899
902
|
action: "pointer_target",
|
|
900
903
|
selector,
|
|
904
|
+
...(options.inputProbe ? { inputProbe: options.inputProbe } : {}),
|
|
901
905
|
});
|
|
902
906
|
assertPointerTarget(target);
|
|
903
907
|
return target;
|
|
904
908
|
}
|
|
905
909
|
|
|
910
|
+
async readInputProbe(token, disarm) {
|
|
911
|
+
try {
|
|
912
|
+
const probe = await this.runPageAction({
|
|
913
|
+
action: "input_probe",
|
|
914
|
+
token,
|
|
915
|
+
disarm,
|
|
916
|
+
});
|
|
917
|
+
return probe.armed ? probe : null;
|
|
918
|
+
} catch {
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
async dispatchClick(point) {
|
|
924
|
+
const press = { button: "left", clickCount: 1 };
|
|
925
|
+
await this.dispatchMouse("mouseMoved", point);
|
|
926
|
+
await this.dispatchMouse("mousePressed", point, { ...press, buttons: 1 });
|
|
927
|
+
await this.dispatchMouse("mouseReleased", point, { ...press, buttons: 0 });
|
|
928
|
+
}
|
|
929
|
+
|
|
906
930
|
async dispatchMouse(type, point, extra = {}) {
|
|
907
931
|
await this.send("Input.dispatchMouseEvent", {
|
|
908
932
|
type,
|
|
@@ -919,21 +943,20 @@ export class CdpSession {
|
|
|
919
943
|
}
|
|
920
944
|
|
|
921
945
|
async click(selector) {
|
|
922
|
-
const
|
|
946
|
+
const token = crypto.randomUUID();
|
|
947
|
+
const target = await this.resolvePointerTarget(selector, {
|
|
948
|
+
inputProbe: token,
|
|
949
|
+
});
|
|
923
950
|
if (!target.found) {
|
|
924
951
|
return target;
|
|
925
952
|
}
|
|
926
953
|
assertEnabled(target);
|
|
927
954
|
|
|
928
|
-
const
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
});
|
|
934
|
-
await this.dispatchMouse("mouseReleased", target.point, {
|
|
935
|
-
...press,
|
|
936
|
-
buttons: 0,
|
|
955
|
+
const delivery = await sendCheckedInput({
|
|
956
|
+
send: () => this.dispatchClick(target.point),
|
|
957
|
+
readProbe: (disarm) => this.readInputProbe(token, disarm),
|
|
958
|
+
recover: () => this.bringToFront(),
|
|
959
|
+
describe: `The click on "${selector}"`,
|
|
937
960
|
});
|
|
938
961
|
|
|
939
962
|
return {
|
|
@@ -941,11 +964,18 @@ export class CdpSession {
|
|
|
941
964
|
selector,
|
|
942
965
|
found: true,
|
|
943
966
|
clicked: true,
|
|
967
|
+
...(delivery.resent ? { resent: true } : {}),
|
|
944
968
|
point: target.point,
|
|
945
|
-
node: target.node,
|
|
969
|
+
node: delivery.node ?? target.node,
|
|
946
970
|
};
|
|
947
971
|
}
|
|
948
972
|
|
|
973
|
+
// Bringing the tab to the front makes Chrome render it again, which is
|
|
974
|
+
// the one recovery tried before resending input the page never received.
|
|
975
|
+
async bringToFront() {
|
|
976
|
+
await this.send("Page.bringToFront").catch(() => {});
|
|
977
|
+
}
|
|
978
|
+
|
|
949
979
|
async hover(selector) {
|
|
950
980
|
const target = await this.resolvePointerTarget(selector);
|
|
951
981
|
if (!target.found) {
|
|
@@ -965,12 +995,14 @@ export class CdpSession {
|
|
|
965
995
|
}
|
|
966
996
|
|
|
967
997
|
async type(selector, text, options = {}) {
|
|
998
|
+
const token = crypto.randomUUID();
|
|
968
999
|
const prepared = await this.runPageAction(
|
|
969
1000
|
{
|
|
970
1001
|
action: "prepare_type",
|
|
971
1002
|
selector,
|
|
972
1003
|
text,
|
|
973
1004
|
clear: options.clear,
|
|
1005
|
+
inputProbe: token,
|
|
974
1006
|
},
|
|
975
1007
|
{ userGesture: true },
|
|
976
1008
|
);
|
|
@@ -982,32 +1014,51 @@ export class CdpSession {
|
|
|
982
1014
|
// see trusted beforeinput/input events and the real value change.
|
|
983
1015
|
// Newlines become Enter presses, matching the Firefox path: a line break
|
|
984
1016
|
// in a textarea, implicit submission in a single-line input.
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1017
|
+
const sendText = async () => {
|
|
1018
|
+
if (text) {
|
|
1019
|
+
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
1020
|
+
for (const [index, line] of lines.entries()) {
|
|
1021
|
+
if (index > 0) {
|
|
1022
|
+
await this.dispatchKeyEvents(buildCdpKeyEvents("Enter"));
|
|
1023
|
+
}
|
|
1024
|
+
if (line) {
|
|
1025
|
+
await this.send("Input.insertText", { text: line });
|
|
1026
|
+
}
|
|
993
1027
|
}
|
|
1028
|
+
} else if (options.clear !== false) {
|
|
1029
|
+
await this.dispatchKeyEvents(buildCdpKeyEvents("Delete"));
|
|
994
1030
|
}
|
|
995
|
-
}
|
|
996
|
-
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
let delivery = { node: null, resent: false };
|
|
1034
|
+
if (text || options.clear !== false) {
|
|
1035
|
+
delivery = await sendCheckedInput({
|
|
1036
|
+
send: sendText,
|
|
1037
|
+
readProbe: (disarm) => this.readInputProbe(token, disarm),
|
|
1038
|
+
recover: () => this.bringToFront(),
|
|
1039
|
+
describe: `The text for "${selector}"`,
|
|
1040
|
+
});
|
|
1041
|
+
} else {
|
|
1042
|
+
await this.readInputProbe(token, true);
|
|
997
1043
|
}
|
|
998
1044
|
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1045
|
+
let node = delivery.node;
|
|
1046
|
+
if (!node) {
|
|
1047
|
+
const typed = await this.runPageAction({
|
|
1048
|
+
action: "inspect",
|
|
1049
|
+
selector,
|
|
1050
|
+
scrollIntoView: false,
|
|
1051
|
+
});
|
|
1052
|
+
node = typed.found ? typed.node : prepared.node;
|
|
1053
|
+
}
|
|
1004
1054
|
|
|
1005
1055
|
return {
|
|
1006
1056
|
browserFamily: cdpBrowserFamily(this.config),
|
|
1007
1057
|
selector,
|
|
1008
1058
|
found: true,
|
|
1009
1059
|
typedText: text,
|
|
1010
|
-
|
|
1060
|
+
...(delivery.resent ? { resent: true } : {}),
|
|
1061
|
+
node,
|
|
1011
1062
|
};
|
|
1012
1063
|
}
|
|
1013
1064
|
|
|
@@ -1025,10 +1076,12 @@ export class CdpSession {
|
|
|
1025
1076
|
|
|
1026
1077
|
async pressKey(key, selector = null) {
|
|
1027
1078
|
const events = buildCdpKeyEvents(key);
|
|
1079
|
+
const token = crypto.randomUUID();
|
|
1028
1080
|
const focused = await this.runPageAction(
|
|
1029
1081
|
{
|
|
1030
1082
|
action: "focus",
|
|
1031
1083
|
selector,
|
|
1084
|
+
inputProbe: token,
|
|
1032
1085
|
},
|
|
1033
1086
|
{ userGesture: true },
|
|
1034
1087
|
);
|
|
@@ -1036,12 +1089,18 @@ export class CdpSession {
|
|
|
1036
1089
|
return focused;
|
|
1037
1090
|
}
|
|
1038
1091
|
|
|
1039
|
-
await
|
|
1092
|
+
const delivery = await sendCheckedInput({
|
|
1093
|
+
send: () => this.dispatchKeyEvents(events),
|
|
1094
|
+
readProbe: (disarm) => this.readInputProbe(token, disarm),
|
|
1095
|
+
recover: () => this.bringToFront(),
|
|
1096
|
+
describe: `The key press "${key}"`,
|
|
1097
|
+
});
|
|
1040
1098
|
|
|
1041
1099
|
return {
|
|
1042
1100
|
browserFamily: cdpBrowserFamily(this.config),
|
|
1043
1101
|
key,
|
|
1044
1102
|
dispatched: true,
|
|
1103
|
+
...(delivery.resent ? { resent: true } : {}),
|
|
1045
1104
|
target: focused.target,
|
|
1046
1105
|
};
|
|
1047
1106
|
}
|
|
@@ -1153,6 +1212,23 @@ export class CdpSession {
|
|
|
1153
1212
|
};
|
|
1154
1213
|
}
|
|
1155
1214
|
|
|
1215
|
+
// Counts trusted input from the start of every later document, and from
|
|
1216
|
+
// now on in the current one, so input actions can check that their input
|
|
1217
|
+
// arrived. Without it they are reported as sent, unchecked.
|
|
1218
|
+
async installInputRecorder() {
|
|
1219
|
+
try {
|
|
1220
|
+
await this.send("Page.addScriptToEvaluateOnNewDocument", {
|
|
1221
|
+
source: buildInputRecorderExpression(true),
|
|
1222
|
+
});
|
|
1223
|
+
await this.evaluateRuntime(buildInputRecorderExpression(false), {
|
|
1224
|
+
awaitPromise: false,
|
|
1225
|
+
replMode: false,
|
|
1226
|
+
});
|
|
1227
|
+
} catch {
|
|
1228
|
+
// Ignore recorder failures so attach still succeeds.
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1156
1232
|
async seedBufferedState() {
|
|
1157
1233
|
try {
|
|
1158
1234
|
const snapshot = await this.runPageAction({
|
|
@@ -9,9 +9,11 @@ import { waitForPageCondition } from "./wait-for.mjs";
|
|
|
9
9
|
import {
|
|
10
10
|
assertEnabled,
|
|
11
11
|
assertPointerTarget,
|
|
12
|
+
buildInputRecorderExpression,
|
|
12
13
|
buildPageContextExpression,
|
|
13
14
|
} from "./page-context.mjs";
|
|
14
15
|
import { buildBidiKeyActions, buildBidiTextActions } from "./keyboard.mjs";
|
|
16
|
+
import { sendCheckedInput } from "./input-delivery.mjs";
|
|
15
17
|
|
|
16
18
|
const POINTER_SOURCE = {
|
|
17
19
|
type: "pointer",
|
|
@@ -826,6 +828,7 @@ export class FirefoxBidiSessionManager {
|
|
|
826
828
|
|
|
827
829
|
session.subscription = subscription.subscription ?? null;
|
|
828
830
|
this.sessions.set(session.id, session);
|
|
831
|
+
await this.installInputRecorder(session);
|
|
829
832
|
await this.seedBufferedState(session);
|
|
830
833
|
return session.getSummary();
|
|
831
834
|
}
|
|
@@ -838,6 +841,11 @@ export class FirefoxBidiSessionManager {
|
|
|
838
841
|
subscriptions: [session.subscription],
|
|
839
842
|
}).catch(() => {});
|
|
840
843
|
}
|
|
844
|
+
if (session.inputRecorderScript) {
|
|
845
|
+
await this.send("script.removePreloadScript", {
|
|
846
|
+
script: session.inputRecorderScript,
|
|
847
|
+
}).catch(() => {});
|
|
848
|
+
}
|
|
841
849
|
|
|
842
850
|
session.closed = true;
|
|
843
851
|
this.sessions.delete(sessionId);
|
|
@@ -1053,41 +1061,64 @@ export class FirefoxBidiSessionManager {
|
|
|
1053
1061
|
]);
|
|
1054
1062
|
}
|
|
1055
1063
|
|
|
1056
|
-
async resolvePointerTarget(session, selector) {
|
|
1064
|
+
async resolvePointerTarget(session, selector, options = {}) {
|
|
1057
1065
|
const target = await this.runPageAction(session, {
|
|
1058
1066
|
action: "pointer_target",
|
|
1059
1067
|
selector,
|
|
1068
|
+
...(options.inputProbe ? { inputProbe: options.inputProbe } : {}),
|
|
1060
1069
|
});
|
|
1061
1070
|
assertPointerTarget(target);
|
|
1062
1071
|
return target;
|
|
1063
1072
|
}
|
|
1064
1073
|
|
|
1074
|
+
async readInputProbe(session, token, disarm) {
|
|
1075
|
+
try {
|
|
1076
|
+
const probe = await this.runPageAction(session, {
|
|
1077
|
+
action: "input_probe",
|
|
1078
|
+
token,
|
|
1079
|
+
disarm,
|
|
1080
|
+
});
|
|
1081
|
+
return probe.armed ? probe : null;
|
|
1082
|
+
} catch {
|
|
1083
|
+
return null;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1065
1087
|
async click(sessionId, selector) {
|
|
1066
1088
|
const session = this.getSession(sessionId);
|
|
1067
|
-
const
|
|
1089
|
+
const token = crypto.randomUUID();
|
|
1090
|
+
const target = await this.resolvePointerTarget(session, selector, {
|
|
1091
|
+
inputProbe: token,
|
|
1092
|
+
});
|
|
1068
1093
|
if (!target.found) {
|
|
1069
1094
|
return target;
|
|
1070
1095
|
}
|
|
1071
1096
|
assertEnabled(target);
|
|
1072
1097
|
|
|
1073
|
-
await
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1098
|
+
const delivery = await sendCheckedInput({
|
|
1099
|
+
send: () =>
|
|
1100
|
+
this.performActions(session, [
|
|
1101
|
+
{
|
|
1102
|
+
...POINTER_SOURCE,
|
|
1103
|
+
actions: [
|
|
1104
|
+
pointerMove(target.point),
|
|
1105
|
+
{ type: "pointerDown", button: 0 },
|
|
1106
|
+
{ type: "pointerUp", button: 0 },
|
|
1107
|
+
],
|
|
1108
|
+
},
|
|
1109
|
+
]),
|
|
1110
|
+
readProbe: (disarm) => this.readInputProbe(session, token, disarm),
|
|
1111
|
+
describe: `The click on "${selector}"`,
|
|
1112
|
+
});
|
|
1083
1113
|
|
|
1084
1114
|
return {
|
|
1085
1115
|
browserFamily: "firefox",
|
|
1086
1116
|
selector,
|
|
1087
1117
|
found: true,
|
|
1088
1118
|
clicked: true,
|
|
1119
|
+
...(delivery.resent ? { resent: true } : {}),
|
|
1089
1120
|
point: target.point,
|
|
1090
|
-
node: target.node,
|
|
1121
|
+
node: delivery.node ?? target.node,
|
|
1091
1122
|
};
|
|
1092
1123
|
}
|
|
1093
1124
|
|
|
@@ -1114,6 +1145,7 @@ export class FirefoxBidiSessionManager {
|
|
|
1114
1145
|
|
|
1115
1146
|
async type(sessionId, selector, text, options = {}) {
|
|
1116
1147
|
const session = this.getSession(sessionId);
|
|
1148
|
+
const token = crypto.randomUUID();
|
|
1117
1149
|
const prepared = await this.runPageAction(
|
|
1118
1150
|
session,
|
|
1119
1151
|
{
|
|
@@ -1121,6 +1153,7 @@ export class FirefoxBidiSessionManager {
|
|
|
1121
1153
|
selector,
|
|
1122
1154
|
text,
|
|
1123
1155
|
clear: options.clear,
|
|
1156
|
+
inputProbe: token,
|
|
1124
1157
|
},
|
|
1125
1158
|
{ userActivation: true },
|
|
1126
1159
|
);
|
|
@@ -1128,24 +1161,37 @@ export class FirefoxBidiSessionManager {
|
|
|
1128
1161
|
return prepared;
|
|
1129
1162
|
}
|
|
1130
1163
|
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1164
|
+
let delivery = { node: null, resent: false };
|
|
1165
|
+
if (text || options.clear !== false) {
|
|
1166
|
+
const actions = text
|
|
1167
|
+
? buildBidiTextActions(text)
|
|
1168
|
+
: buildBidiKeyActions("Backspace");
|
|
1169
|
+
delivery = await sendCheckedInput({
|
|
1170
|
+
send: () => this.performKeyActions(session, actions),
|
|
1171
|
+
readProbe: (disarm) => this.readInputProbe(session, token, disarm),
|
|
1172
|
+
describe: `The text for "${selector}"`,
|
|
1173
|
+
});
|
|
1174
|
+
} else {
|
|
1175
|
+
await this.readInputProbe(session, token, true);
|
|
1135
1176
|
}
|
|
1136
1177
|
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1178
|
+
let node = delivery.node;
|
|
1179
|
+
if (!node) {
|
|
1180
|
+
const typed = await this.runPageAction(session, {
|
|
1181
|
+
action: "inspect",
|
|
1182
|
+
selector,
|
|
1183
|
+
scrollIntoView: false,
|
|
1184
|
+
});
|
|
1185
|
+
node = typed.found ? typed.node : prepared.node;
|
|
1186
|
+
}
|
|
1142
1187
|
|
|
1143
1188
|
return {
|
|
1144
1189
|
browserFamily: "firefox",
|
|
1145
1190
|
selector,
|
|
1146
1191
|
found: true,
|
|
1147
1192
|
typedText: text,
|
|
1148
|
-
|
|
1193
|
+
...(delivery.resent ? { resent: true } : {}),
|
|
1194
|
+
node,
|
|
1149
1195
|
};
|
|
1150
1196
|
}
|
|
1151
1197
|
|
|
@@ -1165,11 +1211,13 @@ export class FirefoxBidiSessionManager {
|
|
|
1165
1211
|
async pressKey(sessionId, key, selector = null) {
|
|
1166
1212
|
const session = this.getSession(sessionId);
|
|
1167
1213
|
const actions = buildBidiKeyActions(key);
|
|
1214
|
+
const token = crypto.randomUUID();
|
|
1168
1215
|
const focused = await this.runPageAction(
|
|
1169
1216
|
session,
|
|
1170
1217
|
{
|
|
1171
1218
|
action: "focus",
|
|
1172
1219
|
selector,
|
|
1220
|
+
inputProbe: token,
|
|
1173
1221
|
},
|
|
1174
1222
|
{ userActivation: true },
|
|
1175
1223
|
);
|
|
@@ -1177,12 +1225,17 @@ export class FirefoxBidiSessionManager {
|
|
|
1177
1225
|
return focused;
|
|
1178
1226
|
}
|
|
1179
1227
|
|
|
1180
|
-
await
|
|
1228
|
+
const delivery = await sendCheckedInput({
|
|
1229
|
+
send: () => this.performKeyActions(session, actions),
|
|
1230
|
+
readProbe: (disarm) => this.readInputProbe(session, token, disarm),
|
|
1231
|
+
describe: `The key press "${key}"`,
|
|
1232
|
+
});
|
|
1181
1233
|
|
|
1182
1234
|
return {
|
|
1183
1235
|
browserFamily: "firefox",
|
|
1184
1236
|
key,
|
|
1185
1237
|
dispatched: true,
|
|
1238
|
+
...(delivery.resent ? { resent: true } : {}),
|
|
1186
1239
|
target: focused.target,
|
|
1187
1240
|
};
|
|
1188
1241
|
}
|
|
@@ -1388,6 +1441,26 @@ export class FirefoxBidiSessionManager {
|
|
|
1388
1441
|
});
|
|
1389
1442
|
}
|
|
1390
1443
|
|
|
1444
|
+
// Counts trusted input from the start of every later document, and from
|
|
1445
|
+
// now on in the current one, so input actions can check that their input
|
|
1446
|
+
// arrived. Without it they are reported as sent, unchecked.
|
|
1447
|
+
async installInputRecorder(session) {
|
|
1448
|
+
try {
|
|
1449
|
+
const preload = await this.send("script.addPreloadScript", {
|
|
1450
|
+
functionDeclaration: `() => ${buildInputRecorderExpression(true)}`,
|
|
1451
|
+
contexts: [session.target.targetId],
|
|
1452
|
+
});
|
|
1453
|
+
session.inputRecorderScript = preload?.script ?? null;
|
|
1454
|
+
await this.evaluateInContext(
|
|
1455
|
+
session,
|
|
1456
|
+
buildInputRecorderExpression(false),
|
|
1457
|
+
{ awaitPromise: false },
|
|
1458
|
+
);
|
|
1459
|
+
} catch {
|
|
1460
|
+
// Ignore recorder failures so attach still succeeds.
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1391
1464
|
async seedBufferedState(session) {
|
|
1392
1465
|
try {
|
|
1393
1466
|
const snapshot = await this.runPageAction(session, {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
2
|
+
|
|
3
|
+
export const INPUT_NOT_DELIVERED = "INPUT_NOT_DELIVERED";
|
|
4
|
+
|
|
5
|
+
// Input normally reaches the page before the browser acknowledges it, so
|
|
6
|
+
// these re-checks only cover a late delivery.
|
|
7
|
+
const RECHECK_DELAYS_MS = [50, 150];
|
|
8
|
+
|
|
9
|
+
// A browser can acknowledge input it never delivers: Chrome has been seen
|
|
10
|
+
// dropping every CDP input event to a tab after a navigation while still
|
|
11
|
+
// reporting success. So send the input and check with the probe the page
|
|
12
|
+
// script armed that it arrived. If it did not, and the probe can be sure of
|
|
13
|
+
// that, recover and send it once more. Input that still does not arrive is
|
|
14
|
+
// reported as an error rather than a success that did nothing. When the
|
|
15
|
+
// probe cannot be sure, because page listeners might have hidden the input,
|
|
16
|
+
// it is not sent again, so an action is never repeated.
|
|
17
|
+
//
|
|
18
|
+
// readProbe(disarm) returns the probe reading ({ delivered, conclusive,
|
|
19
|
+
// node }), or null when delivery cannot be checked, for example because the
|
|
20
|
+
// page navigated away. The page removes the probe once input arrives;
|
|
21
|
+
// disarm removes it when giving up.
|
|
22
|
+
export async function sendCheckedInput({ send, readProbe, recover, describe }) {
|
|
23
|
+
let settled = false;
|
|
24
|
+
const settle = (reading, resent) => {
|
|
25
|
+
settled = true;
|
|
26
|
+
return { node: reading?.node ?? null, resent };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
let resent = false;
|
|
30
|
+
try {
|
|
31
|
+
await send();
|
|
32
|
+
let reading = await awaitDelivery(readProbe);
|
|
33
|
+
if (reading.delivered) {
|
|
34
|
+
return settle(reading, false);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (reading.conclusive) {
|
|
38
|
+
await recover?.();
|
|
39
|
+
// The first input may have arrived, or the page navigated, while
|
|
40
|
+
// recovering; sending again then would repeat it or reach another page.
|
|
41
|
+
reading = (await readProbe(false)) ?? { delivered: true };
|
|
42
|
+
if (reading.delivered) {
|
|
43
|
+
return settle(reading, false);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
await send();
|
|
47
|
+
resent = true;
|
|
48
|
+
reading = await awaitDelivery(readProbe);
|
|
49
|
+
if (reading.delivered) {
|
|
50
|
+
return settle(reading, true);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} finally {
|
|
54
|
+
if (!settled) {
|
|
55
|
+
await readProbe(true);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const error = new Error(
|
|
60
|
+
resent
|
|
61
|
+
? `${describe} was sent twice, but the browser delivered no input events to the page, so it had no effect. The tab may not be accepting input yet, for example just after a navigation. Wait and try again, or act from the page with evaluate_js (for example element.click()).`
|
|
62
|
+
: `${describe} was sent, but no input events reached the page, so it most likely had no effect. It was not sent again, because a page listener may have hidden it. Check the page; if nothing changed, wait and try again, or act from the page with evaluate_js (for example element.click()).`,
|
|
63
|
+
);
|
|
64
|
+
error.code = INPUT_NOT_DELIVERED;
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The reading once the input has arrived or the re-checks are used up. A
|
|
69
|
+
// probe that cannot be read counts as delivered.
|
|
70
|
+
async function awaitDelivery(readProbe) {
|
|
71
|
+
let reading;
|
|
72
|
+
for (const delay of [0, ...RECHECK_DELAYS_MS]) {
|
|
73
|
+
if (delay) {
|
|
74
|
+
await sleep(delay);
|
|
75
|
+
}
|
|
76
|
+
reading = (await readProbe(false)) ?? { delivered: true };
|
|
77
|
+
if (reading.delivered) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return reading;
|
|
82
|
+
}
|
package/src/page-context.mjs
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import { pageScript } from "./page-script.mjs";
|
|
1
|
+
import { inputRecorder, pageScript } from "./page-script.mjs";
|
|
2
2
|
|
|
3
3
|
const PAGE_SCRIPT_SOURCE = pageScript.toString();
|
|
4
4
|
|
|
5
|
+
// The input recorder as a function declaration, to run with early = true
|
|
6
|
+
// when each new document starts or false on the current document.
|
|
7
|
+
export const INPUT_RECORDER_FUNCTION = inputRecorder.toString();
|
|
8
|
+
|
|
9
|
+
export function buildInputRecorderExpression(early) {
|
|
10
|
+
return `(${INPUT_RECORDER_FUNCTION})(${early})`;
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
// Builds an expression that runs pageScript in the page with this payload.
|
|
6
14
|
// With serialize, the expression evaluates to the result as a JSON string.
|
|
7
15
|
export function buildPageContextExpression(
|
package/src/page-script.mjs
CHANGED
|
@@ -892,6 +892,86 @@ export function pageScript(payload) {
|
|
|
892
892
|
return Boolean(label && label.control === element);
|
|
893
893
|
}
|
|
894
894
|
|
|
895
|
+
// Tells input the browser silently dropped apart from input the page
|
|
896
|
+
// received, from the trusted events inputRecorder counted between arming
|
|
897
|
+
// the probe and reading it. A recorder installed when the document started
|
|
898
|
+
// runs before every page listener, so no count means nothing arrived. One
|
|
899
|
+
// installed later can miss events that earlier page listeners stopped, so
|
|
900
|
+
// there a click also counts as delivered when the element under the pointer
|
|
901
|
+
// becomes hovered, and typing when the field's content changes: the
|
|
902
|
+
// browser does both before any page listener runs.
|
|
903
|
+
const INPUT_PROBE_KEY = Symbol.for("mcp-browser-dev-tools.inputProbe");
|
|
904
|
+
const INPUT_RECORDER_KEY = Symbol.for("mcp-browser-dev-tools.inputRecorder");
|
|
905
|
+
|
|
906
|
+
// Input aimed at a frame goes to the frame's own document, whose events
|
|
907
|
+
// this window does not see.
|
|
908
|
+
function hostsDocument(element) {
|
|
909
|
+
return ["IFRAME", "FRAME", "OBJECT", "EMBED"].includes(element?.tagName);
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
function isHovered(element) {
|
|
913
|
+
try {
|
|
914
|
+
return element.matches(":hover");
|
|
915
|
+
} catch {
|
|
916
|
+
return false;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function editedContent(element) {
|
|
921
|
+
if (!element) {
|
|
922
|
+
return null;
|
|
923
|
+
}
|
|
924
|
+
return typeof element.value === "string"
|
|
925
|
+
? element.value
|
|
926
|
+
: element.textContent;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
function armInputProbe(token, kind, options = {}) {
|
|
930
|
+
const recorder = window[INPUT_RECORDER_KEY];
|
|
931
|
+
if (!recorder) {
|
|
932
|
+
delete window[INPUT_PROBE_KEY];
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
const hovered = options.pointerAt ?? null;
|
|
936
|
+
window[INPUT_PROBE_KEY] = {
|
|
937
|
+
token,
|
|
938
|
+
kind,
|
|
939
|
+
count: recorder[kind],
|
|
940
|
+
early: recorder.early,
|
|
941
|
+
element: options.element ?? null,
|
|
942
|
+
locator: options.locator ?? null,
|
|
943
|
+
hovered: hovered && !isHovered(hovered) ? hovered : null,
|
|
944
|
+
edited: options.edited ?? null,
|
|
945
|
+
content: editedContent(options.edited),
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// Unarmed means delivery cannot be checked, for example after the page
|
|
950
|
+
// navigated. conclusive says whether "not delivered" can be trusted.
|
|
951
|
+
function readInputProbe(token, disarm) {
|
|
952
|
+
const probe = window[INPUT_PROBE_KEY];
|
|
953
|
+
const recorder = window[INPUT_RECORDER_KEY];
|
|
954
|
+
if (!probe || probe.token !== token || !recorder) {
|
|
955
|
+
return { armed: false, delivered: false, conclusive: false };
|
|
956
|
+
}
|
|
957
|
+
const delivered =
|
|
958
|
+
recorder[probe.kind] > probe.count ||
|
|
959
|
+
Boolean(probe.hovered && isHovered(probe.hovered)) ||
|
|
960
|
+
(probe.edited !== null && editedContent(probe.edited) !== probe.content);
|
|
961
|
+
if (disarm || delivered) {
|
|
962
|
+
delete window[INPUT_PROBE_KEY];
|
|
963
|
+
}
|
|
964
|
+
return {
|
|
965
|
+
armed: true,
|
|
966
|
+
delivered,
|
|
967
|
+
conclusive: probe.early,
|
|
968
|
+
node:
|
|
969
|
+
delivered && probe.element?.isConnected
|
|
970
|
+
? describeElement(probe.element, probe.locator)
|
|
971
|
+
: null,
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
|
|
895
975
|
// Whether the page renders the element at all. innerText falls back to the
|
|
896
976
|
// raw text content for an element inside display: none, which the page
|
|
897
977
|
// does not show; unlike isVisible, size and pointer events do not matter.
|
|
@@ -1046,6 +1126,18 @@ export function pageScript(payload) {
|
|
|
1046
1126
|
const receivesEvents = Boolean(
|
|
1047
1127
|
hit && receivesPointerAt(resolved.element, hit),
|
|
1048
1128
|
);
|
|
1129
|
+
if (
|
|
1130
|
+
receivesEvents &&
|
|
1131
|
+
typeof payload.inputProbe === "string" &&
|
|
1132
|
+
!hostsDocument(hit) &&
|
|
1133
|
+
!isDisabled(resolved.element)
|
|
1134
|
+
) {
|
|
1135
|
+
armInputProbe(payload.inputProbe, "pointer", {
|
|
1136
|
+
element: resolved.element,
|
|
1137
|
+
locator: resolved,
|
|
1138
|
+
pointerAt: hit,
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1049
1141
|
|
|
1050
1142
|
return {
|
|
1051
1143
|
browserFamily: payload.browserFamily,
|
|
@@ -1057,6 +1149,8 @@ export function pageScript(payload) {
|
|
|
1057
1149
|
node: describeElement(resolved.element, resolved),
|
|
1058
1150
|
};
|
|
1059
1151
|
}
|
|
1152
|
+
case "input_probe":
|
|
1153
|
+
return readInputProbe(payload.token, payload.disarm === true);
|
|
1060
1154
|
case "prepare_type": {
|
|
1061
1155
|
const resolved = ensureResolved(payload.selector);
|
|
1062
1156
|
if (!resolved.element) {
|
|
@@ -1122,6 +1216,14 @@ export function pageScript(payload) {
|
|
|
1122
1216
|
selection.addRange(range);
|
|
1123
1217
|
}
|
|
1124
1218
|
|
|
1219
|
+
if (typeof payload.inputProbe === "string") {
|
|
1220
|
+
armInputProbe(payload.inputProbe, "keyboard", {
|
|
1221
|
+
element,
|
|
1222
|
+
locator: resolved,
|
|
1223
|
+
edited: element,
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1125
1227
|
return {
|
|
1126
1228
|
browserFamily: payload.browserFamily,
|
|
1127
1229
|
selector: payload.selector,
|
|
@@ -1181,11 +1283,18 @@ export function pageScript(payload) {
|
|
|
1181
1283
|
}
|
|
1182
1284
|
}
|
|
1183
1285
|
|
|
1286
|
+
const focused = getDeepActiveElement() ?? document.body;
|
|
1287
|
+
if (typeof payload.inputProbe === "string" && !hostsDocument(focused)) {
|
|
1288
|
+
armInputProbe(payload.inputProbe, "keyboard", {
|
|
1289
|
+
edited: isEditable(focused) ? focused : null,
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1184
1293
|
return {
|
|
1185
1294
|
browserFamily: payload.browserFamily,
|
|
1186
1295
|
selector: payload.selector ?? null,
|
|
1187
1296
|
found: true,
|
|
1188
|
-
target: summarizeTarget(
|
|
1297
|
+
target: summarizeTarget(focused),
|
|
1189
1298
|
};
|
|
1190
1299
|
}
|
|
1191
1300
|
case "scroll": {
|
|
@@ -1370,3 +1479,35 @@ export function pageScript(payload) {
|
|
|
1370
1479
|
|
|
1371
1480
|
return runAction();
|
|
1372
1481
|
}
|
|
1482
|
+
|
|
1483
|
+
// Counts trusted pointer and keyboard events in the page from the moment it
|
|
1484
|
+
// runs, for the input probe in pageScript. Registered to run when each new
|
|
1485
|
+
// document starts (early), it sees every event before page listeners can
|
|
1486
|
+
// stop it; run on a document that is already loaded, it misses what
|
|
1487
|
+
// listeners added before it stop. Like pageScript, it is serialized with
|
|
1488
|
+
// toString() and must not reference anything outside itself.
|
|
1489
|
+
export function inputRecorder(early) {
|
|
1490
|
+
const key = Symbol.for("mcp-browser-dev-tools.inputRecorder");
|
|
1491
|
+
if (window[key]) {
|
|
1492
|
+
return;
|
|
1493
|
+
}
|
|
1494
|
+
const counts = { pointer: 0, keyboard: 0, early };
|
|
1495
|
+
const kinds = {
|
|
1496
|
+
pointer: ["pointerdown", "mousedown", "pointerup", "mouseup", "click"],
|
|
1497
|
+
keyboard: ["keydown", "keyup", "beforeinput", "input"],
|
|
1498
|
+
};
|
|
1499
|
+
for (const [kind, types] of Object.entries(kinds)) {
|
|
1500
|
+
for (const type of types) {
|
|
1501
|
+
window.addEventListener(
|
|
1502
|
+
type,
|
|
1503
|
+
(event) => {
|
|
1504
|
+
if (event.isTrusted) {
|
|
1505
|
+
counts[kind] += 1;
|
|
1506
|
+
}
|
|
1507
|
+
},
|
|
1508
|
+
true,
|
|
1509
|
+
);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
Object.defineProperty(window, key, { value: counts });
|
|
1513
|
+
}
|