mcp-browser-dev-tools 0.0.1-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 +141 -0
- package/cli.mjs +9 -0
- package/package.json +87 -0
- package/src/app.mjs +56 -0
- package/src/browser-adapter.mjs +10 -0
- package/src/browser-launcher.mjs +170 -0
- package/src/cdp-client.mjs +599 -0
- package/src/cli.mjs +199 -0
- package/src/config.mjs +86 -0
- package/src/doctor.mjs +88 -0
- package/src/firefox-bidi-client.mjs +645 -0
- package/src/index.mjs +31 -0
- package/src/json-rpc-stdio.mjs +56 -0
- package/src/mcp-server.mjs +506 -0
- package/src/package-info.mjs +5 -0
- package/src/server.mjs +5 -0
- package/src/session-events.mjs +76 -0
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
import {
|
|
2
|
+
filterConsoleMessages,
|
|
3
|
+
summarizeNetworkRequests,
|
|
4
|
+
} from "./session-events.mjs";
|
|
5
|
+
|
|
6
|
+
function toErrorMessage(error) {
|
|
7
|
+
return error instanceof Error ? error.message : String(error);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function inferTitle(url) {
|
|
11
|
+
if (!url) {
|
|
12
|
+
return "Firefox Context";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
const parsed = new URL(url);
|
|
17
|
+
return parsed.hostname || url;
|
|
18
|
+
} catch {
|
|
19
|
+
return url;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function readWebSocketData(data) {
|
|
24
|
+
if (typeof data === "string") {
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (data instanceof ArrayBuffer) {
|
|
29
|
+
return Buffer.from(data).toString("utf8");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (ArrayBuffer.isView(data)) {
|
|
33
|
+
return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString(
|
|
34
|
+
"utf8",
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (typeof data?.text === "function") {
|
|
39
|
+
return data.text();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return String(data);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function normalizeFirefoxContext(context) {
|
|
46
|
+
return {
|
|
47
|
+
targetId: context.context,
|
|
48
|
+
type: "page",
|
|
49
|
+
title: inferTitle(context.url),
|
|
50
|
+
url: context.url,
|
|
51
|
+
attached: false,
|
|
52
|
+
userContext: context.userContext ?? null,
|
|
53
|
+
clientWindow: context.clientWindow ?? null,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function resolveBidiEventContext(method, params = {}) {
|
|
58
|
+
return (
|
|
59
|
+
params.context ??
|
|
60
|
+
params.source?.context ??
|
|
61
|
+
params.navigation?.context ??
|
|
62
|
+
null
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function normalizeMapEntries(entries) {
|
|
67
|
+
return Object.fromEntries(
|
|
68
|
+
entries.map(([key, value]) => [
|
|
69
|
+
String(summarizeBidiRemoteValue(key)),
|
|
70
|
+
summarizeBidiRemoteValue(value),
|
|
71
|
+
]),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function summarizeBidiRemoteValue(value) {
|
|
76
|
+
if (!value || typeof value !== "object") {
|
|
77
|
+
return value ?? null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
switch (value.type) {
|
|
81
|
+
case "undefined":
|
|
82
|
+
return undefined;
|
|
83
|
+
case "null":
|
|
84
|
+
return null;
|
|
85
|
+
case "string":
|
|
86
|
+
case "number":
|
|
87
|
+
case "boolean":
|
|
88
|
+
case "bigint":
|
|
89
|
+
return value.value;
|
|
90
|
+
case "array":
|
|
91
|
+
case "set":
|
|
92
|
+
return Array.isArray(value.value)
|
|
93
|
+
? value.value.map((entry) => summarizeBidiRemoteValue(entry))
|
|
94
|
+
: [];
|
|
95
|
+
case "object":
|
|
96
|
+
case "map":
|
|
97
|
+
return Array.isArray(value.value) ? normalizeMapEntries(value.value) : {};
|
|
98
|
+
default:
|
|
99
|
+
if (value.value !== undefined) {
|
|
100
|
+
return value.value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
type: value.type,
|
|
105
|
+
handle: value.handle ?? null,
|
|
106
|
+
internalId: value.internalId ?? null,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function normalizeFirefoxEvent(method, params = {}) {
|
|
112
|
+
if (method === "log.entryAdded") {
|
|
113
|
+
return {
|
|
114
|
+
kind: "console",
|
|
115
|
+
level: params.level,
|
|
116
|
+
text: params.text,
|
|
117
|
+
timestamp: params.timestamp,
|
|
118
|
+
values: (params.args || []).map((arg) => summarizeBidiRemoteValue(arg)),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (method === "network.beforeRequestSent") {
|
|
123
|
+
return {
|
|
124
|
+
kind: "network",
|
|
125
|
+
phase: "request",
|
|
126
|
+
completed: false,
|
|
127
|
+
timestamp: params.timestamp,
|
|
128
|
+
requestId: params.request?.request,
|
|
129
|
+
method: params.request?.method,
|
|
130
|
+
url: params.request?.url,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (method === "network.responseCompleted") {
|
|
135
|
+
return {
|
|
136
|
+
kind: "network",
|
|
137
|
+
phase: "response",
|
|
138
|
+
completed: true,
|
|
139
|
+
failed: false,
|
|
140
|
+
timestamp: params.timestamp,
|
|
141
|
+
requestId: params.request?.request,
|
|
142
|
+
status: params.response?.status,
|
|
143
|
+
statusText: params.response?.statusText,
|
|
144
|
+
mimeType: params.response?.mimeType,
|
|
145
|
+
url: params.request?.url,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (method === "browsingContext.load") {
|
|
150
|
+
return {
|
|
151
|
+
kind: "page",
|
|
152
|
+
timestamp: params.timestamp ?? null,
|
|
153
|
+
url: params.url ?? null,
|
|
154
|
+
navigation: params.navigation ?? null,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (method === "browsingContext.contextDestroyed") {
|
|
159
|
+
return {
|
|
160
|
+
kind: "lifecycle",
|
|
161
|
+
timestamp: new Date().toISOString(),
|
|
162
|
+
context: params.context,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
kind: "raw",
|
|
168
|
+
params,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function screenshotFormat(format) {
|
|
173
|
+
if (format === "png") {
|
|
174
|
+
return { type: "image/png" };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (format === "jpeg") {
|
|
178
|
+
return { type: "image/jpeg" };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
throw new Error(`Firefox BiDi screenshot format is not supported: ${format}`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function normalizeRequestedDepth(depth) {
|
|
185
|
+
return Number.isInteger(depth) && depth > 0 ? depth : 2;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
class FirefoxBidiTabSession {
|
|
189
|
+
constructor(target, options = {}) {
|
|
190
|
+
this.id = crypto.randomUUID();
|
|
191
|
+
this.target = target;
|
|
192
|
+
this.eventBufferSize = options.eventBufferSize ?? 200;
|
|
193
|
+
this.connectedAt = new Date().toISOString();
|
|
194
|
+
this.bufferedEvents = [];
|
|
195
|
+
this.subscription = null;
|
|
196
|
+
this.closed = false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
bufferEvent(method, params) {
|
|
200
|
+
this.bufferedEvents.push({
|
|
201
|
+
method,
|
|
202
|
+
capturedAt: new Date().toISOString(),
|
|
203
|
+
...normalizeFirefoxEvent(method, params),
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
if (this.bufferedEvents.length > this.eventBufferSize) {
|
|
207
|
+
this.bufferedEvents.shift();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
getEvents(limit = 50) {
|
|
212
|
+
const safeLimit = Math.max(1, limit);
|
|
213
|
+
return this.bufferedEvents.slice(-safeLimit);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
getConsoleMessages(limit = 50) {
|
|
217
|
+
return filterConsoleMessages(this.bufferedEvents, limit);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
getNetworkRequests(limit = 50) {
|
|
221
|
+
return summarizeNetworkRequests(this.bufferedEvents, limit);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
getSummary() {
|
|
225
|
+
return {
|
|
226
|
+
sessionId: this.id,
|
|
227
|
+
targetId: this.target.targetId,
|
|
228
|
+
title: this.target.title,
|
|
229
|
+
url: this.target.url,
|
|
230
|
+
browserFamily: "firefox",
|
|
231
|
+
connectedAt: this.connectedAt,
|
|
232
|
+
bufferedEvents: this.bufferedEvents.length,
|
|
233
|
+
subscription: this.subscription,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export class FirefoxBidiSessionManager {
|
|
239
|
+
constructor(config, options = {}) {
|
|
240
|
+
this.config = config;
|
|
241
|
+
this.sessions = new Map();
|
|
242
|
+
this.websocketFactory =
|
|
243
|
+
options.websocketFactory ?? ((url) => new WebSocket(url));
|
|
244
|
+
this.pending = new Map();
|
|
245
|
+
this.nextMessageId = 1;
|
|
246
|
+
this.websocket = null;
|
|
247
|
+
this.browserSessionId = null;
|
|
248
|
+
this.capabilities = null;
|
|
249
|
+
this.connectPromise = null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async ensureConnected() {
|
|
253
|
+
if (
|
|
254
|
+
this.browserSessionId &&
|
|
255
|
+
this.websocket?.readyState === WebSocket.OPEN
|
|
256
|
+
) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (this.connectPromise) {
|
|
261
|
+
return this.connectPromise;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
this.connectPromise = this.openConnection();
|
|
265
|
+
try {
|
|
266
|
+
await this.connectPromise;
|
|
267
|
+
} finally {
|
|
268
|
+
this.connectPromise = null;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async openConnection() {
|
|
273
|
+
this.websocket = this.websocketFactory(this.config.firefoxBidiWsUrl);
|
|
274
|
+
|
|
275
|
+
await new Promise((resolve, reject) => {
|
|
276
|
+
let settled = false;
|
|
277
|
+
|
|
278
|
+
this.websocket.addEventListener(
|
|
279
|
+
"open",
|
|
280
|
+
() => {
|
|
281
|
+
if (!settled) {
|
|
282
|
+
settled = true;
|
|
283
|
+
resolve();
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
{ once: true },
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
this.websocket.addEventListener(
|
|
290
|
+
"error",
|
|
291
|
+
() => {
|
|
292
|
+
if (!settled) {
|
|
293
|
+
settled = true;
|
|
294
|
+
reject(
|
|
295
|
+
new Error(`Failed to connect to ${this.config.firefoxBidiWsUrl}`),
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
{ once: true },
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
this.websocket.addEventListener("message", (event) => {
|
|
303
|
+
void this.handleMessage(event.data);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
this.websocket.addEventListener("close", () => {
|
|
307
|
+
this.rejectPending(new Error("Firefox BiDi connection closed"));
|
|
308
|
+
this.websocket = null;
|
|
309
|
+
this.browserSessionId = null;
|
|
310
|
+
this.capabilities = null;
|
|
311
|
+
this.sessions.clear();
|
|
312
|
+
if (!settled) {
|
|
313
|
+
settled = true;
|
|
314
|
+
reject(
|
|
315
|
+
new Error("Firefox BiDi connection closed before it connected"),
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
const result = await this.send("session.new", {
|
|
322
|
+
capabilities: {
|
|
323
|
+
alwaysMatch: {},
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
this.browserSessionId = result.sessionId;
|
|
328
|
+
this.capabilities = result.capabilities ?? {};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
rejectPending(error) {
|
|
332
|
+
for (const pending of this.pending.values()) {
|
|
333
|
+
pending.reject(error);
|
|
334
|
+
}
|
|
335
|
+
this.pending.clear();
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
send(method, params = {}) {
|
|
339
|
+
if (!this.websocket || this.websocket.readyState !== WebSocket.OPEN) {
|
|
340
|
+
throw new Error("Firefox BiDi connection is not established");
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const id = this.nextMessageId++;
|
|
344
|
+
const payload = JSON.stringify({ id, method, params });
|
|
345
|
+
|
|
346
|
+
return new Promise((resolve, reject) => {
|
|
347
|
+
this.pending.set(id, { resolve, reject });
|
|
348
|
+
try {
|
|
349
|
+
this.websocket.send(payload);
|
|
350
|
+
} catch (error) {
|
|
351
|
+
this.pending.delete(id);
|
|
352
|
+
reject(error);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
async handleMessage(data) {
|
|
358
|
+
const raw = await readWebSocketData(data);
|
|
359
|
+
const message = JSON.parse(raw);
|
|
360
|
+
|
|
361
|
+
if (message.id !== undefined) {
|
|
362
|
+
const pending = this.pending.get(message.id);
|
|
363
|
+
if (!pending) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
this.pending.delete(message.id);
|
|
368
|
+
if (message.type === "error") {
|
|
369
|
+
pending.reject(
|
|
370
|
+
new Error(message.message || message.error || "BiDi error"),
|
|
371
|
+
);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
pending.resolve(message.result);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (message.type !== "event" || !message.method) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const contextId = resolveBidiEventContext(message.method, message.params);
|
|
384
|
+
if (!contextId) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
for (const session of this.sessions.values()) {
|
|
389
|
+
if (session.target.targetId === contextId) {
|
|
390
|
+
session.bufferEvent(message.method, message.params ?? {});
|
|
391
|
+
if (message.method === "browsingContext.contextDestroyed") {
|
|
392
|
+
session.closed = true;
|
|
393
|
+
this.sessions.delete(session.id);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async getBrowserStatus() {
|
|
400
|
+
try {
|
|
401
|
+
await this.ensureConnected();
|
|
402
|
+
return {
|
|
403
|
+
available: true,
|
|
404
|
+
endpoint: this.config.firefoxBidiWsUrl,
|
|
405
|
+
sessionCount: this.sessions.size,
|
|
406
|
+
browser: this.capabilities?.browserName || "firefox",
|
|
407
|
+
browserVersion: this.capabilities?.browserVersion ?? null,
|
|
408
|
+
protocol: "webdriver-bidi",
|
|
409
|
+
webSocketUrl: this.config.firefoxBidiWsUrl,
|
|
410
|
+
};
|
|
411
|
+
} catch (error) {
|
|
412
|
+
return {
|
|
413
|
+
available: false,
|
|
414
|
+
endpoint: this.config.firefoxBidiWsUrl,
|
|
415
|
+
sessionCount: this.sessions.size,
|
|
416
|
+
protocol: "webdriver-bidi",
|
|
417
|
+
error: toErrorMessage(error),
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async listTargets() {
|
|
423
|
+
await this.ensureConnected();
|
|
424
|
+
const result = await this.send("browsingContext.getTree", {});
|
|
425
|
+
return (result.contexts || []).map((context) =>
|
|
426
|
+
normalizeFirefoxContext(context),
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
listSessions() {
|
|
431
|
+
return Array.from(this.sessions.values(), (session) =>
|
|
432
|
+
session.getSummary(),
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
getSession(sessionId) {
|
|
437
|
+
const session = this.sessions.get(sessionId);
|
|
438
|
+
if (!session) {
|
|
439
|
+
throw new Error(`No active session found for ${sessionId}`);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return session;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async attachToTarget(targetId) {
|
|
446
|
+
const targets = await this.listTargets();
|
|
447
|
+
const target = targets.find((candidate) => candidate.targetId === targetId);
|
|
448
|
+
if (!target) {
|
|
449
|
+
throw new Error(`No Firefox browsing context found for ${targetId}`);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const session = new FirefoxBidiTabSession(target, {
|
|
453
|
+
eventBufferSize: this.config.eventBufferSize,
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
const subscription = await this.send("session.subscribe", {
|
|
457
|
+
events: [
|
|
458
|
+
"log.entryAdded",
|
|
459
|
+
"network.beforeRequestSent",
|
|
460
|
+
"network.responseCompleted",
|
|
461
|
+
"browsingContext.load",
|
|
462
|
+
"browsingContext.contextDestroyed",
|
|
463
|
+
],
|
|
464
|
+
contexts: [targetId],
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
session.subscription = subscription.subscription ?? null;
|
|
468
|
+
this.sessions.set(session.id, session);
|
|
469
|
+
return session.getSummary();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async detachSession(sessionId) {
|
|
473
|
+
const session = this.getSession(sessionId);
|
|
474
|
+
|
|
475
|
+
if (session.subscription) {
|
|
476
|
+
await this.send("session.unsubscribe", {
|
|
477
|
+
subscriptions: [session.subscription],
|
|
478
|
+
}).catch(() => {});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
session.closed = true;
|
|
482
|
+
this.sessions.delete(sessionId);
|
|
483
|
+
return {
|
|
484
|
+
detached: true,
|
|
485
|
+
sessionId,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
async evaluate(sessionId, expression, options = {}) {
|
|
490
|
+
const session = this.getSession(sessionId);
|
|
491
|
+
const result = await this.send("script.evaluate", {
|
|
492
|
+
expression,
|
|
493
|
+
target: {
|
|
494
|
+
context: session.target.targetId,
|
|
495
|
+
},
|
|
496
|
+
awaitPromise: options.awaitPromise ?? true,
|
|
497
|
+
resultOwnership: "none",
|
|
498
|
+
serializationOptions: {
|
|
499
|
+
maxObjectDepth: 5,
|
|
500
|
+
maxDomDepth: 0,
|
|
501
|
+
},
|
|
502
|
+
userActivation: false,
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
return {
|
|
506
|
+
result:
|
|
507
|
+
result.type === "success"
|
|
508
|
+
? summarizeBidiRemoteValue(result.result)
|
|
509
|
+
: null,
|
|
510
|
+
exceptionDetails:
|
|
511
|
+
result.type === "exception" ? result.exceptionDetails : null,
|
|
512
|
+
realm: result.realm ?? null,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
async getDocument(sessionId, depth) {
|
|
517
|
+
const session = this.getSession(sessionId);
|
|
518
|
+
const requestedDepth = normalizeRequestedDepth(depth);
|
|
519
|
+
const result = await this.send("script.evaluate", {
|
|
520
|
+
expression: `JSON.stringify({
|
|
521
|
+
title: document.title,
|
|
522
|
+
url: location.href,
|
|
523
|
+
readyState: document.readyState,
|
|
524
|
+
requestedDepth: ${JSON.stringify(requestedDepth)},
|
|
525
|
+
outerHTML: document.documentElement ? document.documentElement.outerHTML : null
|
|
526
|
+
})`,
|
|
527
|
+
target: {
|
|
528
|
+
context: session.target.targetId,
|
|
529
|
+
},
|
|
530
|
+
awaitPromise: true,
|
|
531
|
+
resultOwnership: "none",
|
|
532
|
+
serializationOptions: {
|
|
533
|
+
maxObjectDepth: 1,
|
|
534
|
+
maxDomDepth: 0,
|
|
535
|
+
},
|
|
536
|
+
userActivation: false,
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
if (result.type !== "success") {
|
|
540
|
+
return {
|
|
541
|
+
exceptionDetails: result.exceptionDetails ?? null,
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const payload = summarizeBidiRemoteValue(result.result);
|
|
546
|
+
return {
|
|
547
|
+
browserFamily: "firefox",
|
|
548
|
+
format: "html",
|
|
549
|
+
...JSON.parse(payload),
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
async takeScreenshot(sessionId, format) {
|
|
554
|
+
const session = this.getSession(sessionId);
|
|
555
|
+
return this.send("browsingContext.captureScreenshot", {
|
|
556
|
+
context: session.target.targetId,
|
|
557
|
+
format: screenshotFormat(format),
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
getConsoleMessages(sessionId, limit) {
|
|
562
|
+
return this.getSession(sessionId).getConsoleMessages(limit);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
getNetworkRequests(sessionId, limit) {
|
|
566
|
+
return this.getSession(sessionId).getNetworkRequests(limit);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
async inspectElement(sessionId, selector) {
|
|
570
|
+
const session = this.getSession(sessionId);
|
|
571
|
+
const result = await this.send("script.evaluate", {
|
|
572
|
+
expression: `(() => {
|
|
573
|
+
const selector = ${JSON.stringify(selector)};
|
|
574
|
+
const element = document.querySelector(selector);
|
|
575
|
+
if (!element) {
|
|
576
|
+
return JSON.stringify({
|
|
577
|
+
browserFamily: "firefox",
|
|
578
|
+
selector,
|
|
579
|
+
found: false
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const attributes = Object.fromEntries(
|
|
584
|
+
Array.from(element.attributes, (attribute) => [attribute.name, attribute.value])
|
|
585
|
+
);
|
|
586
|
+
|
|
587
|
+
return JSON.stringify({
|
|
588
|
+
browserFamily: "firefox",
|
|
589
|
+
selector,
|
|
590
|
+
found: true,
|
|
591
|
+
node: {
|
|
592
|
+
tagName: element.tagName,
|
|
593
|
+
id: element.getAttribute("id"),
|
|
594
|
+
className: element.getAttribute("class"),
|
|
595
|
+
childElementCount: element.childElementCount,
|
|
596
|
+
attributes,
|
|
597
|
+
textContent: element.textContent,
|
|
598
|
+
innerText: typeof element.innerText === "string" ? element.innerText : null,
|
|
599
|
+
outerHTML: element.outerHTML
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
})()`,
|
|
603
|
+
target: {
|
|
604
|
+
context: session.target.targetId,
|
|
605
|
+
},
|
|
606
|
+
awaitPromise: true,
|
|
607
|
+
resultOwnership: "none",
|
|
608
|
+
serializationOptions: {
|
|
609
|
+
maxObjectDepth: 1,
|
|
610
|
+
maxDomDepth: 0,
|
|
611
|
+
},
|
|
612
|
+
userActivation: false,
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
if (result.type !== "success") {
|
|
616
|
+
return {
|
|
617
|
+
browserFamily: "firefox",
|
|
618
|
+
selector,
|
|
619
|
+
found: false,
|
|
620
|
+
exceptionDetails: result.exceptionDetails ?? null,
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const payload = summarizeBidiRemoteValue(result.result);
|
|
625
|
+
return JSON.parse(payload);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
getEvents(sessionId, limit) {
|
|
629
|
+
return this.getSession(sessionId).getEvents(limit);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
async closeAll() {
|
|
633
|
+
await Promise.allSettled(
|
|
634
|
+
Array.from(this.sessions.keys(), (sessionId) =>
|
|
635
|
+
this.detachSession(sessionId),
|
|
636
|
+
),
|
|
637
|
+
);
|
|
638
|
+
|
|
639
|
+
if (this.websocket && this.websocket.readyState < WebSocket.CLOSING) {
|
|
640
|
+
this.websocket.close();
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
this.sessions.clear();
|
|
644
|
+
}
|
|
645
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export { createBrowserDevToolsApp } from "./app.mjs";
|
|
2
|
+
export { createBrowserAdapter } from "./browser-adapter.mjs";
|
|
3
|
+
export {
|
|
4
|
+
buildBrowserLaunchArgs,
|
|
5
|
+
detectInstalledBrowsers,
|
|
6
|
+
findBrowserExecutable,
|
|
7
|
+
getBrowserCandidates,
|
|
8
|
+
} from "./browser-launcher.mjs";
|
|
9
|
+
export {
|
|
10
|
+
DEFAULT_BROWSER_FAMILY,
|
|
11
|
+
DEFAULT_CDP_BASE_URL,
|
|
12
|
+
DEFAULT_EVENT_BUFFER_SIZE,
|
|
13
|
+
DEFAULT_FIREFOX_BIDI_WS_URL,
|
|
14
|
+
DEFAULT_PROTOCOL_VERSION,
|
|
15
|
+
isLoopbackHost,
|
|
16
|
+
isLoopbackOrigin,
|
|
17
|
+
isTruthyFlag,
|
|
18
|
+
loadConfig,
|
|
19
|
+
normalizeBaseUrl,
|
|
20
|
+
normalizeWebSocketUrl,
|
|
21
|
+
parseBrowserFamily,
|
|
22
|
+
parsePositiveInteger,
|
|
23
|
+
} from "./config.mjs";
|
|
24
|
+
export { collectDoctorReport, renderDoctorReport } from "./doctor.mjs";
|
|
25
|
+
export { parseCliArgs, runCli } from "./cli.mjs";
|
|
26
|
+
export { McpBrowserDevToolsServer } from "./mcp-server.mjs";
|
|
27
|
+
export {
|
|
28
|
+
PACKAGE_DESCRIPTION,
|
|
29
|
+
PACKAGE_NAME,
|
|
30
|
+
PACKAGE_VERSION,
|
|
31
|
+
} from "./package-info.mjs";
|