mcp-browser-dev-tools 0.0.5 → 0.0.6-beta.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/package.json +2 -1
- package/src/cdp-client.mjs +120 -20
- package/src/firefox-bidi-client.mjs +127 -22
- package/src/keyboard.mjs +278 -0
- package/src/mcp-server.mjs +132 -37
- package/src/page-context.mjs +210 -88
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-browser-dev-tools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6-beta.0",
|
|
4
4
|
"description": "Local MCP server for Chromium DevTools Protocol and Firefox WebDriver BiDi",
|
|
5
5
|
"author": "Hinaser <rindo.hinase@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"src/firefox-bidi-client.mjs",
|
|
33
33
|
"src/index.mjs",
|
|
34
34
|
"src/json-rpc-stdio.mjs",
|
|
35
|
+
"src/keyboard.mjs",
|
|
35
36
|
"src/logger.mjs",
|
|
36
37
|
"src/mcp-server.mjs",
|
|
37
38
|
"src/page-context.mjs",
|
package/src/cdp-client.mjs
CHANGED
|
@@ -5,7 +5,11 @@ import {
|
|
|
5
5
|
} from "./session-events.mjs";
|
|
6
6
|
import { DEFAULT_CDP_BASE_URL } from "./config.mjs";
|
|
7
7
|
import { waitForPageCondition } from "./wait-for.mjs";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
assertPointerTarget,
|
|
10
|
+
buildPageContextExpression,
|
|
11
|
+
} from "./page-context.mjs";
|
|
12
|
+
import { buildCdpKeyEvents } from "./keyboard.mjs";
|
|
9
13
|
|
|
10
14
|
function toErrorMessage(error) {
|
|
11
15
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -807,36 +811,120 @@ export class CdpSession {
|
|
|
807
811
|
});
|
|
808
812
|
}
|
|
809
813
|
|
|
814
|
+
async resolvePointerTarget(selector) {
|
|
815
|
+
const target = await this.runPageAction({
|
|
816
|
+
action: "pointer_target",
|
|
817
|
+
selector,
|
|
818
|
+
});
|
|
819
|
+
assertPointerTarget(target);
|
|
820
|
+
return target;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
async dispatchMouse(type, point, extra = {}) {
|
|
824
|
+
await this.send("Input.dispatchMouseEvent", {
|
|
825
|
+
type,
|
|
826
|
+
x: point.x,
|
|
827
|
+
y: point.y,
|
|
828
|
+
...extra,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
async dispatchKeyEvents(events) {
|
|
833
|
+
for (const params of events) {
|
|
834
|
+
await this.send("Input.dispatchKeyEvent", params);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
810
838
|
async click(selector) {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
);
|
|
839
|
+
const target = await this.resolvePointerTarget(selector);
|
|
840
|
+
if (!target.found) {
|
|
841
|
+
return target;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
const press = { button: "left", clickCount: 1 };
|
|
845
|
+
await this.dispatchMouse("mouseMoved", target.point);
|
|
846
|
+
await this.dispatchMouse("mousePressed", target.point, {
|
|
847
|
+
...press,
|
|
848
|
+
buttons: 1,
|
|
849
|
+
});
|
|
850
|
+
await this.dispatchMouse("mouseReleased", target.point, {
|
|
851
|
+
...press,
|
|
852
|
+
buttons: 0,
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
return {
|
|
856
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
857
|
+
selector,
|
|
858
|
+
found: true,
|
|
859
|
+
clicked: true,
|
|
860
|
+
point: target.point,
|
|
861
|
+
node: target.node,
|
|
862
|
+
};
|
|
818
863
|
}
|
|
819
864
|
|
|
820
865
|
async hover(selector) {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
866
|
+
const target = await this.resolvePointerTarget(selector);
|
|
867
|
+
if (!target.found) {
|
|
868
|
+
return target;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
await this.dispatchMouse("mouseMoved", target.point);
|
|
872
|
+
|
|
873
|
+
return {
|
|
874
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
875
|
+
selector,
|
|
876
|
+
found: true,
|
|
877
|
+
hovered: true,
|
|
878
|
+
point: target.point,
|
|
879
|
+
node: target.node,
|
|
880
|
+
};
|
|
828
881
|
}
|
|
829
882
|
|
|
830
883
|
async type(selector, text, options = {}) {
|
|
831
|
-
|
|
884
|
+
const prepared = await this.runPageAction(
|
|
832
885
|
{
|
|
833
|
-
action: "
|
|
886
|
+
action: "prepare_type",
|
|
834
887
|
selector,
|
|
835
888
|
text,
|
|
836
889
|
clear: options.clear,
|
|
837
890
|
},
|
|
838
891
|
{ userGesture: true },
|
|
839
892
|
);
|
|
893
|
+
if (!prepared.found || prepared.method !== "native") {
|
|
894
|
+
return prepared;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// insertText goes through the browser's editing pipeline, so frameworks
|
|
898
|
+
// see trusted beforeinput/input events and the real value change.
|
|
899
|
+
// Newlines become Enter presses, matching the Firefox path: a line break
|
|
900
|
+
// in a textarea, implicit submission in a single-line input.
|
|
901
|
+
if (text) {
|
|
902
|
+
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
903
|
+
for (const [index, line] of lines.entries()) {
|
|
904
|
+
if (index > 0) {
|
|
905
|
+
await this.dispatchKeyEvents(buildCdpKeyEvents("Enter"));
|
|
906
|
+
}
|
|
907
|
+
if (line) {
|
|
908
|
+
await this.send("Input.insertText", { text: line });
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
} else if (options.clear !== false) {
|
|
912
|
+
await this.dispatchKeyEvents(buildCdpKeyEvents("Delete"));
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
const typed = await this.runPageAction({
|
|
916
|
+
action: "inspect",
|
|
917
|
+
selector,
|
|
918
|
+
scrollIntoView: false,
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
return {
|
|
922
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
923
|
+
selector,
|
|
924
|
+
found: true,
|
|
925
|
+
typedText: text,
|
|
926
|
+
node: typed.found ? typed.node : prepared.node,
|
|
927
|
+
};
|
|
840
928
|
}
|
|
841
929
|
|
|
842
930
|
async select(selector, options = {}) {
|
|
@@ -852,14 +940,26 @@ export class CdpSession {
|
|
|
852
940
|
}
|
|
853
941
|
|
|
854
942
|
async pressKey(key, selector = null) {
|
|
855
|
-
|
|
943
|
+
const events = buildCdpKeyEvents(key);
|
|
944
|
+
const focused = await this.runPageAction(
|
|
856
945
|
{
|
|
857
|
-
action: "
|
|
858
|
-
key,
|
|
946
|
+
action: "focus",
|
|
859
947
|
selector,
|
|
860
948
|
},
|
|
861
949
|
{ userGesture: true },
|
|
862
950
|
);
|
|
951
|
+
if (!focused.found) {
|
|
952
|
+
return focused;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
await this.dispatchKeyEvents(events);
|
|
956
|
+
|
|
957
|
+
return {
|
|
958
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
959
|
+
key,
|
|
960
|
+
dispatched: true,
|
|
961
|
+
target: focused.target,
|
|
962
|
+
};
|
|
863
963
|
}
|
|
864
964
|
|
|
865
965
|
async scroll(options = {}) {
|
|
@@ -5,7 +5,26 @@ import {
|
|
|
5
5
|
} from "./session-events.mjs";
|
|
6
6
|
import { DEFAULT_FIREFOX_BIDI_WS_URL } from "./config.mjs";
|
|
7
7
|
import { waitForPageCondition } from "./wait-for.mjs";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
assertPointerTarget,
|
|
10
|
+
buildPageContextExpression,
|
|
11
|
+
} from "./page-context.mjs";
|
|
12
|
+
import { buildBidiKeyActions, buildBidiTextActions } from "./keyboard.mjs";
|
|
13
|
+
|
|
14
|
+
const POINTER_SOURCE = {
|
|
15
|
+
type: "pointer",
|
|
16
|
+
id: "mcp-mouse",
|
|
17
|
+
parameters: { pointerType: "mouse" },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function pointerMove(point) {
|
|
21
|
+
return {
|
|
22
|
+
type: "pointerMove",
|
|
23
|
+
x: Math.round(point.x),
|
|
24
|
+
y: Math.round(point.y),
|
|
25
|
+
origin: "viewport",
|
|
26
|
+
};
|
|
27
|
+
}
|
|
9
28
|
|
|
10
29
|
function toErrorMessage(error) {
|
|
11
30
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -1013,39 +1032,112 @@ export class FirefoxBidiSessionManager {
|
|
|
1013
1032
|
};
|
|
1014
1033
|
}
|
|
1015
1034
|
|
|
1035
|
+
async performActions(session, actions) {
|
|
1036
|
+
await this.send("input.performActions", {
|
|
1037
|
+
context: session.target.targetId,
|
|
1038
|
+
actions,
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
async performKeyActions(session, actions) {
|
|
1043
|
+
await this.performActions(session, [
|
|
1044
|
+
{ type: "key", id: "mcp-keyboard", actions },
|
|
1045
|
+
]);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
async resolvePointerTarget(session, selector) {
|
|
1049
|
+
const target = await this.runPageAction(session, {
|
|
1050
|
+
action: "pointer_target",
|
|
1051
|
+
selector,
|
|
1052
|
+
});
|
|
1053
|
+
assertPointerTarget(target);
|
|
1054
|
+
return target;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1016
1057
|
async click(sessionId, selector) {
|
|
1017
|
-
|
|
1018
|
-
|
|
1058
|
+
const session = this.getSession(sessionId);
|
|
1059
|
+
const target = await this.resolvePointerTarget(session, selector);
|
|
1060
|
+
if (!target.found) {
|
|
1061
|
+
return target;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
await this.performActions(session, [
|
|
1019
1065
|
{
|
|
1020
|
-
|
|
1021
|
-
|
|
1066
|
+
...POINTER_SOURCE,
|
|
1067
|
+
actions: [
|
|
1068
|
+
pointerMove(target.point),
|
|
1069
|
+
{ type: "pointerDown", button: 0 },
|
|
1070
|
+
{ type: "pointerUp", button: 0 },
|
|
1071
|
+
],
|
|
1022
1072
|
},
|
|
1023
|
-
|
|
1024
|
-
|
|
1073
|
+
]);
|
|
1074
|
+
|
|
1075
|
+
return {
|
|
1076
|
+
browserFamily: "firefox",
|
|
1077
|
+
selector,
|
|
1078
|
+
found: true,
|
|
1079
|
+
clicked: true,
|
|
1080
|
+
point: target.point,
|
|
1081
|
+
node: target.node,
|
|
1082
|
+
};
|
|
1025
1083
|
}
|
|
1026
1084
|
|
|
1027
1085
|
async hover(sessionId, selector) {
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1086
|
+
const session = this.getSession(sessionId);
|
|
1087
|
+
const target = await this.resolvePointerTarget(session, selector);
|
|
1088
|
+
if (!target.found) {
|
|
1089
|
+
return target;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
await this.performActions(session, [
|
|
1093
|
+
{ ...POINTER_SOURCE, actions: [pointerMove(target.point)] },
|
|
1094
|
+
]);
|
|
1095
|
+
|
|
1096
|
+
return {
|
|
1097
|
+
browserFamily: "firefox",
|
|
1098
|
+
selector,
|
|
1099
|
+
found: true,
|
|
1100
|
+
hovered: true,
|
|
1101
|
+
point: target.point,
|
|
1102
|
+
node: target.node,
|
|
1103
|
+
};
|
|
1036
1104
|
}
|
|
1037
1105
|
|
|
1038
1106
|
async type(sessionId, selector, text, options = {}) {
|
|
1039
|
-
|
|
1040
|
-
|
|
1107
|
+
const session = this.getSession(sessionId);
|
|
1108
|
+
const prepared = await this.runPageAction(
|
|
1109
|
+
session,
|
|
1041
1110
|
{
|
|
1042
|
-
action: "
|
|
1111
|
+
action: "prepare_type",
|
|
1043
1112
|
selector,
|
|
1044
1113
|
text,
|
|
1045
1114
|
clear: options.clear,
|
|
1046
1115
|
},
|
|
1047
1116
|
{ userActivation: true },
|
|
1048
1117
|
);
|
|
1118
|
+
if (!prepared.found || prepared.method !== "native") {
|
|
1119
|
+
return prepared;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
if (text) {
|
|
1123
|
+
await this.performKeyActions(session, buildBidiTextActions(text));
|
|
1124
|
+
} else if (options.clear !== false) {
|
|
1125
|
+
await this.performKeyActions(session, buildBidiKeyActions("Backspace"));
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
const typed = await this.runPageAction(session, {
|
|
1129
|
+
action: "inspect",
|
|
1130
|
+
selector,
|
|
1131
|
+
scrollIntoView: false,
|
|
1132
|
+
});
|
|
1133
|
+
|
|
1134
|
+
return {
|
|
1135
|
+
browserFamily: "firefox",
|
|
1136
|
+
selector,
|
|
1137
|
+
found: true,
|
|
1138
|
+
typedText: text,
|
|
1139
|
+
node: typed.found ? typed.node : prepared.node,
|
|
1140
|
+
};
|
|
1049
1141
|
}
|
|
1050
1142
|
|
|
1051
1143
|
async select(sessionId, selector, options = {}) {
|
|
@@ -1062,15 +1154,28 @@ export class FirefoxBidiSessionManager {
|
|
|
1062
1154
|
}
|
|
1063
1155
|
|
|
1064
1156
|
async pressKey(sessionId, key, selector = null) {
|
|
1065
|
-
|
|
1066
|
-
|
|
1157
|
+
const session = this.getSession(sessionId);
|
|
1158
|
+
const actions = buildBidiKeyActions(key);
|
|
1159
|
+
const focused = await this.runPageAction(
|
|
1160
|
+
session,
|
|
1067
1161
|
{
|
|
1068
|
-
action: "
|
|
1069
|
-
key,
|
|
1162
|
+
action: "focus",
|
|
1070
1163
|
selector,
|
|
1071
1164
|
},
|
|
1072
1165
|
{ userActivation: true },
|
|
1073
1166
|
);
|
|
1167
|
+
if (!focused.found) {
|
|
1168
|
+
return focused;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
await this.performKeyActions(session, actions);
|
|
1172
|
+
|
|
1173
|
+
return {
|
|
1174
|
+
browserFamily: "firefox",
|
|
1175
|
+
key,
|
|
1176
|
+
dispatched: true,
|
|
1177
|
+
target: focused.target,
|
|
1178
|
+
};
|
|
1074
1179
|
}
|
|
1075
1180
|
|
|
1076
1181
|
async scroll(sessionId, options = {}) {
|
package/src/keyboard.mjs
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Key descriptions shared by the CDP (Input.dispatchKeyEvent) and
|
|
2
|
+
// WebDriver BiDi (input.performActions) input paths.
|
|
3
|
+
|
|
4
|
+
const MODIFIER_BITS = {
|
|
5
|
+
Alt: 1,
|
|
6
|
+
Control: 2,
|
|
7
|
+
Meta: 4,
|
|
8
|
+
Shift: 8,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function namedKey(key, code, keyCode, webdriver, extra = {}) {
|
|
12
|
+
return { key, code, keyCode, webdriver, ...extra };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const NAMED_KEYS = [
|
|
16
|
+
namedKey("Enter", "Enter", 13, "", { text: "\r" }),
|
|
17
|
+
namedKey("Tab", "Tab", 9, ""),
|
|
18
|
+
namedKey("Escape", "Escape", 27, ""),
|
|
19
|
+
namedKey("Backspace", "Backspace", 8, ""),
|
|
20
|
+
namedKey("Delete", "Delete", 46, ""),
|
|
21
|
+
namedKey("Insert", "Insert", 45, ""),
|
|
22
|
+
namedKey("ArrowUp", "ArrowUp", 38, ""),
|
|
23
|
+
namedKey("ArrowDown", "ArrowDown", 40, ""),
|
|
24
|
+
namedKey("ArrowLeft", "ArrowLeft", 37, ""),
|
|
25
|
+
namedKey("ArrowRight", "ArrowRight", 39, ""),
|
|
26
|
+
namedKey("Home", "Home", 36, ""),
|
|
27
|
+
namedKey("End", "End", 35, ""),
|
|
28
|
+
namedKey("PageUp", "PageUp", 33, ""),
|
|
29
|
+
namedKey("PageDown", "PageDown", 34, ""),
|
|
30
|
+
namedKey(" ", "Space", 32, " ", { text: " " }),
|
|
31
|
+
namedKey("Shift", "ShiftLeft", 16, "", { location: 1 }),
|
|
32
|
+
namedKey("Control", "ControlLeft", 17, "", { location: 1 }),
|
|
33
|
+
namedKey("Alt", "AltLeft", 18, "", { location: 1 }),
|
|
34
|
+
namedKey("Meta", "MetaLeft", 91, "", { location: 1 }),
|
|
35
|
+
...Array.from({ length: 12 }, (_, index) =>
|
|
36
|
+
namedKey(
|
|
37
|
+
`F${index + 1}`,
|
|
38
|
+
`F${index + 1}`,
|
|
39
|
+
112 + index,
|
|
40
|
+
String.fromCharCode(0xe031 + index),
|
|
41
|
+
),
|
|
42
|
+
),
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const KEY_ALIASES = {
|
|
46
|
+
space: " ",
|
|
47
|
+
esc: "Escape",
|
|
48
|
+
return: "Enter",
|
|
49
|
+
del: "Delete",
|
|
50
|
+
up: "ArrowUp",
|
|
51
|
+
down: "ArrowDown",
|
|
52
|
+
left: "ArrowLeft",
|
|
53
|
+
right: "ArrowRight",
|
|
54
|
+
ctrl: "Control",
|
|
55
|
+
cmd: "Meta",
|
|
56
|
+
command: "Meta",
|
|
57
|
+
option: "Alt",
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const NAMED_KEYS_BY_NAME = new Map(
|
|
61
|
+
NAMED_KEYS.flatMap((definition) => [
|
|
62
|
+
[definition.key.toLowerCase(), definition],
|
|
63
|
+
[definition.code.toLowerCase(), definition],
|
|
64
|
+
]),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
for (const [alias, key] of Object.entries(KEY_ALIASES)) {
|
|
68
|
+
NAMED_KEYS_BY_NAME.set(alias, NAMED_KEYS_BY_NAME.get(key.toLowerCase()));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// US layout codes for unshifted punctuation.
|
|
72
|
+
const PUNCTUATION = {
|
|
73
|
+
"-": ["Minus", 189],
|
|
74
|
+
"=": ["Equal", 187],
|
|
75
|
+
"[": ["BracketLeft", 219],
|
|
76
|
+
"]": ["BracketRight", 221],
|
|
77
|
+
"\\": ["Backslash", 220],
|
|
78
|
+
";": ["Semicolon", 186],
|
|
79
|
+
"'": ["Quote", 222],
|
|
80
|
+
",": ["Comma", 188],
|
|
81
|
+
".": ["Period", 190],
|
|
82
|
+
"/": ["Slash", 191],
|
|
83
|
+
"`": ["Backquote", 192],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// US layout: the character Shift produces for each digit or punctuation key.
|
|
87
|
+
const SHIFTED = {
|
|
88
|
+
1: "!",
|
|
89
|
+
2: "@",
|
|
90
|
+
3: "#",
|
|
91
|
+
4: "$",
|
|
92
|
+
5: "%",
|
|
93
|
+
6: "^",
|
|
94
|
+
7: "&",
|
|
95
|
+
8: "*",
|
|
96
|
+
9: "(",
|
|
97
|
+
0: ")",
|
|
98
|
+
"-": "_",
|
|
99
|
+
"=": "+",
|
|
100
|
+
"[": "{",
|
|
101
|
+
"]": "}",
|
|
102
|
+
"\\": "|",
|
|
103
|
+
";": ":",
|
|
104
|
+
"'": '"',
|
|
105
|
+
",": "<",
|
|
106
|
+
".": ">",
|
|
107
|
+
"/": "?",
|
|
108
|
+
"`": "~",
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// Chrome on macOS routes these shortcuts through the app menu, so CDP key
|
|
112
|
+
// events alone do not trigger them; they must be sent as editing commands.
|
|
113
|
+
const META_EDITING_COMMANDS = {
|
|
114
|
+
KeyA: "selectAll",
|
|
115
|
+
KeyC: "copy",
|
|
116
|
+
KeyX: "cut",
|
|
117
|
+
KeyV: "paste",
|
|
118
|
+
KeyZ: "undo",
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
function describeCharacter(char, shift) {
|
|
122
|
+
const text = shift && /^[a-z]$/.test(char) ? char.toUpperCase() : char;
|
|
123
|
+
|
|
124
|
+
if (/^[a-z]$/i.test(char)) {
|
|
125
|
+
const upper = char.toUpperCase();
|
|
126
|
+
return {
|
|
127
|
+
key: text,
|
|
128
|
+
code: `Key${upper}`,
|
|
129
|
+
keyCode: upper.charCodeAt(0),
|
|
130
|
+
webdriver: text,
|
|
131
|
+
text,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const produced = shift ? (SHIFTED[char] ?? char) : char;
|
|
136
|
+
|
|
137
|
+
if (/^[0-9]$/.test(char)) {
|
|
138
|
+
return {
|
|
139
|
+
key: produced,
|
|
140
|
+
code: `Digit${char}`,
|
|
141
|
+
keyCode: char.charCodeAt(0),
|
|
142
|
+
webdriver: produced,
|
|
143
|
+
text: produced,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const [code, keyCode] = PUNCTUATION[char] ?? ["", 0];
|
|
148
|
+
return {
|
|
149
|
+
key: produced,
|
|
150
|
+
code,
|
|
151
|
+
keyCode,
|
|
152
|
+
webdriver: produced,
|
|
153
|
+
text: produced,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function describeKey(name, shift = false) {
|
|
158
|
+
if ([...name].length === 1) {
|
|
159
|
+
return describeCharacter(name, shift);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const definition = NAMED_KEYS_BY_NAME.get(name.toLowerCase());
|
|
163
|
+
if (!definition) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
`Unsupported key "${name}". Use a single character or a named key such as Enter, Tab, Escape, Backspace, ArrowDown, or F5.`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return definition;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function parseKeyCombo(combo) {
|
|
173
|
+
if (typeof combo !== "string" || combo.length === 0) {
|
|
174
|
+
throw new Error("key must be a non-empty string");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let parts;
|
|
178
|
+
if (combo === "+") {
|
|
179
|
+
parts = ["+"];
|
|
180
|
+
} else if (combo.endsWith("++")) {
|
|
181
|
+
parts = [...combo.slice(0, -2).split("+"), "+"];
|
|
182
|
+
} else {
|
|
183
|
+
parts = combo.split("+");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const keyName = parts.pop();
|
|
187
|
+
const modifiers = parts.map((part) => {
|
|
188
|
+
const definition = NAMED_KEYS_BY_NAME.get(part.toLowerCase());
|
|
189
|
+
if (!definition || !(definition.key in MODIFIER_BITS)) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
`Unsupported modifier "${part}" in "${combo}". Use Shift, Control, Alt, or Meta.`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
return definition;
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const shift = modifiers.some((modifier) => modifier.key === "Shift");
|
|
198
|
+
return {
|
|
199
|
+
modifiers,
|
|
200
|
+
key: describeKey(keyName, shift),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function cdpKeyParams(type, definition, modifiers, extra = {}) {
|
|
205
|
+
return {
|
|
206
|
+
type,
|
|
207
|
+
modifiers,
|
|
208
|
+
key: definition.key,
|
|
209
|
+
code: definition.code,
|
|
210
|
+
windowsVirtualKeyCode: definition.keyCode,
|
|
211
|
+
location: definition.location ?? 0,
|
|
212
|
+
...extra,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function buildCdpKeyEvents(combo) {
|
|
217
|
+
const { modifiers, key } = parseKeyCombo(combo);
|
|
218
|
+
const events = [];
|
|
219
|
+
let modifierBits = 0;
|
|
220
|
+
|
|
221
|
+
for (const modifier of modifiers) {
|
|
222
|
+
modifierBits |= MODIFIER_BITS[modifier.key];
|
|
223
|
+
events.push(cdpKeyParams("rawKeyDown", modifier, modifierBits));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Shortcuts (anything beyond Shift) must not insert text.
|
|
227
|
+
const text = modifierBits & ~MODIFIER_BITS.Shift ? "" : (key.text ?? "");
|
|
228
|
+
const extra = text ? { text, unmodifiedText: text } : {};
|
|
229
|
+
|
|
230
|
+
const command =
|
|
231
|
+
modifierBits & MODIFIER_BITS.Meta
|
|
232
|
+
? key.code === "KeyZ" && modifierBits & MODIFIER_BITS.Shift
|
|
233
|
+
? "redo"
|
|
234
|
+
: META_EDITING_COMMANDS[key.code]
|
|
235
|
+
: null;
|
|
236
|
+
if (command) {
|
|
237
|
+
extra.commands = [command];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
events.push(
|
|
241
|
+
cdpKeyParams(text ? "keyDown" : "rawKeyDown", key, modifierBits, extra),
|
|
242
|
+
);
|
|
243
|
+
events.push(cdpKeyParams("keyUp", key, modifierBits));
|
|
244
|
+
|
|
245
|
+
for (const modifier of [...modifiers].reverse()) {
|
|
246
|
+
modifierBits &= ~MODIFIER_BITS[modifier.key];
|
|
247
|
+
events.push(cdpKeyParams("keyUp", modifier, modifierBits));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return events;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function buildBidiKeyActions(combo) {
|
|
254
|
+
const { modifiers, key } = parseKeyCombo(combo);
|
|
255
|
+
return [
|
|
256
|
+
...modifiers.map((modifier) => ({
|
|
257
|
+
type: "keyDown",
|
|
258
|
+
value: modifier.webdriver,
|
|
259
|
+
})),
|
|
260
|
+
{ type: "keyDown", value: key.webdriver },
|
|
261
|
+
{ type: "keyUp", value: key.webdriver },
|
|
262
|
+
...[...modifiers].reverse().map((modifier) => ({
|
|
263
|
+
type: "keyUp",
|
|
264
|
+
value: modifier.webdriver,
|
|
265
|
+
})),
|
|
266
|
+
];
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function buildBidiTextActions(text) {
|
|
270
|
+
const enter = NAMED_KEYS_BY_NAME.get("enter").webdriver;
|
|
271
|
+
return [...text.replace(/\r\n?/g, "\n")].flatMap((char) => {
|
|
272
|
+
const value = char === "\n" ? enter : char;
|
|
273
|
+
return [
|
|
274
|
+
{ type: "keyDown", value },
|
|
275
|
+
{ type: "keyUp", value },
|
|
276
|
+
];
|
|
277
|
+
});
|
|
278
|
+
}
|