@xbrowser/cli 0.16.0 → 1.0.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.
Files changed (52) hide show
  1. package/README.md +17 -26
  2. package/dist/{browser-R7B255ML.js → browser-GITRHHFO.js} +4 -1
  3. package/dist/{browser-GWBH6OJK.js → browser-R56O3CW6.js} +3 -1
  4. package/dist/{browser-I2HJZ7IP.js → browser-ZJOZB5CR.js} +4 -2
  5. package/dist/cdp-driver-BE3FOMRN.js +2803 -0
  6. package/dist/cdp-driver-TOPYJIFL.js +47 -0
  7. package/dist/chunk-2SVQTI2O.js +2794 -0
  8. package/dist/{chunk-KDYXFLAC.js → chunk-ACFE6PKF.js} +1015 -121
  9. package/dist/chunk-BBMRDUYQ.js +260 -0
  10. package/dist/chunk-CAFNSGYM.js +4834 -0
  11. package/dist/{chunk-DTJRVA76.js → chunk-ETCO4SNK.js} +2 -2
  12. package/dist/{chunk-RS6YYWTK.js → chunk-JPA2ZT2R.js} +140 -72
  13. package/dist/chunk-JPHCY4TC.js +260 -0
  14. package/dist/chunk-KFQGP6VL.js +33 -0
  15. package/dist/{chunk-ITKPSIP7.js → chunk-MDAPTB7C.js} +6 -25
  16. package/dist/chunk-OZKD3W4X.js +417 -0
  17. package/dist/chunk-PPG4D2EW.js +2796 -0
  18. package/dist/{chunk-ATFTAKMN.js → chunk-Q4IGYTKR.js} +39 -7
  19. package/dist/{chunk-F3ZWFCJJ.js → chunk-QIK2I3VQ.js} +141 -72
  20. package/dist/chunk-WJRE55TN.js +83 -0
  21. package/dist/cli.js +2358 -1086
  22. package/dist/{convert-4DUWZIKH.js → convert-LB3GJTLR.js} +4 -2
  23. package/dist/{convert-EKQVHKB4.js → convert-R3XXYKC6.js} +2 -2
  24. package/dist/{daemon-client-GX2UYIW4.js → daemon-client-DRCUMNHK.js} +45 -72
  25. package/dist/{daemon-client-XWSSQBEA.js → daemon-client-UZZEHHIV.js} +8 -1
  26. package/dist/daemon-main.js +3067 -1688
  27. package/dist/{extract-JUOQQX4V.js → extract-2ZFW2MX7.js} +1 -1
  28. package/dist/{extract-EGRXZSSK.js → extract-BSYBM4MR.js} +2 -0
  29. package/dist/{filter-OLAE26HN.js → filter-KCFO4RSV.js} +2 -0
  30. package/dist/{filter-VID2GGZ7.js → filter-T7DSZ2X7.js} +1 -1
  31. package/dist/{human-interaction-W753RVJB.js → human-interaction-UKAS5ZXV.js} +2 -2
  32. package/dist/index.d.ts +745 -148
  33. package/dist/index.js +3488 -1719
  34. package/dist/launcher-QUJ4M2VS.js +19 -0
  35. package/dist/launcher-YARP45UY.js +19 -0
  36. package/dist/{network-store-YAF5OIBH.js → network-store-XGZ25FFC.js} +1 -0
  37. package/dist/{network-store-BN6QEZ7R.js → network-store-YVDNUREI.js} +1 -1
  38. package/dist/{parse-action-dsl-T3DYC33D.js → parse-action-dsl-UM333TL2.js} +1 -1
  39. package/dist/{proxy-WKGUCH2C.js → proxy-LV4BJ5RC.js} +1 -1
  40. package/dist/session-recorder-RTDGURIJ.js +8 -0
  41. package/dist/session-recorder-YI7YYM36.js +7 -0
  42. package/dist/session-replayer-GLTUICSD.js +276 -0
  43. package/dist/site-knowledge-SYC6VCDB.js +23 -0
  44. package/package.json +6 -6
  45. package/dist/chunk-2ONMTDLK.js +0 -2050
  46. package/dist/daemon-client-3IJD6X4B.js +0 -59
  47. package/dist/network-store-2S5HATEV.js +0 -194
  48. package/dist/parse-action-dsl-DRSPBALP.js +0 -72
  49. package/dist/screenshot-CWAWMXVA.js +0 -28
  50. package/dist/screenshot-MB6R7RSS.js +0 -26
  51. package/dist/session-recorder-ILSSV2UC.js +0 -6
  52. package/dist/session-recorder-XET3DNML.js +0 -7
