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/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
|
+
}
|