mcp-browser-dev-tools 0.0.1-beta.1 → 0.0.1-beta.11
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 +260 -13
- package/package.json +4 -1
- package/src/browser-adapter.mjs +275 -0
- package/src/browser-launcher.mjs +35 -0
- package/src/cdp-client.mjs +526 -85
- package/src/cli.mjs +212 -9
- package/src/config.mjs +24 -9
- package/src/doctor.mjs +25 -5
- package/src/firefox-bidi-client.mjs +509 -100
- package/src/index.mjs +13 -1
- package/src/json-rpc-stdio.mjs +130 -21
- package/src/mcp-server.mjs +360 -5
- package/src/page-context.mjs +734 -0
- package/src/session-events.mjs +16 -2
- package/src/tcp-relay.mjs +194 -0
package/README.md
CHANGED
|
@@ -9,10 +9,11 @@ It is designed for a local trust boundary:
|
|
|
9
9
|
## What You Get
|
|
10
10
|
|
|
11
11
|
- A stdio MCP server for local desktop and terminal clients
|
|
12
|
-
- Browser discovery and attach/detach for Chromium-family browsers and Firefox
|
|
13
|
-
-
|
|
12
|
+
- Browser discovery and attach/detach for Chrome, Edge, other Chromium-family browsers, and Firefox
|
|
13
|
+
- Inspection tools for DOM lookup, richer element details, console messages, network requests, screenshots, tab listing, and buffered events
|
|
14
|
+
- Page interaction tools for navigation, reload, click, hover, type, select, key presses, scroll, and viewport overrides
|
|
14
15
|
- Optional JavaScript evaluation behind an explicit environment flag
|
|
15
|
-
- Helper commands to check browser connectivity
|
|
16
|
+
- Helper commands to check browser connectivity, launch a debug-enabled browser, and relay CDP traffic across a local machine boundary
|
|
16
17
|
|
|
17
18
|
## Requirements
|
|
18
19
|
|
|
@@ -22,6 +23,10 @@ It is designed for a local trust boundary:
|
|
|
22
23
|
|
|
23
24
|
## Install
|
|
24
25
|
|
|
26
|
+
If you are trying the prerelease from npm, replace `mcp-browser-dev-tools` below with `mcp-browser-dev-tools@beta`.
|
|
27
|
+
|
|
28
|
+
The published package name stays `mcp-browser-dev-tools`. The preferred installed CLI command is `mbdt`.
|
|
29
|
+
|
|
25
30
|
One-off execution with `npx`:
|
|
26
31
|
|
|
27
32
|
```bash
|
|
@@ -32,19 +37,159 @@ Global install:
|
|
|
32
37
|
|
|
33
38
|
```bash
|
|
34
39
|
npm install -g mcp-browser-dev-tools
|
|
35
|
-
|
|
40
|
+
mbdt serve
|
|
36
41
|
```
|
|
37
42
|
|
|
38
43
|
Project-local install:
|
|
39
44
|
|
|
40
45
|
```bash
|
|
41
46
|
npm install mcp-browser-dev-tools
|
|
42
|
-
npx
|
|
47
|
+
npx mbdt serve
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Try It Locally
|
|
51
|
+
|
|
52
|
+
You can smoke-test the broker before wiring it into an MCP client.
|
|
53
|
+
|
|
54
|
+
For Windows, WSL, macOS, and Linux-specific setup paths, see [docs/setup.md](docs/setup.md).
|
|
55
|
+
|
|
56
|
+
Quick Chromium flow:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Launch a local browser with remote debugging enabled.
|
|
60
|
+
# open waits for the debug endpoint and prints the doctor summary automatically.
|
|
61
|
+
npx -y mcp-browser-dev-tools@beta open about:blank --family chromium
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If you want to verify a specific app URL too:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npx -y mcp-browser-dev-tools@beta doctor --url http://127.0.0.1:3000
|
|
43
68
|
```
|
|
44
69
|
|
|
70
|
+
Then start the MCP server in a dedicated terminal:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npx -y mcp-browser-dev-tools@beta serve
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`serve` stays attached to stdio because MCP clients talk to it over standard input and output. Run it in its own terminal or let your MCP client spawn it directly.
|
|
77
|
+
|
|
78
|
+
If you want one broker to handle Chromium and Firefox at the same time, run it in `auto` mode and point each adapter at its own endpoint:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
MCP_BROWSER_FAMILY=auto \
|
|
82
|
+
CDP_BASE_URL=http://127.0.0.1:9223 \
|
|
83
|
+
FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 \
|
|
84
|
+
npx -y mcp-browser-dev-tools@beta serve
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Use distinct ports in `auto` mode. A practical local split is Firefox on `9222` and Chrome or Edge on `9223`.
|
|
88
|
+
|
|
89
|
+
If you already have a browser listening on `http://127.0.0.1:9222`, `doctor` is enough to confirm that the broker can reach it:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
CDP_BASE_URL=http://127.0.0.1:9222 npx -y mcp-browser-dev-tools@beta doctor
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
If you need to bridge Windows Chrome into WSL without changing WSL networking mode, run the relay on Windows and point WSL at the relay port instead. The full procedure is in [docs/setup.md](docs/setup.md).
|
|
96
|
+
|
|
45
97
|
## MCP Client Configuration
|
|
46
98
|
|
|
47
|
-
|
|
99
|
+
Use the same server command across MCP clients:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
npx -y mcp-browser-dev-tools@beta serve
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
If you installed the package already, the shorter equivalent is:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
mbdt serve
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Codex
|
|
112
|
+
|
|
113
|
+
Add the server with the Codex CLI:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
codex mcp add browser-devtools \
|
|
117
|
+
--env MCP_BROWSER_FAMILY=chromium \
|
|
118
|
+
--env CDP_BASE_URL=http://127.0.0.1:9222 \
|
|
119
|
+
-- npx -y mcp-browser-dev-tools@beta serve
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Equivalent `~/.codex/config.toml` entry:
|
|
123
|
+
|
|
124
|
+
```toml
|
|
125
|
+
[mcp_servers.browser-devtools]
|
|
126
|
+
command = "npx"
|
|
127
|
+
args = ["-y", "mcp-browser-dev-tools@beta", "serve"]
|
|
128
|
+
|
|
129
|
+
[mcp_servers.browser-devtools.env]
|
|
130
|
+
MCP_BROWSER_FAMILY = "chromium"
|
|
131
|
+
CDP_BASE_URL = "http://127.0.0.1:9222"
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
If Codex runs in WSL but the browser runs on Windows, run the broker on Windows too so it can connect to Windows loopback directly. This avoids relay and WSL networking issues.
|
|
135
|
+
|
|
136
|
+
Example `~/.codex/config.toml` entry for Firefox on Windows:
|
|
137
|
+
|
|
138
|
+
```toml
|
|
139
|
+
[mcp_servers.browser-devtools]
|
|
140
|
+
startup_timeout_sec = 30
|
|
141
|
+
command = "/mnt/c/nvm4w/nodejs/node.exe"
|
|
142
|
+
args = ["-e", "process.env.MCP_BROWSER_FAMILY='firefox';process.env.FIREFOX_BIDI_WS_URL='ws://127.0.0.1:9222';import('//wsl.localhost/<wsl-distro>/<repo-wsl-path>/src/cli.mjs').then(({ runCli }) => runCli(['serve'])).catch((error) => { console.error(error?.stack || String(error)); process.exit(1); });"]
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Example `auto` mode entry for Firefox plus Chrome or Edge on Windows:
|
|
146
|
+
|
|
147
|
+
```toml
|
|
148
|
+
[mcp_servers.browser-devtools]
|
|
149
|
+
startup_timeout_sec = 30
|
|
150
|
+
command = "/mnt/c/nvm4w/nodejs/node.exe"
|
|
151
|
+
args = ["-e", "process.env.MCP_BROWSER_FAMILY='auto';process.env.CDP_BASE_URL='http://127.0.0.1:9223';process.env.FIREFOX_BIDI_WS_URL='ws://127.0.0.1:9222';import('//wsl.localhost/<wsl-distro>/<repo-wsl-path>/src/cli.mjs').then(({ runCli }) => runCli(['serve'])).catch((error) => { console.error(error?.stack || String(error)); process.exit(1); });"]
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Replace `<wsl-distro>` and `<repo-wsl-path>` with your own WSL distro name and repository path.
|
|
155
|
+
|
|
156
|
+
### Claude Code
|
|
157
|
+
|
|
158
|
+
Add the server with Claude Code:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
claude mcp add browser-devtools --scope user \
|
|
162
|
+
--env MCP_BROWSER_FAMILY=chromium \
|
|
163
|
+
--env CDP_BASE_URL=http://127.0.0.1:9222 \
|
|
164
|
+
-- npx -y mcp-browser-dev-tools@beta serve
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
On native Windows, wrap `npx` with `cmd /c`:
|
|
168
|
+
|
|
169
|
+
```powershell
|
|
170
|
+
claude mcp add browser-devtools --scope user --env MCP_BROWSER_FAMILY=chromium --env CDP_BASE_URL=http://127.0.0.1:9222 -- cmd /c npx -y mcp-browser-dev-tools@beta serve
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Equivalent `.mcp.json` shape:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{
|
|
177
|
+
"mcpServers": {
|
|
178
|
+
"browser-devtools": {
|
|
179
|
+
"command": "npx",
|
|
180
|
+
"args": ["-y", "mcp-browser-dev-tools@beta", "serve"],
|
|
181
|
+
"env": {
|
|
182
|
+
"MCP_BROWSER_FAMILY": "chromium",
|
|
183
|
+
"CDP_BASE_URL": "http://127.0.0.1:9222"
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Cursor
|
|
191
|
+
|
|
192
|
+
Cursor reads MCP servers from `mcp.json`:
|
|
48
193
|
|
|
49
194
|
```json
|
|
50
195
|
{
|
|
@@ -61,29 +206,80 @@ Generic stdio client configuration:
|
|
|
61
206
|
}
|
|
62
207
|
```
|
|
63
208
|
|
|
64
|
-
|
|
209
|
+
### Common Variants
|
|
210
|
+
|
|
211
|
+
Auto mode with both browsers attached to one MCP server:
|
|
212
|
+
|
|
213
|
+
```json
|
|
214
|
+
{
|
|
215
|
+
"MCP_BROWSER_FAMILY": "auto",
|
|
216
|
+
"CDP_BASE_URL": "http://127.0.0.1:9223",
|
|
217
|
+
"FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Firefox:
|
|
222
|
+
|
|
223
|
+
```json
|
|
224
|
+
{
|
|
225
|
+
"MCP_BROWSER_FAMILY": "firefox",
|
|
226
|
+
"FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Microsoft Edge:
|
|
231
|
+
|
|
232
|
+
```json
|
|
233
|
+
{
|
|
234
|
+
"MCP_BROWSER_FAMILY": "edge",
|
|
235
|
+
"CDP_BASE_URL": "http://127.0.0.1:9222"
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Windows browser bridged into WSL through the relay:
|
|
240
|
+
|
|
241
|
+
```json
|
|
242
|
+
{
|
|
243
|
+
"MCP_BROWSER_FAMILY": "chromium",
|
|
244
|
+
"MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS": "1",
|
|
245
|
+
"CDP_BASE_URL": "http://<windows-host-ip>:9223"
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Enable `evaluate_js`:
|
|
250
|
+
|
|
251
|
+
```json
|
|
252
|
+
{
|
|
253
|
+
"MCP_BROWSER_ENABLE_EVAL": "1"
|
|
254
|
+
}
|
|
255
|
+
```
|
|
65
256
|
|
|
66
257
|
## Commands
|
|
67
258
|
|
|
68
259
|
- `serve` runs the MCP broker over stdio
|
|
69
260
|
- `doctor [--url URL]` checks browser reachability, local display state, and optional page access
|
|
70
261
|
- `open <url>` launches a local browser with remote debugging enabled
|
|
262
|
+
- `relay` forwards TCP traffic, useful for Windows-to-WSL DevTools bridging
|
|
71
263
|
- `--help` and `help` print usage
|
|
72
264
|
- `--version` and `version` print the package version
|
|
73
265
|
|
|
74
266
|
Examples:
|
|
75
267
|
|
|
76
268
|
```bash
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
269
|
+
mbdt doctor
|
|
270
|
+
mbdt doctor --url http://127.0.0.1:3000
|
|
271
|
+
mbdt open http://127.0.0.1:3000 --family chromium
|
|
272
|
+
mbdt open http://127.0.0.1:3000 --family edge --port 9223 --user-data-dir /tmp/mbdt-edge
|
|
273
|
+
mbdt open about:blank --family firefox --user-data-dir /tmp/mbdt-firefox
|
|
274
|
+
mbdt relay --wsl
|
|
80
275
|
```
|
|
81
276
|
|
|
82
277
|
## Configuration
|
|
83
278
|
|
|
84
|
-
- `MCP_BROWSER_FAMILY` defaults to `chromium`; set `firefox` for Firefox BiDi
|
|
279
|
+
- `MCP_BROWSER_FAMILY` defaults to `chromium`; set `edge` for Microsoft Edge, `firefox` for Firefox BiDi, or `auto` to multiplex both adapters
|
|
85
280
|
- `CDP_BASE_URL` defaults to `http://127.0.0.1:9222`
|
|
86
|
-
- `FIREFOX_BIDI_WS_URL` defaults to `ws://127.0.0.1:9222`
|
|
281
|
+
- `FIREFOX_BIDI_WS_URL` defaults to `ws://127.0.0.1:9222`; when pointed at the root Firefox remote debugging port, the broker connects to the `/session` websocket and creates a BiDi session there
|
|
282
|
+
- in `auto` mode, assign CDP and Firefox different ports so both browsers can run at once
|
|
87
283
|
- `MCP_BROWSER_EVENT_BUFFER_SIZE` sets the per-session buffered event limit
|
|
88
284
|
- `MCP_BROWSER_ENABLE_EVAL=1` enables `evaluate_js`
|
|
89
285
|
- `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1` allows non-loopback CDP or BiDi endpoints
|
|
@@ -92,13 +288,28 @@ mcp-browser-dev-tools open http://127.0.0.1:3000 --family chromium
|
|
|
92
288
|
|
|
93
289
|
The `open` command also requires `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1` before it will bind Chromium remote debugging to a non-loopback address.
|
|
94
290
|
|
|
291
|
+
The `relay` command defaults to `127.0.0.1:9223 -> 127.0.0.1:9222`. Non-loopback relay binds require either `--wsl` on Windows or `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1`.
|
|
292
|
+
|
|
95
293
|
## Exposed Tools
|
|
96
294
|
|
|
97
|
-
- `browser_status`
|
|
295
|
+
- `browser_status` returns broker metadata too: `serverName` and `serverVersion`
|
|
296
|
+
In `auto` mode it also includes per-browser adapter status under `browsers`
|
|
98
297
|
- `list_tabs`
|
|
298
|
+
In `auto` mode each `targetId` is namespaced as `chromium:<id>` or `firefox:<id>`
|
|
99
299
|
- `list_sessions`
|
|
300
|
+
In `auto` mode each `sessionId` is namespaced the same way
|
|
100
301
|
- `attach_tab`
|
|
101
302
|
- `detach_tab`
|
|
303
|
+
- `get_page_state`
|
|
304
|
+
- `navigate`
|
|
305
|
+
- `reload`
|
|
306
|
+
- `click`
|
|
307
|
+
- `hover`
|
|
308
|
+
- `type`
|
|
309
|
+
- `select`
|
|
310
|
+
- `press_key`
|
|
311
|
+
- `scroll`
|
|
312
|
+
- `set_viewport`
|
|
102
313
|
- `get_console_messages`
|
|
103
314
|
- `get_network_requests`
|
|
104
315
|
- `get_document`
|
|
@@ -110,11 +321,46 @@ The `open` command also requires `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1` before i
|
|
|
110
321
|
|
|
111
322
|
For tools that take `sessionId`, call `attach_tab` first and reuse the returned session.
|
|
112
323
|
|
|
324
|
+
### Locator Syntax
|
|
325
|
+
|
|
326
|
+
Interaction and inspection tools accept these locator forms:
|
|
327
|
+
|
|
328
|
+
- CSS selectors such as `#app button.primary` or `css=.modal button`
|
|
329
|
+
- visible-text lookup such as `text=Open settings`
|
|
330
|
+
- role plus accessible name such as `role=button[name="Open settings"]`
|
|
331
|
+
- accessible-name lookup such as `name=Open settings`
|
|
332
|
+
|
|
333
|
+
`inspect_element` returns layout and accessibility-focused metadata including bounding box, visibility flags, interactivity flags, accessible name, inferred role, and a subset of computed styles. Invalid CSS selectors now return an explicit locator error instead of a generic DOM failure.
|
|
334
|
+
|
|
335
|
+
### Screenshot Output
|
|
336
|
+
|
|
337
|
+
`take_screenshot` returns base64 image data plus metadata such as `mimeType`, `byteLength`, and `scope`. Pass `selector` to capture a single element instead of the full page.
|
|
338
|
+
|
|
113
339
|
## Browser Notes
|
|
114
340
|
|
|
115
341
|
- Chromium uses the standard DevTools endpoints at `/json/version` and `/json/list`
|
|
116
342
|
- Firefox support expects a direct BiDi websocket endpoint
|
|
117
343
|
- In WSL, Linux browser executables are preferred before Windows fallback paths
|
|
344
|
+
- For Windows Chrome + WSL, prefer the `relay` command over changing Chrome's remote debugging bind
|
|
345
|
+
|
|
346
|
+
## Why Not Playwright?
|
|
347
|
+
|
|
348
|
+
Playwright is still the stronger choice for deterministic browser automation and end-to-end tests. It has a more mature locator model, assertions, waiting semantics, tracing, and CI story.
|
|
349
|
+
|
|
350
|
+
`mcp-browser-dev-tools` solves a different problem:
|
|
351
|
+
|
|
352
|
+
- it is MCP-native, so AI clients call a bounded tool surface instead of generating and executing Playwright scripts
|
|
353
|
+
- it attaches to an already-open browser tab and inspects the current session state, cookies, login state, extensions, console, and network history
|
|
354
|
+
- it is designed for local AI debugging workflows, including loopback-only defaults, opt-in evaluation, and the Windows-to-WSL relay path
|
|
355
|
+
- it presents one MCP interface across Chromium CDP and Firefox BiDi instead of requiring the AI client to know browser protocol details
|
|
356
|
+
|
|
357
|
+
Use Playwright when you want reproducible automation. Use this project when you want an AI assistant to inspect and manipulate a live browser session through MCP.
|
|
358
|
+
|
|
359
|
+
## Debugging Notes
|
|
360
|
+
|
|
361
|
+
- `attach_tab` now seeds network history from the Performance API so already-loaded pages still show useful requests
|
|
362
|
+
- console buffers include source URL, line and column where the browser reports them, plus stack frames when available
|
|
363
|
+
- `get_page_state` reports the current URL, title, viewport, and scroll position without enabling `evaluate_js`
|
|
118
364
|
|
|
119
365
|
## Safety Defaults
|
|
120
366
|
|
|
@@ -137,5 +383,6 @@ The repository uses `pnpm` for local development and CI. End-user installation a
|
|
|
137
383
|
Additional docs:
|
|
138
384
|
|
|
139
385
|
- [Architecture](docs/architecture.md)
|
|
386
|
+
- [Setup Guide](docs/setup.md)
|
|
140
387
|
- [Repository Settings](docs/repository-settings.md)
|
|
141
388
|
- [Publishing](PUBLISHING.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-browser-dev-tools",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.11",
|
|
4
4
|
"description": "Local MCP server for Chromium DevTools Protocol and Firefox WebDriver BiDi",
|
|
5
5
|
"author": "Hinaser <rindo.hinase@gmail.com>",
|
|
6
6
|
"type": "module",
|
|
@@ -31,12 +31,15 @@
|
|
|
31
31
|
"src/index.mjs",
|
|
32
32
|
"src/json-rpc-stdio.mjs",
|
|
33
33
|
"src/mcp-server.mjs",
|
|
34
|
+
"src/page-context.mjs",
|
|
34
35
|
"src/package-info.mjs",
|
|
35
36
|
"src/session-events.mjs",
|
|
36
37
|
"src/server.mjs",
|
|
38
|
+
"src/tcp-relay.mjs",
|
|
37
39
|
"README.md"
|
|
38
40
|
],
|
|
39
41
|
"bin": {
|
|
42
|
+
"mbdt": "cli.mjs",
|
|
40
43
|
"mcp-browser-dev-tools": "cli.mjs"
|
|
41
44
|
},
|
|
42
45
|
"keywords": [
|
package/src/browser-adapter.mjs
CHANGED
|
@@ -1,10 +1,285 @@
|
|
|
1
1
|
import { CdpSessionManager } from "./cdp-client.mjs";
|
|
2
2
|
import { FirefoxBidiSessionManager } from "./firefox-bidi-client.mjs";
|
|
3
3
|
|
|
4
|
+
const SUPPORTED_FAMILIES = ["chromium", "firefox"];
|
|
5
|
+
|
|
6
|
+
function prefixId(browserFamily, value) {
|
|
7
|
+
return `${browserFamily}:${value}`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function resolvePrefixedId(value, kind) {
|
|
11
|
+
if (typeof value !== "string") {
|
|
12
|
+
throw new Error(`${kind} must be a string`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
for (const browserFamily of SUPPORTED_FAMILIES) {
|
|
16
|
+
const prefix = `${browserFamily}:`;
|
|
17
|
+
if (value.startsWith(prefix)) {
|
|
18
|
+
return {
|
|
19
|
+
browserFamily,
|
|
20
|
+
localId: value.slice(prefix.length),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
throw new Error(
|
|
26
|
+
`${kind} must include a browser prefix (${SUPPORTED_FAMILIES.join(", ")})`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function mapPrefixedFields(value, browserFamily) {
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
return value.map((entry) => mapPrefixedFields(entry, browserFamily));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!value || typeof value !== "object") {
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Object.fromEntries(
|
|
40
|
+
Object.entries(value).map(([key, entry]) => {
|
|
41
|
+
if (key === "sessionId" && typeof entry === "string") {
|
|
42
|
+
return [key, prefixId(browserFamily, entry)];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (key === "targetId" && typeof entry === "string") {
|
|
46
|
+
return [key, prefixId(browserFamily, entry)];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return [key, mapPrefixedFields(entry, browserFamily)];
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function mergeStatusError(statuses) {
|
|
55
|
+
const failures = Object.entries(statuses)
|
|
56
|
+
.filter(([, status]) => !status.available)
|
|
57
|
+
.map(([browserFamily, status]) => `${browserFamily}: ${status.error}`);
|
|
58
|
+
|
|
59
|
+
return failures.length > 0 ? failures.join("; ") : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class MultiBrowserAdapter {
|
|
63
|
+
constructor(adapters) {
|
|
64
|
+
this.adapters = adapters;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getAdapter(browserFamily) {
|
|
68
|
+
const adapter = this.adapters[browserFamily];
|
|
69
|
+
if (!adapter) {
|
|
70
|
+
throw new Error(`Unsupported browser family: ${browserFamily}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return adapter;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async getBrowserStatus() {
|
|
77
|
+
const entries = await Promise.all(
|
|
78
|
+
Object.entries(this.adapters).map(async ([browserFamily, adapter]) => [
|
|
79
|
+
browserFamily,
|
|
80
|
+
await adapter.getBrowserStatus(),
|
|
81
|
+
]),
|
|
82
|
+
);
|
|
83
|
+
const browsers = Object.fromEntries(entries);
|
|
84
|
+
const availableBrowsers = Object.entries(browsers)
|
|
85
|
+
.filter(([, status]) => status.available)
|
|
86
|
+
.map(([browserFamily]) => browserFamily);
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
available: availableBrowsers.length > 0,
|
|
90
|
+
browserFamily: "auto",
|
|
91
|
+
sessionCount: Object.values(browsers).reduce(
|
|
92
|
+
(total, status) => total + (status.sessionCount ?? 0),
|
|
93
|
+
0,
|
|
94
|
+
),
|
|
95
|
+
availableBrowsers,
|
|
96
|
+
browsers,
|
|
97
|
+
error:
|
|
98
|
+
availableBrowsers.length > 0
|
|
99
|
+
? null
|
|
100
|
+
: (mergeStatusError(browsers) ?? "No browser endpoints available"),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async listTargets() {
|
|
105
|
+
const results = await Promise.all(
|
|
106
|
+
Object.entries(this.adapters).map(async ([browserFamily, adapter]) => {
|
|
107
|
+
try {
|
|
108
|
+
return (await adapter.listTargets()).map((target) => ({
|
|
109
|
+
...target,
|
|
110
|
+
browserFamily,
|
|
111
|
+
targetId: prefixId(browserFamily, target.targetId),
|
|
112
|
+
}));
|
|
113
|
+
} catch {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
return results.flat();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
listSessions() {
|
|
123
|
+
return Object.entries(this.adapters).flatMap(([browserFamily, adapter]) =>
|
|
124
|
+
adapter.listSessions().map((session) => ({
|
|
125
|
+
...session,
|
|
126
|
+
browserFamily,
|
|
127
|
+
sessionId: prefixId(browserFamily, session.sessionId),
|
|
128
|
+
targetId:
|
|
129
|
+
typeof session.targetId === "string"
|
|
130
|
+
? prefixId(browserFamily, session.targetId)
|
|
131
|
+
: session.targetId,
|
|
132
|
+
})),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async attachToTarget(targetId) {
|
|
137
|
+
const { browserFamily, localId } = resolvePrefixedId(targetId, "targetId");
|
|
138
|
+
const session =
|
|
139
|
+
await this.getAdapter(browserFamily).attachToTarget(localId);
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
...session,
|
|
143
|
+
browserFamily,
|
|
144
|
+
sessionId: prefixId(browserFamily, session.sessionId),
|
|
145
|
+
targetId:
|
|
146
|
+
typeof session.targetId === "string"
|
|
147
|
+
? prefixId(browserFamily, session.targetId)
|
|
148
|
+
: targetId,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async detachSession(sessionId) {
|
|
153
|
+
const { browserFamily, localId } = resolvePrefixedId(
|
|
154
|
+
sessionId,
|
|
155
|
+
"sessionId",
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
return mapPrefixedFields(
|
|
159
|
+
await this.getAdapter(browserFamily).detachSession(localId),
|
|
160
|
+
browserFamily,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async delegateSession(sessionId, methodName, args = []) {
|
|
165
|
+
const { browserFamily, localId } = resolvePrefixedId(
|
|
166
|
+
sessionId,
|
|
167
|
+
"sessionId",
|
|
168
|
+
);
|
|
169
|
+
const adapter = this.getAdapter(browserFamily);
|
|
170
|
+
const result = await adapter[methodName](localId, ...args);
|
|
171
|
+
|
|
172
|
+
if (result === undefined) {
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (methodName === "getConsoleMessages" || methodName === "getEvents") {
|
|
177
|
+
return mapPrefixedFields(result, browserFamily);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (methodName === "getNetworkRequests") {
|
|
181
|
+
return mapPrefixedFields(result, browserFamily);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return mapPrefixedFields(result, browserFamily);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
getPageState(sessionId) {
|
|
188
|
+
return this.delegateSession(sessionId, "getPageState");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
navigate(sessionId, url, options) {
|
|
192
|
+
return this.delegateSession(sessionId, "navigate", [url, options]);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
reload(sessionId, options) {
|
|
196
|
+
return this.delegateSession(sessionId, "reload", [options]);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
click(sessionId, selector) {
|
|
200
|
+
return this.delegateSession(sessionId, "click", [selector]);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
hover(sessionId, selector) {
|
|
204
|
+
return this.delegateSession(sessionId, "hover", [selector]);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
type(sessionId, selector, text, options) {
|
|
208
|
+
return this.delegateSession(sessionId, "type", [selector, text, options]);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
select(sessionId, selector, options) {
|
|
212
|
+
return this.delegateSession(sessionId, "select", [selector, options]);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
pressKey(sessionId, key, selector) {
|
|
216
|
+
return this.delegateSession(sessionId, "pressKey", [key, selector]);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
scroll(sessionId, options) {
|
|
220
|
+
return this.delegateSession(sessionId, "scroll", [options]);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
setViewport(sessionId, options) {
|
|
224
|
+
return this.delegateSession(sessionId, "setViewport", [options]);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
evaluate(sessionId, expression) {
|
|
228
|
+
return this.delegateSession(sessionId, "evaluate", [expression]);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
getDocument(sessionId, depth) {
|
|
232
|
+
return this.delegateSession(sessionId, "getDocument", [depth]);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
getConsoleMessages(sessionId, limit) {
|
|
236
|
+
return this.delegateSession(sessionId, "getConsoleMessages", [limit]);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
getNetworkRequests(sessionId, limit) {
|
|
240
|
+
return this.delegateSession(sessionId, "getNetworkRequests", [limit]);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
inspectElement(sessionId, selector) {
|
|
244
|
+
return this.delegateSession(sessionId, "inspectElement", [selector]);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
takeScreenshot(sessionId, format, options) {
|
|
248
|
+
return this.delegateSession(sessionId, "takeScreenshot", [format, options]);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
getEvents(sessionId, limit) {
|
|
252
|
+
return this.delegateSession(sessionId, "getEvents", [limit]);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async closeAll() {
|
|
256
|
+
await Promise.all(
|
|
257
|
+
Object.values(this.adapters).map((adapter) => adapter.closeAll()),
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
4
262
|
export function createBrowserAdapter(config) {
|
|
5
263
|
if (config.browserFamily === "firefox") {
|
|
6
264
|
return new FirefoxBidiSessionManager(config);
|
|
7
265
|
}
|
|
8
266
|
|
|
267
|
+
if (config.browserFamily === "auto") {
|
|
268
|
+
return new MultiBrowserAdapter({
|
|
269
|
+
chromium: new CdpSessionManager({
|
|
270
|
+
...config,
|
|
271
|
+
browserFamily: "chromium",
|
|
272
|
+
}),
|
|
273
|
+
firefox: new FirefoxBidiSessionManager({
|
|
274
|
+
...config,
|
|
275
|
+
browserFamily: "firefox",
|
|
276
|
+
}),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (config.browserFamily === "edge") {
|
|
281
|
+
return new CdpSessionManager(config);
|
|
282
|
+
}
|
|
283
|
+
|
|
9
284
|
return new CdpSessionManager(config);
|
|
10
285
|
}
|