@@ -0,0 +1,4834 @@
1
+ import {
2
+ connectToCDP,
3
+ launchChrome
4
+ } from "./chunk-BBMRDUYQ.js";
5
+ import {
6
+ __require
7
+ } from "./chunk-KFQGP6VL.js";
8
+
9
+ // src/browser.ts
10
+ import { randomUUID } from "crypto";
11
+ import { existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "fs";
12
+ import { join } from "path";
13
+ import { homedir } from "os";
14
+
15
+ // src/cdp-driver/browser.ts
16
+ import { EventEmitter as EventEmitter3 } from "events";
17
+
18
+ // src/cdp-driver/context.ts
19
+ import { EventEmitter as EventEmitter2 } from "events";
20
+
21
+ // src/cdp-driver/page.ts
22
+ import { EventEmitter } from "events";
23
+
24
+ // src/cdp-driver/mouse.ts
25
+ var XBMouseImpl = class {
26
+ conn;
27
+ sessionId;
28
+ _x = 0;
29
+ _y = 0;
30
+ _button = "none";
31
+ constructor(conn, sessionId) {
32
+ this.conn = conn;
33
+ this.sessionId = sessionId;
34
+ }
35
+ /** Current cursor X position */
36
+ get x() {
37
+ return this._x;
38
+ }
39
+ /** Current cursor Y position */
40
+ get y() {
41
+ return this._y;
42
+ }
43
+ async click(x, y, opts = {}) {
44
+ const button = opts.button ?? "left";
45
+ const clickCount = opts.clickCount ?? 1;
46
+ const delay = opts.delay ?? 0;
47
+ await this.move(x, y);
48
+ await this.down({ button });
49
+ if (delay > 0) {
50
+ await sleep(delay);
51
+ }
52
+ await this.up({ button });
53
+ for (let i = 1; i < clickCount; i++) {
54
+ if (delay > 0) await sleep(delay);
55
+ await this.down({ button });
56
+ if (delay > 0) await sleep(delay);
57
+ await this.up({ button });
58
+ }
59
+ }
60
+ async dblclick(x, y, opts = {}) {
61
+ await this.click(x, y, { clickCount: 2, button: opts.button });
62
+ }
63
+ async down(opts = {}) {
64
+ const button = opts.button ?? "left";
65
+ this._button = button;
66
+ await this.send("Input.dispatchMouseEvent", {
67
+ type: "mousePressed",
68
+ x: this._x,
69
+ y: this._y,
70
+ button,
71
+ clickCount: 1
72
+ });
73
+ }
74
+ async up(opts = {}) {
75
+ const button = opts.button ?? "left";
76
+ this._button = "none";
77
+ await this.send("Input.dispatchMouseEvent", {
78
+ type: "mouseReleased",
79
+ x: this._x,
80
+ y: this._y,
81
+ button,
82
+ clickCount: 1
83
+ });
84
+ }
85
+ async move(x, y, opts = {}) {
86
+ const steps = Math.max(1, opts.steps ?? 1);
87
+ const fromX = this._x;
88
+ const fromY = this._y;
89
+ const dx = x - fromX;
90
+ const dy = y - fromY;
91
+ for (let i = 1; i <= steps; i++) {
92
+ const t = i / steps;
93
+ this._x = fromX + dx * t;
94
+ this._y = fromY + dy * t;
95
+ await this.send("Input.dispatchMouseEvent", {
96
+ type: "mouseMoved",
97
+ x: this._x,
98
+ y: this._y,
99
+ button: this._button
100
+ });
101
+ }
102
+ this._x = x;
103
+ this._y = y;
104
+ }
105
+ async wheel(deltaX, deltaY) {
106
+ await this.send("Input.dispatchMouseEvent", {
107
+ type: "mouseWheel",
108
+ x: this._x,
109
+ y: this._y,
110
+ deltaX,
111
+ deltaY
112
+ });
113
+ }
114
+ async send(method, params) {
115
+ await this.conn.send(method, params, this.sessionId);
116
+ }
117
+ };
118
+ function sleep(ms) {
119
+ return new Promise((resolve) => setTimeout(resolve, ms));
120
+ }
121
+
122
+ // src/cdp-driver/keyboard.ts
123
+ var XBKeyboardImpl = class {
124
+ conn;
125
+ sessionId;
126
+ constructor(conn, sessionId) {
127
+ this.conn = conn;
128
+ this.sessionId = sessionId;
129
+ }
130
+ async press(key, opts = {}) {
131
+ const delay = opts.delay ?? 0;
132
+ const mapping = resolveKeyMapping(key);
133
+ const downParams = {
134
+ type: "rawKeyDown",
135
+ key: mapping.key,
136
+ code: mapping.code
137
+ };
138
+ if (mapping.text) {
139
+ downParams.text = mapping.text;
140
+ downParams.unmodifiedText = mapping.text;
141
+ }
142
+ if (mapping.keyCode) {
143
+ downParams.windowsVirtualKeyCode = mapping.keyCode;
144
+ }
145
+ await this.dispatchKeyEvent(downParams);
146
+ if (mapping.text) {
147
+ await this.dispatchKeyEvent({
148
+ type: "char",
149
+ text: mapping.text
150
+ });
151
+ }
152
+ if (delay > 0) await sleep2(delay);
153
+ const upParams = {
154
+ type: "keyUp",
155
+ key: mapping.key,
156
+ code: mapping.code
157
+ };
158
+ if (mapping.keyCode) {
159
+ upParams.windowsVirtualKeyCode = mapping.keyCode;
160
+ }
161
+ await this.dispatchKeyEvent(upParams);
162
+ }
163
+ async down(key) {
164
+ const mapping = resolveKeyMapping(key);
165
+ const params = {
166
+ type: "rawKeyDown",
167
+ key: mapping.key,
168
+ code: mapping.code
169
+ };
170
+ if (mapping.text) {
171
+ params.text = mapping.text;
172
+ params.unmodifiedText = mapping.text;
173
+ }
174
+ if (mapping.keyCode) {
175
+ params.windowsVirtualKeyCode = mapping.keyCode;
176
+ }
177
+ await this.dispatchKeyEvent(params);
178
+ }
179
+ async up(key) {
180
+ const mapping = resolveKeyMapping(key);
181
+ const params = {
182
+ type: "keyUp",
183
+ key: mapping.key,
184
+ code: mapping.code
185
+ };
186
+ if (mapping.keyCode) {
187
+ params.windowsVirtualKeyCode = mapping.keyCode;
188
+ }
189
+ await this.dispatchKeyEvent(params);
190
+ }
191
+ async type(text, opts = {}) {
192
+ const delay = opts.delay ?? 0;
193
+ for (const char of text) {
194
+ if (delay > 0) await sleep2(delay);
195
+ const mapping = resolveKeyMapping(char);
196
+ const downParams = {
197
+ type: "rawKeyDown",
198
+ key: mapping.key,
199
+ code: mapping.code
200
+ };
201
+ if (mapping.text) {
202
+ downParams.text = mapping.text;
203
+ downParams.unmodifiedText = mapping.text;
204
+ }
205
+ if (mapping.keyCode) {
206
+ downParams.windowsVirtualKeyCode = mapping.keyCode;
207
+ }
208
+ await this.dispatchKeyEvent(downParams);
209
+ if (mapping.text) {
210
+ await this.dispatchKeyEvent({
211
+ type: "char",
212
+ text: mapping.text
213
+ });
214
+ }
215
+ await this.dispatchKeyEvent({
216
+ type: "keyUp",
217
+ key: mapping.key,
218
+ code: mapping.code,
219
+ ...mapping.keyCode ? { windowsVirtualKeyCode: mapping.keyCode } : {}
220
+ });
221
+ }
222
+ }
223
+ async insertText(text) {
224
+ await this.conn.send(
225
+ "Input.insertText",
226
+ { text },
227
+ this.sessionId
228
+ );
229
+ }
230
+ async dispatchKeyEvent(params) {
231
+ await this.conn.send("Input.dispatchKeyEvent", params, this.sessionId);
232
+ }
233
+ };
234
+ function resolveKeyMapping(key) {
235
+ if (KEY_MAP[key]) return KEY_MAP[key];
236
+ if (key.length === 1) {
237
+ const lower = key.toLowerCase();
238
+ if (lower >= "a" && lower <= "z") {
239
+ const code = `Key${lower.toUpperCase()}`;
240
+ const keyCode = lower.charCodeAt(0) - 32;
241
+ return { key, code, text: key, keyCode };
242
+ }
243
+ if (key >= "0" && key <= "9") {
244
+ const code = `Digit${key}`;
245
+ const keyCode = key.charCodeAt(0);
246
+ return { key, code, text: key, keyCode };
247
+ }
248
+ return { key, code: key, text: key };
249
+ }
250
+ return { key, code: key };
251
+ }
252
+ var KEY_MAP = {
253
+ Enter: { key: "Enter", code: "Enter", text: "\r", keyCode: 13 },
254
+ Tab: { key: "Tab", code: "Tab", text: " ", keyCode: 9 },
255
+ Escape: { key: "Escape", code: "Escape", keyCode: 27 },
256
+ Backspace: { key: "Backspace", code: "Backspace", keyCode: 8 },
257
+ Delete: { key: "Delete", code: "Delete", keyCode: 46 },
258
+ Space: { key: " ", code: "Space", text: " ", keyCode: 32 },
259
+ ArrowUp: { key: "ArrowUp", code: "ArrowUp", keyCode: 38 },
260
+ ArrowDown: { key: "ArrowDown", code: "ArrowDown", keyCode: 40 },
261
+ ArrowLeft: { key: "ArrowLeft", code: "ArrowLeft", keyCode: 37 },
262
+ ArrowRight: { key: "ArrowRight", code: "ArrowRight", keyCode: 39 },
263
+ Home: { key: "Home", code: "Home", keyCode: 36 },
264
+ End: { key: "End", code: "End", keyCode: 35 },
265
+ PageUp: { key: "PageUp", code: "PageUp", keyCode: 33 },
266
+ PageDown: { key: "PageDown", code: "PageDown", keyCode: 34 },
267
+ Control: { key: "Control", code: "ControlLeft", keyCode: 17 },
268
+ Shift: { key: "Shift", code: "ShiftLeft", keyCode: 16 },
269
+ Alt: { key: "Alt", code: "AltLeft", keyCode: 18 },
270
+ Meta: { key: "Meta", code: "MetaLeft", keyCode: 91 },
271
+ F1: { key: "F1", code: "F1", keyCode: 112 },
272
+ F2: { key: "F2", code: "F2", keyCode: 113 },
273
+ F3: { key: "F3", code: "F3", keyCode: 114 },
274
+ F4: { key: "F4", code: "F4", keyCode: 115 },
275
+ F5: { key: "F5", code: "F5", keyCode: 116 },
276
+ F6: { key: "F6", code: "F6", keyCode: 117 },
277
+ F7: { key: "F7", code: "F7", keyCode: 118 },
278
+ F8: { key: "F8", code: "F8", keyCode: 119 },
279
+ F9: { key: "F9", code: "F9", keyCode: 120 },
280
+ F10: { key: "F10", code: "F10", keyCode: 121 },
281
+ F11: { key: "F11", code: "F11", keyCode: 122 },
282
+ F12: { key: "F12", code: "F12", keyCode: 123 }
283
+ };
284
+ function sleep2(ms) {
285
+ return new Promise((resolve) => setTimeout(resolve, ms));
286
+ }
287
+
288
+ // src/cdp-driver/actionability.ts
289
+ async function waitForActionable(page, selector, opts = {}) {
290
+ const timeout = opts.timeout ?? 3e4;
291
+ if (opts.force) {
292
+ const nodeId = await page.querySelector(selector);
293
+ if (!nodeId) throw new Error(`Element not found: ${selector}`);
294
+ const rect = await page.getBoxModel(nodeId);
295
+ if (!rect) throw new Error(`Element has no box: ${selector}`);
296
+ return { nodeId, rect };
297
+ }
298
+ const deadline = Date.now() + timeout;
299
+ while (Date.now() < deadline) {
300
+ const result = await checkActionable(page, selector);
301
+ if (result.ok && result.rect) {
302
+ const nodeId = await page.querySelector(selector);
303
+ if (nodeId) return { nodeId, rect: result.rect };
304
+ }
305
+ await page.waitForTimeout(50);
306
+ }
307
+ throw new Error(`Actionability timeout: element '${selector}' not ready after ${timeout}ms`);
308
+ }
309
+ async function checkActionable(page, selector) {
310
+ const result = await page.evaluate(`
311
+ (function() {
312
+ const el = document.querySelector(${JSON.stringify(selector)});
313
+ if (!el) return { ok: false, reason: 'not_found' };
314
+
315
+ // Check attached to DOM
316
+ if (!el.isConnected) return { ok: false, reason: 'detached' };
317
+
318
+ // Check visibility
319
+ const style = window.getComputedStyle(el);
320
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') {
321
+ return { ok: false, reason: 'invisible' };
322
+ }
323
+
324
+ // Check non-zero size
325
+ const rect = el.getBoundingClientRect();
326
+ if (rect.width === 0 || rect.height === 0) {
327
+ return { ok: false, reason: 'zero_size' };
328
+ }
329
+
330
+ // Check enabled (for form elements)
331
+ if (el.disabled) return { ok: false, reason: 'disabled' };
332
+ if (el.tagName === 'OPTION' && el.closest('select')?.disabled) {
333
+ return { ok: false, reason: 'parent_disabled' };
334
+ }
335
+
336
+ // Check not covered by another element at center
337
+ const cx = rect.x + rect.width / 2;
338
+ const cy = rect.y + rect.height / 2;
339
+ const topEl = document.elementFromPoint(cx, cy);
340
+ if (topEl && topEl !== el && !el.contains(topEl) && !topEl.contains(el)) {
341
+ return { ok: false, reason: 'covered', rect: { x: rect.x, y: rect.y, width: rect.width, height: rect.height } };
342
+ }
343
+
344
+ return {
345
+ ok: true,
346
+ rect: { x: rect.x, y: rect.y, width: rect.width, height: rect.height },
347
+ };
348
+ })()
349
+ `);
350
+ return result;
351
+ }
352
+ async function scrollIntoView(page, selector) {
353
+ await page.evaluate(`
354
+ (function() {
355
+ const el = document.querySelector(${JSON.stringify(selector)});
356
+ if (el) el.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });
357
+ })()
358
+ `);
359
+ }
360
+
361
+ // src/cdp-driver/locator.ts
362
+ var XBLocatorImpl = class _XBLocatorImpl {
363
+ page;
364
+ selector;
365
+ constructor(page, selector) {
366
+ this.page = page;
367
+ this.selector = selector;
368
+ }
369
+ // ── Actions ─────────────────────────────────────────────────
370
+ async click(opts = {}) {
371
+ const { rect } = await waitForActionable(this.page, this.selector, opts);
372
+ await scrollIntoView(this.page, this.selector);
373
+ const updatedRect = await this.page.evaluate(`
374
+ (function() {
375
+ const el = document.querySelector(${JSON.stringify(this.selector)});
376
+ if (!el) return null;
377
+ const rect = el.getBoundingClientRect();
378
+ return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
379
+ })()
380
+ `);
381
+ const finalRect = updatedRect ?? rect;
382
+ const cx = finalRect.x + finalRect.width / 2;
383
+ const cy = finalRect.y + finalRect.height / 2;
384
+ await this.page.mouse.click(cx, cy, {
385
+ button: opts.button ?? "left",
386
+ clickCount: opts.clickCount ?? 1,
387
+ delay: opts.delay
388
+ });
389
+ }
390
+ async fill(value, opts = {}) {
391
+ await waitForActionable(this.page, this.selector, opts);
392
+ await scrollIntoView(this.page, this.selector);
393
+ await this.page.evaluate(`
394
+ (function() {
395
+ const el = document.querySelector(${JSON.stringify(this.selector)});
396
+ if (!el) throw new Error('Element not found: ${this.selector.replace(/'/g, "\\'")}');
397
+ el.focus();
398
+ el.value = '';
399
+ el.dispatchEvent(new Event('input', { bubbles: true }));
400
+ })()
401
+ `);
402
+ await this.page.keyboard.insertText(value);
403
+ await this.page.evaluate(`
404
+ (function() {
405
+ const el = document.querySelector(${JSON.stringify(this.selector)});
406
+ if (el) {
407
+ el.dispatchEvent(new Event('input', { bubbles: true }));
408
+ el.dispatchEvent(new Event('change', { bubbles: true }));
409
+ }
410
+ })()
411
+ `);
412
+ }
413
+ async press(key, opts = {}) {
414
+ await waitForActionable(this.page, this.selector, { timeout: opts.timeout });
415
+ await scrollIntoView(this.page, this.selector);
416
+ await this.page.evaluate(`
417
+ (function() {
418
+ const el = document.querySelector(${JSON.stringify(this.selector)});
419
+ if (el) el.focus();
420
+ })()
421
+ `);
422
+ await this.page.keyboard.press(key);
423
+ }
424
+ async pressSequentially(text, opts = {}) {
425
+ await waitForActionable(this.page, this.selector, { timeout: opts.timeout });
426
+ await scrollIntoView(this.page, this.selector);
427
+ await this.page.evaluate(`
428
+ (function() {
429
+ const el = document.querySelector(${JSON.stringify(this.selector)});
430
+ if (el) el.focus();
431
+ })()
432
+ `);
433
+ await this.page.keyboard.type(text, { delay: opts.delay });
434
+ }
435
+ async hover(opts = {}) {
436
+ if (!opts.force) {
437
+ const { rect } = await waitForActionable(this.page, this.selector, { timeout: opts.timeout });
438
+ await scrollIntoView(this.page, this.selector);
439
+ const cx = rect.x + rect.width / 2;
440
+ const cy = rect.y + rect.height / 2;
441
+ await this.page.mouse.move(cx, cy);
442
+ } else {
443
+ await scrollIntoView(this.page, this.selector);
444
+ const rect = await this.page.evaluate(`
445
+ (function() {
446
+ const el = document.querySelector(${JSON.stringify(this.selector)});
447
+ if (!el) return { x: 0, y: 0, width: 0, height: 0 };
448
+ const r = el.getBoundingClientRect();
449
+ return { x: r.x, y: r.y, width: r.width, height: r.height };
450
+ })()
451
+ `);
452
+ const cx = rect.x + rect.width / 2;
453
+ const cy = rect.y + rect.height / 2;
454
+ await this.page.mouse.move(cx, cy);
455
+ }
456
+ }
457
+ async type(text, opts = {}) {
458
+ await this.pressSequentially(text, opts);
459
+ }
460
+ async check(opts = {}) {
461
+ await waitForActionable(this.page, this.selector, { timeout: opts.timeout });
462
+ const isChecked = await this.page.evaluate(`
463
+ (function() {
464
+ const el = document.querySelector(${JSON.stringify(this.selector)});
465
+ return el?.checked === true;
466
+ })()
467
+ `);
468
+ if (!isChecked) {
469
+ await this.click({ timeout: opts.timeout });
470
+ }
471
+ }
472
+ async uncheck(opts = {}) {
473
+ await waitForActionable(this.page, this.selector, { timeout: opts.timeout });
474
+ const isChecked = await this.page.evaluate(`
475
+ (function() {
476
+ const el = document.querySelector(${JSON.stringify(this.selector)});
477
+ return el?.checked === true;
478
+ })()
479
+ `);
480
+ if (isChecked) {
481
+ await this.click({ timeout: opts.timeout });
482
+ }
483
+ }
484
+ async selectOption(value) {
485
+ await waitForActionable(this.page, this.selector);
486
+ const values = Array.isArray(value) ? value : [value];
487
+ const selected = await this.page.evaluate(`
488
+ (function() {
489
+ const el = document.querySelector(${JSON.stringify(this.selector)});
490
+ if (!el || el.tagName !== 'SELECT') throw new Error('Not a select element');
491
+
492
+ const values = ${JSON.stringify(values)};
493
+ const selectedValues = [];
494
+
495
+ for (const opt of el.options) {
496
+ for (const v of values) {
497
+ if (typeof v === 'object') {
498
+ if (v.label && opt.label === v.label) { opt.selected = true; selectedValues.push(opt.value); }
499
+ else if (v.value && opt.value === v.value) { opt.selected = true; selectedValues.push(opt.value); }
500
+ else if (v.index !== undefined && opt.index === v.index) { opt.selected = true; selectedValues.push(opt.value); }
501
+ } else if (opt.value === v || opt.label === v) {
502
+ opt.selected = true;
503
+ selectedValues.push(opt.value);
504
+ }
505
+ }
506
+ }
507
+
508
+ el.dispatchEvent(new Event('input', { bubbles: true }));
509
+ el.dispatchEvent(new Event('change', { bubbles: true }));
510
+ return selectedValues;
511
+ })()
512
+ `);
513
+ return selected;
514
+ }
515
+ async screenshot(opts = {}) {
516
+ await waitForActionable(this.page, this.selector);
517
+ const nodeId = await this.page.querySelector(this.selector);
518
+ if (!nodeId) throw new Error(`Element not found: ${this.selector}`);
519
+ const box = await this.page.getBoxModel(nodeId);
520
+ if (!box) throw new Error("Element has no box");
521
+ return this.page.screenshot({
522
+ ...opts,
523
+ clip: { x: box.x, y: box.y, width: box.width, height: box.height }
524
+ });
525
+ }
526
+ // ── State checks ────────────────────────────────────────────
527
+ async waitFor(opts = {}) {
528
+ await this.page.waitForSelector(this.selector, opts);
529
+ }
530
+ async count() {
531
+ return this.page.evaluate(`
532
+ document.querySelectorAll(${JSON.stringify(this.selector)}).length
533
+ `);
534
+ }
535
+ async isVisible() {
536
+ try {
537
+ const result = await this.page.evaluate(`
538
+ (function() {
539
+ const el = document.querySelector(${JSON.stringify(this.selector)});
540
+ if (!el) return false;
541
+ if (!el.isConnected) return false;
542
+ const style = window.getComputedStyle(el);
543
+ if (style.display === 'none' || style.visibility === 'hidden') return false;
544
+ const rect = el.getBoundingClientRect();
545
+ return rect.width > 0 && rect.height > 0;
546
+ })()
547
+ `);
548
+ return Boolean(result);
549
+ } catch {
550
+ return false;
551
+ }
552
+ }
553
+ async isHidden() {
554
+ return !await this.isVisible();
555
+ }
556
+ async isEnabled() {
557
+ return this.page.evaluate(`
558
+ (function() {
559
+ const el = document.querySelector(${JSON.stringify(this.selector)});
560
+ if (!el) return false;
561
+ return !el.disabled;
562
+ })()
563
+ `);
564
+ }
565
+ async isDisabled() {
566
+ return !await this.isEnabled();
567
+ }
568
+ async boundingBox() {
569
+ const nodeId = await this.page.querySelector(this.selector);
570
+ if (!nodeId) return null;
571
+ return this.page.getBoxModel(nodeId);
572
+ }
573
+ // ── Text/HTML ───────────────────────────────────────────────
574
+ async textContent() {
575
+ return this.page.evaluate(`
576
+ (function() {
577
+ const el = document.querySelector(${JSON.stringify(this.selector)});
578
+ return el?.textContent ?? null;
579
+ })()
580
+ `);
581
+ }
582
+ async innerText() {
583
+ return this.page.evaluate(`
584
+ (function() {
585
+ const el = document.querySelector(${JSON.stringify(this.selector)});
586
+ if (!el) throw new Error('Element not found');
587
+ return el.innerText;
588
+ })()
589
+ `);
590
+ }
591
+ async innerHTML() {
592
+ return this.page.evaluate(`
593
+ (function() {
594
+ const el = document.querySelector(${JSON.stringify(this.selector)});
595
+ if (!el) throw new Error('Element not found');
596
+ return el.innerHTML;
597
+ })()
598
+ `);
599
+ }
600
+ async getAttribute(name) {
601
+ return this.page.evaluate(`
602
+ (function() {
603
+ const el = document.querySelector(${JSON.stringify(this.selector)});
604
+ return el?.getAttribute(${JSON.stringify(name)}) ?? null;
605
+ })()
606
+ `);
607
+ }
608
+ // ── Evaluate ────────────────────────────────────────────────
609
+ async evaluate(fn, ...args) {
610
+ const fnBody = typeof fn === "function" ? fn.toString() : fn;
611
+ return this.page.evaluate(
612
+ `(function(sel, fnStr, ...evalArgs) {
613
+ const el = document.querySelector(sel);
614
+ if (!el) throw new Error('No element found for selector: ' + sel);
615
+ const fn = new Function('return ' + fnStr)();
616
+ return fn(el, ...evalArgs);
617
+ })(${JSON.stringify(this.selector)}, ${JSON.stringify(fnBody)}${args.length > 0 ? ", " + args.map((a) => JSON.stringify(a)).join(", ") : ""})`
618
+ );
619
+ }
620
+ async ariaSnapshot() {
621
+ const result = await this.page._cdpSend(
622
+ "Accessibility.getFullAXTree"
623
+ );
624
+ return result.nodes.map((n) => `${n.role?.value}: ${n.name?.value ?? ""}`).join("\n");
625
+ }
626
+ // ── Filtering ───────────────────────────────────────────────
627
+ first() {
628
+ return new FilteredLocator(this.page, this.selector, 0);
629
+ }
630
+ last() {
631
+ return new FilteredLocator(this.page, this.selector, -1);
632
+ }
633
+ nth(index) {
634
+ return new FilteredLocator(this.page, this.selector, index);
635
+ }
636
+ filter(opts) {
637
+ if (opts.visible) {
638
+ return new VisibleFilteredLocator(this.page, this.selector);
639
+ }
640
+ return new _XBLocatorImpl(this.page, this.selector);
641
+ }
642
+ async all() {
643
+ const n = await this.page.evaluate(`
644
+ document.querySelectorAll(${JSON.stringify(this.selector)}).length
645
+ `);
646
+ const locators = [];
647
+ for (let i = 0; i < n; i++) {
648
+ locators.push(new FilteredLocator(this.page, this.selector, i));
649
+ }
650
+ return locators;
651
+ }
652
+ async focus() {
653
+ await this.page.evaluate(`
654
+ (function() {
655
+ const el = document.querySelector(${JSON.stringify(this.selector)});
656
+ if (el) el.focus();
657
+ })()
658
+ `);
659
+ }
660
+ };
661
+ var FilteredLocator = class extends XBLocatorImpl {
662
+ constructor(page, selector, index) {
663
+ const indexedSelector = index === -1 ? `${selector}:last-of-type` : `${selector}:nth-of-type(${index + 1})`;
664
+ super(page, indexedSelector);
665
+ }
666
+ };
667
+ var VisibleFilteredLocator = class extends XBLocatorImpl {
668
+ async _withVisibleTag(fn) {
669
+ const tag = `data-xb-vt-${Date.now()}-${Math.floor(Math.random() * 1e6)}`;
670
+ const found = await this.page.evaluate(`
671
+ (function() {
672
+ const els = document.querySelectorAll(${JSON.stringify(this.selector)});
673
+ for (const el of els) {
674
+ if (!el.isConnected) continue;
675
+ const style = window.getComputedStyle(el);
676
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') continue;
677
+ const rect = el.getBoundingClientRect();
678
+ if (rect.width === 0 || rect.height === 0) continue;
679
+ el.setAttribute(${JSON.stringify(tag)}, '');
680
+ return true;
681
+ }
682
+ return false;
683
+ })()
684
+ `);
685
+ if (!found) throw new Error(`No visible element found for: ${this.selector}`);
686
+ try {
687
+ return await fn(`[${tag}]`);
688
+ } finally {
689
+ await this.page.evaluate(`
690
+ document.querySelectorAll(${JSON.stringify(`[${tag}]`)}).forEach(el => el.removeAttribute(${JSON.stringify(tag)}))
691
+ `);
692
+ }
693
+ }
694
+ async click(opts) {
695
+ return this._withVisibleTag((tagSel) => new XBLocatorImpl(this.page, tagSel).click(opts));
696
+ }
697
+ async fill(value, opts) {
698
+ return this._withVisibleTag((tagSel) => new XBLocatorImpl(this.page, tagSel).fill(value, opts));
699
+ }
700
+ async press(key, opts) {
701
+ return this._withVisibleTag((tagSel) => new XBLocatorImpl(this.page, tagSel).press(key, opts));
702
+ }
703
+ async hover(opts) {
704
+ return this._withVisibleTag((tagSel) => new XBLocatorImpl(this.page, tagSel).hover(opts));
705
+ }
706
+ async count() {
707
+ return this.page.evaluate(`
708
+ (function() {
709
+ let count = 0;
710
+ const els = document.querySelectorAll(${JSON.stringify(this.selector)});
711
+ for (const el of els) {
712
+ if (!el.isConnected) continue;
713
+ const style = window.getComputedStyle(el);
714
+ if (style.display === 'none' || style.visibility === 'hidden') continue;
715
+ const rect = el.getBoundingClientRect();
716
+ if (rect.width === 0 || rect.height === 0) continue;
717
+ count++;
718
+ }
719
+ return count;
720
+ })()
721
+ `);
722
+ }
723
+ async isVisible() {
724
+ try {
725
+ const result = await this.page.evaluate(`
726
+ (function() {
727
+ const els = document.querySelectorAll(${JSON.stringify(this.selector)});
728
+ for (const el of els) {
729
+ if (!el.isConnected) continue;
730
+ const style = window.getComputedStyle(el);
731
+ if (style.display === 'none' || style.visibility === 'hidden') continue;
732
+ const rect = el.getBoundingClientRect();
733
+ if (rect.width > 0 && rect.height > 0) return true;
734
+ }
735
+ return false;
736
+ })()
737
+ `);
738
+ return result;
739
+ } catch {
740
+ return false;
741
+ }
742
+ }
743
+ async textContent() {
744
+ return this._withVisibleTag((tagSel) => new XBLocatorImpl(this.page, tagSel).textContent());
745
+ }
746
+ async innerText() {
747
+ return this._withVisibleTag((tagSel) => new XBLocatorImpl(this.page, tagSel).innerText());
748
+ }
749
+ async waitFor(opts) {
750
+ const deadline = Date.now() + (opts?.timeout ?? 3e4);
751
+ while (Date.now() < deadline) {
752
+ if (await this.isVisible()) return;
753
+ await this.page.waitForTimeout(50);
754
+ }
755
+ throw new Error(`Timeout waiting for visible element: ${this.selector}`);
756
+ }
757
+ };
758
+
759
+ // src/cdp-driver/element-handle.ts
760
+ var XBElementHandleImpl = class {
761
+ page;
762
+ nodeId;
763
+ disposed = false;
764
+ constructor(page, nodeId) {
765
+ this.page = page;
766
+ this.nodeId = nodeId;
767
+ }
768
+ get _nodeId() {
769
+ return this.nodeId;
770
+ }
771
+ async click(opts = {}) {
772
+ if (this.disposed) throw new Error("Element handle disposed");
773
+ const box = await this.boundingBox();
774
+ if (!box) throw new Error("Element has no box");
775
+ await this.scrollIntoViewIfNeeded();
776
+ const cx = box.x + box.width / 2;
777
+ const cy = box.y + box.height / 2;
778
+ await this.page.mouse.click(cx, cy, {
779
+ button: opts.button ?? "left",
780
+ clickCount: opts.clickCount ?? 1,
781
+ delay: opts.delay
782
+ });
783
+ }
784
+ async fill(value, _opts = {}) {
785
+ if (this.disposed) throw new Error("Element handle disposed");
786
+ const objectId = await this.page.resolveNode(this.nodeId);
787
+ await this.page.callFunctionOn(
788
+ objectId,
789
+ `function(value) {
790
+ this.focus();
791
+ this.value = '';
792
+ this.dispatchEvent(new Event('input', { bubbles: true }));
793
+ }`,
794
+ [value]
795
+ );
796
+ await this.page.keyboard.insertText(value);
797
+ await this.page.callFunctionOn(
798
+ objectId,
799
+ `function() {
800
+ this.dispatchEvent(new Event('input', { bubbles: true }));
801
+ this.dispatchEvent(new Event('change', { bubbles: true }));
802
+ }`
803
+ );
804
+ }
805
+ async hover() {
806
+ const box = await this.boundingBox();
807
+ if (!box) throw new Error("Element has no box");
808
+ await this.scrollIntoViewIfNeeded();
809
+ await this.page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
810
+ }
811
+ async press(key) {
812
+ const objectId = await this.page.resolveNode(this.nodeId);
813
+ await this.page.callFunctionOn(objectId, "function() { this.focus(); }");
814
+ await this.page.keyboard.press(key);
815
+ }
816
+ async screenshot(opts = {}) {
817
+ const box = await this.boundingBox();
818
+ if (!box) throw new Error("Element has no box");
819
+ return this.page.screenshot({
820
+ ...opts,
821
+ clip: box
822
+ });
823
+ }
824
+ async boundingBox() {
825
+ return this.page.getBoxModel(this.nodeId);
826
+ }
827
+ async isVisible() {
828
+ try {
829
+ const objectId = await this.page.resolveNode(this.nodeId);
830
+ const result = await this.page.callFunctionOn(
831
+ objectId,
832
+ `function() {
833
+ if (!this.isConnected) return false;
834
+ const style = window.getComputedStyle(this);
835
+ if (style.display === 'none' || style.visibility === 'hidden') return false;
836
+ const rect = this.getBoundingClientRect();
837
+ return rect.width > 0 && rect.height > 0;
838
+ }`
839
+ );
840
+ return Boolean(result);
841
+ } catch {
842
+ return false;
843
+ }
844
+ }
845
+ async isEnabled() {
846
+ const objectId = await this.page.resolveNode(this.nodeId);
847
+ return this.page.callFunctionOn(objectId, "function() { return !this.disabled; }");
848
+ }
849
+ async textContent() {
850
+ const objectId = await this.page.resolveNode(this.nodeId);
851
+ return this.page.callFunctionOn(objectId, "function() { return this.textContent; }");
852
+ }
853
+ async innerText() {
854
+ const objectId = await this.page.resolveNode(this.nodeId);
855
+ return this.page.callFunctionOn(objectId, "function() { return this.innerText; }");
856
+ }
857
+ async innerHTML() {
858
+ const objectId = await this.page.resolveNode(this.nodeId);
859
+ return this.page.callFunctionOn(objectId, "function() { return this.innerHTML; }");
860
+ }
861
+ async getAttribute(name) {
862
+ const objectId = await this.page.resolveNode(this.nodeId);
863
+ return this.page.callFunctionOn(objectId, `function() { return this.getAttribute(${JSON.stringify(name)}); }`);
864
+ }
865
+ async scrollIntoViewIfNeeded() {
866
+ if (this.disposed) return;
867
+ const objectId = await this.page.resolveNode(this.nodeId);
868
+ await this.page.callFunctionOn(
869
+ objectId,
870
+ 'function() { this.scrollIntoView({ block: "center", inline: "center", behavior: "instant" }); }'
871
+ );
872
+ }
873
+ dispose() {
874
+ this.disposed = true;
875
+ }
876
+ };
877
+
878
+ // src/cdp-driver/page-helpers.ts
879
+ function globToRegex(glob) {
880
+ let pattern = glob.replace(/[\\^${}()|[\]+]/g, "\\$&").replace(/\./g, "\\.").replace(/\*\*/g, "{{DOUBLESTAR}}").replace(/\*/g, "[^/]*").replace(/{{DOUBLESTAR}}/g, ".*").replace(/\?/g, ".");
881
+ if (!pattern.startsWith("http") && !pattern.startsWith("\\.")) {
882
+ pattern = ".*" + pattern;
883
+ }
884
+ return new RegExp("^" + pattern + "$", "i");
885
+ }
886
+ function matchGlob(pattern, url) {
887
+ return globToRegex(pattern).test(url);
888
+ }
889
+ function createResponsePredicate(urlOrPredicate) {
890
+ if (typeof urlOrPredicate === "function") {
891
+ return urlOrPredicate;
892
+ }
893
+ if (urlOrPredicate instanceof RegExp) {
894
+ return (resp) => urlOrPredicate.test(resp.url());
895
+ }
896
+ const regex = globToRegex(urlOrPredicate);
897
+ return (resp) => regex.test(resp.url());
898
+ }
899
+ function createRequestPredicate(urlOrPredicate) {
900
+ if (typeof urlOrPredicate === "function") {
901
+ return urlOrPredicate;
902
+ }
903
+ if (urlOrPredicate instanceof RegExp) {
904
+ return (req) => urlOrPredicate.test(req.url());
905
+ }
906
+ const regex = globToRegex(urlOrPredicate);
907
+ return (req) => regex.test(req.url());
908
+ }
909
+ function createXBResponse(data, conn, sessionId, requestData) {
910
+ const request = requestData ? createXBRequest(null, requestData) : createXBRequest(null, { requestId: data.requestId, url: data.url, method: "GET", headers: {}, postData: null, resourceType: "other" });
911
+ return {
912
+ status: () => data.status,
913
+ statusText: () => "",
914
+ url: () => data.url,
915
+ headers: () => data.headers,
916
+ ok: () => data.status >= 200 && data.status < 300,
917
+ body: async () => {
918
+ if (!conn) throw new Error("Response body not available");
919
+ try {
920
+ const resp = await conn.send(
921
+ "Network.getResponseBody",
922
+ { requestId: data.requestId },
923
+ sessionId
924
+ );
925
+ return Buffer.from(resp.body, resp.base64Encoded ? "base64" : "utf8");
926
+ } catch {
927
+ throw new Error("Response body not available");
928
+ }
929
+ },
930
+ text: async () => {
931
+ if (!conn) throw new Error("Response body not available");
932
+ try {
933
+ const resp = await conn.send(
934
+ "Network.getResponseBody",
935
+ { requestId: data.requestId },
936
+ sessionId
937
+ );
938
+ return resp.base64Encoded ? Buffer.from(resp.body, "base64").toString("utf8") : resp.body;
939
+ } catch {
940
+ throw new Error("Response body not available");
941
+ }
942
+ },
943
+ json: async () => {
944
+ const text = await (async () => {
945
+ if (!conn) throw new Error("Response body not available");
946
+ try {
947
+ const resp = await conn.send(
948
+ "Network.getResponseBody",
949
+ { requestId: data.requestId },
950
+ sessionId
951
+ );
952
+ return resp.base64Encoded ? Buffer.from(resp.body, "base64").toString("utf8") : resp.body;
953
+ } catch {
954
+ throw new Error("Response body not available");
955
+ }
956
+ })();
957
+ return JSON.parse(text);
958
+ },
959
+ request: () => request
960
+ };
961
+ }
962
+ function createXBRequest(page, data) {
963
+ return {
964
+ url: () => data.url,
965
+ method: () => data.method,
966
+ headers: () => data.headers,
967
+ postData: () => data.postData,
968
+ resourceType: () => data.resourceType,
969
+ response: async () => {
970
+ if (!page?._networkResponses) return null;
971
+ const resp = page._networkResponses.get(data.requestId);
972
+ if (!resp) return null;
973
+ return createXBResponse(resp, page._connection, page.sessionId, data);
974
+ }
975
+ };
976
+ }
977
+ function createXBRouteFetch(conn, sessionId, params, emitter) {
978
+ const request = createXBRequest(null, {
979
+ requestId: params.requestId,
980
+ url: params.request.url,
981
+ method: params.request.method,
982
+ headers: params.request.headers,
983
+ postData: params.request.postData ?? null,
984
+ resourceType: params.resourceType
985
+ });
986
+ return {
987
+ request: () => request,
988
+ abort: async (errorCode) => {
989
+ await conn.send("Fetch.failRequest", {
990
+ requestId: params.requestId,
991
+ errorReason: errorCode || "Failed"
992
+ }, sessionId);
993
+ },
994
+ continue: async (opts) => {
995
+ await conn.send("Fetch.continueRequest", {
996
+ requestId: params.requestId,
997
+ url: opts?.url,
998
+ method: opts?.method,
999
+ headers: opts?.headers ? Object.entries(opts.headers).map(([k, v]) => ({ name: k, value: v })) : void 0,
1000
+ postData: opts?.postData ? Buffer.from(opts.postData).toString("base64") : void 0
1001
+ }, sessionId);
1002
+ },
1003
+ fulfill: async (opts) => {
1004
+ const bodyStr = typeof opts.body === "string" ? opts.body : opts.body ? opts.body.toString("utf8") : "";
1005
+ const bodyBytes = Buffer.from(bodyStr, "utf8");
1006
+ const headers = { ...opts.headers };
1007
+ if (opts.contentType) headers["content-type"] = opts.contentType;
1008
+ headers["access-control-allow-origin"] = "*";
1009
+ await conn.send("Fetch.fulfillRequest", {
1010
+ requestId: params.requestId,
1011
+ responseCode: opts.status ?? 200,
1012
+ responseHeaders: Object.entries(headers).map(([k, v]) => ({ name: k, value: v })),
1013
+ body: bodyBytes.toString("base64")
1014
+ }, sessionId);
1015
+ if (emitter) {
1016
+ const responseData = {
1017
+ requestId: params.requestId,
1018
+ status: opts.status ?? 200,
1019
+ url: params.request.url,
1020
+ headers
1021
+ };
1022
+ const response = createXBResponse(responseData, conn, sessionId);
1023
+ emitter.emit("response", response);
1024
+ }
1025
+ }
1026
+ };
1027
+ }
1028
+
1029
+ // src/cdp-driver/page.ts
1030
+ var XBPageImpl = class _XBPageImpl {
1031
+ conn;
1032
+ _emitter = new EventEmitter();
1033
+ _subscriptions = [];
1034
+ sessionId;
1035
+ _targetId;
1036
+ _contextImpl;
1037
+ _browserImpl;
1038
+ _closed = false;
1039
+ _url = "about:blank";
1040
+ _title = "";
1041
+ _viewportSize = null;
1042
+ _loadState = {
1043
+ loadFired: true,
1044
+ domContentFired: true,
1045
+ networkIdle: true
1046
+ };
1047
+ mouse;
1048
+ keyboard;
1049
+ // Network tracking for waitForLoadState('networkidle')
1050
+ inflightRequests = /* @__PURE__ */ new Set();
1051
+ networkIdleResolve = null;
1052
+ networkIdleTimer = null;
1053
+ static NETWORK_IDLE_MS = 500;
1054
+ constructor(conn, sessionId, targetId, context, browser) {
1055
+ this.conn = conn;
1056
+ this.sessionId = sessionId;
1057
+ this._targetId = targetId;
1058
+ this._contextImpl = context;
1059
+ this._browserImpl = browser;
1060
+ this.mouse = new XBMouseImpl(conn, sessionId);
1061
+ this.keyboard = new XBKeyboardImpl(conn, sessionId);
1062
+ }
1063
+ _emit(event, ...args) {
1064
+ this._emitter.emit(event, ...args);
1065
+ }
1066
+ /** Internal initialization — must be called after construction */
1067
+ async _init() {
1068
+ await this.conn.send("Page.enable", void 0, this.sessionId);
1069
+ await this.conn.send("Runtime.enable", void 0, this.sessionId);
1070
+ await this.conn.send("Network.enable", void 0, this.sessionId);
1071
+ this.setupPageEvents();
1072
+ this.setupNetworkEvents();
1073
+ this.setupConsoleEvents();
1074
+ await this.conn.send("Runtime.runIfWaitingForDebugger", void 0, this.sessionId).catch(() => {
1075
+ });
1076
+ try {
1077
+ const info = await this.conn.send(
1078
+ "Target.getTargetInfo",
1079
+ { targetId: this._targetId }
1080
+ );
1081
+ this._url = info.url;
1082
+ this._title = info.title;
1083
+ } catch {
1084
+ }
1085
+ }
1086
+ get _connection() {
1087
+ return this.conn;
1088
+ }
1089
+ // ── Navigation ──────────────────────────────────────────────
1090
+ async goto(url, opts = {}) {
1091
+ if (this._closed) throw new Error("Page is closed");
1092
+ const waitUntil = opts.waitUntil ?? "load";
1093
+ const timeout = opts.timeout ?? 3e4;
1094
+ this._loadState = { loadFired: false, domContentFired: false, networkIdle: false };
1095
+ const result = await this.conn.send(
1096
+ "Page.navigate",
1097
+ { url, referrer: opts.referer },
1098
+ this.sessionId
1099
+ );
1100
+ if (result.errorText) {
1101
+ throw new Error(`Navigation failed: ${result.errorText}`);
1102
+ }
1103
+ await this.waitForLoadState(waitUntil, timeout);
1104
+ this._url = url;
1105
+ const statusCode = 200;
1106
+ const finalUrl = url;
1107
+ const headers = {};
1108
+ return {
1109
+ status: () => statusCode,
1110
+ ok: () => statusCode >= 200 && statusCode < 300,
1111
+ url: () => finalUrl,
1112
+ headers: () => headers
1113
+ };
1114
+ }
1115
+ async goBack(opts = {}) {
1116
+ await this.evaluate("() => history.back()");
1117
+ await this.waitForLoadState(opts.waitUntil ?? "load", opts.timeout);
1118
+ }
1119
+ async goForward(opts = {}) {
1120
+ await this.evaluate("() => history.forward()");
1121
+ await this.waitForLoadState(opts.waitUntil ?? "load", opts.timeout);
1122
+ }
1123
+ async reload(opts = {}) {
1124
+ this._loadState = { loadFired: false, domContentFired: false, networkIdle: false };
1125
+ await this.conn.send("Page.reload", void 0, this.sessionId);
1126
+ await this.waitForLoadState(opts.waitUntil ?? "load", opts.timeout);
1127
+ }
1128
+ async waitForLoadState(state = "load", timeout = 3e4) {
1129
+ if (this._closed) throw new Error("Page is closed");
1130
+ const checkState = () => {
1131
+ switch (state) {
1132
+ case "domcontentloaded":
1133
+ return this._loadState.domContentFired;
1134
+ case "load":
1135
+ return this._loadState.loadFired;
1136
+ case "networkidle":
1137
+ return this._loadState.networkIdle;
1138
+ case "commit":
1139
+ return this._loadState.domContentFired;
1140
+ default:
1141
+ return true;
1142
+ }
1143
+ };
1144
+ if (checkState()) return;
1145
+ return new Promise((resolve, reject) => {
1146
+ const timer = setTimeout(() => {
1147
+ reject(new Error(`waitForLoadState('${state}') timeout after ${timeout}ms`));
1148
+ }, timeout);
1149
+ const check = () => {
1150
+ if (checkState()) {
1151
+ clearTimeout(timer);
1152
+ resolve();
1153
+ } else {
1154
+ setTimeout(check, 50);
1155
+ }
1156
+ };
1157
+ check();
1158
+ });
1159
+ }
1160
+ async waitForTimeout(ms) {
1161
+ await new Promise((resolve) => {
1162
+ const timer = setTimeout(resolve, ms);
1163
+ if (typeof timer.unref === "function") timer.unref();
1164
+ });
1165
+ }
1166
+ async waitForSelector(selector, opts = {}) {
1167
+ const state = opts.state ?? "visible";
1168
+ const timeout = opts.timeout ?? 3e4;
1169
+ const deadline = Date.now() + timeout;
1170
+ while (Date.now() < deadline) {
1171
+ const exists = await this.evaluate(
1172
+ `(function() { const el = document.querySelector(${JSON.stringify(selector)}); return !!el; })()`
1173
+ );
1174
+ if (state === "attached" && exists) return;
1175
+ if (state === "detached" && !exists) return;
1176
+ if (state === "visible" && exists) {
1177
+ const visible = await this.evaluate(
1178
+ `(function() { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) return false; const rect = el.getBoundingClientRect(); const style = window.getComputedStyle(el); return rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none'; })()`
1179
+ );
1180
+ if (visible) return;
1181
+ }
1182
+ if (state === "hidden") {
1183
+ if (!exists) return;
1184
+ const visible = await this.evaluate(
1185
+ `(function() { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) return false; const rect = el.getBoundingClientRect(); const style = window.getComputedStyle(el); return rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none'; })()`
1186
+ );
1187
+ if (!visible) return;
1188
+ }
1189
+ await this.waitForTimeout(100);
1190
+ }
1191
+ throw new Error(`waitForSelector('${selector}', state='${state}') timeout after ${timeout}ms`);
1192
+ }
1193
+ async waitForFunction(fn, opts = {}, ...args) {
1194
+ const timeout = opts.timeout ?? 3e4;
1195
+ const polling = opts.polling ?? 100;
1196
+ const deadline = Date.now() + timeout;
1197
+ const fnBody = typeof fn === "function" ? fn.toString() : fn;
1198
+ let lastError = null;
1199
+ while (Date.now() < deadline) {
1200
+ try {
1201
+ const result = await this.evaluate(
1202
+ `(function(fnStr, ...evalArgs) { const fn = new Function('return ' + fnStr); return fn(...evalArgs); })(${JSON.stringify(fnBody)}${args.length > 0 ? ", " + args.map((a) => JSON.stringify(a)).join(", ") : ""})`
1203
+ );
1204
+ if (result) return result;
1205
+ } catch (err) {
1206
+ lastError = err;
1207
+ }
1208
+ const pollMs = typeof polling === "number" ? polling : 16;
1209
+ await this.waitForTimeout(pollMs);
1210
+ }
1211
+ const detail = lastError ? `
1212
+ Last error: ${lastError.message}` : "";
1213
+ throw new Error(`waitForFunction timeout after ${timeout}ms${detail}`);
1214
+ }
1215
+ url() {
1216
+ return this._url;
1217
+ }
1218
+ async title() {
1219
+ this._title = await this.evaluate("document.title");
1220
+ return this._title;
1221
+ }
1222
+ async content() {
1223
+ return this.evaluate("document.documentElement.outerHTML");
1224
+ }
1225
+ // ── Evaluation ──────────────────────────────────────────────
1226
+ async evaluate(fn, ...args) {
1227
+ if (this._closed) throw new Error("Page is closed");
1228
+ let expression;
1229
+ if (typeof fn === "string") {
1230
+ expression = fn;
1231
+ } else {
1232
+ const argStr = args.length > 0 ? `...${JSON.stringify(args)}` : "";
1233
+ expression = `(()=>{const __fn=(${fn.toString()});return __fn(${argStr});})()`;
1234
+ }
1235
+ const result = await this.conn.send("Runtime.evaluate", {
1236
+ expression,
1237
+ returnByValue: true,
1238
+ awaitPromise: true
1239
+ }, this.sessionId);
1240
+ if (result.exceptionDetails) {
1241
+ const detail = result.exceptionDetails.exception?.description ?? result.exceptionDetails.exception?.value ?? result.exceptionDetails.text;
1242
+ throw new Error(`${detail}`);
1243
+ }
1244
+ return result.result?.value;
1245
+ }
1246
+ /** evaluateHandle — evaluates fn and returns a handle for element bounding box */
1247
+ async evaluateHandle(fn, ...args) {
1248
+ let expression;
1249
+ if (typeof fn === "string") {
1250
+ expression = fn;
1251
+ } else {
1252
+ const argStr = args.length > 0 ? `...${JSON.stringify(args)}` : "";
1253
+ expression = `(()=>{const __fn=(${fn.toString()});const __el=__fn(${argStr});if(__el&&typeof __el.getBoundingClientRect==='function'){const r=__el.getBoundingClientRect();return JSON.parse(JSON.stringify({x:r.x,y:r.y,w:r.width,h:r.height}));}return null;})()`;
1254
+ }
1255
+ const result = await this.conn.send("Runtime.evaluate", { expression, returnByValue: true }).catch(() => ({ result: { value: null } }));
1256
+ let box = null;
1257
+ try {
1258
+ box = JSON.parse(result.result?.value);
1259
+ } catch {
1260
+ }
1261
+ return {
1262
+ asElement: () => box ? { boundingBox: async () => box } : null
1263
+ };
1264
+ }
1265
+ async $eval(selector, fn, ...args) {
1266
+ const fnBody = typeof fn === "function" ? fn.toString() : fn;
1267
+ return this.evaluate(
1268
+ `(function(sel, fnStr, ...evalArgs) {
1269
+ const el = document.querySelector(sel);
1270
+ if (!el) throw new Error('No element found for selector: ' + sel);
1271
+ const fn = new Function('return ' + fnStr)();
1272
+ return fn(el, ...evalArgs);
1273
+ })(${JSON.stringify(selector)}, ${JSON.stringify(fnBody)}${args.length > 0 ? ", " + args.map((a) => JSON.stringify(a)).join(", ") : ""})`
1274
+ );
1275
+ }
1276
+ async $$eval(selector, fn, ...args) {
1277
+ const fnBody = typeof fn === "function" ? fn.toString() : fn;
1278
+ return this.evaluate(
1279
+ `(function(sel, fnStr, ...evalArgs) {
1280
+ const els = Array.from(document.querySelectorAll(sel));
1281
+ const fn = new Function('return ' + fnStr)();
1282
+ return fn(els, ...evalArgs);
1283
+ })(${JSON.stringify(selector)}, ${JSON.stringify(fnBody)}${args.length > 0 ? ", " + args.map((a) => JSON.stringify(a)).join(", ") : ""})`
1284
+ );
1285
+ }
1286
+ // ── Locator ─────────────────────────────────────────────────
1287
+ locator(selector) {
1288
+ return new XBLocatorImpl(this, selector);
1289
+ }
1290
+ getByText(text, opts) {
1291
+ const escaped = text.replace(/'/g, "\\'");
1292
+ if (opts?.exact) {
1293
+ return this.locator(`xpath=//*[normalize-space(text())='${escaped}']`);
1294
+ }
1295
+ return this.locator(`xpath=//*[contains(text(),'${escaped}')]`);
1296
+ }
1297
+ getByRole(role, opts) {
1298
+ let sel = `[role="${role}"]`;
1299
+ if (opts?.name) {
1300
+ sel += opts.exact ? `[aria-label="${opts.name}"]` : `[aria-label*="${opts.name}"]`;
1301
+ }
1302
+ return this.locator(sel);
1303
+ }
1304
+ getByLabel(label, opts) {
1305
+ const escaped = label.replace(/'/g, "\\'");
1306
+ const sel = opts?.exact ? `xpath=//*[@aria-label='${escaped}' or @id=//label[normalize-space(text())='${escaped}']/@for]` : `xpath=//*[contains(@aria-label,'${escaped}') or @id=//label[contains(text(),'${escaped}')]/@for]`;
1307
+ return this.locator(sel);
1308
+ }
1309
+ getByPlaceholder(text, opts) {
1310
+ return this.locator(
1311
+ opts?.exact ? `[placeholder="${text}"]` : `[placeholder*="${text}"]`
1312
+ );
1313
+ }
1314
+ getByTestId(id) {
1315
+ return this.locator(`[data-testid="${id}"]`);
1316
+ }
1317
+ getByAltText(text, opts) {
1318
+ return this.locator(opts?.exact ? `[alt="${text}"]` : `[alt*="${text}"]`);
1319
+ }
1320
+ getByTitle(title, opts) {
1321
+ return this.locator(
1322
+ opts?.exact ? `[title="${title}"]` : `[title*="${title}"]`
1323
+ );
1324
+ }
1325
+ // ── Interaction shortcuts ───────────────────────────────────
1326
+ async click(selector, opts = {}) {
1327
+ await this.locator(selector).click(opts);
1328
+ }
1329
+ async dblclick(selector, opts = {}) {
1330
+ await this.locator(selector).click({ ...opts, clickCount: 2 });
1331
+ }
1332
+ async fill(selector, value, opts = {}) {
1333
+ await this.locator(selector).fill(value, opts);
1334
+ }
1335
+ async press(selector, key, opts) {
1336
+ await this.locator(selector).press(key, opts);
1337
+ }
1338
+ async hover(selector, opts) {
1339
+ await this.locator(selector).hover(opts);
1340
+ }
1341
+ async type(selector, text, opts = {}) {
1342
+ await this.locator(selector).type(text, opts);
1343
+ }
1344
+ async check(selector, opts) {
1345
+ await this.locator(selector).check(opts);
1346
+ }
1347
+ async uncheck(selector, opts) {
1348
+ await this.locator(selector).uncheck(opts);
1349
+ }
1350
+ async selectOption(selector, value) {
1351
+ return this.locator(selector).selectOption(value);
1352
+ }
1353
+ // ── Convenience selectors ───────────────────────────────────
1354
+ async textContent(selector) {
1355
+ return this.evaluate(
1356
+ `(function() { const el = document.querySelector(${JSON.stringify(selector)}); return el?.textContent ?? null; })()`
1357
+ );
1358
+ }
1359
+ async innerText(selector) {
1360
+ return this.evaluate(
1361
+ `(function() { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) throw new Error('Element not found'); return el.innerText; })()`
1362
+ );
1363
+ }
1364
+ async innerHTML(selector) {
1365
+ return this.evaluate(
1366
+ `(function() { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) throw new Error('Element not found'); return el.innerHTML; })()`
1367
+ );
1368
+ }
1369
+ async getAttribute(selector, name) {
1370
+ return this.evaluate(
1371
+ `(function() { const el = document.querySelector(${JSON.stringify(selector)}); return el?.getAttribute(${JSON.stringify(name)}) ?? null; })()`
1372
+ );
1373
+ }
1374
+ // ── Query ───────────────────────────────────────────────────
1375
+ async $(selector) {
1376
+ const nodeId = await this.querySelector(selector);
1377
+ if (!nodeId) return null;
1378
+ return new XBElementHandleImpl(this, nodeId);
1379
+ }
1380
+ async $$(selector) {
1381
+ const nodeIds = await this.querySelectorAll(selector);
1382
+ return nodeIds.map((id) => new XBElementHandleImpl(this, id));
1383
+ }
1384
+ // ── Screen ──────────────────────────────────────────────────
1385
+ async screenshot(opts = {}) {
1386
+ const params = {
1387
+ format: opts.type ?? "png"
1388
+ };
1389
+ if (opts.quality !== void 0 && (opts.type === "jpeg" || !opts.type && opts.quality)) {
1390
+ params.quality = opts.quality;
1391
+ }
1392
+ if (opts.fullPage) {
1393
+ params.captureBeyondViewport = true;
1394
+ }
1395
+ if (opts.clip) {
1396
+ const clip = {
1397
+ x: Math.round(opts.clip.x),
1398
+ y: Math.round(opts.clip.y),
1399
+ width: Math.round(Math.max(1, opts.clip.width)),
1400
+ height: Math.round(Math.max(1, opts.clip.height)),
1401
+ scale: 1
1402
+ };
1403
+ params.clip = clip;
1404
+ }
1405
+ if (opts.omitBackground) {
1406
+ params.omitBackground = true;
1407
+ }
1408
+ const result = await this.conn.send(
1409
+ "Page.captureScreenshot",
1410
+ params,
1411
+ this.sessionId
1412
+ );
1413
+ return Buffer.from(result.data, "base64");
1414
+ }
1415
+ async pdf(opts = {}) {
1416
+ const params = {};
1417
+ if (opts.landscape !== void 0) params.landscape = opts.landscape;
1418
+ if (opts.printBackground !== void 0) params.printBackground = opts.printBackground;
1419
+ if (opts.scale !== void 0) params.scale = opts.scale;
1420
+ if (opts.format) params.paperFormat = opts.format;
1421
+ if (opts.preferCSSPageSize !== void 0) params.preferCSSPageSize = opts.preferCSSPageSize;
1422
+ if (opts.margin) {
1423
+ if (opts.margin.top) params.marginTop = parseFloat(opts.margin.top);
1424
+ if (opts.margin.bottom) params.marginBottom = parseFloat(opts.margin.bottom);
1425
+ if (opts.margin.left) params.marginLeft = parseFloat(opts.margin.left);
1426
+ if (opts.margin.right) params.marginRight = parseFloat(opts.margin.right);
1427
+ }
1428
+ const result = await this.conn.send("Page.printToPDF", params, this.sessionId);
1429
+ return Buffer.from(result.data, "base64");
1430
+ }
1431
+ viewportSize() {
1432
+ return this._viewportSize ?? null;
1433
+ }
1434
+ async setViewportSize(size) {
1435
+ await this.conn.send(
1436
+ "Emulation.setDeviceMetricsOverride",
1437
+ {
1438
+ width: size.width,
1439
+ height: size.height,
1440
+ deviceScaleFactor: 1,
1441
+ mobile: false
1442
+ },
1443
+ this.sessionId
1444
+ );
1445
+ this._viewportSize = size;
1446
+ }
1447
+ // ── Scripts ─────────────────────────────────────────────────
1448
+ async addInitScript(script) {
1449
+ await this.conn.send(
1450
+ "Page.addScriptToEvaluateOnNewDocument",
1451
+ { source: script },
1452
+ this.sessionId
1453
+ );
1454
+ }
1455
+ /** Internal: set user agent */
1456
+ async _setUserAgent(userAgent) {
1457
+ await this.conn.send(
1458
+ "Network.setUserAgentOverride",
1459
+ { userAgent },
1460
+ this.sessionId
1461
+ );
1462
+ }
1463
+ /** Internal: set extra HTTP headers */
1464
+ async _setExtraHTTPHeaders(headers) {
1465
+ await this.setExtraHTTPHeaders(headers);
1466
+ }
1467
+ async bringToFront() {
1468
+ await this.conn.send("Page.bringToFront", void 0, this.sessionId);
1469
+ }
1470
+ async setExtraHTTPHeaders(headers) {
1471
+ await this.conn.send("Network.setExtraHTTPHeaders", { headers }, this.sessionId);
1472
+ }
1473
+ // ── Events ──────────────────────────────────────────────────
1474
+ on(event, handler) {
1475
+ this._emitter.on(event, handler);
1476
+ }
1477
+ off(event, handler) {
1478
+ this._emitter.off(event, handler);
1479
+ }
1480
+ /**
1481
+ * Wait for a one-shot event (Playwright-compatible subset).
1482
+ * Used to listen for 'filechooser', 'dialog', 'popup', 'framenavigated', etc.
1483
+ */
1484
+ async waitForEvent(event, opts = {}) {
1485
+ const timeout = opts.timeout ?? 3e4;
1486
+ return new Promise((resolve, reject) => {
1487
+ const timer = setTimeout(() => {
1488
+ this._emitter.off(event, handler);
1489
+ reject(new Error(`waitForEvent('${event}') timeout after ${timeout}ms`));
1490
+ }, timeout);
1491
+ const handler = (...args) => {
1492
+ if (opts.predicate && !opts.predicate(...args)) return;
1493
+ clearTimeout(timer);
1494
+ this._emitter.off(event, handler);
1495
+ resolve(args.length === 1 ? args[0] : args);
1496
+ };
1497
+ this._emitter.on(event, handler);
1498
+ });
1499
+ }
1500
+ // ── Lifecycle ───────────────────────────────────────────────
1501
+ async close() {
1502
+ if (this._closed) return;
1503
+ this._closed = true;
1504
+ if (this.networkIdleTimer) {
1505
+ clearTimeout(this.networkIdleTimer);
1506
+ this.networkIdleTimer = null;
1507
+ }
1508
+ for (const unsub of this._subscriptions) {
1509
+ unsub();
1510
+ }
1511
+ this._subscriptions = [];
1512
+ await this._browserImpl._closeTarget(this._targetId).catch(() => {
1513
+ });
1514
+ await this._browserImpl._detachFromTarget(this.sessionId).catch(() => {
1515
+ });
1516
+ this._emitter.emit("close");
1517
+ }
1518
+ isClosed() {
1519
+ return this._closed;
1520
+ }
1521
+ context() {
1522
+ return this._contextImpl;
1523
+ }
1524
+ browser() {
1525
+ return this._browserImpl;
1526
+ }
1527
+ mainFrame() {
1528
+ return {
1529
+ url: () => this._url,
1530
+ name: () => "",
1531
+ isDetached: () => this._closed,
1532
+ page: () => this,
1533
+ evaluate: (fn, ...args) => this.evaluate(fn, ...args),
1534
+ $: (sel) => this.$(sel),
1535
+ $$: (sel) => this.$$(sel)
1536
+ };
1537
+ }
1538
+ frames() {
1539
+ return [this.mainFrame()];
1540
+ }
1541
+ // ── CDP helpers exposed for locator/element ─────────────────
1542
+ /** Query a single element, returns CDP nodeId or 0 if not found */
1543
+ async querySelector(selector) {
1544
+ const doc = await this.conn.send(
1545
+ "DOM.getDocument",
1546
+ { depth: 0 },
1547
+ this.sessionId
1548
+ );
1549
+ const result = await this.conn.send(
1550
+ "DOM.querySelector",
1551
+ { nodeId: doc.root.nodeId, selector },
1552
+ this.sessionId
1553
+ );
1554
+ return result.nodeId;
1555
+ }
1556
+ /** Query all matching elements, returns array of CDP nodeIds */
1557
+ async querySelectorAll(selector) {
1558
+ const doc = await this.conn.send(
1559
+ "DOM.getDocument",
1560
+ { depth: 0 },
1561
+ this.sessionId
1562
+ );
1563
+ const result = await this.conn.send(
1564
+ "DOM.querySelectorAll",
1565
+ { nodeId: doc.root.nodeId, selector },
1566
+ this.sessionId
1567
+ );
1568
+ return result.nodeIds ?? [];
1569
+ }
1570
+ /** Resolve a CDP nodeId to a RemoteObject for evaluate */
1571
+ async resolveNode(nodeId) {
1572
+ const result = await this.conn.send(
1573
+ "DOM.resolveNode",
1574
+ { nodeId },
1575
+ this.sessionId
1576
+ );
1577
+ return result.object.objectId;
1578
+ }
1579
+ /** Get the box model for a nodeId */
1580
+ async getBoxModel(nodeId) {
1581
+ try {
1582
+ const result = await this.conn.send("DOM.getBoxModel", { nodeId }, this.sessionId);
1583
+ const c = result.model?.content;
1584
+ if (!c || c.length < 8) return null;
1585
+ const x1 = c[0];
1586
+ const y1 = c[1];
1587
+ const x2 = c[4];
1588
+ const y2 = c[5];
1589
+ return {
1590
+ x: Math.min(x1, x2),
1591
+ y: Math.min(y1, y2),
1592
+ width: Math.abs(x2 - x1),
1593
+ height: Math.abs(y2 - y1)
1594
+ };
1595
+ } catch {
1596
+ return null;
1597
+ }
1598
+ }
1599
+ /** Call a function on a RemoteObject */
1600
+ async callFunctionOn(objectId, functionDeclaration, args = []) {
1601
+ const result = await this.conn.send("Runtime.callFunctionOn", {
1602
+ objectId,
1603
+ functionDeclaration,
1604
+ arguments: args.map((a) => ({ value: a })),
1605
+ returnByValue: true
1606
+ }, this.sessionId);
1607
+ if (result.exceptionDetails) {
1608
+ throw new Error(`CallFunctionOn error: ${result.exceptionDetails.text}`);
1609
+ }
1610
+ return result.result?.value;
1611
+ }
1612
+ /** Send a CDP command scoped to this page's session */
1613
+ async _cdpSend(method, params) {
1614
+ return this.conn.send(method, params, this.sessionId);
1615
+ }
1616
+ /** Subscribe to a CDP event on this page's session. Returns unsubscribe function. */
1617
+ _subscribe(event, handler) {
1618
+ return this.conn.subscribe(event, this.sessionId, handler);
1619
+ }
1620
+ // ── Private: Event Setup ────────────────────────────────────
1621
+ setupPageEvents() {
1622
+ this._subscriptions.push(
1623
+ this.conn.subscribe("Page.frameNavigated", this.sessionId, (params) => {
1624
+ const p = params;
1625
+ if (p.frame) {
1626
+ this._url = p.frame.url;
1627
+ }
1628
+ this._emit("framenavigated", this.mainFrame());
1629
+ })
1630
+ );
1631
+ this._subscriptions.push(
1632
+ this.conn.subscribe("Page.loadEventFired", this.sessionId, () => {
1633
+ this._loadState.loadFired = true;
1634
+ })
1635
+ );
1636
+ this._subscriptions.push(
1637
+ this.conn.subscribe("Page.domContentEventFired", this.sessionId, () => {
1638
+ this._loadState.domContentFired = true;
1639
+ })
1640
+ );
1641
+ this._subscriptions.push(
1642
+ this.conn.subscribe("Page.javascriptDialogOpening", this.sessionId, (params) => {
1643
+ const p = params;
1644
+ const dialog = {
1645
+ type: p.type,
1646
+ message: () => p.message,
1647
+ defaultValue: () => p.defaultValue,
1648
+ accept: async (text) => {
1649
+ await this.conn.send("Page.handleJavaScriptDialog", {
1650
+ accept: true,
1651
+ promptText: text
1652
+ }, this.sessionId);
1653
+ },
1654
+ dismiss: async () => {
1655
+ await this.conn.send("Page.handleJavaScriptDialog", {
1656
+ accept: false
1657
+ }, this.sessionId);
1658
+ }
1659
+ };
1660
+ this._emit("dialog", dialog);
1661
+ })
1662
+ );
1663
+ this._subscriptions.push(
1664
+ this.conn.subscribe("Page.fileChooserOpened", this.sessionId, async (params) => {
1665
+ const p = params;
1666
+ let selector = "";
1667
+ try {
1668
+ const result = await this.conn.send("DOM.describeNode", { backendNodeId: p.backendNodeId }, this.sessionId);
1669
+ const attrs = result.node?.attributes || [];
1670
+ const idIdx = attrs.indexOf("id");
1671
+ if (idIdx >= 0) selector = "#" + attrs[idIdx + 1];
1672
+ } catch {
1673
+ }
1674
+ if (!selector) {
1675
+ try {
1676
+ const result = await this.conn.send("DOM.resolveNode", { backendNodeId: p.backendNodeId }, this.sessionId);
1677
+ const evalResult = await this.conn.send("Runtime.callFunctionOn", {
1678
+ objectId: result.objectId,
1679
+ functionDeclaration: 'function() { return this.id || this.name || "" }',
1680
+ returnByValue: true
1681
+ }, this.sessionId);
1682
+ if (evalResult.result?.value) selector = "#" + evalResult.result.value;
1683
+ } catch {
1684
+ }
1685
+ }
1686
+ const fileChooser = {
1687
+ selector,
1688
+ isMultiple: p.mode === "selectMultiple",
1689
+ setFiles: async (files) => {
1690
+ const fileArray = Array.isArray(files) ? files : [files];
1691
+ await this.setInputFiles(selector || 'input[type="file"]', fileArray);
1692
+ }
1693
+ };
1694
+ this._emit("filechooser", fileChooser);
1695
+ })
1696
+ );
1697
+ }
1698
+ setupNetworkEvents() {
1699
+ this._subscriptions.push(
1700
+ this.conn.subscribe("Network.requestWillBeSent", this.sessionId, (params) => {
1701
+ const p = params;
1702
+ this.inflightRequests.add(p.requestId);
1703
+ this._storeNetworkRequest(p.requestId, {
1704
+ url: p.request.url,
1705
+ method: p.request.method,
1706
+ headers: p.request.headers,
1707
+ postData: p.request.postData ?? null,
1708
+ resourceType: p.type
1709
+ });
1710
+ this._emit("request", createXBRequest(
1711
+ null,
1712
+ {
1713
+ requestId: p.requestId,
1714
+ url: p.request.url,
1715
+ method: p.request.method,
1716
+ headers: p.request.headers,
1717
+ postData: p.request.postData ?? null,
1718
+ resourceType: p.type
1719
+ }
1720
+ ));
1721
+ this.checkNetworkIdle();
1722
+ })
1723
+ );
1724
+ this._subscriptions.push(
1725
+ this.conn.subscribe("Network.responseReceived", this.sessionId, (params) => {
1726
+ const p = params;
1727
+ this._storeNetworkResponse(p.requestId, {
1728
+ status: p.response.status,
1729
+ url: p.response.url,
1730
+ headers: p.response.headers
1731
+ });
1732
+ this._emit("response", createXBResponse(
1733
+ { requestId: p.requestId, status: p.response.status, url: p.response.url, headers: p.response.headers },
1734
+ this.conn,
1735
+ this.sessionId
1736
+ ));
1737
+ })
1738
+ );
1739
+ this._subscriptions.push(
1740
+ this.conn.subscribe("Network.loadingFinished", this.sessionId, (params) => {
1741
+ const p = params;
1742
+ this.inflightRequests.delete(p.requestId);
1743
+ this._emit("requestfinished", p);
1744
+ this.checkNetworkIdle();
1745
+ })
1746
+ );
1747
+ this._subscriptions.push(
1748
+ this.conn.subscribe("Network.loadingFailed", this.sessionId, (params) => {
1749
+ const p = params;
1750
+ this.inflightRequests.delete(p.requestId);
1751
+ this.checkNetworkIdle();
1752
+ })
1753
+ );
1754
+ }
1755
+ setupConsoleEvents() {
1756
+ this._subscriptions.push(
1757
+ this.conn.subscribe("Runtime.consoleAPICalled", this.sessionId, (params) => {
1758
+ const p = params;
1759
+ const text = p.args.map((a) => {
1760
+ if (a.value !== void 0) return String(a.value);
1761
+ return a.description ?? "";
1762
+ }).join(" ");
1763
+ const location = p.stackTrace?.callFrames?.[0] ? {
1764
+ url: p.stackTrace.callFrames[0].url,
1765
+ lineNumber: p.stackTrace.callFrames[0].lineNumber,
1766
+ columnNumber: p.stackTrace.callFrames[0].columnNumber
1767
+ } : { url: "", lineNumber: 0, columnNumber: 0 };
1768
+ const msg = {
1769
+ type: () => p.type,
1770
+ text: () => text,
1771
+ location: () => location
1772
+ };
1773
+ this._emit("console", msg);
1774
+ })
1775
+ );
1776
+ }
1777
+ checkNetworkIdle() {
1778
+ if (this.inflightRequests.size === 0) {
1779
+ if (this.networkIdleTimer) clearTimeout(this.networkIdleTimer);
1780
+ this.networkIdleTimer = setTimeout(() => {
1781
+ if (this.inflightRequests.size === 0) {
1782
+ this._loadState.networkIdle = true;
1783
+ if (this.networkIdleResolve) {
1784
+ this.networkIdleResolve();
1785
+ this.networkIdleResolve = null;
1786
+ }
1787
+ }
1788
+ }, _XBPageImpl.NETWORK_IDLE_MS);
1789
+ } else {
1790
+ if (this.networkIdleTimer) {
1791
+ clearTimeout(this.networkIdleTimer);
1792
+ this.networkIdleTimer = null;
1793
+ }
1794
+ }
1795
+ }
1796
+ // ── Network Data Store (for waitForResponse/waitForRequest) ──
1797
+ _networkResponses = /* @__PURE__ */ new Map();
1798
+ _networkRequests = /* @__PURE__ */ new Map();
1799
+ _routeHandlers = [];
1800
+ _interceptionEnabled = false;
1801
+ /** Store network data — called by browser.ts installNetworkCapture or internal event handlers */
1802
+ _storeNetworkRequest(requestId, data) {
1803
+ this._networkRequests.set(requestId, { requestId, ...data });
1804
+ }
1805
+ _storeNetworkResponse(requestId, data) {
1806
+ this._networkResponses.set(requestId, { requestId, ...data });
1807
+ }
1808
+ // ── waitForResponse ─────────────────────────────────────────
1809
+ async waitForResponse(urlOrPredicate, opts = {}) {
1810
+ const timeout = opts.timeout ?? 3e4;
1811
+ const predicate = createResponsePredicate(urlOrPredicate);
1812
+ for (const [, data] of this._networkResponses) {
1813
+ const response = createXBResponse(data, this.conn, this.sessionId);
1814
+ if (predicate(response)) return response;
1815
+ }
1816
+ return new Promise((resolve, reject) => {
1817
+ const timer = setTimeout(() => {
1818
+ this._emitter.removeListener("response", handler);
1819
+ reject(new Error(`waitForResponse timed out after ${timeout}ms`));
1820
+ }, timeout);
1821
+ const handler = (params) => {
1822
+ let response;
1823
+ const respObj = params;
1824
+ if (respObj.response) {
1825
+ const data = {
1826
+ requestId: respObj.requestId || "",
1827
+ status: respObj.response.status || 0,
1828
+ url: respObj.response.url || "",
1829
+ headers: respObj.response.headers || {}
1830
+ };
1831
+ response = createXBResponse(data, this.conn, this.sessionId);
1832
+ } else if (typeof params.status === "function") {
1833
+ response = params;
1834
+ } else {
1835
+ return;
1836
+ }
1837
+ if (predicate(response)) {
1838
+ clearTimeout(timer);
1839
+ this._emitter.removeListener("response", handler);
1840
+ resolve(response);
1841
+ }
1842
+ };
1843
+ this._emitter.on("response", handler);
1844
+ });
1845
+ }
1846
+ // ── waitForRequest ──────────────────────────────────────────
1847
+ async waitForRequest(urlOrPredicate, opts = {}) {
1848
+ const timeout = opts.timeout ?? 3e4;
1849
+ const predicate = createRequestPredicate(urlOrPredicate);
1850
+ for (const [, data] of this._networkRequests) {
1851
+ const request = createXBRequest(this, data);
1852
+ if (predicate(request)) return request;
1853
+ }
1854
+ return new Promise((resolve, reject) => {
1855
+ const timer = setTimeout(() => {
1856
+ this._emitter.removeListener("request", handler);
1857
+ reject(new Error(`waitForRequest timed out after ${timeout}ms`));
1858
+ }, timeout);
1859
+ const handler = (params) => {
1860
+ let request;
1861
+ const reqObj = params;
1862
+ if (reqObj.request) {
1863
+ request = createXBRequest(this, {
1864
+ requestId: reqObj.requestId || "",
1865
+ url: reqObj.request.url || "",
1866
+ method: reqObj.request.method || "",
1867
+ headers: reqObj.request.headers || {},
1868
+ postData: reqObj.request.postData ?? null,
1869
+ resourceType: reqObj.type || ""
1870
+ });
1871
+ } else if (typeof params.url === "function") {
1872
+ request = params;
1873
+ } else {
1874
+ return;
1875
+ }
1876
+ if (predicate(request)) {
1877
+ clearTimeout(timer);
1878
+ this._emitter.removeListener("request", handler);
1879
+ resolve(request);
1880
+ }
1881
+ };
1882
+ this._emitter.on("request", handler);
1883
+ });
1884
+ }
1885
+ // ── waitForURL ──────────────────────────────────────────────
1886
+ async waitForURL(url, opts = {}) {
1887
+ const timeout = opts.timeout ?? 3e4;
1888
+ const checkFn = typeof url === "function" ? url : typeof url === "string" ? (current) => matchGlob(url, current) : (current) => url.test(current);
1889
+ if (checkFn(this._url)) return;
1890
+ return new Promise((resolve, reject) => {
1891
+ const timer = setTimeout(() => {
1892
+ this._emitter.removeListener("framenavigated", handler);
1893
+ reject(new Error(`waitForURL timed out after ${timeout}ms`));
1894
+ }, timeout);
1895
+ const handler = () => {
1896
+ if (checkFn(this._url)) {
1897
+ clearTimeout(timer);
1898
+ this._emitter.removeListener("framenavigated", handler);
1899
+ resolve();
1900
+ }
1901
+ };
1902
+ this._emitter.on("framenavigated", handler);
1903
+ });
1904
+ }
1905
+ // ── route / unroute ─────────────────────────────────────────
1906
+ async route(url, handler) {
1907
+ const regex = typeof url === "string" ? globToRegex(url) : url;
1908
+ this._routeHandlers.push({ pattern: String(url), regex, handler });
1909
+ if (!this._interceptionEnabled) {
1910
+ this._interceptionEnabled = true;
1911
+ await this.conn.send("Fetch.enable", {
1912
+ patterns: [{ urlPattern: "*", requestStage: "Request" }]
1913
+ }, this.sessionId);
1914
+ this._subscriptions.push(
1915
+ this.conn.subscribe("Fetch.requestPaused", this.sessionId, (params) => {
1916
+ this._handleRequestPaused(params);
1917
+ })
1918
+ );
1919
+ }
1920
+ }
1921
+ async unroute(url, handler) {
1922
+ const regex = typeof url === "string" ? globToRegex(url) : url;
1923
+ this._routeHandlers = this._routeHandlers.filter(
1924
+ (h) => !(regex.source === h.regex.source && (!handler || h.handler === handler))
1925
+ );
1926
+ if (this._routeHandlers.length === 0 && this._interceptionEnabled) {
1927
+ this._interceptionEnabled = false;
1928
+ await this.conn.send("Fetch.disable", void 0, this.sessionId).catch(() => {
1929
+ });
1930
+ }
1931
+ }
1932
+ async _handleRequestPaused(params) {
1933
+ const requestUrl = params.request.url;
1934
+ for (const { regex, handler } of this._routeHandlers) {
1935
+ if (regex.test(requestUrl)) {
1936
+ this._emit("request", createXBRequest(
1937
+ null,
1938
+ {
1939
+ requestId: params.requestId,
1940
+ url: params.request.url,
1941
+ method: params.request.method,
1942
+ headers: params.request.headers,
1943
+ postData: params.request.postData ?? null,
1944
+ resourceType: params.resourceType
1945
+ }
1946
+ ));
1947
+ const route = createXBRouteFetch(this.conn, this.sessionId, params, this._emitter);
1948
+ try {
1949
+ await handler(route);
1950
+ } catch {
1951
+ await this.conn.send("Fetch.continueRequest", {
1952
+ requestId: params.requestId
1953
+ }, this.sessionId).catch(() => {
1954
+ });
1955
+ }
1956
+ return;
1957
+ }
1958
+ }
1959
+ await this.conn.send("Fetch.continueRequest", {
1960
+ requestId: params.requestId
1961
+ }, this.sessionId).catch(() => {
1962
+ });
1963
+ }
1964
+ // ── setInputFiles ───────────────────────────────────────────
1965
+ async setInputFiles(selector, files) {
1966
+ const fileArr = Array.isArray(files) ? files : [files];
1967
+ const fileList = fileArr.map((f) => ({
1968
+ name: f.name,
1969
+ type: f.mimeType,
1970
+ dataBase64: f.buffer.toString("base64")
1971
+ }));
1972
+ await this.evaluate(`
1973
+ (function() {
1974
+ var selector = ${JSON.stringify(selector)};
1975
+ var input = document.querySelector(selector);
1976
+ if (!input) throw new Error('Element not found: ' + selector);
1977
+
1978
+ var fileList = ${JSON.stringify(fileList)};
1979
+ var dt = new DataTransfer();
1980
+
1981
+ fileList.forEach(function(f) {
1982
+ var binary = atob(f.dataBase64);
1983
+ var bytes = new Uint8Array(binary.length);
1984
+ for (var i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
1985
+ var blob = new Blob([bytes], { type: f.type });
1986
+ var file = new File([blob], f.name, { type: f.type });
1987
+ dt.items.add(file);
1988
+ });
1989
+
1990
+ input.files = dt.files;
1991
+ input.dispatchEvent(new Event('input', { bubbles: true }));
1992
+ input.dispatchEvent(new Event('change', { bubbles: true }));
1993
+ })()
1994
+ `);
1995
+ }
1996
+ // ── dragAndDrop ─────────────────────────────────────────────
1997
+ async dragAndDrop(source, target) {
1998
+ const sourceRect = await this.evaluate(`
1999
+ (function() {
2000
+ const el = document.querySelector(${JSON.stringify(source)});
2001
+ if (!el) throw new Error('Source not found: ${source}');
2002
+ el.scrollIntoView({ behavior: 'instant', block: 'center' });
2003
+ const r = el.getBoundingClientRect();
2004
+ return { x: r.x, y: r.y, width: r.width, height: r.height };
2005
+ })()
2006
+ `);
2007
+ const targetRect = await this.evaluate(`
2008
+ (function() {
2009
+ const el = document.querySelector(${JSON.stringify(target)});
2010
+ if (!el) throw new Error('Target not found: ${target}');
2011
+ el.scrollIntoView({ behavior: 'instant', block: 'center' });
2012
+ const r = el.getBoundingClientRect();
2013
+ return { x: r.x, y: r.y, width: r.width, height: r.height };
2014
+ })()
2015
+ `);
2016
+ const sx = sourceRect.x + sourceRect.width / 2;
2017
+ const sy = sourceRect.y + sourceRect.height / 2;
2018
+ const tx = targetRect.x + targetRect.width / 2;
2019
+ const ty = targetRect.y + targetRect.height / 2;
2020
+ try {
2021
+ await this.conn.send("Input.dispatchDragEvent", {
2022
+ type: "dragStart",
2023
+ x: sx,
2024
+ y: sy,
2025
+ data: { items: [], dragOperations: ["copy", "move", "link"] }
2026
+ }, this.sessionId);
2027
+ await this.conn.send("Input.dispatchDragEvent", {
2028
+ type: "dragOver",
2029
+ x: tx,
2030
+ y: ty,
2031
+ data: { items: [], dragOperations: ["copy", "move", "link"] }
2032
+ }, this.sessionId);
2033
+ await this.conn.send("Input.dispatchDragEvent", {
2034
+ type: "drop",
2035
+ x: tx,
2036
+ y: ty,
2037
+ data: { items: [], dragOperations: ["copy", "move", "link"] }
2038
+ }, this.sessionId);
2039
+ await this.conn.send("Input.dispatchDragEvent", {
2040
+ type: "dragCancel",
2041
+ x: sx,
2042
+ y: sy,
2043
+ data: { items: [], dragOperations: ["copy", "move", "link"] }
2044
+ }, this.sessionId);
2045
+ } catch {
2046
+ await this.mouse.move(sx, sy);
2047
+ await this.mouse.down();
2048
+ await this.mouse.move(tx, ty, { steps: 10 });
2049
+ await this.mouse.up();
2050
+ }
2051
+ }
2052
+ // ── setOfflineMode ──────────────────────────────────────────
2053
+ async setOfflineMode(offline) {
2054
+ await this.conn.send("Network.emulateNetworkConditions", {
2055
+ offline,
2056
+ latency: 0,
2057
+ downloadThroughput: offline ? 0 : -1,
2058
+ uploadThroughput: offline ? 0 : -1
2059
+ }, this.sessionId);
2060
+ }
2061
+ };
2062
+
2063
+ // src/cdp-driver/cdp-session.ts
2064
+ var XBCDPSessionImpl = class {
2065
+ conn;
2066
+ sessionId;
2067
+ constructor(conn, sessionId) {
2068
+ this.conn = conn;
2069
+ this.sessionId = sessionId;
2070
+ }
2071
+ async send(method, params) {
2072
+ return this.conn.send(method, params, this.sessionId);
2073
+ }
2074
+ on(event, handler) {
2075
+ this.conn.on(event, (params, sid) => {
2076
+ if (sid === this.sessionId || !this.sessionId && !sid) {
2077
+ handler(params);
2078
+ }
2079
+ });
2080
+ }
2081
+ off(event, handler) {
2082
+ this.conn.off(event, handler);
2083
+ }
2084
+ async detach() {
2085
+ if (!this.sessionId) return;
2086
+ }
2087
+ };
2088
+
2089
+ // src/cdp-driver/context.ts
2090
+ var XBContextImpl = class {
2091
+ conn;
2092
+ _emitter = new EventEmitter2();
2093
+ _browser;
2094
+ contextId;
2095
+ _pages = [];
2096
+ closed = false;
2097
+ options;
2098
+ targetAttachedHandler = null;
2099
+ _initScripts = [];
2100
+ constructor(conn, contextId, browser, opts) {
2101
+ this.conn = conn;
2102
+ this.contextId = contextId;
2103
+ this._browser = browser;
2104
+ this.options = opts;
2105
+ this.setupAutoAttach();
2106
+ }
2107
+ async newPage() {
2108
+ if (this.closed) throw new Error("Context is closed");
2109
+ const { targetId } = await this._browser._createTarget(this.contextId);
2110
+ const sessionId = await this._browser._attachToTarget(targetId);
2111
+ const page = new XBPageImpl(this.conn, sessionId, targetId, this, this._browser);
2112
+ await page._init();
2113
+ this._pages.push(page);
2114
+ this.forwardPageEvents(page);
2115
+ if (this.options.viewport) {
2116
+ await page.setViewportSize(this.options.viewport).catch(() => {
2117
+ });
2118
+ }
2119
+ if (this.options.userAgent) {
2120
+ await page._setUserAgent(this.options.userAgent);
2121
+ }
2122
+ if (this.options.extraHTTPHeaders) {
2123
+ await page._setExtraHTTPHeaders(this.options.extraHTTPHeaders);
2124
+ }
2125
+ for (const script of this._initScripts) {
2126
+ await page.addInitScript(script);
2127
+ }
2128
+ return page;
2129
+ }
2130
+ pages() {
2131
+ return [...this._pages];
2132
+ }
2133
+ browser() {
2134
+ return this._browser;
2135
+ }
2136
+ async close() {
2137
+ if (this.closed) return;
2138
+ this.closed = true;
2139
+ for (const page of this._pages) {
2140
+ await page.close().catch(() => {
2141
+ });
2142
+ }
2143
+ this._pages = [];
2144
+ if (this.targetAttachedHandler) {
2145
+ this.conn.off("Target.attachedToTarget", this.targetAttachedHandler);
2146
+ this.targetAttachedHandler = null;
2147
+ }
2148
+ if (this.contextId && this.contextId !== "default") {
2149
+ await this.conn.send("Target.disposeBrowserContext", {
2150
+ browserContextId: this.contextId
2151
+ }).catch(() => {
2152
+ });
2153
+ }
2154
+ this._browser._removeContext(this.contextId);
2155
+ }
2156
+ async newCDPSession(page) {
2157
+ if (page instanceof XBPageImpl) {
2158
+ return new XBCDPSessionImpl(this.conn, page.sessionId);
2159
+ }
2160
+ return new XBCDPSessionImpl(this.conn);
2161
+ }
2162
+ async addInitScript(script) {
2163
+ this._initScripts.push(script);
2164
+ for (const page of this._pages) {
2165
+ await page.addInitScript(script);
2166
+ }
2167
+ }
2168
+ // ── Cookies ─────────────────────────────────────────────────
2169
+ async cookies(urls) {
2170
+ const urlList = typeof urls === "string" ? [urls] : urls;
2171
+ const result = await this.conn.send("Network.getCookies", urlList ? { urls: urlList } : void 0);
2172
+ return result.cookies;
2173
+ }
2174
+ async addCookies(cookies) {
2175
+ const cdpCookies = cookies.map((c) => ({
2176
+ name: c.name,
2177
+ value: c.value,
2178
+ domain: c.domain,
2179
+ path: c.path || "/",
2180
+ expires: c.expires,
2181
+ httpOnly: c.httpOnly,
2182
+ secure: c.secure,
2183
+ sameSite: c.sameSite
2184
+ }));
2185
+ await this.conn.send("Network.setCookies", { cookies: cdpCookies });
2186
+ }
2187
+ async clearCookies() {
2188
+ await this.conn.send("Network.clearBrowserCookies");
2189
+ }
2190
+ on(event, handler) {
2191
+ this._emitter.on(event, handler);
2192
+ }
2193
+ off(event, handler) {
2194
+ this._emitter.off(event, handler);
2195
+ }
2196
+ /**
2197
+ * Register a page that was attached to an existing target (discovered via
2198
+ * Target.getTargets). Used by XBBrowserImpl.discoverContexts() to wire up
2199
+ * pages from the user's existing browser session into the context wrapper
2200
+ * so they appear in `context.pages()` and can be reused by plugins.
2201
+ */
2202
+ _addDiscoveredPage(page) {
2203
+ const exists = this._pages.some((p) => p._targetId === page._targetId);
2204
+ if (exists) return;
2205
+ this._pages.push(page);
2206
+ this.forwardPageEvents(page);
2207
+ }
2208
+ // ── Private ─────────────────────────────────────────────────
2209
+ /** Forward page-level events (request, response, etc.) to context listeners */
2210
+ forwardPageEvents(page) {
2211
+ const forward = (event) => {
2212
+ page.on(event, (...args) => {
2213
+ this._emitter.emit(event, ...args);
2214
+ });
2215
+ };
2216
+ forward("request");
2217
+ forward("response");
2218
+ forward("requestfailed");
2219
+ forward("requestfinished");
2220
+ }
2221
+ setupAutoAttach() {
2222
+ this.targetAttachedHandler = (paramsRaw) => {
2223
+ const params = paramsRaw;
2224
+ if (this.contextId !== "default" && params.targetInfo.browserContextId !== this.contextId) return;
2225
+ if (params.targetInfo.type !== "page") return;
2226
+ const exists = this._pages.some(
2227
+ (p) => p._targetId === params.targetInfo.targetId
2228
+ );
2229
+ if (exists) return;
2230
+ const page = new XBPageImpl(
2231
+ this.conn,
2232
+ params.sessionId,
2233
+ params.targetInfo.targetId,
2234
+ this,
2235
+ this._browser
2236
+ );
2237
+ this.conn.send("Runtime.runIfWaitingForDebugger", void 0, params.sessionId).catch(() => {
2238
+ });
2239
+ page._init().then(async () => {
2240
+ for (const script of this._initScripts) {
2241
+ await page.addInitScript(script).catch(() => {
2242
+ });
2243
+ }
2244
+ this._pages.push(page);
2245
+ this.forwardPageEvents(page);
2246
+ this._emitter.emit("page", page);
2247
+ });
2248
+ };
2249
+ this.conn.on("Target.attachedToTarget", this.targetAttachedHandler);
2250
+ }
2251
+ };
2252
+
2253
+ // src/cdp-driver/browser.ts
2254
+ var XBBrowserImpl = class {
2255
+ conn;
2256
+ _emitter = new EventEmitter3();
2257
+ _contexts = /* @__PURE__ */ new Map();
2258
+ _disconnected = false;
2259
+ childProcess = null;
2260
+ tmpDir;
2261
+ _exitHandler = null;
2262
+ /**
2263
+ * Original CDP endpoint (HTTP or ws URL) used to construct this browser.
2264
+ * Used by discoverContexts() as a fallback to HTTP /json/list when
2265
+ * Target.getTargets doesn't return page-type targets (e.g. cdp-tunnel proxy).
2266
+ */
2267
+ cdpEndpoint;
2268
+ constructor(conn, childProcess, tmpDir, cdpEndpoint) {
2269
+ this.conn = conn;
2270
+ this.childProcess = childProcess ?? null;
2271
+ this.tmpDir = tmpDir;
2272
+ this.cdpEndpoint = cdpEndpoint;
2273
+ conn.on("disconnect", () => {
2274
+ this._disconnected = true;
2275
+ this._emitter.emit("disconnected");
2276
+ });
2277
+ if (this.childProcess) {
2278
+ this._exitHandler = () => {
2279
+ try {
2280
+ if (this.childProcess?.exitCode === null) {
2281
+ this.childProcess.kill("SIGKILL");
2282
+ }
2283
+ } catch {
2284
+ }
2285
+ if (this.tmpDir) {
2286
+ try {
2287
+ const { rmSync } = __require("fs");
2288
+ rmSync(this.tmpDir, { recursive: true, force: true });
2289
+ } catch {
2290
+ }
2291
+ }
2292
+ };
2293
+ process.on("exit", this._exitHandler);
2294
+ }
2295
+ }
2296
+ get disconnected() {
2297
+ return this._disconnected;
2298
+ }
2299
+ /** The underlying CDP connection (for advanced use) */
2300
+ get connection() {
2301
+ return this.conn;
2302
+ }
2303
+ async close() {
2304
+ if (this._disconnected) return;
2305
+ this._disconnected = true;
2306
+ for (const [, info] of this._contexts) {
2307
+ await info.context.close().catch(() => {
2308
+ });
2309
+ }
2310
+ this._contexts.clear();
2311
+ if (this._exitHandler) {
2312
+ process.removeListener("exit", this._exitHandler);
2313
+ this._exitHandler = null;
2314
+ }
2315
+ if (this.childProcess) {
2316
+ const { killChrome: killChrome2 } = await import("./launcher-QUJ4M2VS.js");
2317
+ await killChrome2(this.childProcess, this.tmpDir);
2318
+ }
2319
+ await this.conn.close();
2320
+ this._emitter.emit("disconnected");
2321
+ }
2322
+ async newContext(opts = {}) {
2323
+ if (this._disconnected) {
2324
+ throw new Error("Browser is disconnected");
2325
+ }
2326
+ let contextId = "default";
2327
+ try {
2328
+ const result = await this.conn.send(
2329
+ "Target.createBrowserContext",
2330
+ { disposeOnDetach: true },
2331
+ void 0,
2332
+ 1e4
2333
+ // 10s timeout instead of default 30s
2334
+ );
2335
+ contextId = result.browserContextId;
2336
+ } catch {
2337
+ }
2338
+ const context = new XBContextImpl(this.conn, contextId, this, opts);
2339
+ context.on("page", (page) => {
2340
+ this._emitter.emit("page", page);
2341
+ });
2342
+ this._contexts.set(contextId, {
2343
+ contextId,
2344
+ context
2345
+ });
2346
+ if (this.childProcess) {
2347
+ this._enableAutoAttach().catch(() => {
2348
+ });
2349
+ }
2350
+ return context;
2351
+ }
2352
+ contexts() {
2353
+ return Array.from(this._contexts.values()).map((info) => info.context);
2354
+ }
2355
+ on(event, handler) {
2356
+ this._emitter.on(event, handler);
2357
+ }
2358
+ off(event, handler) {
2359
+ this._emitter.off(event, handler);
2360
+ }
2361
+ /** Called by context.close() to remove from registry */
2362
+ _removeContext(contextId) {
2363
+ this._contexts.delete(contextId);
2364
+ }
2365
+ // ── CDP helpers exposed for context/page ────────────────────
2366
+ /** Attach to a target and get a session ID for flat protocol */
2367
+ async _attachToTarget(targetId) {
2368
+ const result = await this.conn.send(
2369
+ "Target.attachToTarget",
2370
+ { targetId, flatten: true }
2371
+ );
2372
+ return result.sessionId;
2373
+ }
2374
+ /** Detach from a target session */
2375
+ async _detachFromTarget(sessionId) {
2376
+ await this.conn.send("Target.detachFromTarget", { sessionId });
2377
+ }
2378
+ /**
2379
+ * Derive the HTTP /json base URL from the original cdpEndpoint for use
2380
+ * as a fallback when Target.getTargets doesn't return page targets.
2381
+ * Supports both http:// and ws:// input formats.
2382
+ */
2383
+ _httpFallbackURL() {
2384
+ if (!this.cdpEndpoint) return void 0;
2385
+ if (this.cdpEndpoint.startsWith("http://") || this.cdpEndpoint.startsWith("https://")) {
2386
+ return this.cdpEndpoint;
2387
+ }
2388
+ if (this.cdpEndpoint.startsWith("ws://") || this.cdpEndpoint.startsWith("wss://")) {
2389
+ const url = this.cdpEndpoint.replace(/^ws/, "http");
2390
+ const slashIdx = url.indexOf("/", url.indexOf("//") + 2);
2391
+ return slashIdx >= 0 ? url.substring(0, slashIdx) : url;
2392
+ }
2393
+ return void 0;
2394
+ }
2395
+ /** Create a new page target within a browser context */
2396
+ async _createTarget(contextId, url = "about:blank") {
2397
+ const params = { url };
2398
+ if (contextId && contextId !== "default") {
2399
+ params.browserContextId = contextId;
2400
+ }
2401
+ return this.conn.send("Target.createTarget", params);
2402
+ }
2403
+ /** Close a target */
2404
+ async _closeTarget(targetId) {
2405
+ await this.conn.send("Target.closeTarget", { targetId });
2406
+ }
2407
+ /** Enable auto-attach for new targets */
2408
+ async _enableAutoAttach() {
2409
+ await this.conn.send("Target.setAutoAttach", {
2410
+ autoAttach: true,
2411
+ waitForDebuggerOnStart: false,
2412
+ flatten: true
2413
+ });
2414
+ }
2415
+ /**
2416
+ * Discover existing browser contexts and pages via Target.getTargets.
2417
+ *
2418
+ * For CDP tunnel connections (cdp-tunnel, attach scenarios), the
2419
+ * Target.attachedToTarget auto-attach flow is unreliable. Without this
2420
+ * call, `b.contexts()` would return [] and callers would fall back to
2421
+ * `b.newContext()` — which creates an isolated context with NO cookies
2422
+ * shared with the user's existing browser session (causing login failures).
2423
+ *
2424
+ * This method:
2425
+ * 1. Queries Target.getTargets to enumerate all page targets
2426
+ * 2. Groups them by browserContextId
2427
+ * 3. Attaches to each existing page via Target.attachToTarget
2428
+ * 4. Wraps the discovered pages in a XBContextImpl and registers it in
2429
+ * this._contexts so `contexts()` returns the user's actual contexts
2430
+ * 5. Enables Target.setAutoAttach for future pages
2431
+ *
2432
+ * No-op for self-launched browsers (they already populated contexts via
2433
+ * newContext() + childProcess-gated auto-attach).
2434
+ */
2435
+ async discoverContexts() {
2436
+ if (this._disconnected) return;
2437
+ let targetInfos = [];
2438
+ try {
2439
+ const result = await this.conn.send(
2440
+ "Target.getTargets"
2441
+ );
2442
+ targetInfos = result.targetInfos ?? [];
2443
+ } catch {
2444
+ return;
2445
+ }
2446
+ const pageTargets = targetInfos.filter((t) => t.type === "page");
2447
+ const httpFallbackUrl = this._httpFallbackURL();
2448
+ if (pageTargets.length === 0 && httpFallbackUrl) {
2449
+ console.log(`[discoverContexts] Target.getTargets returned ${targetInfos.length} targets (0 page type). Falling back to HTTP /json/list at ${httpFallbackUrl}`);
2450
+ try {
2451
+ const { getCDPTargets: getCDPTargets3 } = await import("./launcher-QUJ4M2VS.js");
2452
+ const httpPages = await getCDPTargets3(httpFallbackUrl);
2453
+ console.log(`[discoverContexts] HTTP /json/list returned ${httpPages.length} pages`);
2454
+ for (const p of httpPages) {
2455
+ if (p.type !== "page") continue;
2456
+ if (!p.url || p.url.startsWith("chrome://") || p.url.startsWith("devtools://")) continue;
2457
+ targetInfos.push({
2458
+ targetId: p.id,
2459
+ type: "page",
2460
+ url: p.url,
2461
+ title: p.title
2462
+ });
2463
+ }
2464
+ console.log(`[discoverContexts] After HTTP fallback: ${targetInfos.length} total targets, ${targetInfos.filter((t) => t.type === "page").length} pages`);
2465
+ } catch (err) {
2466
+ console.log(`[discoverContexts] HTTP fallback failed: ${err.message}`);
2467
+ }
2468
+ }
2469
+ const pagesByContext = /* @__PURE__ */ new Map();
2470
+ for (const t of targetInfos) {
2471
+ if (t.type !== "page") continue;
2472
+ if (!t.url || t.url.startsWith("chrome://") || t.url.startsWith("devtools://")) {
2473
+ continue;
2474
+ }
2475
+ const ctxId = t.browserContextId || "default";
2476
+ if (!pagesByContext.has(ctxId)) pagesByContext.set(ctxId, []);
2477
+ pagesByContext.get(ctxId).push(t);
2478
+ }
2479
+ for (const [ctxId, pages] of pagesByContext) {
2480
+ if (this._contexts.has(ctxId)) continue;
2481
+ const context = new XBContextImpl(this.conn, ctxId, this, {});
2482
+ for (const p of pages) {
2483
+ try {
2484
+ const sessionId = await this._attachToTarget(p.targetId);
2485
+ const page = new XBPageImpl(this.conn, sessionId, p.targetId, context, this);
2486
+ await page._init();
2487
+ context._addDiscoveredPage(page);
2488
+ } catch {
2489
+ }
2490
+ }
2491
+ this._contexts.set(ctxId, { contextId: ctxId, context });
2492
+ }
2493
+ try {
2494
+ await this.conn.send("Target.setAutoAttach", {
2495
+ autoAttach: true,
2496
+ waitForDebuggerOnStart: false,
2497
+ flatten: true
2498
+ });
2499
+ } catch {
2500
+ }
2501
+ }
2502
+ };
2503
+
2504
+ // src/cdp-driver/connection.ts
2505
+ import { EventEmitter as EventEmitter4 } from "events";
2506
+ import { WebSocket } from "ws";
2507
+ var CDPConnection = class extends EventEmitter4 {
2508
+ ws;
2509
+ nextId = 1;
2510
+ pending = /* @__PURE__ */ new Map();
2511
+ closed = false;
2512
+ closeReason = null;
2513
+ /** Default session ID for flat session protocol (Target.attachToTarget) */
2514
+ defaultSessionId;
2515
+ constructor(wsOrUrl, sessionId) {
2516
+ super();
2517
+ this.defaultSessionId = sessionId;
2518
+ if (typeof wsOrUrl === "string") {
2519
+ this.ws = new WebSocket(wsOrUrl);
2520
+ } else {
2521
+ this.ws = wsOrUrl;
2522
+ }
2523
+ this.bindWebSocket();
2524
+ }
2525
+ /** Wait for the connection to be fully open */
2526
+ async ready() {
2527
+ if (this.ws.readyState === WebSocket.OPEN) return;
2528
+ if (this.ws.readyState === WebSocket.CLOSED || this.ws.readyState === WebSocket.CLOSING) {
2529
+ throw new Error(`WebSocket already closed: ${this.closeReason ?? "unknown"}`);
2530
+ }
2531
+ return new Promise((resolve, reject) => {
2532
+ const onOpen = () => {
2533
+ this.ws.off("error", onError);
2534
+ resolve();
2535
+ };
2536
+ const onError = (err) => {
2537
+ this.ws.off("open", onOpen);
2538
+ reject(err);
2539
+ };
2540
+ this.ws.once("open", onOpen);
2541
+ this.ws.once("error", onError);
2542
+ });
2543
+ }
2544
+ /** Is the underlying WebSocket alive? */
2545
+ get isOpen() {
2546
+ return !this.closed && this.ws.readyState === WebSocket.OPEN;
2547
+ }
2548
+ /**
2549
+ * Send a CDP command and await its response.
2550
+ *
2551
+ * @param method — CDP domain.method (e.g. "Page.navigate")
2552
+ * @param params — method parameters
2553
+ * @param sessionId — optional flat session ID for sub-targets
2554
+ * @param timeoutMs — response timeout (default: 30s)
2555
+ * @returns the `result` field from the CDP response
2556
+ */
2557
+ async send(method, params, sessionId, timeoutMs = 3e4) {
2558
+ if (this.closed) {
2559
+ throw new Error(`CDP connection closed: ${this.closeReason ?? "unknown"}`);
2560
+ }
2561
+ if (!this.isOpen) {
2562
+ throw new Error(`CDP connection not open (state: ${this.ws.readyState})`);
2563
+ }
2564
+ const id = this.nextId++;
2565
+ const sid = sessionId ?? this.defaultSessionId;
2566
+ const message = { id, method };
2567
+ if (params !== void 0) message.params = params;
2568
+ if (sid !== void 0) message.sessionId = sid;
2569
+ return new Promise((resolve, reject) => {
2570
+ const timeout = setTimeout(() => {
2571
+ this.pending.delete(id);
2572
+ reject(new Error(`CDP timeout: ${method} (${timeoutMs}ms)`));
2573
+ }, timeoutMs);
2574
+ this.pending.set(id, {
2575
+ resolve: (v) => {
2576
+ clearTimeout(timeout);
2577
+ this.pending.delete(id);
2578
+ resolve(v);
2579
+ },
2580
+ reject: (err) => {
2581
+ clearTimeout(timeout);
2582
+ this.pending.delete(id);
2583
+ reject(err);
2584
+ },
2585
+ method,
2586
+ timeout
2587
+ });
2588
+ const data = JSON.stringify(message);
2589
+ try {
2590
+ this.ws.send(data);
2591
+ } catch (err) {
2592
+ clearTimeout(timeout);
2593
+ this.pending.delete(id);
2594
+ reject(new Error(`CDP send failed: ${method} \u2014 ${err instanceof Error ? err.message : String(err)}`));
2595
+ }
2596
+ });
2597
+ }
2598
+ /**
2599
+ * Subscribe to a CDP event.
2600
+ *
2601
+ * @param event — full event name (e.g. "Page.frameNavigated")
2602
+ * @param handler — called with the event params
2603
+ * @param sessionId — optional session filter
2604
+ */
2605
+ on(event, handler) {
2606
+ return super.on(event, handler);
2607
+ }
2608
+ once(event, handler) {
2609
+ return super.once(event, handler);
2610
+ }
2611
+ /** Remove an event listener */
2612
+ off(event, handler) {
2613
+ super.off(event, handler);
2614
+ return this;
2615
+ }
2616
+ /**
2617
+ * Subscribe to a CDP event for a specific session.
2618
+ * Returns an unsubscribe function.
2619
+ */
2620
+ subscribe(event, sessionId, handler) {
2621
+ const wrapper = (params, sid) => {
2622
+ if (sid === sessionId || !sessionId && !sid) handler(params);
2623
+ };
2624
+ this.on(event, wrapper);
2625
+ return () => this.off(event, wrapper);
2626
+ }
2627
+ /** Close the WebSocket */
2628
+ async close() {
2629
+ if (this.closed) return;
2630
+ this.closed = true;
2631
+ this.closeReason = "closed by caller";
2632
+ for (const [id, pending] of this.pending) {
2633
+ clearTimeout(pending.timeout);
2634
+ pending.reject(new Error(`Connection closed: ${pending.method}`));
2635
+ this.pending.delete(id);
2636
+ }
2637
+ if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {
2638
+ this.ws.close(1e3, "normal closure");
2639
+ }
2640
+ }
2641
+ /** Set the default session ID for flat protocol */
2642
+ setDefaultSessionId(sid) {
2643
+ this.defaultSessionId = sid;
2644
+ }
2645
+ // ── Private ─────────────────────────────────────────────────
2646
+ bindWebSocket() {
2647
+ this.ws.on("message", (raw) => {
2648
+ let msg;
2649
+ try {
2650
+ msg = JSON.parse(raw.toString());
2651
+ } catch {
2652
+ return;
2653
+ }
2654
+ if (msg.id !== void 0) {
2655
+ const pending = this.pending.get(msg.id);
2656
+ if (!pending) return;
2657
+ if (msg.error) {
2658
+ pending.reject(new CDPProtocolError(msg.error.code, msg.error.message, pending.method));
2659
+ } else {
2660
+ pending.resolve(msg.result ?? {});
2661
+ }
2662
+ return;
2663
+ }
2664
+ if (msg.method) {
2665
+ this.emit(msg.method, msg.params ?? {}, msg.sessionId);
2666
+ this.emit("*", msg.method, msg.params ?? {}, msg.sessionId);
2667
+ }
2668
+ });
2669
+ this.ws.on("close", (code, reason) => {
2670
+ if (this.closed) return;
2671
+ this.closed = true;
2672
+ this.closeReason = `WebSocket closed: ${code} ${reason?.toString() ?? ""}`.trim();
2673
+ for (const [id, pending] of this.pending) {
2674
+ clearTimeout(pending.timeout);
2675
+ pending.reject(new Error(`Connection closed: ${pending.method}`));
2676
+ this.pending.delete(id);
2677
+ }
2678
+ this.emit("disconnect");
2679
+ });
2680
+ this.ws.on("error", (err) => {
2681
+ if (this.closed) return;
2682
+ this.emit("ws-error", err);
2683
+ });
2684
+ }
2685
+ };
2686
+ var CDPProtocolError = class extends Error {
2687
+ code;
2688
+ method;
2689
+ data;
2690
+ constructor(code, message, method, data) {
2691
+ super(`CDP error [${code}] in ${method}: ${message}`);
2692
+ this.name = "CDPProtocolError";
2693
+ this.code = code;
2694
+ this.method = method;
2695
+ this.data = data;
2696
+ }
2697
+ };
2698
+
2699
+ // src/cdp-driver/index.ts
2700
+ async function launch(options = {}) {
2701
+ let wsEndpoint;
2702
+ let childProcess;
2703
+ let tmpDir;
2704
+ if (options.cdpEndpoint) {
2705
+ wsEndpoint = await connectToCDP(options.cdpEndpoint);
2706
+ } else {
2707
+ const result = await launchChrome({
2708
+ executablePath: options.executablePath,
2709
+ headless: options.headless,
2710
+ args: options.args,
2711
+ userDataDir: options.userDataDir,
2712
+ timeout: options.timeout,
2713
+ env: options.env
2714
+ });
2715
+ wsEndpoint = result.wsEndpoint;
2716
+ childProcess = result.process;
2717
+ tmpDir = result.tmpDir;
2718
+ }
2719
+ const conn = new CDPConnection(wsEndpoint);
2720
+ await conn.ready();
2721
+ const httpEndpoint = options.cdpEndpoint && !options.cdpEndpoint.startsWith("ws") ? options.cdpEndpoint : void 0;
2722
+ const browser = new XBBrowserImpl(conn, childProcess, tmpDir, httpEndpoint);
2723
+ return { browser, wsEndpoint };
2724
+ }
2725
+
2726
+ // src/cdp-interceptor/proxy.ts
2727
+ import { WebSocketServer, WebSocket as WebSocket2 } from "ws";
2728
+
2729
+ // src/cdp-interceptor/rules/shared.ts
2730
+ var PLAYWRIGHT_INTERNAL_MARKERS = [
2731
+ "__commonJS",
2732
+ "module.exports",
2733
+ "__require",
2734
+ "__toESM",
2735
+ "inject_utils"
2736
+ ];
2737
+ function isPlaywrightInternal(code) {
2738
+ return PLAYWRIGHT_INTERNAL_MARKERS.some((marker) => code.includes(marker));
2739
+ }
2740
+ function extractUserCode(ctx) {
2741
+ if (ctx.method === "Runtime.evaluate") {
2742
+ const expr = ctx.params.expression;
2743
+ if (typeof expr === "string") {
2744
+ if (isPlaywrightInternal(expr)) return null;
2745
+ return expr;
2746
+ }
2747
+ }
2748
+ if (ctx.method === "Runtime.callFunctionOn") {
2749
+ const decl = ctx.params.functionDeclaration;
2750
+ if (typeof decl === "string" && decl.includes("utilityScript.evaluate")) {
2751
+ return extractAllStrings(ctx.params.arguments);
2752
+ }
2753
+ if (typeof decl === "string") return decl;
2754
+ }
2755
+ return null;
2756
+ }
2757
+ function extractAllStrings(rawArgs) {
2758
+ if (!Array.isArray(rawArgs)) return null;
2759
+ const strings = [];
2760
+ for (const arg of rawArgs) {
2761
+ if (arg && typeof arg === "object" && "value" in arg) {
2762
+ const val = arg.value;
2763
+ if (typeof val === "string" && val.length > 5 && !["true", "false"].includes(val)) {
2764
+ strings.push(val);
2765
+ }
2766
+ }
2767
+ }
2768
+ return strings.length > 0 ? strings.join("\n") : null;
2769
+ }
2770
+
2771
+ // src/cdp-interceptor/rules/dom-mutation.ts
2772
+ var DOM_PATTERNS = [
2773
+ // ── P0: Value/Checked (bypasses React onChange) ────────────
2774
+ { pattern: /\.value\s*=\s*(?!["'\s]*$)/, name: ".value =", severity: "danger", action: "block", errorCode: -32001, suggestion: "Use page.fill(selector, value) which dispatches proper input/change events." },
2775
+ { pattern: /\.checked\s*=\s*(?:true|false)/, name: ".checked =", severity: "danger", action: "block", errorCode: -32001, suggestion: "Use page.check(selector) or page.uncheck(selector)." },
2776
+ { pattern: /\.indeterminate\s*=\s*true/, name: ".indeterminate =", severity: "warn", action: "block", errorCode: -32021, suggestion: "No human can set indeterminate state \u2014 remove this call." },
2777
+ { pattern: /\.valueAsDate\s*=/, name: ".valueAsDate =", severity: "warn", action: "block", errorCode: -32022, suggestion: "Use page.fill() with formatted date string instead." },
2778
+ { pattern: /\.valueAsNumber\s*=/, name: ".valueAsNumber =", severity: "warn", action: "block", errorCode: -32022, suggestion: "Use page.fill() with numeric string instead." },
2779
+ // ── P1: Select/Option (bypasses React onChange on select) ─
2780
+ { pattern: /\.selectedIndex\s*=\s*\d+/, name: ".selectedIndex =", severity: "danger", action: "block", errorCode: -32003, suggestion: "Use page.selectOption(selector, value)." },
2781
+ { pattern: /(?:options|children)\s*\[[^\]]*\]\s*\.\s*selected\s*=\s*(?:true|false)/, name: "options[N].selected =", severity: "danger", action: "block", errorCode: -32003, suggestion: "Use page.selectOption(selector, value)." },
2782
+ { pattern: /\.value\s*=\s*["'][^"']*["']\s*[;)]?\s*$/, name: "selectElement.value =", severity: "warn", action: "block", errorCode: -32023, suggestion: "For select elements, use page.selectOption(selector, value)." },
2783
+ // ── P2: Content properties (bypasses virtual DOM diffing) ──
2784
+ { pattern: /\.innerHTML\s*=/, name: ".innerHTML =", severity: "info", action: "pass", errorCode: -32007, suggestion: ".innerHTML bypasses React/Vue diffing. Use component state or page.setContent()." },
2785
+ { pattern: /\.outerHTML\s*=/, name: ".outerHTML =", severity: "info", action: "pass", errorCode: -32007, suggestion: "outerHTML replacement destroys React fiber tree. Use page.setContent()." },
2786
+ { pattern: /\.innerText\s*=/, name: ".innerText =", severity: "warn", action: "block", errorCode: -32024, suggestion: "Framework components should be updated via state, not innerText." },
2787
+ { pattern: /\.textContent\s*=/, name: ".textContent =", severity: "warn", action: "block", errorCode: -32024, suggestion: "textContent bypasses React/Vue diffing. Use component state instead." },
2788
+ { pattern: /\.outerText\s*=/, name: ".outerText =", severity: "warn", action: "block", errorCode: -32024, suggestion: "Non-standard property \u2014 use proper framework update methods." },
2789
+ { pattern: /nodeValue\s*=/, name: "node.nodeValue =", severity: "info", action: "block", errorCode: -32025, suggestion: "Direct text node mutation. Use textContent instead if needed." },
2790
+ // ── P3: Style properties ──────────────────────────────────
2791
+ { pattern: /\.style\s*=\s*["']/, name: ".style = (string override)", severity: "info", action: "pass", errorCode: -32008, suggestion: 'Setting style as a string overwrites CSSStyleDeclaration. Use element.style.prop = "value".' },
2792
+ { pattern: /\.style\.cssText\s*=/, name: ".style.cssText =", severity: "info", action: "pass", errorCode: -32008, suggestion: "style.cssText replacement destroys inline styles \u2014 use individual property set." },
2793
+ { pattern: /\.style\.setProperty\s*\(/, name: ".style.setProperty()", severity: "info", action: "pass", errorCode: -32008, suggestion: "Direct style property set bypasses CSS transitions." },
2794
+ { pattern: /\.style\.removeProperty\s*\(/, name: ".style.removeProperty()", severity: "info", action: "pass", errorCode: -32026, suggestion: "Style removal without user interaction is suspicious." },
2795
+ { pattern: /\.style\.animation\s*=/, name: ".style.animation =", severity: "info", action: "pass", errorCode: -32027, suggestion: "Forcing animation state via CDP is detectable." },
2796
+ { pattern: /\.style\.transition\s*=/, name: ".style.transition =", severity: "info", action: "pass", errorCode: -32027, suggestion: "Manipulating CSS transitions is rare in normal browsing." },
2797
+ // ── P4: Class/Attribute properties ────────────────────────
2798
+ { pattern: /\.className\s*=/, name: ".className =", severity: "info", action: "pass", errorCode: -32009, suggestion: "Use component state or a Playwright locator instead." },
2799
+ { pattern: /\.classList\.add\s*\(/, name: ".classList.add()", severity: "info", action: "pass", errorCode: -32009, suggestion: "classList manipulation via CDP bypasses framework state tracking." },
2800
+ { pattern: /\.classList\.remove\s*\(/, name: ".classList.remove()", severity: "info", action: "pass", errorCode: -32009, suggestion: "Use component state or attribute selectors instead." },
2801
+ { pattern: /\.classList\.toggle\s*\(/, name: ".classList.toggle()", severity: "info", action: "pass", errorCode: -32009, suggestion: "Toggle without user interaction is detectable." },
2802
+ { pattern: /\.classList\.replace\s*\(/, name: ".classList.replace()", severity: "info", action: "pass", errorCode: -32028, suggestion: "Rare operation \u2014 likely automated." },
2803
+ // ── P5: Attribute manipulation ────────────────────────────
2804
+ { pattern: /\.setAttribute\s*\(/, name: ".setAttribute()", severity: "info", action: "pass", errorCode: -32010, suggestion: "setAttribute bypasses framework attribute tracking. Use component state." },
2805
+ { pattern: /\.removeAttribute\s*\(/, name: ".removeAttribute()", severity: "info", action: "pass", errorCode: -32010, suggestion: "Attribute removal without user interaction is suspicious." },
2806
+ { pattern: /\.toggleAttribute\s*\(/, name: ".toggleAttribute()", severity: "info", action: "pass", errorCode: -32029, suggestion: "Attribute toggling via CDP is detectable." },
2807
+ { pattern: /\.dataset\.\w+\s*=/, name: ".dataset.* =", severity: "info", action: "pass", errorCode: -32030, suggestion: "dataset mutations via evaluate bypass native mutation observers." },
2808
+ // ── P6: Focus/Selection properties ────────────────────────
2809
+ { pattern: /\.selectionStart\s*=/, name: ".selectionStart =", severity: "info", action: "pass", errorCode: -32011, suggestion: "Setting cursor position without focus is detectable." },
2810
+ { pattern: /\.selectionEnd\s*=/, name: ".selectionEnd =", severity: "info", action: "pass", errorCode: -32011, suggestion: "Selection range manipulation without user input is suspicious." },
2811
+ { pattern: /\.selectionDirection\s*=/, name: ".selectionDirection =", severity: "info", action: "pass", errorCode: -32031, suggestion: "Selection direction changes normally via mouse drag." },
2812
+ // ── P7: Boolean properties ────────────────────────────────
2813
+ { pattern: /\.disabled\s*=/, name: ".disabled =", severity: "info", action: "pass", errorCode: -32012, suggestion: "Disabling elements via CDP mid-interaction is detectable." },
2814
+ { pattern: /\.readOnly\s*=/, name: ".readOnly =", severity: "info", action: "pass", errorCode: -32032, suggestion: "readOnly changes without user action." },
2815
+ { pattern: /\.hidden\s*=/, name: ".hidden =", severity: "info", action: "pass", errorCode: -32012, suggestion: "Hiding elements is a common scraper tactic \u2014 detectable." },
2816
+ { pattern: /\.required\s*=/, name: ".required =", severity: "info", action: "pass", errorCode: -32032, suggestion: "Validation constraint changes mid-session." },
2817
+ { pattern: /\.multiple\s*=/, name: ".multiple =", severity: "info", action: "pass", errorCode: -32032, suggestion: "Multiple attribute toggle is rare in normal browsing." },
2818
+ { pattern: /\.autofocus\s*=/, name: ".autofocus =", severity: "info", action: "pass", errorCode: -32032, suggestion: "autofocus changes mid-session are suspicious." },
2819
+ // ── P8: Frame/Navigation properties ───────────────────────
2820
+ { pattern: /\.src\s*=/, name: ".src = (on img/iframe/script)", severity: "info", action: "pass", errorCode: -32013, suggestion: "Changing src via CDP bypasses user interaction. Use page.click() on the element." },
2821
+ { pattern: /\.href\s*=/, name: ".href =", severity: "info", action: "pass", errorCode: -32033, suggestion: "Changing anchor href via evaluate \u2014 use page.click() instead." },
2822
+ { pattern: /\.action\s*=/, name: "form.action =", severity: "info", action: "pass", errorCode: -32034, suggestion: "Changing form action URL is highly suspicious." },
2823
+ { pattern: /\.method\s*=/, name: "form.method =", severity: "info", action: "pass", errorCode: -32034, suggestion: "Form method changes without user interaction." },
2824
+ { pattern: /\.target\s*=/, name: "link/area.target =", severity: "info", action: "pass", errorCode: -32034, suggestion: "Link target manipulation via CDP." },
2825
+ // ── P9: Media properties ──────────────────────────────────
2826
+ { pattern: /\.currentTime\s*=/, name: "media.currentTime = (seeking)", severity: "info", action: "pass", errorCode: -32014, suggestion: "Video seeking without user interaction \u2014 common scraper pattern." },
2827
+ { pattern: /\.playbackRate\s*=/, name: "media.playbackRate =", severity: "info", action: "pass", errorCode: -32014, suggestion: "Changing playback speed is detectable automation signal." },
2828
+ { pattern: /\.volume\s*=/, name: "media.volume =", severity: "info", action: "pass", errorCode: -32035, suggestion: "Volume setting via evaluate is suspicious." },
2829
+ { pattern: /\.muted\s*=/, name: "media.muted =", severity: "info", action: "pass", errorCode: -32035, suggestion: "Muting media without user click is suspicious." },
2830
+ // ── P10: Element geometry ─────────────────────────────────
2831
+ { pattern: /\.scrollTop\s*=/, name: ".scrollTop =", severity: "info", action: "pass", errorCode: -32015, suggestion: "Programmatic scroll without user gesture. Use page.mouse.wheel()." },
2832
+ { pattern: /\.scrollLeft\s*=/, name: ".scrollLeft =", severity: "info", action: "pass", errorCode: -32015, suggestion: "Horizontal scroll without user gesture." },
2833
+ { pattern: /\.scrollTo\s*\(/, name: ".scrollTo()", severity: "info", action: "pass", errorCode: -32015, suggestion: "ScrollTo bypasses user scroll detection." },
2834
+ { pattern: /\.scrollBy\s*\(/, name: ".scrollBy()", severity: "info", action: "pass", errorCode: -32015, suggestion: "ScrollBy without user gesture." },
2835
+ { pattern: /\.scrollIntoView\s*\(/, name: ".scrollIntoView()", severity: "info", action: "pass", errorCode: -32015, suggestion: "scrollIntoView is a common bot pattern. Let Playwright handle scrolling." },
2836
+ // ── P11: Shadow DOM ───────────────────────────────────────
2837
+ { pattern: /\.shadowRoot\s*=/, name: ".shadowRoot = (override)", severity: "info", action: "block", errorCode: -32036, suggestion: "ShadowRoot is read-only \u2014 this set attempt is detectable." },
2838
+ // ── P12: Force reflow / layout thrashing ──────────────────
2839
+ { pattern: /\.offsetHeight\b(?!\s*===?\s*)/, name: ".offsetHeight read (forced reflow)", severity: "warn", action: "pass", errorCode: -32016, suggestion: "Reading offsetHeight triggers forced reflow \u2014 anti-crawlers detect this as layout probing." },
2840
+ { pattern: /\.offsetWidth\b(?!\s*===?\s*)/, name: ".offsetWidth read (forced reflow)", severity: "warn", action: "pass", errorCode: -32016, suggestion: "Reading offsetWidth triggers forced reflow \u2014 detectable." },
2841
+ { pattern: /getBoundingClientRect\s*\(/, name: "getBoundingClientRect()", severity: "warn", action: "pass", errorCode: -32037, suggestion: "getBoundingClientRect triggers reflow. Minimize calls." },
2842
+ { pattern: /getComputedStyle\s*\(/, name: "getComputedStyle()", severity: "info", action: "pass", errorCode: -32038, suggestion: "getComputedStyle can trigger style recalculation." },
2843
+ { pattern: /\.clientHeight\b/, name: ".clientHeight read", severity: "info", action: "pass", errorCode: -32038, suggestion: "clientHeight read triggers layout." },
2844
+ { pattern: /\.clientWidth\b/, name: ".clientWidth read", severity: "info", action: "pass", errorCode: -32038, suggestion: "clientWidth read triggers layout." }
2845
+ ];
2846
+ var domMutationRule = {
2847
+ id: "dom-mutation",
2848
+ name: "DOM Property Mutation Detection (50+ setters)",
2849
+ priority: 10,
2850
+ canHandle(ctx) {
2851
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn";
2852
+ },
2853
+ evaluate(ctx) {
2854
+ const userCode = extractUserCode(ctx);
2855
+ if (!userCode) return null;
2856
+ for (const p of DOM_PATTERNS) {
2857
+ if (p.pattern.test(userCode)) {
2858
+ return {
2859
+ ruleId: "dom-mutation",
2860
+ action: p.action,
2861
+ severity: p.severity,
2862
+ reason: `Direct DOM property setter: "${p.name}". This bypasses framework event systems and is detectable as automation.`,
2863
+ suggestion: p.suggestion,
2864
+ errorCode: p.errorCode,
2865
+ errorMessage: p.severity === "danger" ? `[CDP Firewall] ${p.name} blocked \u2014 bypasses framework reactivity` : `[CDP Firewall] ${p.name} detected \u2014 use proper interaction API`
2866
+ };
2867
+ }
2868
+ }
2869
+ return null;
2870
+ }
2871
+ };
2872
+
2873
+ // src/cdp-interceptor/rules/mouse-trajectory.ts
2874
+ var TRACKER_KEY = "mouse-trajectory-tracker";
2875
+ var MAX_SAMPLES = 200;
2876
+ var MIN_SAMPLES_FOR_ANALYSIS = 5;
2877
+ var mouseTrajectoryRule = {
2878
+ id: "mouse-trajectory",
2879
+ name: "Mouse Trajectory Analysis",
2880
+ priority: 20,
2881
+ canHandle(ctx) {
2882
+ return ctx.method === "Input.dispatchMouseEvent";
2883
+ },
2884
+ evaluate(ctx) {
2885
+ const type = ctx.params.type;
2886
+ const x = ctx.params.x;
2887
+ const y = ctx.params.y;
2888
+ if (typeof x !== "number" || typeof y !== "number") return null;
2889
+ let tracker = ctx.sessionState.get(TRACKER_KEY);
2890
+ if (!tracker) {
2891
+ tracker = { samples: [], lastMouseDownAt: 0, isDragging: false };
2892
+ ctx.sessionState.set(TRACKER_KEY, tracker);
2893
+ }
2894
+ if (type === "mousePressed") {
2895
+ const priorResult = analyzeTrajectory(tracker.samples);
2896
+ tracker.lastMouseDownAt = Date.now();
2897
+ tracker.isDragging = true;
2898
+ tracker.samples = [];
2899
+ return priorResult;
2900
+ }
2901
+ if (type === "mouseReleased") {
2902
+ tracker.isDragging = false;
2903
+ const result = analyzeTrajectory(tracker.samples);
2904
+ tracker.samples = [];
2905
+ return result;
2906
+ }
2907
+ if (type === "mouseMoved") {
2908
+ const now = Date.now();
2909
+ const prevSample = tracker.samples[tracker.samples.length - 1];
2910
+ const distanceTraveled = prevSample ? prevSample.distanceTraveled + distance(prevSample.x, prevSample.y, x, y) : 0;
2911
+ const sample = { x, y, timestamp: now, distanceTraveled };
2912
+ tracker.samples.push(sample);
2913
+ if (tracker.samples.length > MAX_SAMPLES) {
2914
+ tracker.samples.shift();
2915
+ }
2916
+ return null;
2917
+ }
2918
+ if (type === "mouseReleased" && tracker.samples.length < MIN_SAMPLES_FOR_ANALYSIS) {
2919
+ tracker.samples = [];
2920
+ return null;
2921
+ }
2922
+ return null;
2923
+ }
2924
+ };
2925
+ function analyzeTrajectory(samples) {
2926
+ if (samples.length < MIN_SAMPLES_FOR_ANALYSIS) return null;
2927
+ const issues = [];
2928
+ const startX = samples[0].x;
2929
+ const startY = samples[0].y;
2930
+ const endX = samples[samples.length - 1].x;
2931
+ const endY = samples[samples.length - 1].y;
2932
+ const lineLength = distance(startX, startY, endX, endY);
2933
+ const collinearityResult = checkCollinearity(samples, startX, startY, endX, endY, lineLength);
2934
+ if (collinearityResult) {
2935
+ issues.push(collinearityResult);
2936
+ }
2937
+ const velocityResult = checkConstantVelocity(samples);
2938
+ if (velocityResult) {
2939
+ issues.push(velocityResult);
2940
+ }
2941
+ const jitterResult = checkJitter(samples);
2942
+ if (jitterResult) {
2943
+ issues.push(jitterResult);
2944
+ }
2945
+ if (issues.length === 0) return null;
2946
+ const stopX = samples[samples.length - 1].x;
2947
+ const stopY = samples[samples.length - 1].y;
2948
+ return {
2949
+ ruleId: "mouse-trajectory",
2950
+ action: "block",
2951
+ severity: "danger",
2952
+ reason: `Suspicious mouse trajectory: ${issues.join("; ")}`,
2953
+ suggestion: `This mouse movement appears automated (straight line A\u2192B, no natural variation).
2954
+ Use a humanized mouse API that generates:
2955
+ - Bezier curves instead of straight lines
2956
+ - Random acceleration/deceleration
2957
+ - 1-3px micro-jitter per sample
2958
+
2959
+ Example: The 'faker' or 'ghost-cursor' libraries generate realistic mouse paths.
2960
+ Target was: (${Math.round(startX)}, ${Math.round(startY)}) \u2192 (${Math.round(stopX)}, ${Math.round(stopY)})`,
2961
+ errorCode: -32002,
2962
+ errorMessage: "[CDP Firewall] Automated mouse trajectory blocked \u2014 appears non-human"
2963
+ };
2964
+ }
2965
+ function distance(x1, y1, x2, y2) {
2966
+ return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
2967
+ }
2968
+ function checkCollinearity(samples, x1, y1, x2, y2, lineLength) {
2969
+ if (lineLength < 5) return null;
2970
+ let maxDeviation = 0;
2971
+ const dx = x2 - x1;
2972
+ const dy = y2 - y1;
2973
+ for (let i = 1; i < samples.length - 1; i++) {
2974
+ const { x, y } = samples[i];
2975
+ const cross = Math.abs(dy * x - dx * y + x2 * y1 - y2 * x1);
2976
+ const dev = cross / lineLength;
2977
+ if (dev > maxDeviation) maxDeviation = dev;
2978
+ }
2979
+ if (maxDeviation < 1.5 && lineLength > 20) {
2980
+ return `Perfectly straight line (max deviation ${maxDeviation.toFixed(1)}px over ${lineLength.toFixed(0)}px)`;
2981
+ }
2982
+ if (maxDeviation < 0.5 && lineLength > 10) {
2983
+ return `Near-perfect straight line (max deviation ${maxDeviation.toFixed(1)}px)`;
2984
+ }
2985
+ return null;
2986
+ }
2987
+ function checkConstantVelocity(samples) {
2988
+ if (samples.length < 3) return null;
2989
+ const speeds = [];
2990
+ for (let i = 1; i < samples.length; i++) {
2991
+ const d = distance(
2992
+ samples[i - 1].x,
2993
+ samples[i - 1].y,
2994
+ samples[i].x,
2995
+ samples[i].y
2996
+ );
2997
+ const dt = samples[i].timestamp - samples[i - 1].timestamp;
2998
+ if (dt > 0) speeds.push(d / dt);
2999
+ }
3000
+ if (speeds.length < 2) return null;
3001
+ const mean = speeds.reduce((a, b) => a + b, 0) / speeds.length;
3002
+ if (mean === 0) return null;
3003
+ const variance = speeds.reduce((sum, v) => sum + (v - mean) ** 2, 0) / speeds.length;
3004
+ const stddev = Math.sqrt(variance);
3005
+ const cv = stddev / mean;
3006
+ if (cv < 0.05) {
3007
+ return `Constant velocity (CV=${cv.toFixed(3)}, mean=${mean.toFixed(1)} px/ms)`;
3008
+ }
3009
+ return null;
3010
+ }
3011
+ function checkJitter(samples) {
3012
+ if (samples.length < 4) return null;
3013
+ let totalLateralChange = 0;
3014
+ for (let i = 1; i < samples.length; i++) {
3015
+ const d = distance(
3016
+ samples[i - 1].x,
3017
+ samples[i - 1].y,
3018
+ samples[i].x,
3019
+ samples[i].y
3020
+ );
3021
+ totalLateralChange += d;
3022
+ }
3023
+ const avgLateral = totalLateralChange / (samples.length - 1);
3024
+ if (avgLateral < 0.3) {
3025
+ return "No micro-jitter (sub-pixel precision, impossible for human)";
3026
+ }
3027
+ return null;
3028
+ }
3029
+
3030
+ // src/cdp-interceptor/rules/input-keystroke.ts
3031
+ var TRACKER_KEY2 = "keystroke-tracker";
3032
+ var MIN_KEYS_FOR_ANALYSIS = 4;
3033
+ var inputKeystrokeRule = {
3034
+ id: "input-keystroke",
3035
+ name: "Input Keystroke Timing Analysis",
3036
+ priority: 40,
3037
+ canHandle(ctx) {
3038
+ return ctx.method === "Input.dispatchKeyEvent" || ctx.method === "Input.insertText";
3039
+ },
3040
+ evaluate(ctx) {
3041
+ if (ctx.method === "Input.insertText") {
3042
+ return {
3043
+ ruleId: "input-keystroke",
3044
+ action: "pass",
3045
+ severity: "info",
3046
+ reason: "Input.insertText bypasses native keyboard events. Playwright uses this internally for page.fill().",
3047
+ suggestion: "Prefer page.type() with variable delay for human-like input.",
3048
+ errorCode: -32004,
3049
+ errorMessage: "[CDP Firewall] Input.insertText detected \u2014 note: Playwright uses this for fill()"
3050
+ };
3051
+ }
3052
+ let tracker = ctx.sessionState.get(TRACKER_KEY2);
3053
+ if (!tracker) {
3054
+ tracker = { samples: [] };
3055
+ ctx.sessionState.set(TRACKER_KEY2, tracker);
3056
+ }
3057
+ const type = ctx.params.type;
3058
+ const code = ctx.params.code;
3059
+ const key = ctx.params.key;
3060
+ if (type === "keyDown" && key && key.length === 1) {
3061
+ tracker.samples.push({
3062
+ code,
3063
+ key,
3064
+ timestamp: Date.now(),
3065
+ type
3066
+ });
3067
+ }
3068
+ if (tracker.samples.length >= MIN_KEYS_FOR_ANALYSIS && (type === "keyUp" || type === "keyDown")) {
3069
+ return analyzeKeyTiming(tracker.samples);
3070
+ }
3071
+ return null;
3072
+ }
3073
+ };
3074
+ function analyzeKeyTiming(samples) {
3075
+ if (samples.length < MIN_KEYS_FOR_ANALYSIS) return null;
3076
+ const intervals = [];
3077
+ for (let i = 1; i < samples.length; i++) {
3078
+ const dt = samples[i].timestamp - samples[i - 1].timestamp;
3079
+ if (dt > 0) intervals.push(dt);
3080
+ }
3081
+ if (intervals.length < 3) return null;
3082
+ const mean = intervals.reduce((a, b) => a + b, 0) / intervals.length;
3083
+ if (mean === 0) return null;
3084
+ const variance = intervals.reduce((sum, v) => sum + (v - mean) ** 2, 0) / intervals.length;
3085
+ const stddev = Math.sqrt(variance);
3086
+ const cv = stddev / mean;
3087
+ const uniqueIntervals = new Set(intervals);
3088
+ const allIdentical = uniqueIntervals.size === 1;
3089
+ if (allIdentical) {
3090
+ return {
3091
+ ruleId: "input-keystroke",
3092
+ action: "block",
3093
+ severity: "danger",
3094
+ reason: `All ${intervals.length} keystroke intervals are exactly ${intervals[0]}ms \u2014 impossible for human typing.`,
3095
+ suggestion: `Use page.fill(selector, text) instead of page.type() with delay.
3096
+ Or add random variation: page.type(selector, text, {delay: 50 + Math.random() * 80}).`,
3097
+ errorCode: -32004,
3098
+ errorMessage: "[CDP Firewall] Constant keystroke timing detected \u2014 automated typing pattern"
3099
+ };
3100
+ }
3101
+ if (cv < 0.08) {
3102
+ return {
3103
+ ruleId: "input-keystroke",
3104
+ action: "block",
3105
+ severity: "warn",
3106
+ reason: `Unnatural keystroke timing (CV=${cv.toFixed(3)}). Human typing has CV > 0.2 on average.`,
3107
+ suggestion: `Add random variation to your typing delay: page.type(selector, text, {delay: 50 + Math.random() * 80}).`,
3108
+ errorCode: -32004,
3109
+ errorMessage: "[CDP Firewall] Suspicious keystroke timing \u2014 likely automated"
3110
+ };
3111
+ }
3112
+ return null;
3113
+ }
3114
+
3115
+ // src/cdp-interceptor/rules/automation-signals.ts
3116
+ var AUTOMATION_PATTERNS = [
3117
+ // ── Playwright markers ─────────────────────────
3118
+ { pattern: /window\s*\.\s*__playwright/, name: "window.__playwright", severity: "danger", errorCode: -32040 },
3119
+ { pattern: /window\s*\.\s*__pw_[a-zA-Z]/, name: "window.__pw_*", severity: "danger", errorCode: -32040 },
3120
+ { pattern: /window\s*\.\s*__pw_paused/, name: "window.__pw_paused", severity: "danger", errorCode: -32040 },
3121
+ { pattern: /window\s*\.\s*playwright\b/, name: "window.playwright", severity: "danger", errorCode: -32040 },
3122
+ { pattern: /window\s*\.\s*__pw_recorder/, name: "window.__pw_recorder", severity: "warn", errorCode: -32041 },
3123
+ { pattern: /window\s*\.\s*__pw_trace/, name: "window.__pw_trace", severity: "warn", errorCode: -32041 },
3124
+ // ── Puppeteer markers ──────────────────────────
3125
+ { pattern: /window\s*\.\s*__puppeteer\b/, name: "window.__puppeteer", severity: "danger", errorCode: -32040 },
3126
+ { pattern: /window\s*\.\s*__puppeteer_evaluation_script/, name: "window.__puppeteer_evaluation_script", severity: "danger", errorCode: -32040 },
3127
+ { pattern: /window\s*\.\s*__puppeteer_testId/, name: "window.__puppeteer_testId", severity: "warn", errorCode: -32041 },
3128
+ { pattern: /window\s*\.\s*__puppeteer_/, name: "window.__puppeteer_*", severity: "danger", errorCode: -32040 },
3129
+ { pattern: /window\s*\.\s*_puppeteer\b/, name: "window._puppeteer", severity: "warn", errorCode: -32041 },
3130
+ // ── Selenium / WebDriver markers ───────────────
3131
+ { pattern: /window\s*\.\s*__webdriver_script_fn/, name: "window.__webdriver_script_fn", severity: "danger", errorCode: -32040 },
3132
+ { pattern: /window\s*\.\s*__selenium\b/, name: "window.__selenium", severity: "danger", errorCode: -32040 },
3133
+ { pattern: /window\s*\.\s*__selenium_evaluate/, name: "window.__selenium_evaluate", severity: "danger", errorCode: -32040 },
3134
+ { pattern: /window\s*\.\s*__driver_evaluate/, name: "window.__driver_evaluate", severity: "warn", errorCode: -32041 },
3135
+ { pattern: /window\s*\.\s*__webdriver_evaluate/, name: "window.__webdriver_evaluate", severity: "danger", errorCode: -32040 },
3136
+ { pattern: /document\.\$cdc_/, name: "document.$cdc_* (Selenium marker)", severity: "danger", errorCode: -32040 },
3137
+ { pattern: /document\.\$chrome_asyncScriptInfo/, name: "document.$chrome_asyncScriptInfo", severity: "danger", errorCode: -32040 },
3138
+ // ── navigator.webdriver detection ─────────────
3139
+ { pattern: /navigator\s*\.\s*webdriver/, name: "navigator.webdriver read", severity: "danger", errorCode: -32042 },
3140
+ { pattern: /navigator\[["']webdriver["']\]/, name: 'navigator["webdriver"]', severity: "danger", errorCode: -32042 },
3141
+ // ── Chrome headless API surface checks ────────
3142
+ { pattern: /chrome\s*\.\s*app/, name: "chrome.app detection", severity: "danger", errorCode: -32043 },
3143
+ { pattern: /chrome\s*\.\s*runtime/, name: "chrome.runtime detection", severity: "danger", errorCode: -32043 },
3144
+ { pattern: /chrome\s*\.\s*loadTimes/, name: "chrome.loadTimes detection", severity: "warn", errorCode: -32044 },
3145
+ { pattern: /chrome\s*\.\s*csi\b/, name: "chrome.csi detection", severity: "warn", errorCode: -32044 },
3146
+ { pattern: /window\s*\.\s*chrome\b/, name: "window.chrome object probe", severity: "warn", errorCode: -32044 },
3147
+ { pattern: /navigator\s*\.\s*plugins\b/, name: "navigator.plugins enumeration", severity: "warn", errorCode: -32044 },
3148
+ { pattern: /navigator\s*\.\s*mimeTypes/, name: "navigator.mimeTypes enumeration", severity: "warn", errorCode: -32044 },
3149
+ { pattern: /navigator\s*\.\s*hardwareConcurrency/, name: "navigator.hardwareConcurrency", severity: "warn", errorCode: -32044 },
3150
+ { pattern: /navigator\s*\.\s*deviceMemory/, name: "navigator.deviceMemory", severity: "info", errorCode: -32045 },
3151
+ { pattern: /navigator\s*\.\s*maxTouchPoints/, name: "navigator.maxTouchPoints", severity: "warn", errorCode: -32044 },
3152
+ { pattern: /navigator\s*\.\s*languages/, name: "navigator.languages", severity: "info", errorCode: -32045 },
3153
+ { pattern: /navigator\s*\.\s*platform/, name: "navigator.platform", severity: "info", errorCode: -32045 },
3154
+ // ── Anti-detection injection attempts ─────────
3155
+ { pattern: /navigator\.webdriver\s*=\s*(false|undefined|null)/, name: "navigator.webdriver override attempt", severity: "danger", errorCode: -32046, suggestionOverride: "Overriding navigator.webdriver is detectable. Use CDP Page.addScriptToEvaluateOnNewDocument instead." },
3156
+ { pattern: /Object\.defineProperty\s*\([^)]*webdriver/, name: "Object.defineProperty navigator.webdriver", severity: "danger", errorCode: -32046, suggestionOverride: "Object.defineProperty patches are detectable via Function.prototype.toString checks." },
3157
+ { pattern: /delete\s+navigator\s*\.\s*webdriver/, name: "delete navigator.webdriver", severity: "danger", errorCode: -32046, suggestionOverride: "Deleting webdriver flag is detectable \u2014 the delete itself is visible." },
3158
+ // ── PhantomJS / Headless markers ──────────────
3159
+ { pattern: /window\s*\.\s*callPhantom/, name: "window.callPhantom", severity: "danger", errorCode: -32040 },
3160
+ { pattern: /window\s*\.\s*_phantom/, name: "window._phantom", severity: "danger", errorCode: -32040 },
3161
+ { pattern: /window\s*\.\s*phantom\b/, name: "window.phantom", severity: "danger", errorCode: -32040 },
3162
+ { pattern: /window\s*\.\s*Buffer\b/, name: "window.Buffer (Node.js leak)", severity: "danger", errorCode: -32040 },
3163
+ { pattern: /window\s*\.\s*process\b/, name: "window.process (Node.js leak)", severity: "danger", errorCode: -32040 },
3164
+ { pattern: /window\s*\.\s*global\b/, name: "window.global (Node.js leak)", severity: "danger", errorCode: -32040 },
3165
+ { pattern: /window\s*\.\s*__dirname/, name: "window.__dirname (Node.js leak)", severity: "danger", errorCode: -32040 }
3166
+ ];
3167
+ var automationSignalsRule = {
3168
+ id: "automation-signals",
3169
+ name: "Browser Automation Signal Detection (35+ markers)",
3170
+ priority: 20,
3171
+ canHandle(ctx) {
3172
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn" || ctx.method === "Page.addScriptToEvaluateOnNewDocument";
3173
+ },
3174
+ evaluate(ctx) {
3175
+ if (ctx.method === "Page.addScriptToEvaluateOnNewDocument") {
3176
+ const source = ctx.params.source;
3177
+ if (typeof source === "string") {
3178
+ for (const p of AUTOMATION_PATTERNS) {
3179
+ if (p.pattern.test(source)) {
3180
+ return makeDecision(p, source.substring(0, 60));
3181
+ }
3182
+ }
3183
+ }
3184
+ return null;
3185
+ }
3186
+ const userCode = extractUserCode(ctx);
3187
+ if (!userCode) return null;
3188
+ for (const p of AUTOMATION_PATTERNS) {
3189
+ if (p.pattern.test(userCode)) {
3190
+ return makeDecision(p, userCode.substring(0, 60));
3191
+ }
3192
+ }
3193
+ return null;
3194
+ }
3195
+ };
3196
+ function makeDecision(p, context) {
3197
+ const suggestion = p.suggestionOverride ?? `Detected: "${p.name}" \u2014 an automation tool marker that anti-crawler systems immediately flag. Remove this pattern from your code.`;
3198
+ return {
3199
+ ruleId: "automation-signals",
3200
+ action: "block",
3201
+ severity: p.severity,
3202
+ reason: `Automation marker detected: "${p.name}". Context: "${context}..."`,
3203
+ suggestion,
3204
+ errorCode: p.errorCode,
3205
+ errorMessage: p.severity === "danger" ? `[CDP Firewall] ${p.name} blocked \u2014 automation tool marker detected` : `[CDP Firewall] ${p.name} detected \u2014 potential automation signal`
3206
+ };
3207
+ }
3208
+
3209
+ // src/cdp-interceptor/rules/fingerprinting.ts
3210
+ var FP_PATTERNS = [
3211
+ // ── Canvas Rendering fingerprinting ────────────────────
3212
+ { pattern: /\.toDataURL\s*\(/, name: "canvas.toDataURL()", severity: "danger", errorCode: -32050, suggestion: "canvas.toDataURL() returns a unique hash that identifies the browser engine. Minimize calls." },
3213
+ { pattern: /\.toBlob\s*\(/, name: "canvas.toBlob()", severity: "danger", errorCode: -32050, suggestion: "canvas.toBlob() is used for canvas fingerprinting. Avoid if possible." },
3214
+ { pattern: /getImageData\s*\(/, name: "CanvasRenderingContext2D.getImageData()", severity: "danger", errorCode: -32050, suggestion: "getImageData reads pixel-level data used for fingerprinting." },
3215
+ { pattern: /measureText\s*\(/, name: "CanvasRenderingContext2D.measureText()", severity: "warn", errorCode: -32051, suggestion: "Font metrics reveal installed fonts \u2014 a fingerprinting vector." },
3216
+ { pattern: /OffscreenCanvas\s*\(/, name: "new OffscreenCanvas()", severity: "warn", errorCode: -32051, suggestion: "OffscreenCanvas is sometimes used to avoid visibility detection." },
3217
+ { pattern: /convertToBlob\s*\(/, name: "OffscreenCanvas.convertToBlob()", severity: "warn", errorCode: -32051, suggestion: "Headless OffscreenCanvas rendering differs from real browser." },
3218
+ // ── WebGL Fingerprinting ──────────────────────────────
3219
+ { pattern: /getParameter\s*\([^)]*(?:VENDOR|RENDERER|VERSION)/, name: "WebGL getParameter(VENDOR/RENDERER)", severity: "danger", errorCode: -32052, suggestion: "WebGL VENDOR/RENDERER returns emulated values in headless. Cannot be reliably spoofed." },
3220
+ { pattern: /getSupportedExtensions\s*\(/, name: "WebGL getSupportedExtensions()", severity: "warn", errorCode: -32053, suggestion: "WebGL extension list differs in headless mode." },
3221
+ { pattern: /getShaderPrecisionFormat\s*\(/, name: "WebGL getShaderPrecisionFormat()", severity: "info", errorCode: -32054, suggestion: "Shader precision differs between headless and real GPU." },
3222
+ { pattern: /UNMASKED_VENDOR_WEBGL/, name: "WEBGL_debug_renderer_info UNMASKED_VENDOR", severity: "warn", errorCode: -32053, suggestion: "Unmasked vendor info reveals the real GPU \u2014 blocked in many envs." },
3223
+ { pattern: /UNMASKED_RENDERER_WEBGL/, name: "WEBGL_debug_renderer_info UNMASKED_RENDERER", severity: "warn", errorCode: -32053, suggestion: "Unmasked renderer string reveals the real GPU." },
3224
+ // ── AudioContext Fingerprinting ────────────────────────
3225
+ { pattern: /AnalyserNode\s*\(/, name: "new AnalyserNode()", severity: "warn", errorCode: -32055, suggestion: "Audio fingerprinting via AnalyserNode \u2014 produces silence in headless." },
3226
+ { pattern: /getFloatFrequencyData\s*\(/, name: "AnalyserNode.getFloatFrequencyData()", severity: "danger", errorCode: -32050, suggestion: "Audio frequency data in headless returns silence (all zeros) \u2014 detectable." },
3227
+ { pattern: /getByteFrequencyData\s*\(/, name: "AnalyserNode.getByteFrequencyData()", severity: "danger", errorCode: -32050, suggestion: "Audio byte frequency data in headless returns zeros." },
3228
+ { pattern: /getByteTimeDomainData\s*\(/, name: "AnalyserNode.getByteTimeDomainData()", severity: "danger", errorCode: -32050, suggestion: "Time domain audio data in headless is a flat line \u2014 detectable." },
3229
+ { pattern: /OfflineAudioContext\s*\(/, name: "new OfflineAudioContext()", severity: "warn", errorCode: -32055, suggestion: "Offline audio rendering is a known fingerprinting method." },
3230
+ { pattern: /OscillatorNode\s*\(/, name: "new OscillatorNode()", severity: "info", errorCode: -32056, suggestion: "Audio oscillator used in fingerprinting probes." },
3231
+ // ── Navigator property probing ───────────────────────
3232
+ { pattern: /navigator\s*\.\s*connection\b/, name: "navigator.connection", severity: "info", errorCode: -32057, suggestion: 'Network connection info is used for fingerprinting (always "4g" in bots).' },
3233
+ { pattern: /navigator\s*\.\s*getBattery\s*\(/, name: "navigator.getBattery()", severity: "warn", errorCode: -32058, suggestion: "Battery API is a fingerprinting vector. Returns fixed values in headless." },
3234
+ { pattern: /navigator\s*\.\s*mediaDevices\s*\.\s*enumerateDevices/, name: "navigator.mediaDevices.enumerateDevices()", severity: "warn", errorCode: -32058, suggestion: "Media device enumeration returns empty/no devices in headless." },
3235
+ { pattern: /navigator\s*\.\s*permissions\s*\.\s*query/, name: "navigator.permissions.query()", severity: "info", errorCode: -32059, suggestion: "Permission queries can reveal automation environment." },
3236
+ // ── Screen / Window geometry probing ──────────────────
3237
+ { pattern: /screen\.avail(Width|Height|Left|Top)/, name: "screen.avail*", severity: "warn", errorCode: -32060, suggestion: "screen.avail* values differ in headless (no OS chrome)." },
3238
+ { pattern: /window\.outerWidth\s*-?\s*window\.innerWidth/, name: "window.outerWidth - window.innerWidth", severity: "danger", errorCode: -32050, suggestion: "This difference is 0 in headless (no browser chrome) \u2014 100% detection rate." },
3239
+ { pattern: /window\.outerHeight\s*-?\s*window\.innerHeight/, name: "window.outerHeight - window.innerHeight", severity: "danger", errorCode: -32050, suggestion: "This difference is 0 in headless \u2014 immediate automation detection." },
3240
+ { pattern: /screen\.width\b/, name: "screen.width", severity: "info", errorCode: -32061, suggestion: "Screen dimensions can be spoofed but inconsistencies with viewport are detectable." },
3241
+ { pattern: /screen\.height\b/, name: "screen.height", severity: "info", errorCode: -32061, suggestion: "Screen dimension probes for viewport inconsistency detection." },
3242
+ { pattern: /window\.devicePixelRatio/, name: "window.devicePixelRatio", severity: "warn", errorCode: -32060, suggestion: "devicePixelRatio is always 1 in headless \u2014 differs from real displays." },
3243
+ { pattern: /matchMedia\s*\(/, name: "window.matchMedia()", severity: "warn", errorCode: -32060, suggestion: "matchMedia can detect CDP overridden viewport dimensions." },
3244
+ // ── Performance / Timing API ──────────────────────────
3245
+ { pattern: /performance\s*\.\s*now\s*\(/, name: "performance.now()", severity: "info", errorCode: -32062, suggestion: "High-resolution timer used for timing attacks and bot detection." },
3246
+ { pattern: /performance\s*\.\s*memory/, name: "performance.memory", severity: "warn", errorCode: -32063, suggestion: "performance.memory shows VM memory limits in containers." },
3247
+ { pattern: /performance\.getEntriesByType\s*\(\s*["']navigation["']\s*\)/, name: 'performance.getEntriesByType("navigation")', severity: "info", errorCode: -32062, suggestion: "Navigation timing reveals request pattern inconsistencies." },
3248
+ { pattern: /performance\.getEntriesByType\s*\(\s*["']resource["']\s*\)/, name: 'performance.getEntriesByType("resource")', severity: "info", errorCode: -32062, suggestion: "Resource loading timing analysis for bot detection." },
3249
+ // ── Font Detection ────────────────────────────────────
3250
+ { pattern: /document\.fonts\.check\s*\(/, name: "document.fonts.check()", severity: "warn", errorCode: -32064, suggestion: "Font availability checks are used for fingerprinting. Installed font list is unique per user." },
3251
+ { pattern: /document\.fonts\.ready/, name: "document.fonts.ready", severity: "info", errorCode: -32065, suggestion: "Font loading state probe for fingerprinting." },
3252
+ // ── WebRTC / Connectivity ────────────────────────────
3253
+ { pattern: /RTCPeerConnection\s*\(/, name: "new RTCPeerConnection()", severity: "warn", errorCode: -32066, suggestion: "WebRTC can leak internal IP and is used for connectivity fingerprinting." },
3254
+ { pattern: /navigator\.mediaDevices\.getUserMedia/, name: "navigator.mediaDevices.getUserMedia()", severity: "info", errorCode: -32067, suggestion: "getUserMedia always fails in headless (no camera)." },
3255
+ // ── Feature Consistency Checks ─────────────────────────
3256
+ { pattern: /Intl\.DateTimeFormat.*resolvedOptions.*timeZone/, name: "Intl.DateTimeFormat timezone check", severity: "warn", errorCode: -32068, suggestion: "Timezone from Intl API vs Emulation.setTimezoneOverride will be inconsistent when mocked." },
3257
+ { pattern: /new\s+Date\s*\(\s*\)\s*\.\s*getTimezoneOffset/, name: "Date.getTimezoneOffset()", severity: "info", errorCode: -32069, suggestion: "Timezone offset used for timing consistency cross-checks." },
3258
+ { pattern: /Error\s*\(\s*\)\s*\.\s*stack/, name: "Error().stack format check", severity: "info", errorCode: -32070, suggestion: "Stack trace format differs between headless and full Chrome." },
3259
+ { pattern: /Function\.prototype\.toString/, name: "Function.prototype.toString on native fn", severity: "info", errorCode: -32070, suggestion: "Native function toString() format can reveal patched APIs." }
3260
+ ];
3261
+ var fingerprintingRule = {
3262
+ id: "fingerprinting",
3263
+ name: "Browser Fingerprinting Access Detection (35+ APIs)",
3264
+ priority: 30,
3265
+ canHandle(ctx) {
3266
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn";
3267
+ },
3268
+ evaluate(ctx) {
3269
+ const userCode = extractUserCode(ctx);
3270
+ if (!userCode) return null;
3271
+ for (const p of FP_PATTERNS) {
3272
+ if (p.pattern.test(userCode)) {
3273
+ return {
3274
+ ruleId: "fingerprinting",
3275
+ action: "block",
3276
+ severity: p.severity,
3277
+ reason: `Browser fingerprinting API accessed: "${p.name}". Anti-crawler systems use this to identify your browser.`,
3278
+ suggestion: p.suggestion,
3279
+ errorCode: p.errorCode,
3280
+ errorMessage: `[CDP Firewall] ${p.name} blocked \u2014 fingerprinting API access detected`
3281
+ };
3282
+ }
3283
+ }
3284
+ return null;
3285
+ }
3286
+ };
3287
+
3288
+ // src/cdp-interceptor/rules/event-simulation.ts
3289
+ var EVENT_PATTERNS = [
3290
+ // ── Direct method calls (all isTrusted=false) ─────────
3291
+ { pattern: /\.click\s*\(\s*\)/, name: "el.click()", severity: "danger", errorCode: -32070, suggestion: "el.click() fires isTrusted=false events. Use page.click(selector) which uses Input.dispatchMouseEvent (isTrusted=true)." },
3292
+ { pattern: /\.focus\s*\(\s*\)/, name: "el.focus()", severity: "danger", errorCode: -32071, suggestion: "el.focus() without user interaction is detectable. Use page.click(selector) which naturally focuses." },
3293
+ { pattern: /\.blur\s*\(\s*\)/, name: "el.blur()", severity: "danger", errorCode: -32071, suggestion: "el.blur() without user interaction. Avoid in automation scripts." },
3294
+ { pattern: /\.submit\s*\(\s*\)/, name: "el.submit()", severity: "danger", errorCode: -32072, suggestion: `form.submit() bypasses onSubmit handlers. Click the submit button: page.click('button[type="submit"]').` },
3295
+ { pattern: /\.reset\s*\(\s*\)/, name: "el.reset()", severity: "danger", errorCode: -32072, suggestion: "form.reset() without user action. Let the user clear fields manually." },
3296
+ { pattern: /\.select\s*\(\s*\)/, name: "el.select()", severity: "warn", errorCode: -32073, suggestion: "input.select() selects text without user interaction. Use page.click(selector) instead." },
3297
+ { pattern: /dialog\.close\s*\(\s*\)|showModal\(\)/, name: "dialog.showModal()/close()", severity: "danger", errorCode: -32074, suggestion: "dialog.showModal() requires user gesture. Let the user open the dialog naturally." },
3298
+ { pattern: /\.showPopover\s*\(\s*\)/, name: "el.showPopover()", severity: "warn", errorCode: -32073, suggestion: "Popover.show() requires user gesture. Simulate a click on the popover trigger." },
3299
+ { pattern: /\.hidePopover\s*\(\s*\)/, name: "el.hidePopover()", severity: "warn", errorCode: -32073, suggestion: "Hiding popovers via CDP is detectable." },
3300
+ { pattern: /\.requestFullscreen\s*\(\s*\)/, name: "el.requestFullscreen()", severity: "danger", errorCode: -32074, suggestion: "Fullscreen requests require user gesture. Cannot be triggered by automation." },
3301
+ { pattern: /\.requestPointerLock\s*\(\s*\)/, name: "el.requestPointerLock()", severity: "danger", errorCode: -32074, suggestion: "Pointer lock requires user gesture. Blocked in automation." },
3302
+ { pattern: /\.setSelectionRange\s*\(/, name: "el.setSelectionRange()", severity: "warn", errorCode: -32073, suggestion: "Setting selection range without user interaction is suspicious." },
3303
+ { pattern: /\.setRangeText\s*\(/, name: "el.setRangeText()", severity: "info", errorCode: -32076, suggestion: "Range text replacement without user input is detectable." },
3304
+ { pattern: /\.showPicker\s*\(\s*\)/, name: "HTMLInputElement.showPicker()", severity: "info", errorCode: -32076, suggestion: "Date/color picker shown without click \u2014 detectable." },
3305
+ { pattern: /\.reportValidity\s*\(\s*\)/, name: "el.reportValidity()", severity: "info", errorCode: -32076, suggestion: "Validity reporting without form submission attempt." },
3306
+ // ── dispatchEvent with synthetic events (isTrusted=false) ──
3307
+ { pattern: /dispatchEvent\s*\(\s*new\s+(?:Event|CustomEvent)\s*\(/, name: "dispatchEvent(new Event/CustomEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic events have isTrusted=false. Use Input.dispatch* CDP methods for trusted events." },
3308
+ { pattern: /dispatchEvent\s*\(\s*new\s+MouseEvent\s*\(/, name: "dispatchEvent(new MouseEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic mouse events (isTrusted=false). Use Input.dispatchMouseEvent CDP method." },
3309
+ { pattern: /dispatchEvent\s*\(\s*new\s+KeyboardEvent\s*\(/, name: "dispatchEvent(new KeyboardEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic keyboard events (isTrusted=false). Use Input.dispatchKeyEvent CDP method." },
3310
+ { pattern: /dispatchEvent\s*\(\s*new\s+FocusEvent\s*\(/, name: "dispatchEvent(new FocusEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic focus events bypass user interaction." },
3311
+ { pattern: /dispatchEvent\s*\(\s*new\s+InputEvent\s*\(/, name: "dispatchEvent(new InputEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic input events (isTrusted=false). Use page.fill() or page.type()." },
3312
+ { pattern: /dispatchEvent\s*\(\s*new\s+(?:Event)\s*\(\s*["'](?:input|change|submit|reset)/, name: 'dispatchEvent("input"/"change"/"submit")', severity: "danger", errorCode: -32077, suggestion: "Synthetic input/change/submit events are trusted=false. Always detectable." },
3313
+ { pattern: /dispatchEvent\s*\(\s*new\s+PointerEvent\s*\(/, name: "dispatchEvent(new PointerEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic pointer events bypass the trusted input pipeline." },
3314
+ { pattern: /dispatchEvent\s*\(\s*new\s+TouchEvent\s*\(/, name: "dispatchEvent(new TouchEvent)", severity: "danger", errorCode: -32077, suggestion: "Synthetic touch events on mobile are detectable." },
3315
+ { pattern: /dispatchEvent\s*\(\s*new\s+WheelEvent\s*\(/, name: "dispatchEvent(new WheelEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic scroll events (isTrusted=false). Use Input.dispatchMouseEvent with wheel type." },
3316
+ { pattern: /dispatchEvent\s*\(\s*new\s+ClipboardEvent\s*\(/, name: "dispatchEvent(new ClipboardEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic clipboard events are detectable." },
3317
+ { pattern: /dispatchEvent\s*\(\s*new\s+DragEvent\s*\(/, name: "dispatchEvent(new DragEvent)", severity: "warn", errorCode: -32078, suggestion: "Synthetic drag events bypass real user interaction." },
3318
+ { pattern: /dispatchEvent\s*\(\s*new\s+CompositionEvent\s*\(/, name: "dispatchEvent(new CompositionEvent)", severity: "info", errorCode: -32079, suggestion: "Synthetic IME composition events \u2014 detectable pattern." },
3319
+ { pattern: /dispatchEvent\s*\(\s*new\s+AnimationEvent\s*\(/, name: "dispatchEvent(new AnimationEvent)", severity: "info", errorCode: -32079, suggestion: "Forcing animation state transitions via fake events." },
3320
+ { pattern: /dispatchEvent\s*\(\s*new\s+TransitionEvent\s*\(/, name: "dispatchEvent(new TransitionEvent)", severity: "info", errorCode: -32079, suggestion: "Forcing CSS transition end via fake events." }
3321
+ ];
3322
+ var eventSimulationRule = {
3323
+ id: "event-simulation",
3324
+ name: "Event Simulation Detection (30+ patterns)",
3325
+ priority: 40,
3326
+ canHandle(ctx) {
3327
+ return ctx.method === "Runtime.evaluate" || ctx.method === "Runtime.callFunctionOn";
3328
+ },
3329
+ evaluate(ctx) {
3330
+ const userCode = extractUserCode(ctx);
3331
+ if (!userCode) return null;
3332
+ for (const p of EVENT_PATTERNS) {
3333
+ if (p.pattern.test(userCode)) {
3334
+ return {
3335
+ ruleId: "event-simulation",
3336
+ action: "block",
3337
+ severity: p.severity,
3338
+ reason: `Event simulation detected: "${p.name}". Synthetic events have isTrusted=false and are 100% detectable.`,
3339
+ suggestion: p.suggestion,
3340
+ errorCode: p.errorCode,
3341
+ errorMessage: p.severity === "danger" ? `[CDP Firewall] ${p.name} blocked \u2014 synthetic event (isTrusted=false)` : `[CDP Firewall] ${p.name} detected \u2014 event simulation`
3342
+ };
3343
+ }
3344
+ }
3345
+ return null;
3346
+ }
3347
+ };
3348
+
3349
+ // src/cdp-interceptor/rules/emulation-override.ts
3350
+ var OVERRIDE_PATTERNS = [
3351
+ // ── Emulation overrides ──────────────────────────────
3352
+ // NOTE: Emulation.setDeviceMetricsOverride is NOT included here because
3353
+ // Playwright calls it internally for every new page (viewport setup).
3354
+ // Blocking it would break page creation.
3355
+ { method: "Emulation.setUserAgentOverride", name: "Emulation.setUserAgentOverride", severity: "danger", errorCode: -32080, suggestion: "navigator.userAgent override can be detected by checking consistency with navigator.plugins, WebGL vendor, etc." },
3356
+ { method: "Emulation.setTouchEmulationEnabled", name: "Emulation.setTouchEmulationEnabled", severity: "warn", errorCode: -32081, suggestion: "Touch emulation creates inconsistent touch/mouse state. Windows touch events without real touch hardware." },
3357
+ { method: "Emulation.setGeolocationOverride", name: "Emulation.setGeolocationOverride", severity: "danger", errorCode: -32082, suggestion: "Geolocation changing mid-session without user travel is impossible. Detectable via IP geo vs overridden geo." },
3358
+ { method: "Emulation.setLocaleOverride", name: "Emulation.setLocaleOverride", severity: "warn", errorCode: -32081, suggestion: "Locale change without browser restart detectable via navigator.languages vs Accept-Language consistency." },
3359
+ { method: "Emulation.setTimezoneOverride", name: "Emulation.setTimezoneOverride", severity: "danger", errorCode: -32082, suggestion: "Timezone mismatch vs IP geolocation + Date() is 100% detectable." },
3360
+ { method: "Emulation.setDisabledImageTypes", name: "Emulation.setDisabledImageTypes", severity: "info", errorCode: -32083, suggestion: "Disabling image types prevents normal resource loading \u2014 visible to performance API." },
3361
+ { method: "Emulation.setScriptExecutionDisabled", name: "Emulation.setScriptExecutionDisabled", severity: "info", errorCode: -32083, suggestion: "Disabling JS mid-session kills page interactivity \u2014 immediately obvious." },
3362
+ { method: "Emulation.setCPUThrottlingRate", name: "Emulation.setCPUThrottlingRate", severity: "info", errorCode: -32083, suggestion: "CPU throttling creates unrealistic performance.now() profiles." },
3363
+ { method: "Emulation.setVirtualTimePolicy", name: "Emulation.setVirtualTimePolicy", severity: "info", errorCode: -32083, suggestion: "Virtual time breaks Date.now() and performance.now() based detections \u2014 detectable via timer drift." },
3364
+ // ── Network overrides ────────────────────────────────
3365
+ { method: "Network.setUserAgentOverride", name: "Network.setUserAgentOverride (HTTP layer)", severity: "danger", errorCode: -32080, suggestion: "HTTP User-Agent vs navigator.userAgent inconsistency = immediate detection." },
3366
+ { method: "Network.setExtraHTTPHeaders", name: "Network.setExtraHTTPHeaders", severity: "danger", errorCode: -32084, suggestion: "Custom headers can conflict with browser-generated headers. Missing client hints (Sec-CH-UA) are also detectable." },
3367
+ { method: "Network.emulateNetworkConditions", name: "Network.emulateNetworkConditions", severity: "warn", errorCode: -32081, suggestion: "Network throttling creates unrealistic load timing patterns." },
3368
+ { method: "Network.setCookie", name: "Network.setCookie", severity: "warn", errorCode: -32085, suggestion: "CDP-injected cookies are detectable via document.cookie vs Network.getCookies inconsistency." },
3369
+ { method: "Network.deleteCookies", name: "Network.deleteCookies", severity: "warn", errorCode: -32085, suggestion: "Cookie deletion via CDP bypasses HTTP cookie expiration \u2014 detectable." },
3370
+ // ── Security overrides ──────────────────────────────
3371
+ { method: "Security.setIgnoreCertificateErrors", name: "Security.setIgnoreCertificateErrors", severity: "info", errorCode: -32086, suggestion: "Ignoring certificate errors creates unusual TLS behavior visible at network level." },
3372
+ // ── Page overrides ──────────────────────────────────
3373
+ { method: "Page.setDownloadBehavior", name: "Page.setDownloadBehavior", severity: "info", errorCode: -32087, suggestion: "Bypassing download dialogs \u2014 detectable via download event flow." },
3374
+ { method: "Page.setWebLifecycleState", name: "Page.setWebLifecycleState", severity: "info", errorCode: -32087, suggestion: "Forcing page lifecycle transitions is unnatural." },
3375
+ // ── Storage / Permissions ───────────────────────────
3376
+ { method: "Storage.clearDataForOrigin", name: "Storage.clearDataForOrigin", severity: "info", errorCode: -32089, suggestion: "Clearing storage mid-session is unnatural for real users." },
3377
+ { method: "Browser.grantPermissions", name: "Browser.grantPermissions", severity: "warn", errorCode: -32090, suggestion: "Granting permissions via CDP is detectable as the permission flow skips the user prompt." },
3378
+ { method: "Browser.resetPermissions", name: "Browser.resetPermissions", severity: "info", errorCode: -32089, suggestion: "Permission resets without user action are unnatural." }
3379
+ ];
3380
+ var emulationOverrideRule = {
3381
+ id: "emulation-override",
3382
+ name: "CDP Emulation / Override Detection (20+ methods)",
3383
+ priority: 60,
3384
+ canHandle(ctx) {
3385
+ for (const p of OVERRIDE_PATTERNS) {
3386
+ if (ctx.method === p.method) return true;
3387
+ }
3388
+ return false;
3389
+ },
3390
+ evaluate(ctx) {
3391
+ for (const p of OVERRIDE_PATTERNS) {
3392
+ if (ctx.method !== p.method) continue;
3393
+ return {
3394
+ ruleId: "emulation-override",
3395
+ action: "block",
3396
+ severity: p.severity,
3397
+ reason: `CDP emulation/override detected: "${p.name}". This creates detectable inconsistencies between the JS environment and real browser state.`,
3398
+ suggestion: p.suggestion,
3399
+ errorCode: p.errorCode,
3400
+ errorMessage: `[CDP Firewall] ${p.name} blocked \u2014 creates detectable browser state inconsistency`
3401
+ };
3402
+ }
3403
+ return null;
3404
+ }
3405
+ };
3406
+
3407
+ // src/cdp-interceptor/rules/network-anomaly.ts
3408
+ var networkAnomalyRule = {
3409
+ id: "network-anomaly",
3410
+ name: "Network Anomaly Detection (8+ patterns)",
3411
+ priority: 70,
3412
+ canHandle(ctx) {
3413
+ const m = ctx.method;
3414
+ return m === "Network.setExtraHTTPHeaders" || m === "Network.clearBrowserCache" || m === "Network.clearBrowserCookies" || m === "Network.setBlockedURLs" || m === "Network.setBypassServiceWorker" || m === "Fetch.enable" || m === "Network.enable";
3415
+ },
3416
+ evaluate(ctx) {
3417
+ switch (ctx.method) {
3418
+ case "Network.setExtraHTTPHeaders": {
3419
+ const headers = ctx.params.headers;
3420
+ if (headers) {
3421
+ const headerStr = JSON.stringify(headers).toLowerCase();
3422
+ if (!headerStr.includes("sec-ch-ua")) {
3423
+ return {
3424
+ ruleId: "network-anomaly",
3425
+ action: "block",
3426
+ severity: "warn",
3427
+ reason: "Network.setExtraHTTPHeaders called without Sec-CH-UA client hints \u2014 browser normally sends these.",
3428
+ suggestion: "Modern browsers send Sec-CH-UA headers automatically. Adding custom headers without them creates detectable inconsistency.",
3429
+ errorCode: -32110,
3430
+ errorMessage: "[CDP Firewall] Missing client hints in custom headers"
3431
+ };
3432
+ }
3433
+ }
3434
+ return null;
3435
+ }
3436
+ case "Network.clearBrowserCache": {
3437
+ return {
3438
+ ruleId: "network-anomaly",
3439
+ action: "block",
3440
+ severity: "warn",
3441
+ reason: "Network.clearBrowserCache called \u2014 cache clearing mid-session is unnatural for real users.",
3442
+ suggestion: "Avoid cache clearing during sessions. Start with a fresh profile if needed.",
3443
+ errorCode: -32111,
3444
+ errorMessage: "[CDP Firewall] Cache clearing detected"
3445
+ };
3446
+ }
3447
+ case "Network.clearBrowserCookies": {
3448
+ return {
3449
+ ruleId: "network-anomaly",
3450
+ action: "block",
3451
+ severity: "warn",
3452
+ reason: "Network.clearBrowserCookies called \u2014 wiping cookies mid-session is a scraper optimization.",
3453
+ suggestion: "Cookies should only be cleared via normal browser flow (expiration, user action).",
3454
+ errorCode: -32112,
3455
+ errorMessage: "[CDP Firewall] Cookie clearing detected"
3456
+ };
3457
+ }
3458
+ case "Network.setBlockedURLs": {
3459
+ return {
3460
+ ruleId: "network-anomaly",
3461
+ action: "block",
3462
+ severity: "warn",
3463
+ reason: "Network.setBlockedURLs blocks resource loading \u2014 this changes the page behavior and is detectable.",
3464
+ suggestion: "Blocking images/fonts/etc creates measurable differences in performance and page rendering.",
3465
+ errorCode: -32113,
3466
+ errorMessage: "[CDP Firewall] URL blocking detected"
3467
+ };
3468
+ }
3469
+ case "Network.setBypassServiceWorker": {
3470
+ return {
3471
+ ruleId: "network-anomaly",
3472
+ action: "block",
3473
+ severity: "info",
3474
+ reason: "Network.setBypassServiceWorker called \u2014 bypassing service workers for content extraction.",
3475
+ suggestion: "Service worker bypass changes fetch behavior and is detectable server-side.",
3476
+ errorCode: -32114,
3477
+ errorMessage: "[CDP Firewall] Service worker bypass detected"
3478
+ };
3479
+ }
3480
+ case "Fetch.enable": {
3481
+ return {
3482
+ ruleId: "network-anomaly",
3483
+ action: "block",
3484
+ severity: "warn",
3485
+ reason: "Fetch.enable intercepts all network requests \u2014 a man-in-the-middle approach used by scrapers.",
3486
+ suggestion: "Do not use Fetch domain for network interception if avoiding detection.",
3487
+ errorCode: -32115,
3488
+ errorMessage: "[CDP Firewall] Fetch interception detected"
3489
+ };
3490
+ }
3491
+ default:
3492
+ return null;
3493
+ }
3494
+ }
3495
+ };
3496
+
3497
+ // src/cdp-interceptor/rules/page-lifecycle.ts
3498
+ var STAT_KEY = "lifecycle-tracker";
3499
+ var pageLifecycleRule = {
3500
+ id: "page-lifecycle",
3501
+ name: "Page Lifecycle Anomaly Detection (10+ patterns)",
3502
+ priority: 80,
3503
+ canHandle(ctx) {
3504
+ const m = ctx.method;
3505
+ return m === "Page.navigate" || m === "Page.captureScreenshot" || m === "Page.printToPDF" || m === "Page.reload" || m === "Page.close" || m === "Runtime.evaluate" || m === "Runtime.callFunctionOn";
3506
+ },
3507
+ evaluate(ctx) {
3508
+ let state = ctx.sessionState.get(STAT_KEY);
3509
+ if (!state) {
3510
+ state = { navigations: [], screenshots: 0, printToPDF: false, evaluateCount: 0, lastNavTime: 0, lastEvalTime: 0 };
3511
+ ctx.sessionState.set(STAT_KEY, state);
3512
+ }
3513
+ const now = Date.now();
3514
+ switch (ctx.method) {
3515
+ case "Page.navigate": {
3516
+ state.navigations.push(now);
3517
+ if (state.navigations.length >= 2) {
3518
+ const prev = state.navigations[state.navigations.length - 2];
3519
+ const interval = now - prev;
3520
+ if (interval < 100) {
3521
+ return {
3522
+ ruleId: "page-lifecycle",
3523
+ action: "block",
3524
+ severity: "warn",
3525
+ reason: `Multiple Page.navigate calls within ${interval}ms of each other \u2014 unnatural rapid navigation.`,
3526
+ suggestion: "Add proper waits between navigations: wait for page load before navigating again.",
3527
+ errorCode: -32100,
3528
+ errorMessage: "[CDP Firewall] Rapid navigation sequence detected"
3529
+ };
3530
+ }
3531
+ }
3532
+ state.lastNavTime = now;
3533
+ return null;
3534
+ }
3535
+ case "Page.captureScreenshot": {
3536
+ state.screenshots++;
3537
+ if (state.lastNavTime > 0 && now - state.lastNavTime < 500) {
3538
+ return {
3539
+ ruleId: "page-lifecycle",
3540
+ action: "block",
3541
+ severity: "danger",
3542
+ reason: "Page.captureScreenshot called within 500ms of navigation \u2014 content extraction pattern.",
3543
+ suggestion: "Wait for the page to fully render before taking screenshots: wait for load/networkidle.",
3544
+ errorCode: -32101,
3545
+ errorMessage: "[CDP Firewall] Pre-render screenshot blocked \u2014 content extraction pattern"
3546
+ };
3547
+ }
3548
+ if (state.screenshots > 3 && state.navigations.length < 2) {
3549
+ return {
3550
+ ruleId: "page-lifecycle",
3551
+ action: "block",
3552
+ severity: "warn",
3553
+ reason: "Multiple screenshots on a single page without navigation \u2014 suspicious extraction behavior.",
3554
+ suggestion: "Consider if all screenshots are necessary.",
3555
+ errorCode: -32102,
3556
+ errorMessage: "[CDP Firewall] Excessive screenshot detection"
3557
+ };
3558
+ }
3559
+ return null;
3560
+ }
3561
+ case "Page.printToPDF": {
3562
+ return {
3563
+ ruleId: "page-lifecycle",
3564
+ action: "block",
3565
+ severity: "danger",
3566
+ reason: "Page.printToPDF called \u2014 this is a telltale scraper pattern that gives away automation intent.",
3567
+ suggestion: "Avoid PDF generation. If you must, add significant delays and user-like interaction first.",
3568
+ errorCode: -32103,
3569
+ errorMessage: "[CDP Firewall] printToPDF blocked \u2014 scraper intent detected"
3570
+ };
3571
+ }
3572
+ case "Page.reload": {
3573
+ if (state.lastNavTime > 0 && now - state.lastNavTime < 1e3) {
3574
+ return {
3575
+ ruleId: "page-lifecycle",
3576
+ action: "block",
3577
+ severity: "warn",
3578
+ reason: "Page.reload called immediately after navigate \u2014 unnatural fast-reload pattern.",
3579
+ suggestion: "Introduce delays between navigation and reload to simulate human behavior.",
3580
+ errorCode: -32104,
3581
+ errorMessage: "[CDP Firewall] Rapid reload detected"
3582
+ };
3583
+ }
3584
+ return null;
3585
+ }
3586
+ case "Page.close": {
3587
+ if (state.navigations.length < 2) {
3588
+ return {
3589
+ ruleId: "page-lifecycle",
3590
+ action: "block",
3591
+ severity: "info",
3592
+ reason: "Page.close called after minimal interaction \u2014 zombie pages common in automation.",
3593
+ suggestion: "Ensure meaningful interaction before closing pages.",
3594
+ errorCode: -32105,
3595
+ errorMessage: "[CDP Firewall] Page close after minimal interaction"
3596
+ };
3597
+ }
3598
+ return null;
3599
+ }
3600
+ case "Runtime.evaluate":
3601
+ case "Runtime.callFunctionOn": {
3602
+ state.evaluateCount++;
3603
+ if (state.evaluateCount > 50 && state.navigations.length === 0) {
3604
+ return {
3605
+ ruleId: "page-lifecycle",
3606
+ action: "block",
3607
+ severity: "info",
3608
+ reason: "50+ evaluate calls without any Page.navigate \u2014 data extraction without real browsing.",
3609
+ suggestion: "Navigate to a real page first. Evaluate on about:blank is suspicious.",
3610
+ errorCode: -32106,
3611
+ errorMessage: "[CDP Firewall] Excessive evaluate without navigation"
3612
+ };
3613
+ }
3614
+ return null;
3615
+ }
3616
+ default:
3617
+ return null;
3618
+ }
3619
+ }
3620
+ };
3621
+
3622
+ // src/cdp-interceptor/rules-engine.ts
3623
+ var BUILTIN_RULES = [
3624
+ domMutationRule,
3625
+ automationSignalsRule,
3626
+ fingerprintingRule,
3627
+ eventSimulationRule,
3628
+ mouseTrajectoryRule,
3629
+ inputKeystrokeRule,
3630
+ emulationOverrideRule,
3631
+ networkAnomalyRule,
3632
+ pageLifecycleRule
3633
+ ];
3634
+ function createRuleEngine(customRules) {
3635
+ const rules = [...BUILTIN_RULES, ...customRules ?? []].sort((a, b) => a.priority - b.priority);
3636
+ const sessionStates = /* @__PURE__ */ new Map();
3637
+ function getSessionState(sessionId) {
3638
+ let state = sessionStates.get(sessionId);
3639
+ if (!state) {
3640
+ state = /* @__PURE__ */ new Map();
3641
+ sessionStates.set(sessionId, state);
3642
+ }
3643
+ return state;
3644
+ }
3645
+ return {
3646
+ start() {
3647
+ sessionStates.clear();
3648
+ },
3649
+ stop() {
3650
+ sessionStates.clear();
3651
+ },
3652
+ evaluate(ctx) {
3653
+ const fullCtx = {
3654
+ ...ctx,
3655
+ sessionState: getSessionState(ctx.sessionId)
3656
+ };
3657
+ for (const rule of rules) {
3658
+ if (rule.canHandle && !rule.canHandle(fullCtx)) continue;
3659
+ const decision = rule.evaluate(fullCtx);
3660
+ if (!decision) continue;
3661
+ if (decision.action !== "pass") return decision;
3662
+ }
3663
+ return null;
3664
+ }
3665
+ };
3666
+ }
3667
+
3668
+ // src/cdp-interceptor/logger.ts
3669
+ function createLogger(config) {
3670
+ const buffer = [];
3671
+ const MAX_BUFFER = 2e3;
3672
+ return {
3673
+ info(message, meta) {
3674
+ if (!config.enableLogging) return;
3675
+ const ts = (/* @__PURE__ */ new Date()).toISOString();
3676
+ if (meta) {
3677
+ console.log(`[CDPInterceptor ${ts}] ${message}`, JSON.stringify(meta));
3678
+ } else {
3679
+ console.log(`[CDPInterceptor ${ts}] ${message}`);
3680
+ }
3681
+ },
3682
+ log(method, direction, sessionId, payload, decision) {
3683
+ const entry = {
3684
+ timestamp: Date.now(),
3685
+ direction,
3686
+ sessionId,
3687
+ method,
3688
+ payload: sanitizePayload(payload),
3689
+ decision: decision ?? void 0
3690
+ };
3691
+ if (config.enableLogging) {
3692
+ buffer.push(entry);
3693
+ if (buffer.length > MAX_BUFFER) buffer.shift();
3694
+ const tag = decision ? decision.action === "block" ? "\u{1F6AB}BLOCK" : decision.action === "transform" ? "\u{1F504}XFMR" : "\u2705" : " ";
3695
+ const reason = decision ? ` [${decision.severity}] ${decision.reason}` : "";
3696
+ console.log(`[CDP] ${tag} ${direction} ${method}${reason}`);
3697
+ }
3698
+ return entry;
3699
+ },
3700
+ getRecent(count) {
3701
+ return buffer.slice(-count);
3702
+ },
3703
+ flush() {
3704
+ buffer.length = 0;
3705
+ }
3706
+ };
3707
+ }
3708
+ function sanitizePayload(payload) {
3709
+ if (typeof payload !== "object" || payload === null) return { raw: String(payload) };
3710
+ const obj = payload;
3711
+ const cleaned = {};
3712
+ for (const [key, value] of Object.entries(obj)) {
3713
+ if (key === "data" && typeof value === "string" && value.length > 200) {
3714
+ cleaned[key] = `<binary: ${value.length} chars>`;
3715
+ } else if (key === "expression" && typeof value === "string" && value.length > 500) {
3716
+ cleaned[key] = value.substring(0, 500) + "...";
3717
+ } else {
3718
+ cleaned[key] = value;
3719
+ }
3720
+ }
3721
+ return cleaned;
3722
+ }
3723
+
3724
+ // src/cdp-interceptor/advisor.ts
3725
+ function formatBlockMessage(decision, method) {
3726
+ const adv = advise(decision, method);
3727
+ const lines = [
3728
+ `[CDP-FIREWALL-BLOCK] rule=${decision.ruleId}`,
3729
+ `method=${method}`,
3730
+ `reason=${decision.reason}`,
3731
+ `suggestion=${decision.suggestion ?? adv.detail}`
3732
+ ];
3733
+ if (adv.codeExample) {
3734
+ lines.push(`code-example=`);
3735
+ lines.push(adv.codeExample);
3736
+ }
3737
+ return lines.join("\n");
3738
+ }
3739
+ function advise(decision, originalMethod) {
3740
+ const baseAdvice = getBaseAdvice(decision, originalMethod);
3741
+ return {
3742
+ ruleId: decision.ruleId,
3743
+ title: baseAdvice.title,
3744
+ detail: decision.suggestion ?? baseAdvice.detail,
3745
+ codeExample: baseAdvice.codeExample
3746
+ };
3747
+ }
3748
+ function getBaseAdvice(decision, method) {
3749
+ switch (decision.ruleId) {
3750
+ case "dom-mutation":
3751
+ return {
3752
+ title: "Direct DOM property mutation blocked",
3753
+ detail: `Your ${method} call tried to set a DOM property directly. In React/Vue/Angular, this bypasses the framework's virtual DOM completely meaning onChange/onInput never fires. The website CAN detect this mismatch as automation.`,
3754
+ codeExample: [
3755
+ "# \u274C BLOCKED \u2014 what you tried to do:",
3756
+ `page.evaluate(\`el.value = 'hello'\`) # triggers isTrusted=false`,
3757
+ "",
3758
+ "# \u2705 USE INSTEAD \u2014 proper CDP input dispatch:",
3759
+ "page.fill('#selector', 'hello') # Playwright: dispatches input+change events",
3760
+ "page.type('#selector', 'hello', {delay}) # Playwright: real keystrokes",
3761
+ "page.locator('#selector').fill('hello') # Playwright: recommended API"
3762
+ ].join("\n")
3763
+ };
3764
+ case "mouse-trajectory":
3765
+ return {
3766
+ title: "Unnatural mouse trajectory blocked",
3767
+ detail: `Your ${method} sequence formed a perfectly linear path. Human hands have micro-tremors (1-3px variation at any point along the arc), acceleration curves, and never draw straight lines between distant points.`,
3768
+ codeExample: [
3769
+ "# \u274C BLOCKED \u2014 linear interpolation:",
3770
+ "for i in range(20):",
3771
+ " page.mouse.move(x0 + (x1-x0)*(i/20), y0 + (y1-y0)*(i/20))",
3772
+ "",
3773
+ "# \u2705 USE INSTEAD \u2014 Bezier curves with overshoot:",
3774
+ '# Use "ghost-cursor" or similar library',
3775
+ "from ghost_cursor import path_to",
3776
+ "path_to(page, (x1, y1))"
3777
+ ].join("\n")
3778
+ };
3779
+ case "input-keystroke":
3780
+ if (method === "Input.insertText") {
3781
+ return {
3782
+ title: "Input.insertText detected (logged, not blocked)",
3783
+ detail: "Input.insertText bypasses native keyDown\u2192keyPress\u2192input\u2192keyUp events. Playwright uses this internally for page.fill(). Logged for observability."
3784
+ };
3785
+ }
3786
+ return {
3787
+ title: "Unnatural keystroke timing blocked",
3788
+ detail: `Your ${method} calls have unnaturally constant timing (e.g., exact 50ms intervals). Human typing always has variation (CV > 0.2).`,
3789
+ codeExample: [
3790
+ "# \u274C BLOCKED \u2014 exact constant delay:",
3791
+ "page.type('#input', 'hello', {delay: 50}) # every keystroke exactly 50ms apart",
3792
+ "",
3793
+ "# \u2705 USE INSTEAD \u2014 variable delay (human-like):",
3794
+ "page.fill('#input', 'hello') # recommended, dispatches events properly",
3795
+ "# OR: type with randomized delay",
3796
+ "page.type('#input', 'hello', {delay: 50 + Math.floor(Math.random() * 80)})"
3797
+ ].join("\n")
3798
+ };
3799
+ case "automation-signals":
3800
+ return {
3801
+ title: "Browser automation marker detected",
3802
+ detail: `Your ${method} call accessed a property/marker that anti-crawler systems check to detect automation. These markers (navigator.webdriver, window.__playwright, etc.) are the #1 detection vector.`,
3803
+ codeExample: [
3804
+ "# \u274C BLOCKED \u2014 don't check for automation markers:",
3805
+ "navigator.webdriver # NEVER check this",
3806
+ "window.__playwright # NEVER check this",
3807
+ "chrome.runtime # NEVER check this",
3808
+ "",
3809
+ "# \u2705 Just go about your business normally.",
3810
+ "# Anti-detection is handled by the CDP firewall automatically."
3811
+ ].join("\n")
3812
+ };
3813
+ case "fingerprinting":
3814
+ return {
3815
+ title: "Browser fingerprinting API access blocked",
3816
+ detail: `Your ${method} call accessed a known fingerprinting API. These APIs (canvas.toDataURL, WebGL getParameter, AudioContext, etc.) are used by anti-crawler systems to build a unique device fingerprint.`,
3817
+ codeExample: [
3818
+ "# \u274C BLOCKED \u2014 fingerprinting vector:",
3819
+ "canvas.toDataURL() # returns unique browser hash",
3820
+ 'gl.getParameter(gl.VENDOR) # returns "SwiftShader" in headless',
3821
+ "screen.availWidth - screen.availHeight # no OS chrome in headless",
3822
+ "",
3823
+ "# \u2705 Avoid accessing these APIs. They are only used for fingerprinting."
3824
+ ].join("\n")
3825
+ };
3826
+ case "event-simulation":
3827
+ return {
3828
+ title: "Synthetic event simulation blocked",
3829
+ detail: `Your ${method} call simulated user interaction via el.click() or dispatchEvent(new Event(...)). These produce isTrusted=false events, which are 100% detectable by any anti-crawler that checks isTrusted on critical events.`,
3830
+ codeExample: [
3831
+ "# \u274C BLOCKED \u2014 synthetic events (isTrusted=false):",
3832
+ "el.click() # isTrusted=false",
3833
+ 'el.dispatchEvent(new Event("click")) # isTrusted=false',
3834
+ "el.focus() # isTrusted=false",
3835
+ "",
3836
+ "# \u2705 USE INSTEAD \u2014 CDP-level input dispatch (isTrusted=true):",
3837
+ "page.click(selector) # uses Input.dispatchMouseEvent",
3838
+ "page.fill(selector, value) # dispatches real input events"
3839
+ ].join("\n")
3840
+ };
3841
+ case "emulation-override":
3842
+ return {
3843
+ title: "CDP emulation override blocked",
3844
+ detail: `Your ${method} call overrides browser behavior in a way that creates detectable inconsistencies. Anti-crawler systems cross-check multiple sources (e.g., navigator.userAgent vs HTTP User-Agent header) to catch these mismatches.`,
3845
+ codeExample: [
3846
+ "# \u274C BLOCKED \u2014 detectable emulation override:",
3847
+ "Emulation.setUserAgentOverride(...) # JS vs HTTP header mismatch",
3848
+ "Emulation.setGeolocationOverride(...) # IP geo vs overridden geo mismatch",
3849
+ "Emulation.setDeviceMetricsOverride(...) # matchMedia vs actual viewport",
3850
+ "",
3851
+ "# \u2705 These are handled automatically by the CDP firewall.",
3852
+ "# Do NOT call them manually."
3853
+ ].join("\n")
3854
+ };
3855
+ case "network-anomaly":
3856
+ return {
3857
+ title: "Network anomaly detected",
3858
+ detail: `Your ${method} call triggered a network pattern that is characteristic of scrapers: blocking URLs, clearing caches, or intercepting requests.`,
3859
+ codeExample: [
3860
+ "# \u274C BLOCKED \u2014 scraper optimization:",
3861
+ "Network.clearBrowserCache() # natural users never do this",
3862
+ 'Network.setBlockedURLs(["*fonts*"]) # blocking resources is detectable',
3863
+ "Fetch.enable() # MITM-style interception",
3864
+ "",
3865
+ "# \u2705 Let the browser manage its own cache and network normally."
3866
+ ].join("\n")
3867
+ };
3868
+ case "page-lifecycle":
3869
+ return {
3870
+ title: "Suspicious page lifecycle pattern blocked",
3871
+ detail: `Your ${method} call reveals an unnatural page interaction sequence: navigating too fast, taking screenshots before the page renders, or generating PDFs (a telltale scraper giveaway).`,
3872
+ codeExample: [
3873
+ "# \u274C BLOCKED \u2014 unnatural lifecycle:",
3874
+ "page.goto(url); page.pdf() # PDF = scraper giveaway",
3875
+ "page.goto(url); page.screenshot() <500ms # screenshot before render",
3876
+ "page.goto(url) 3x in <100ms # rapid navigation barrage",
3877
+ "",
3878
+ "# \u2705 Add proper waits between actions:",
3879
+ 'page.goto(url, {waitUntil: "networkidle"})',
3880
+ 'page.waitForSelector("body")',
3881
+ "page.screenshot() # after rendering"
3882
+ ].join("\n")
3883
+ };
3884
+ default:
3885
+ return {
3886
+ title: decision.reason,
3887
+ detail: decision.suggestion ?? `The CDP call "${method}" was blocked by rule "${decision.ruleId}".`
3888
+ };
3889
+ }
3890
+ }
3891
+
3892
+ // src/cdp-interceptor/proxy.ts
3893
+ function makeCompoundId(cdpSessionId, rawSessionId) {
3894
+ return `${cdpSessionId ?? "nil"}::${rawSessionId ?? "nil"}`;
3895
+ }
3896
+ var CDPInterceptorProxy = class {
3897
+ wss = null;
3898
+ engine;
3899
+ config;
3900
+ logger;
3901
+ started = false;
3902
+ stats = {
3903
+ totalMessages: 0,
3904
+ blockedMessages: 0,
3905
+ transformedMessages: 0,
3906
+ passedMessages: 0,
3907
+ byRule: {}
3908
+ };
3909
+ constructor(config) {
3910
+ this.config = config;
3911
+ this.engine = createRuleEngine(config.rules);
3912
+ this.logger = createLogger({
3913
+ enableLogging: config.enableLogging ?? true,
3914
+ logDir: config.logDir
3915
+ });
3916
+ }
3917
+ /** The port the proxy is listening on (only valid after start()) */
3918
+ get port() {
3919
+ const addr = this.wss?.address();
3920
+ if (addr && typeof addr === "object") return addr.port;
3921
+ return 0;
3922
+ }
3923
+ /** Start the proxy server */
3924
+ async start() {
3925
+ if (this.started) return this.port;
3926
+ return new Promise((resolve, reject) => {
3927
+ this.wss = new WebSocketServer({ port: this.config.listenPort ?? 0 }, () => {
3928
+ const port = this.port;
3929
+ this.engine.start();
3930
+ this.started = true;
3931
+ this.logger.info("CDP interceptor proxy started", { port, endpoint: this.config.cdpEndpoint });
3932
+ resolve(port);
3933
+ });
3934
+ this.wss.on("error", reject);
3935
+ this.wss.on("connection", (clientWs, _req) => {
3936
+ this.handleConnection(clientWs);
3937
+ });
3938
+ });
3939
+ }
3940
+ /** Stop the proxy server */
3941
+ async stop() {
3942
+ this.engine.stop();
3943
+ this.logger.flush();
3944
+ this.started = false;
3945
+ return new Promise((resolve) => {
3946
+ if (!this.wss) return resolve();
3947
+ this.wss.close(() => resolve());
3948
+ this.wss = null;
3949
+ });
3950
+ }
3951
+ /** Get accumulated statistics */
3952
+ getStats() {
3953
+ return { ...this.stats };
3954
+ }
3955
+ /** Get recent log entries (for inspection) */
3956
+ getRecentLogs(count = 50) {
3957
+ return this.logger.getRecent(count);
3958
+ }
3959
+ // ── Connection handling ──────────────────────────────────────
3960
+ handleConnection(clientWs) {
3961
+ let browserWs = null;
3962
+ let isAlive = true;
3963
+ const pendingMessages = [];
3964
+ browserWs = new WebSocket2(this.config.cdpEndpoint);
3965
+ clientWs.on("message", (raw) => {
3966
+ if (browserWs && browserWs.readyState === WebSocket2.OPEN) {
3967
+ this.handleClientMessage(clientWs, browserWs, raw);
3968
+ } else {
3969
+ pendingMessages.push(raw);
3970
+ }
3971
+ });
3972
+ browserWs.on("open", () => {
3973
+ for (const buf of pendingMessages) {
3974
+ this.handleClientMessage(clientWs, browserWs, buf);
3975
+ }
3976
+ pendingMessages.length = 0;
3977
+ });
3978
+ browserWs.on("error", (err) => {
3979
+ this.logger.info("Browser WebSocket error", { error: String(err) });
3980
+ });
3981
+ browserWs.on("close", (code, reason) => {
3982
+ if (isAlive && clientWs.readyState === WebSocket2.OPEN) {
3983
+ this.logger.info("Browser WS closed, closing client", { code, reason: String(reason) });
3984
+ clientWs.close();
3985
+ }
3986
+ });
3987
+ browserWs.on("message", (raw) => {
3988
+ this.handleBrowserMessage(clientWs, browserWs, raw);
3989
+ });
3990
+ const cleanup = () => {
3991
+ isAlive = false;
3992
+ if (browserWs && browserWs.readyState === WebSocket2.OPEN) {
3993
+ browserWs.close();
3994
+ }
3995
+ };
3996
+ clientWs.on("close", cleanup);
3997
+ clientWs.on("error", cleanup);
3998
+ browserWs.on("close", () => {
3999
+ if (isAlive && clientWs.readyState === WebSocket2.OPEN) {
4000
+ clientWs.close();
4001
+ }
4002
+ });
4003
+ browserWs.on("error", cleanup);
4004
+ }
4005
+ // ── Message processing ───────────────────────────────────────
4006
+ handleClientMessage(clientWs, browserWs, raw) {
4007
+ const msg = this.parseMessage(raw);
4008
+ if (!msg) return;
4009
+ this.stats.totalMessages++;
4010
+ if (!("method" in msg)) {
4011
+ browserWs.send(raw.toString());
4012
+ return;
4013
+ }
4014
+ const request = msg;
4015
+ const ctx = {
4016
+ method: request.method,
4017
+ params: request.params ?? {},
4018
+ sessionId: makeCompoundId(browserWs._cdpSession, request.sessionId),
4019
+ direction: "client\u2192browser"
4020
+ };
4021
+ const decision = this.engine.evaluate(ctx);
4022
+ this.logger.log(ctx.method, "client\u2192browser", ctx.sessionId, { method: ctx.method, params: ctx.params }, decision);
4023
+ if (decision) {
4024
+ this.recordDecision(decision);
4025
+ }
4026
+ if (decision?.action === "block") {
4027
+ const blockMsg = formatBlockMessage(decision, ctx.method);
4028
+ const errorResponse = {
4029
+ id: request.id,
4030
+ error: {
4031
+ code: decision.errorCode ?? -32e3,
4032
+ message: blockMsg
4033
+ },
4034
+ sessionId: request.sessionId
4035
+ };
4036
+ this.stats.blockedMessages++;
4037
+ console.error(`
4038
+ ${blockMsg}
4039
+ `);
4040
+ clientWs.send(JSON.stringify(errorResponse));
4041
+ return;
4042
+ }
4043
+ if (decision?.action === "transform" && decision.transformedParams) {
4044
+ const transformed = { ...request, params: decision.transformedParams };
4045
+ this.stats.transformedMessages++;
4046
+ browserWs.send(JSON.stringify(transformed));
4047
+ return;
4048
+ }
4049
+ this.stats.passedMessages++;
4050
+ browserWs.send(raw.toString());
4051
+ }
4052
+ handleBrowserMessage(clientWs, _browserWs, raw) {
4053
+ const msg = this.parseMessage(raw);
4054
+ if (!msg) {
4055
+ clientWs.send(raw.toString());
4056
+ return;
4057
+ }
4058
+ if ("method" in msg) {
4059
+ const event = msg;
4060
+ const ctx = {
4061
+ method: event.method,
4062
+ params: event.params ?? {},
4063
+ sessionId: event.sessionId ?? "browser",
4064
+ direction: "browser\u2192client"
4065
+ };
4066
+ const decision = this.engine.evaluate(ctx);
4067
+ if (decision?.action === "block") {
4068
+ return;
4069
+ }
4070
+ }
4071
+ clientWs.send(raw.toString());
4072
+ }
4073
+ // ── Utilities ────────────────────────────────────────────────
4074
+ parseMessage(raw) {
4075
+ try {
4076
+ return JSON.parse(raw.toString());
4077
+ } catch {
4078
+ return null;
4079
+ }
4080
+ }
4081
+ recordDecision(decision) {
4082
+ if (!this.stats.byRule[decision.ruleId]) {
4083
+ this.stats.byRule[decision.ruleId] = { matched: 0, blocked: 0, transformed: 0 };
4084
+ }
4085
+ this.stats.byRule[decision.ruleId].matched++;
4086
+ if (decision.action === "block") {
4087
+ this.stats.byRule[decision.ruleId].blocked++;
4088
+ } else if (decision.action === "transform") {
4089
+ this.stats.byRule[decision.ruleId].transformed++;
4090
+ }
4091
+ }
4092
+ };
4093
+
4094
+ // src/utils/cdp.ts
4095
+ async function fetchNoProxy(url) {
4096
+ const savedProxy = {
4097
+ http_proxy: process.env.http_proxy,
4098
+ https_proxy: process.env.https_proxy,
4099
+ HTTP_PROXY: process.env.HTTP_PROXY,
4100
+ HTTPS_PROXY: process.env.HTTPS_PROXY,
4101
+ all_proxy: process.env.all_proxy,
4102
+ ALL_PROXY: process.env.ALL_PROXY
4103
+ };
4104
+ for (const key of Object.keys(savedProxy)) delete process.env[key];
4105
+ try {
4106
+ return await fetch(url);
4107
+ } finally {
4108
+ for (const [key, val] of Object.entries(savedProxy)) {
4109
+ if (val !== void 0) process.env[key] = val;
4110
+ }
4111
+ }
4112
+ }
4113
+ async function resolveCDPEndpoint(raw) {
4114
+ if (raw === "auto") {
4115
+ const httpResp = await fetchNoProxy("http://localhost:9222/json/version");
4116
+ const data = await httpResp.json();
4117
+ if (!data.webSocketDebuggerUrl) {
4118
+ throw new Error("Could not auto-discover CDP endpoint from localhost:9222");
4119
+ }
4120
+ return data.webSocketDebuggerUrl;
4121
+ }
4122
+ if (/^\d+$/.test(raw)) {
4123
+ const port = raw;
4124
+ const httpResp = await fetchNoProxy(`http://localhost:${port}/json/version`);
4125
+ const data = await httpResp.json();
4126
+ if (!data.webSocketDebuggerUrl) {
4127
+ throw new Error(`Could not discover CDP endpoint from localhost:${port}`);
4128
+ }
4129
+ return data.webSocketDebuggerUrl;
4130
+ }
4131
+ if (raw.startsWith("http://") || raw.startsWith("https://")) {
4132
+ try {
4133
+ const httpResp = await fetchNoProxy(`${raw}/json/version`);
4134
+ const data = await httpResp.json();
4135
+ if (!data.webSocketDebuggerUrl) {
4136
+ throw new Error(`Could not discover CDP endpoint from ${raw}`);
4137
+ }
4138
+ return data.webSocketDebuggerUrl;
4139
+ } catch (error) {
4140
+ console.warn(`Failed to fetch WebSocket URL from ${raw}, using endpoint directly: ${error instanceof Error ? error.message : String(error)}`);
4141
+ return raw;
4142
+ }
4143
+ }
4144
+ return raw;
4145
+ }
4146
+
4147
+ // src/browser.ts
4148
+ import { SessionStore } from "@dyyz1993/xcli-core";
4149
+ function logSessionEvent(event, details) {
4150
+ const ts = (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").substring(0, 19);
4151
+ const pid = process.pid;
4152
+ console.error(`[SESSION] ${ts} [PID:${pid}] ${event} | ${details}`);
4153
+ }
4154
+ var SESSION_DIR = join(homedir(), ".xbrowser", "sessions");
4155
+ function sessionFile(name) {
4156
+ return join(SESSION_DIR, `${name}.json`);
4157
+ }
4158
+ function ensureSessionDir() {
4159
+ mkdirSync(SESSION_DIR, { recursive: true });
4160
+ }
4161
+ var sessions = new SessionStore();
4162
+ var _sharedBrowser = null;
4163
+ var _sharedCdpProxy = null;
4164
+ var IDLE_TIMEOUT_MS = (process.env.XBROWSER_IDLE_TIMEOUT ? parseInt(process.env.XBROWSER_IDLE_TIMEOUT, 10) : 30) * 60 * 1e3;
4165
+ var idleTimer = null;
4166
+ function resetIdleTimer() {
4167
+ if (idleTimer) clearTimeout(idleTimer);
4168
+ idleTimer = setTimeout(async () => {
4169
+ const now = Date.now();
4170
+ let allIdle = true;
4171
+ const idleSessions = [];
4172
+ for (const s of sessions) {
4173
+ if (now - s.lastActivityAt < IDLE_TIMEOUT_MS) {
4174
+ allIdle = false;
4175
+ } else {
4176
+ idleSessions.push(`${s.name}(${(now - s.lastActivityAt) / 1e3}s idle)`);
4177
+ }
4178
+ }
4179
+ if (allIdle && (sessions.size > 0 || _sharedBrowser)) {
4180
+ logSessionEvent("idle_timeout", `Sessions idle for >${IDLE_TIMEOUT_MS / 6e4}min. Sessions: ${idleSessions.join(", ") || "all"}. Calling destroyBrowser()`);
4181
+ await destroyBrowser().catch(() => {
4182
+ });
4183
+ }
4184
+ }, IDLE_TIMEOUT_MS);
4185
+ if (idleTimer && typeof idleTimer.unref === "function") {
4186
+ idleTimer.unref();
4187
+ }
4188
+ }
4189
+ function touchSession(id) {
4190
+ const s = sessions.get(id);
4191
+ if (s) s.lastActivityAt = Date.now();
4192
+ resetIdleTimer();
4193
+ }
4194
+ process.on("exit", () => {
4195
+ for (const session of sessions.list()) {
4196
+ if (session.isCDP) {
4197
+ logSessionEvent("process_exit", `Session "${session.name}": CDP connection (not closing external browser).`);
4198
+ } else {
4199
+ logSessionEvent("process_exit", `Session "${session.name}": Closing browser.`);
4200
+ try {
4201
+ session.browser?.close();
4202
+ } catch {
4203
+ }
4204
+ }
4205
+ }
4206
+ if (_sharedBrowser) {
4207
+ logSessionEvent("process_exit", "Closing shared browser.");
4208
+ try {
4209
+ _sharedBrowser.close();
4210
+ } catch {
4211
+ }
4212
+ _sharedBrowser = null;
4213
+ }
4214
+ if (_sharedCdpProxy) {
4215
+ try {
4216
+ _sharedCdpProxy.stop();
4217
+ } catch {
4218
+ }
4219
+ _sharedCdpProxy = null;
4220
+ }
4221
+ sessions.clear();
4222
+ });
4223
+ async function getCDPTargets2(cdpEndpoint) {
4224
+ try {
4225
+ const ep = String(cdpEndpoint);
4226
+ let host = "localhost";
4227
+ let port = "9222";
4228
+ if (ep.startsWith("http://") || ep.startsWith("https://")) {
4229
+ const u = new URL(ep);
4230
+ host = u.hostname;
4231
+ port = u.port || "9222";
4232
+ } else if (/^\d+$/.test(ep)) {
4233
+ port = ep;
4234
+ }
4235
+ const url = `http://${host}:${port}/json/list`;
4236
+ const resp = await fetch(url);
4237
+ return await resp.json();
4238
+ } catch {
4239
+ return [];
4240
+ }
4241
+ }
4242
+ async function findTargetPage(cdpEndpoint, target) {
4243
+ const targets = await getCDPTargets2(cdpEndpoint);
4244
+ const pages = targets.filter((t) => t.url && !t.url.startsWith("about:blank") && !t.url.startsWith("chrome://"));
4245
+ const byId = pages.find((t) => t.id === target);
4246
+ if (byId) return { pageId: byId.id, wsUrl: byId.webSocketDebuggerUrl, title: byId.title, url: byId.url };
4247
+ const lowerTarget = target.toLowerCase();
4248
+ const byTitle = pages.find((t) => t.title && t.title.toLowerCase().includes(lowerTarget));
4249
+ if (byTitle) return { pageId: byTitle.id, wsUrl: byTitle.webSocketDebuggerUrl, title: byTitle.title, url: byTitle.url };
4250
+ const byUrl = pages.find((t) => t.url.toLowerCase().includes(lowerTarget));
4251
+ if (byUrl) return { pageId: byUrl.id, wsUrl: byUrl.webSocketDebuggerUrl, title: byUrl.title, url: byUrl.url };
4252
+ return null;
4253
+ }
4254
+ function resolveLaunchOpts(ctx) {
4255
+ if (ctx.cdpEndpoint) {
4256
+ return { cdpEndpoint: ctx.cdpEndpoint };
4257
+ }
4258
+ return { headless: true };
4259
+ }
4260
+ var CHROMIUM_CANDIDATES = [
4261
+ "/Applications/Chromium.app/Contents/MacOS/Chromium",
4262
+ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
4263
+ "/usr/bin/chromium-browser",
4264
+ "/usr/bin/chromium",
4265
+ "/usr/bin/google-chrome"
4266
+ ];
4267
+ function discoverChromiumPath() {
4268
+ for (const p of CHROMIUM_CANDIDATES) {
4269
+ if (existsSync(p)) return p;
4270
+ }
4271
+ return void 0;
4272
+ }
4273
+ async function createBrowser(options) {
4274
+ if (options?.cdpEndpoint) {
4275
+ const realEndpoint = await resolveCDPEndpoint(options.cdpEndpoint);
4276
+ if (options.intercept) {
4277
+ const config = typeof options.intercept === "object" ? { ...options.intercept, cdpEndpoint: realEndpoint } : { cdpEndpoint: realEndpoint };
4278
+ _sharedCdpProxy = new CDPInterceptorProxy(config);
4279
+ const proxyPort = await _sharedCdpProxy.start();
4280
+ console.error(`[CDP Interceptor] Proxy running on ws://localhost:${proxyPort}, forwarding to ${realEndpoint}`);
4281
+ const { browser: browser3 } = await launch({ cdpEndpoint: `ws://localhost:${proxyPort}` });
4282
+ return browser3;
4283
+ }
4284
+ const { browser: browser2 } = await launch({ cdpEndpoint: realEndpoint });
4285
+ await browser2.discoverContexts().catch((err) => {
4286
+ console.error(`[browser] discoverContexts failed: ${err.message}`);
4287
+ });
4288
+ return browser2;
4289
+ }
4290
+ const executablePath = options?.executablePath || process.env.XBROWSER_CHROMIUM_PATH || discoverChromiumPath();
4291
+ const { browser } = await launch({ executablePath, headless: options?.headless ?? true });
4292
+ return browser;
4293
+ }
4294
+ async function getBrowser(options) {
4295
+ if (_sharedBrowser) return _sharedBrowser;
4296
+ _sharedBrowser = await createBrowser(options);
4297
+ if (options?.cdpEndpoint && options.intercept) {
4298
+ }
4299
+ return _sharedBrowser;
4300
+ }
4301
+ function findSession(name) {
4302
+ return sessions.find(name);
4303
+ }
4304
+ function getSessionById(id) {
4305
+ return sessions.get(id);
4306
+ }
4307
+ function setActivePage(session, page) {
4308
+ session.page = page;
4309
+ session.lastActivityAt = Date.now();
4310
+ }
4311
+ function saveSessionDiskMeta(name, data) {
4312
+ ensureSessionDir();
4313
+ const file = sessionFile(name);
4314
+ let existing = {};
4315
+ try {
4316
+ existing = JSON.parse(readFileSync(file, "utf8"));
4317
+ } catch {
4318
+ }
4319
+ Object.assign(existing, data, { name });
4320
+ writeFileSync(file, JSON.stringify(existing, null, 2));
4321
+ }
4322
+ function readSessionDiskMeta(name) {
4323
+ const file = sessionFile(name);
4324
+ try {
4325
+ return JSON.parse(readFileSync(file, "utf8"));
4326
+ } catch {
4327
+ return null;
4328
+ }
4329
+ }
4330
+ function deleteSessionDiskMeta(name) {
4331
+ const file = sessionFile(name);
4332
+ try {
4333
+ unlinkSync(file);
4334
+ } catch {
4335
+ }
4336
+ }
4337
+ async function findOrRestoreSession(name, cdpEndpoint) {
4338
+ const inMem = findSession(name);
4339
+ if (inMem) return inMem;
4340
+ const meta = readSessionDiskMeta(name);
4341
+ if (!meta) return void 0;
4342
+ const ep = cdpEndpoint || meta.cdpEndpoint;
4343
+ if (!ep) return void 0;
4344
+ try {
4345
+ const b = await createBrowser({ cdpEndpoint: ep });
4346
+ await new Promise((r) => setTimeout(r, 500));
4347
+ let contexts = b.contexts();
4348
+ if (contexts.length === 0) {
4349
+ await new Promise((r) => setTimeout(r, 500));
4350
+ contexts = b.contexts();
4351
+ }
4352
+ const context = contexts[0] || await b.newContext();
4353
+ const savedUrl = meta.conversationUrl || meta.url;
4354
+ const targetHostname = savedUrl ? (() => {
4355
+ try {
4356
+ return new URL(savedUrl).hostname;
4357
+ } catch {
4358
+ return "";
4359
+ }
4360
+ })() : "";
4361
+ let page = null;
4362
+ let fallbackPage = null;
4363
+ for (const ctx of contexts) {
4364
+ const pages = ctx.pages();
4365
+ for (const p of pages) {
4366
+ const pUrl = p.url();
4367
+ if (pUrl && pUrl !== "about:blank" && !pUrl.startsWith("chrome://")) {
4368
+ if (targetHostname && pUrl.includes(targetHostname)) {
4369
+ page = p;
4370
+ break;
4371
+ }
4372
+ if (!fallbackPage) {
4373
+ fallbackPage = p;
4374
+ }
4375
+ }
4376
+ }
4377
+ if (page) break;
4378
+ }
4379
+ page = page || fallbackPage;
4380
+ if (!page) {
4381
+ const targets = await getCDPTargets2(ep);
4382
+ const matchTarget = targets.find(
4383
+ (t) => t.url && t.url !== "about:blank" && !t.url.startsWith("chrome://") && (targetHostname ? t.url.includes(targetHostname) : true)
4384
+ );
4385
+ if (matchTarget && matchTarget.url) {
4386
+ page = await context.newPage();
4387
+ await page.goto(matchTarget.url, { waitUntil: "domcontentloaded", timeout: 15e3 }).catch(() => {
4388
+ });
4389
+ }
4390
+ }
4391
+ if (!page) {
4392
+ const pages = context.pages();
4393
+ page = pages.length > 0 ? pages[0] : await context.newPage();
4394
+ }
4395
+ try {
4396
+ await Promise.race([
4397
+ page.evaluate(() => true),
4398
+ new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), 3e3))
4399
+ ]);
4400
+ } catch {
4401
+ console.log(`[Session] "${name}" restored page unresponsive, creating fresh session`);
4402
+ deleteSessionDiskMeta(name);
4403
+ return void 0;
4404
+ }
4405
+ const targetUrl = meta.conversationUrl || meta.url;
4406
+ if (targetUrl && page.url() !== targetUrl) {
4407
+ try {
4408
+ if (!page.url().includes(new URL(targetUrl).hostname)) {
4409
+ await page.goto(targetUrl, { waitUntil: "domcontentloaded", timeout: 3e4 }).catch(() => {
4410
+ });
4411
+ }
4412
+ } catch {
4413
+ }
4414
+ }
4415
+ const session = {
4416
+ id: meta.id || randomUUID(),
4417
+ name,
4418
+ context,
4419
+ page,
4420
+ browser: b,
4421
+ createdAt: meta.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
4422
+ lastActivityAt: Date.now(),
4423
+ isCDP: true,
4424
+ cdpEndpoint: ep
4425
+ };
4426
+ for (const existingSession of sessions.list()) {
4427
+ if (existingSession.name === name) {
4428
+ logSessionEvent("remove_stale", `Removing stale session name="${name}" id="${existingSession.id}" during restore`);
4429
+ sessions.removeById(existingSession.id);
4430
+ }
4431
+ }
4432
+ sessions.set(session);
4433
+ resetIdleTimer();
4434
+ await installNetworkCapture(page, name);
4435
+ return session;
4436
+ } catch (e) {
4437
+ console.error(`[Session Restore] Failed for "${name}":`, e.message);
4438
+ deleteSessionDiskMeta(name);
4439
+ return void 0;
4440
+ }
4441
+ }
4442
+ async function createEphemeralContext(options) {
4443
+ if (options?.cdpEndpoint) {
4444
+ const endpoint = await resolveCDPEndpoint(options.cdpEndpoint);
4445
+ const { browser: b2 } = await launch({ cdpEndpoint: endpoint });
4446
+ const contexts = b2.contexts();
4447
+ const ctx = contexts[0] || await b2.newContext();
4448
+ const allPages = ctx.pages();
4449
+ const existingPages = allPages.filter((p) => {
4450
+ const url = p.url();
4451
+ return url !== "about:blank" && !url.startsWith("chrome://");
4452
+ });
4453
+ const page2 = existingPages.length > 0 ? existingPages[0] : allPages.length > 0 ? allPages[0] : await ctx.newPage();
4454
+ resetIdleTimer();
4455
+ ephemeralConnections.set(page2, b2);
4456
+ return { context: ctx, page: page2 };
4457
+ }
4458
+ const b = await getBrowser(options);
4459
+ const context = await b.newContext();
4460
+ const page = await context.newPage();
4461
+ resetIdleTimer();
4462
+ return { context, page };
4463
+ }
4464
+ var ephemeralConnections = /* @__PURE__ */ new WeakMap();
4465
+ async function closeEphemeralContext(context) {
4466
+ try {
4467
+ const pages = context.pages();
4468
+ for (const p of pages) {
4469
+ const conn = ephemeralConnections.get(p);
4470
+ if (conn) {
4471
+ ephemeralConnections.delete(p);
4472
+ await conn.close();
4473
+ break;
4474
+ }
4475
+ }
4476
+ await context.close();
4477
+ } catch {
4478
+ }
4479
+ if (sessions.size === 0 && idleTimer) {
4480
+ clearTimeout(idleTimer);
4481
+ idleTimer = null;
4482
+ }
4483
+ }
4484
+ function getAllSessions() {
4485
+ return sessions.list();
4486
+ }
4487
+ async function installNetworkCapture(page, sessionName) {
4488
+ if (process.env.XBROWSER_DAEMON_WORKER !== "1") return;
4489
+ const { networkStore } = await import("./network-store-YVDNUREI.js");
4490
+ const requestData = /* @__PURE__ */ new Map();
4491
+ const responseMeta = /* @__PURE__ */ new Map();
4492
+ const xbPage = page;
4493
+ xbPage.on("request", (params) => {
4494
+ try {
4495
+ const p = params;
4496
+ requestData.set(p.requestId, {
4497
+ method: p.request.method,
4498
+ headers: p.request.headers,
4499
+ postData: p.request.postData ?? null,
4500
+ resourceType: p.type
4501
+ });
4502
+ } catch {
4503
+ }
4504
+ });
4505
+ xbPage.on("response", (params) => {
4506
+ try {
4507
+ const p = params;
4508
+ responseMeta.set(p.requestId, {
4509
+ status: p.response.status,
4510
+ url: p.response.url,
4511
+ headers: p.response.headers,
4512
+ mimeType: p.response.mimeType,
4513
+ type: p.type
4514
+ });
4515
+ } catch {
4516
+ }
4517
+ });
4518
+ xbPage.on("requestfinished", async (params) => {
4519
+ try {
4520
+ const p = params;
4521
+ const meta = responseMeta.get(p.requestId);
4522
+ if (!meta) return;
4523
+ const req = requestData.get(p.requestId);
4524
+ const method = req?.method ?? "GET";
4525
+ const contentType = meta.headers["content-type"] || meta.headers["Content-Type"] || "";
4526
+ const resourceType = req?.resourceType ?? meta.type;
4527
+ const requestHeaders = req?.headers ?? {};
4528
+ let requestBody = void 0;
4529
+ const isPostLike = ["POST", "PATCH", "PUT"].includes(method);
4530
+ if (isPostLike && requestHeaders["content-type"]?.includes("application/json")) {
4531
+ const postData = req?.postData;
4532
+ if (postData) {
4533
+ try {
4534
+ requestBody = JSON.parse(postData);
4535
+ } catch {
4536
+ requestBody = postData;
4537
+ }
4538
+ }
4539
+ }
4540
+ let responseBody = void 0;
4541
+ let size = 0;
4542
+ const isJsonish = contentType.includes("json") || contentType.includes("javascript") || contentType.includes("text/");
4543
+ if (isJsonish) {
4544
+ try {
4545
+ const bodyResult = await xbPage._cdpSend(
4546
+ "Network.getResponseBody",
4547
+ { requestId: p.requestId }
4548
+ );
4549
+ const text = bodyResult.body ?? "";
4550
+ size = text.length;
4551
+ if (size <= 10240) {
4552
+ try {
4553
+ responseBody = JSON.parse(text);
4554
+ } catch {
4555
+ responseBody = text.slice(0, 200);
4556
+ }
4557
+ }
4558
+ } catch {
4559
+ }
4560
+ } else {
4561
+ try {
4562
+ const bodyResult = await xbPage._cdpSend(
4563
+ "Network.getResponseBody",
4564
+ { requestId: p.requestId }
4565
+ );
4566
+ size = bodyResult.body?.length ?? 0;
4567
+ } catch {
4568
+ size = 0;
4569
+ }
4570
+ }
4571
+ networkStore.add(sessionName, {
4572
+ timestamp: Date.now(),
4573
+ method,
4574
+ url: meta.url,
4575
+ path: new URL(meta.url).pathname,
4576
+ status: meta.status,
4577
+ contentType,
4578
+ size,
4579
+ headers: meta.headers,
4580
+ body: responseBody,
4581
+ requestHeaders,
4582
+ requestBody,
4583
+ resourceType
4584
+ });
4585
+ requestData.delete(p.requestId);
4586
+ responseMeta.delete(p.requestId);
4587
+ } catch {
4588
+ }
4589
+ });
4590
+ }
4591
+ async function createSession(name, url, options) {
4592
+ const existing = findSession(name);
4593
+ if (existing) {
4594
+ logSessionEvent("replace_session", `name="${name}" id="${existing.id}" \u2014 closing existing session before creating new one`);
4595
+ await closeSessionByName(name);
4596
+ }
4597
+ const b = await createBrowser(options);
4598
+ const isCDP = !!options?.cdpEndpoint;
4599
+ let context;
4600
+ let page;
4601
+ if (isCDP) {
4602
+ await new Promise((r) => setTimeout(r, 500));
4603
+ let contexts = b.contexts();
4604
+ if (contexts.length === 0) {
4605
+ await new Promise((r) => setTimeout(r, 500));
4606
+ contexts = b.contexts();
4607
+ }
4608
+ context = contexts[0] || await b.newContext();
4609
+ let targetPage = null;
4610
+ const targetHostname = url ? (() => {
4611
+ try {
4612
+ return new URL(url).hostname;
4613
+ } catch {
4614
+ return "";
4615
+ }
4616
+ })() : "";
4617
+ if (targetHostname) {
4618
+ for (const ctx of contexts) {
4619
+ const pages = ctx.pages();
4620
+ for (const p of pages) {
4621
+ const pUrl = p.url();
4622
+ if (pUrl && pUrl !== "about:blank" && !pUrl.startsWith("chrome://") && pUrl.includes(targetHostname)) {
4623
+ targetPage = p;
4624
+ break;
4625
+ }
4626
+ }
4627
+ if (targetPage) break;
4628
+ }
4629
+ }
4630
+ if (!targetPage) {
4631
+ for (const ctx of contexts) {
4632
+ const pages = ctx.pages();
4633
+ for (const p of pages) {
4634
+ const pUrl = p.url();
4635
+ if (pUrl && pUrl !== "about:blank" && !pUrl.startsWith("chrome://")) {
4636
+ targetPage = p;
4637
+ break;
4638
+ }
4639
+ }
4640
+ if (targetPage) break;
4641
+ }
4642
+ }
4643
+ if (!targetPage && options?.cdpEndpoint) {
4644
+ const targets = await getCDPTargets2(options.cdpEndpoint);
4645
+ const matchTarget = targets.find(
4646
+ (t) => t.url && t.url !== "about:blank" && !t.url.startsWith("chrome://") && (url ? t.url.includes(new URL(url).hostname) : true)
4647
+ );
4648
+ if (matchTarget && matchTarget.url) {
4649
+ targetPage = await context.newPage();
4650
+ await targetPage.goto(matchTarget.url, { waitUntil: "domcontentloaded", timeout: 15e3 }).catch(() => {
4651
+ });
4652
+ }
4653
+ }
4654
+ if (!targetPage) {
4655
+ const pages = context.pages();
4656
+ if (pages.length > 0) {
4657
+ targetPage = pages[0];
4658
+ } else {
4659
+ targetPage = await context.newPage();
4660
+ }
4661
+ }
4662
+ page = targetPage;
4663
+ } else {
4664
+ context = await b.newContext({ viewport: { width: 1920, height: 1080 } });
4665
+ page = await context.newPage();
4666
+ }
4667
+ if (url && page.url() !== url) {
4668
+ await page.goto(url, { waitUntil: "domcontentloaded", timeout: 15e3 }).catch(() => {
4669
+ });
4670
+ }
4671
+ const session = {
4672
+ id: randomUUID(),
4673
+ name,
4674
+ context,
4675
+ page,
4676
+ browser: b,
4677
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
4678
+ lastActivityAt: Date.now(),
4679
+ isCDP,
4680
+ cdpEndpoint: options?.cdpEndpoint
4681
+ };
4682
+ sessions.set(session);
4683
+ logSessionEvent("create_session", `name="${name}" id="${session.id}" url="${url || "(no url)"}" isCDP=${isCDP} cdpEndpoint=${options?.cdpEndpoint || "(none)"}`);
4684
+ resetIdleTimer();
4685
+ await installNetworkCapture(page, name);
4686
+ return session;
4687
+ }
4688
+ async function closeSessionByName(name) {
4689
+ for (const session of sessions) {
4690
+ if (session.name === name || session.id === name) {
4691
+ logSessionEvent("close_session", `name="${session.name}" id="${session.id}" url="${session.page.url()}"`);
4692
+ if (session.isCDP) {
4693
+ try {
4694
+ await session.page.close();
4695
+ } catch {
4696
+ }
4697
+ if (session.browser) {
4698
+ await session.browser.close().catch(() => {
4699
+ });
4700
+ }
4701
+ } else {
4702
+ await session.context.close();
4703
+ if (session.browser) {
4704
+ await session.browser.close().catch(() => {
4705
+ });
4706
+ }
4707
+ }
4708
+ sessions.removeById(session.id);
4709
+ const file2 = sessionFile(session.name);
4710
+ try {
4711
+ unlinkSync(file2);
4712
+ } catch {
4713
+ }
4714
+ try {
4715
+ const { networkStore, commandLogStore } = await import("./network-store-YVDNUREI.js");
4716
+ networkStore.clear(session.name);
4717
+ commandLogStore.clear(session.name);
4718
+ } catch {
4719
+ }
4720
+ try {
4721
+ const { SessionRecorder } = await import("./session-recorder-RTDGURIJ.js");
4722
+ SessionRecorder.cleanup(session.name);
4723
+ } catch {
4724
+ }
4725
+ return true;
4726
+ }
4727
+ }
4728
+ const file = sessionFile(name);
4729
+ try {
4730
+ unlinkSync(file);
4731
+ } catch {
4732
+ }
4733
+ return false;
4734
+ }
4735
+ async function closeAllSessions() {
4736
+ const names = sessions.list().map((s) => `${s.name}(${s.page.url()})`).join(", ");
4737
+ if (names) logSessionEvent("close_all_sessions", `Closing ${sessions.size} sessions: ${names}`);
4738
+ for (const session of sessions.list()) {
4739
+ try {
4740
+ if (!session.isCDP) {
4741
+ await session.context.close();
4742
+ }
4743
+ if (session.browser) {
4744
+ await session.browser.close().catch(() => {
4745
+ });
4746
+ }
4747
+ sessions.removeById(session.id);
4748
+ } catch {
4749
+ sessions.removeById(session.id);
4750
+ }
4751
+ }
4752
+ }
4753
+ async function destroyBrowser() {
4754
+ logSessionEvent("destroy_browser", `Sessions count: ${sessions.size}. Clearing idle timer and closing all browsers.`);
4755
+ if (idleTimer) {
4756
+ clearTimeout(idleTimer);
4757
+ idleTimer = null;
4758
+ }
4759
+ await closeAllSessions();
4760
+ if (_sharedBrowser) {
4761
+ await _sharedBrowser.close().catch(() => {
4762
+ });
4763
+ _sharedBrowser = null;
4764
+ }
4765
+ if (_sharedCdpProxy) {
4766
+ await _sharedCdpProxy.stop().catch(() => {
4767
+ });
4768
+ _sharedCdpProxy = null;
4769
+ }
4770
+ }
4771
+ function resetForTesting() {
4772
+ sessions.clear();
4773
+ _sharedBrowser = null;
4774
+ _sharedCdpProxy = null;
4775
+ try {
4776
+ for (const f of readdirSync(SESSION_DIR)) {
4777
+ unlinkSync(join(SESSION_DIR, f));
4778
+ }
4779
+ } catch {
4780
+ }
4781
+ }
4782
+ async function ensureProcessCanExit() {
4783
+ if (idleTimer) {
4784
+ clearTimeout(idleTimer);
4785
+ idleTimer = null;
4786
+ }
4787
+ for (const session of sessions.list()) {
4788
+ if (session.browser) {
4789
+ if (session.isCDP) {
4790
+ await session.browser.close().catch(() => {
4791
+ });
4792
+ } else {
4793
+ await session.browser.close().catch(() => {
4794
+ });
4795
+ }
4796
+ }
4797
+ }
4798
+ sessions.clear();
4799
+ if (_sharedBrowser) {
4800
+ await _sharedBrowser.close().catch(() => {
4801
+ });
4802
+ _sharedBrowser = null;
4803
+ }
4804
+ if (_sharedCdpProxy) {
4805
+ await _sharedCdpProxy.stop().catch(() => {
4806
+ });
4807
+ _sharedCdpProxy = null;
4808
+ }
4809
+ }
4810
+
4811
+ export {
4812
+ createRuleEngine,
4813
+ touchSession,
4814
+ findTargetPage,
4815
+ resolveLaunchOpts,
4816
+ createBrowser,
4817
+ getBrowser,
4818
+ findSession,
4819
+ getSessionById,
4820
+ setActivePage,
4821
+ saveSessionDiskMeta,
4822
+ readSessionDiskMeta,
4823
+ deleteSessionDiskMeta,
4824
+ findOrRestoreSession,
4825
+ createEphemeralContext,
4826
+ closeEphemeralContext,
4827
+ getAllSessions,
4828
+ createSession,
4829
+ closeSessionByName,
4830
+ closeAllSessions,
4831
+ destroyBrowser,
4832
+ resetForTesting,
4833
+ ensureProcessCanExit
4834
+ };