@xbrowser/cli 1.29.0 → 1.30.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +2 -2
  2. package/dist/{browser-XD3IOU5I.js → browser-6PFTHZI3.js} +2 -2
  3. package/dist/{browser-2K3XYAKM.js → browser-I2NLJN2A.js} +1 -1
  4. package/dist/{browser-OTEMG5NL.js → browser-KOUA4ZDA.js} +2 -2
  5. package/dist/{cdp-driver-AAS55KT2.js → cdp-driver-3ETNFQA4.js} +1 -1
  6. package/dist/{cdp-driver-XTPY7EXP.js → cdp-driver-U7HDQMQ5.js} +257 -19
  7. package/dist/{cdp-driver-O5I7SJ24.js → cdp-driver-WBFJNOLD.js} +1 -1
  8. package/dist/{chunk-SCYBNSOG.js → chunk-3K57RYVC.js} +378 -115
  9. package/dist/{chunk-FNXA6FDM.js → chunk-BND5O5KC.js} +2 -1
  10. package/dist/{chunk-P33R7F2D.js → chunk-BOZTIIBT.js} +30 -45
  11. package/dist/{chunk-GDKKVT2P.js → chunk-KPTD3SNJ.js} +379 -116
  12. package/dist/{chunk-CO2ZVOVT.js → chunk-NINETOM4.js} +30 -45
  13. package/dist/{chunk-SOPN4XDK.js → chunk-O5PASNI7.js} +2 -1
  14. package/dist/{chunk-NQPXZ74W.js → chunk-OCECE22C.js} +257 -19
  15. package/dist/{chunk-LFSLNHW5.js → chunk-OSIDUT4H.js} +286 -63
  16. package/dist/{chunk-HBMEFSTB.js → chunk-QCV2PI7U.js} +2 -1
  17. package/dist/{chunk-UZ6XPUBD.js → chunk-RMYKVZIZ.js} +78 -0
  18. package/dist/{chunk-N5XTLNKS.js → chunk-SPNWUUHA.js} +257 -19
  19. package/dist/{chunk-PGKV24QN.js → chunk-TBJUSUKF.js} +1 -1
  20. package/dist/{chunk-FCPVQFTQ.js → chunk-VYTQLZAX.js} +78 -0
  21. package/dist/cli.js +45 -16
  22. package/dist/daemon-main.js +581 -157
  23. package/dist/{human-interaction-CTZ4IWXJ.js → human-interaction-ISMMOJOK.js} +2 -2
  24. package/dist/{human-interaction-HJCK45SL.js → human-interaction-STTZZZ5N.js} +1 -1
  25. package/dist/{human-interaction-V3VDGHHI.js → human-interaction-UXFYV7DU.js} +1 -1
  26. package/dist/index.d.ts +106 -12
  27. package/dist/index.js +195 -46
  28. package/dist/{recovery-EBKORYEU.js → recovery-JFGV3DBX.js} +1 -1
  29. package/dist/{recovery-5C4FBA5O.js → recovery-ORHJSBDN.js} +2 -2
  30. package/dist/{recovery-IMZUGWSS.js → recovery-XDKN63O5.js} +1 -1
  31. package/dist/{server-ZTI2K5YJ.js → server-TG47W7MV.js} +3 -3
  32. package/dist/{server-LNZTDAKT.js → server-U3YAX5MJ.js} +2 -2
  33. package/dist/{session-recorder-DF2YGH2E.js → session-recorder-N7VEKJVW.js} +1 -1
  34. package/dist/{session-recorder-R3GDGXLJ.js → session-recorder-RUNQOJ7T.js} +1 -1
  35. package/dist/{session-replayer-CLTZ2URU.js → session-replayer-XLWQ47BB.js} +1 -1
  36. package/package.json +4 -1
@@ -104,12 +104,16 @@ import { EventEmitter as EventEmitter2 } from "events";
104
104
  import { EventEmitter } from "events";
105
105
 
106
106
  // src/cdp-driver/mouse.ts
107
+ function buttonBit(button) {
108
+ return button === "left" ? 1 : button === "right" ? 2 : 4;
109
+ }
107
110
  var XBMouseImpl = class {
108
111
  conn;
109
112
  sessionId;
110
113
  _x = 0;
111
114
  _y = 0;
112
115
  _button = "none";
116
+ _buttons = 0;
113
117
  constructor(conn, sessionId) {
114
118
  this.conn = conn;
115
119
  this.sessionId = sessionId;
@@ -140,7 +144,7 @@ var XBMouseImpl = class {
140
144
  _truncated = true;
141
145
  break;
142
146
  }
143
- await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: p.x, y: p.y, button: this._button });
147
+ await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: p.x, y: p.y, button: this._button, buttons: this._buttons });
144
148
  this._x = p.x;
145
149
  this._y = p.y;
146
150
  await sleep(p.delay);
@@ -148,7 +152,7 @@ var XBMouseImpl = class {
148
152
  this._x = tx;
149
153
  this._y = ty;
150
154
  if (_truncated) {
151
- await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: tx, y: ty, button: this._button });
155
+ await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: tx, y: ty, button: this._button, buttons: this._buttons });
152
156
  }
153
157
  await sleep(rand(...DEFAULT_STEALTH_CONFIG.aimPause));
154
158
  } else {
@@ -160,7 +164,7 @@ var XBMouseImpl = class {
160
164
  const ry = stealth ? this._y + rand(...DEFAULT_STEALTH_CONFIG.releaseDrift) * (Math.random() < 0.5 ? -1 : 1) : this._y;
161
165
  this._x = rx;
162
166
  this._y = ry;
163
- await this.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: rx, y: ry, button, clickCount: opts.clickCount ?? 1 });
167
+ await this.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: rx, y: ry, button, buttons: this._buttons, clickCount: opts.clickCount ?? 1 });
164
168
  for (let i = 1; i < (opts.clickCount ?? 1); i++) {
165
169
  if (opts.delay) await sleep(opts.delay);
166
170
  await this.down({ button });
@@ -173,11 +177,13 @@ var XBMouseImpl = class {
173
177
  async down(opts = {}) {
174
178
  const button = opts.button ?? "left";
175
179
  this._button = button;
180
+ this._buttons |= buttonBit(button);
176
181
  await this.send("Input.dispatchMouseEvent", {
177
182
  type: "mousePressed",
178
183
  x: this._x,
179
184
  y: this._y,
180
185
  button,
186
+ buttons: this._buttons,
181
187
  // r23: press 必须与 release 声明同一 clickCount——Chrome 按序列计数
182
188
  // 合成 click/dblclick,press 缺声明会把计数器重置(dblclick 永不合成)
183
189
  clickCount: opts.clickCount ?? 1
@@ -191,8 +197,11 @@ var XBMouseImpl = class {
191
197
  x: this._x,
192
198
  y: this._y,
193
199
  button,
200
+ // release 先带位发送再清位(Puppeteer 同序——DOM mouseup 的 buttons 反映释放后状态)
201
+ buttons: this._buttons,
194
202
  clickCount: opts.clickCount ?? 1
195
203
  });
204
+ this._buttons &= ~buttonBit(button);
196
205
  }
197
206
  async move(x, y, opts = {}) {
198
207
  const stealth = opts.stealth ?? process.env.XBROWSER_STEALTH !== "off";
@@ -210,7 +219,8 @@ var XBMouseImpl = class {
210
219
  type: "mouseMoved",
211
220
  x: this._x,
212
221
  y: this._y,
213
- button: this._button
222
+ button: this._button,
223
+ buttons: this._buttons
214
224
  });
215
225
  }
216
226
  this._x = x;
@@ -230,9 +240,11 @@ var XBMouseImpl = class {
230
240
  x: this._x,
231
241
  y: this._y,
232
242
  button: "left",
243
+ buttons: 1,
233
244
  clickCount: 1
234
245
  });
235
246
  this._button = "left";
247
+ this._buttons |= 1;
236
248
  const steps = opts.steps ?? Math.max(10, Math.min(24, Math.round(Math.hypot(x - this._x, y - this._y) / 20)));
237
249
  const fx = this._x, fy = this._y;
238
250
  for (let i = 1; i <= steps; i++) {
@@ -244,7 +256,8 @@ var XBMouseImpl = class {
244
256
  type: "mouseMoved",
245
257
  x: this._x,
246
258
  y: this._y,
247
- button: this._button
259
+ button: this._button,
260
+ buttons: this._buttons
248
261
  });
249
262
  }
250
263
  this._x = x;
@@ -255,15 +268,22 @@ var XBMouseImpl = class {
255
268
  x,
256
269
  y,
257
270
  button: "left",
271
+ buttons: this._buttons,
258
272
  clickCount: 1
259
273
  });
260
274
  this._button = "none";
275
+ this._buttons &= ~1;
276
+ }
277
+ async wheelAt(x, y, deltaX, deltaY) {
278
+ await this.move(x, y);
279
+ await this.wheel(deltaX, deltaY);
261
280
  }
262
281
  async wheel(deltaX, deltaY) {
263
282
  await this.send("Input.dispatchMouseEvent", {
264
283
  type: "mouseWheel",
265
284
  x: this._x,
266
285
  y: this._y,
286
+ buttons: this._buttons,
267
287
  deltaX,
268
288
  deltaY
269
289
  });
@@ -1525,7 +1545,7 @@ var MediaService = class {
1525
1545
  y: Math.round(opts.clip.y),
1526
1546
  width: Math.round(Math.max(1, opts.clip.width)),
1527
1547
  height: Math.round(Math.max(1, opts.clip.height)),
1528
- scale: 1
1548
+ scale: opts.clip.scale ?? 1
1529
1549
  };
1530
1550
  params.clip = clip;
1531
1551
  }
@@ -2522,14 +2542,13 @@ var RuntimeEvaluationService = class {
2522
2542
  }
2523
2543
  return result.result?.value;
2524
2544
  };
2525
- try {
2526
- const tg = await this.options.conn.send("Target.getTargets", void 0);
2527
- const hit2 = (tg.targetInfos || []).find((t) => t.type === "iframe" && (t.url || "").includes(urlIncludes));
2528
- if (hit2) {
2529
- const att = await this.options.conn.send("Target.attachToTarget", { targetId: hit2.targetId, flatten: true });
2530
- return evalIn(att.sessionId);
2545
+ const attachedA = await this.tryAttachViaTargets(urlIncludes);
2546
+ if (attachedA) {
2547
+ try {
2548
+ return await evalIn(attachedA);
2549
+ } finally {
2550
+ this.detachQuietly(attachedA);
2531
2551
  }
2532
- } catch {
2533
2552
  }
2534
2553
  const tree = await this.options.conn.send("Page.getFrameTree", void 0, this.options.sessionId);
2535
2554
  const all = [];
@@ -2557,6 +2576,87 @@ var RuntimeEvaluationService = class {
2557
2576
  const ctx = contexts.find((c) => c.frameId === target.id);
2558
2577
  if (ctx) return evalIn(this.options.sessionId, ctx.id);
2559
2578
  }
2579
+ const attachedC = await this.tryAttachViaAutoAttach(urlIncludes);
2580
+ if (attachedC) {
2581
+ try {
2582
+ return await evalIn(attachedC);
2583
+ } finally {
2584
+ this.detachQuietly(attachedC);
2585
+ }
2586
+ }
2587
+ throw new Error(`frame not found for "${urlIncludes}"\uFF08OOPIF target\u3001frame \u6811\u3001auto-attach \u4E09\u8DEF\u5747\u672A\u547D\u4E2D\uFF1B\u53EF\u80FD\u4ECD\u5728\u52A0\u8F7D\uFF09`);
2588
+ }
2589
+ /**
2590
+ * 输入专用 frame session 解析(mouse/click/fill --frame)。
2591
+ *
2592
+ * 与 evaluateInFrame 的区别:必须拿到**真实 target session**(Input 域
2593
+ * 事件派发到该 session 才会进入 iframe 的渲染进程——主页面 session 上
2594
+ * 派发的输入进不去 OOPIF,qqmail 验证码 4 次页面级拖动全败的实证)。
2595
+ * 因此只走 A(getTargets attach)/ C(auto-attach)两路;同进程 frame
2596
+ * 没有 session,但 locator 层的坐标偏移本就支持同源 iframe,直接用普通
2597
+ * click/fill 即可。
2598
+ *
2599
+ * 坐标语义:返回的 session 上派发 Input 事件用 **iframe 局部坐标**
2600
+ * (与该 session 内 getBoundingClientRect 一致)。
2601
+ */
2602
+ async resolveFrameSession(urlIncludes) {
2603
+ if (this.options.isClosed()) throw new Error("Page is closed");
2604
+ const viaTargets = await this.tryAttachViaTargets(urlIncludes);
2605
+ if (viaTargets) return viaTargets;
2606
+ const viaAuto = await this.tryAttachViaAutoAttach(urlIncludes);
2607
+ if (viaAuto) return viaAuto;
2608
+ try {
2609
+ const tree = await this.options.conn.send("Page.getFrameTree", void 0, this.options.sessionId);
2610
+ const urls = [];
2611
+ const walk = (node) => {
2612
+ urls.push(node.frame.url);
2613
+ for (const child of node.childFrames || []) walk(child);
2614
+ };
2615
+ walk(tree.frameTree);
2616
+ if (urls.some((u) => u.includes(urlIncludes))) {
2617
+ throw new Error(`frame "${urlIncludes}" \u662F\u540C\u8FDB\u7A0B iframe\uFF08\u65E0\u72EC\u7ACB target\uFF09\u2014\u2014\u666E\u901A click/fill/mouse \u5373\u53EF\uFF08locator \u5C42\u5DF2\u652F\u6301\u540C\u6E90 iframe \u5750\u6807\u504F\u79FB\uFF09`);
2618
+ }
2619
+ } catch (err) {
2620
+ if (err instanceof Error && err.message.includes("\u540C\u8FDB\u7A0B")) throw err;
2621
+ }
2622
+ throw new Error(`no OOPIF target for "${urlIncludes}"\uFF08\u7528 'xbrowser frames' \u67E5\u770B\u53EF\u89C1\u7684 iframe target\uFF1BOOPIF \u53EA\u5728 Target.getTargets \u91CC\u51FA\u73B0\uFF09`);
2623
+ }
2624
+ /** 浏览器级 iframe target 列表(OOPIF 可见性——Page.getFrameTree 看不到它们) */
2625
+ async listFrameTargets() {
2626
+ try {
2627
+ const tg = await this.options.conn.send("Target.getTargets", void 0);
2628
+ return (tg.targetInfos || []).filter((t) => t.type === "iframe").map((t) => ({ url: t.url || "", targetId: t.targetId }));
2629
+ } catch {
2630
+ return [];
2631
+ }
2632
+ }
2633
+ /** 路径 A:浏览器级 Target.getTargets 平铺 iframe target,按 URL 子串 attach。
2634
+ * 多次导航后列表里可能残留正在销毁的陈旧 target——attach 后用 location.href
2635
+ * 验证候选确实是活目标,不匹配则换下一个(验证码 iframe 重建场景实测会撞上)。 */
2636
+ async tryAttachViaTargets(urlIncludes) {
2637
+ try {
2638
+ const tg = await this.options.conn.send("Target.getTargets", void 0);
2639
+ const candidates = (tg.targetInfos || []).filter((t) => t.type === "iframe" && (t.url || "").includes(urlIncludes));
2640
+ for (const hit of candidates) {
2641
+ try {
2642
+ const att = await this.options.conn.send("Target.attachToTarget", { targetId: hit.targetId, flatten: true });
2643
+ const probe = await this.options.conn.send("Runtime.evaluate", { expression: "location.href", returnByValue: true }, att.sessionId);
2644
+ const href = typeof probe.result?.value === "string" ? probe.result.value : "";
2645
+ if (probe.exceptionDetails || !href.includes(urlIncludes)) {
2646
+ this.detachQuietly(att.sessionId);
2647
+ continue;
2648
+ }
2649
+ return att.sessionId;
2650
+ } catch {
2651
+ continue;
2652
+ }
2653
+ }
2654
+ } catch {
2655
+ }
2656
+ return null;
2657
+ }
2658
+ /** 路径 C:Target.setAutoAttach 监听后续 attach 的 iframe target(兜底) */
2659
+ async tryAttachViaAutoAttach(urlIncludes) {
2560
2660
  const attached = [];
2561
2661
  const onAttach = (raw) => {
2562
2662
  const ev = raw;
@@ -2578,10 +2678,11 @@ var RuntimeEvaluationService = class {
2578
2678
  });
2579
2679
  }
2580
2680
  const hit = attached.find((a) => a.url.includes(urlIncludes));
2581
- if (!hit) {
2582
- throw new Error(`frame not found for "${urlIncludes}"\uFF08OOPIF target\u3001frame \u6811\u3001auto-attach \u4E09\u8DEF\u5747\u672A\u547D\u4E2D\uFF1B\u53EF\u80FD\u4ECD\u5728\u52A0\u8F7D\uFF09`);
2583
- }
2584
- return evalIn(hit.sessionId);
2681
+ return hit ? hit.sessionId : null;
2682
+ }
2683
+ detachQuietly(sessionId) {
2684
+ this.options.conn.send("Target.detachFromTarget", { sessionId }).catch(() => {
2685
+ });
2585
2686
  }
2586
2687
  /** evaluateHandle — evaluates fn and returns a handle for element bounding box */
2587
2688
  async evaluateHandle(fn, ...args) {
@@ -2633,6 +2734,103 @@ var RuntimeEvaluationService = class {
2633
2734
  }
2634
2735
  };
2635
2736
 
2737
+ // src/cdp-driver/page-frame-input.ts
2738
+ function isSessionDeadError(err) {
2739
+ return /session (with given id )?not found|no session with given id/i.test(errMsg(err));
2740
+ }
2741
+ var FrameInputService = class {
2742
+ constructor(deps) {
2743
+ this.deps = deps;
2744
+ }
2745
+ deps;
2746
+ /** urlIncludes → 已 attach 的 session + 鼠标实例(跨命令保持位置状态) */
2747
+ cache = /* @__PURE__ */ new Map();
2748
+ /**
2749
+ * 拿到指定 OOPIF 的鼠标(Input 域派发进 iframe 渲染进程,isTrusted=true)。
2750
+ * 缓存的 session 失效(iframe 重建/刷新)时自动重 attach;重 attach 会
2751
+ * 重置鼠标位置——调用方需重新读取目标元素坐标。
2752
+ */
2753
+ async frameMouse(urlIncludes, opts = {}) {
2754
+ if (opts.fresh) this.cache.delete(urlIncludes);
2755
+ const cached = this.cache.get(urlIncludes);
2756
+ if (cached) {
2757
+ try {
2758
+ await this.deps.conn.send("Runtime.evaluate", { expression: "1", returnByValue: true }, cached.sessionId);
2759
+ return cached.mouse;
2760
+ } catch {
2761
+ this.cache.delete(urlIncludes);
2762
+ }
2763
+ }
2764
+ const sessionId = await this.deps.resolveFrameSession(urlIncludes);
2765
+ const mouse = new XBMouseImpl(this.deps.conn, sessionId);
2766
+ this.cache.set(urlIncludes, { sessionId, mouse });
2767
+ return mouse;
2768
+ }
2769
+ /**
2770
+ * 带重试的 frame 鼠标操作:Chrome 异步销毁旧 OOPIF target 的窗口期里
2771
+ * (连续导航/验证码 iframe 重建),probe 通过后 dispatch 仍可能撞上死
2772
+ * session——session 失效时强制重 attach 再做一次。
2773
+ */
2774
+ async runFrameMouse(urlIncludes, fn) {
2775
+ try {
2776
+ return await fn(await this.frameMouse(urlIncludes));
2777
+ } catch (err) {
2778
+ if (!isSessionDeadError(err)) throw err;
2779
+ this.cache.delete(urlIncludes);
2780
+ return fn(await this.frameMouse(urlIncludes, { fresh: true }));
2781
+ }
2782
+ }
2783
+ /** 在 OOPIF 内点击选择器命中的元素(frame 局部坐标,stealth 落点偏移继承) */
2784
+ async clickInFrame(urlIncludes, selector, opts = {}) {
2785
+ const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
2786
+ const x = rect.x + rect.width / 2;
2787
+ const y = rect.y + rect.height / 2;
2788
+ await this.runFrameMouse(urlIncludes, (mouse) => mouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height }));
2789
+ return { x, y };
2790
+ }
2791
+ /** 在 OOPIF 内填输入框:点击聚焦 → el.select() 全选 → insertText 整体替换 → 回读验证 */
2792
+ async fillInFrame(urlIncludes, selector, text, opts = {}) {
2793
+ try {
2794
+ return await this.fillInFrameInner(urlIncludes, selector, text, opts);
2795
+ } catch (err) {
2796
+ if (!isSessionDeadError(err)) throw err;
2797
+ this.cache.delete(urlIncludes);
2798
+ return this.fillInFrameInner(urlIncludes, selector, text, opts);
2799
+ }
2800
+ }
2801
+ async fillInFrameInner(urlIncludes, selector, text, opts = {}) {
2802
+ await this.clickInFrame(urlIncludes, selector, opts);
2803
+ const cached = this.cache.get(urlIncludes);
2804
+ if (!cached) throw new Error(`frame session lost for "${urlIncludes}"`);
2805
+ await this.deps.evaluateInFrame(
2806
+ urlIncludes,
2807
+ `(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (el && typeof el.select === 'function') el.select(); return true; })()`
2808
+ );
2809
+ await this.deps.conn.send("Input.insertText", { text }, cached.sessionId);
2810
+ const readback = await this.deps.evaluateInFrame(
2811
+ urlIncludes,
2812
+ `(() => { const el = document.querySelector(${JSON.stringify(selector)}); return el ? String(el.value ?? '') : ''; })()`
2813
+ );
2814
+ return { value: readback ?? "" };
2815
+ }
2816
+ /** 轮询 OOPIF 内元素 rect(等待元素出现/可测量) */
2817
+ async pollFrameRect(urlIncludes, selector, timeoutMs) {
2818
+ const expr = `(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (!el) return null; const r = el.getBoundingClientRect(); if (r.width === 0 || r.height === 0) return null; return { x: r.x, y: r.y, width: r.width, height: r.height }; })()`;
2819
+ const deadline = Date.now() + timeoutMs;
2820
+ let lastErr;
2821
+ while (Date.now() < deadline) {
2822
+ try {
2823
+ const rect = await this.deps.evaluateInFrame(urlIncludes, expr);
2824
+ if (rect) return rect;
2825
+ } catch (err) {
2826
+ lastErr = err;
2827
+ }
2828
+ await new Promise((r) => setTimeout(r, 200));
2829
+ }
2830
+ throw new Error(`[frame ${urlIncludes}] element "${selector}" not found/visible within ${timeoutMs}ms${lastErr instanceof Error ? `\uFF08last: ${lastErr.message}\uFF09` : ""}`);
2831
+ }
2832
+ };
2833
+
2636
2834
  // src/cdp-driver/page.ts
2637
2835
  var XBPageImpl = class _XBPageImpl {
2638
2836
  conn;
@@ -2659,6 +2857,7 @@ var XBPageImpl = class _XBPageImpl {
2659
2857
  navigationService;
2660
2858
  domQueryService;
2661
2859
  runtimeEvaluationService;
2860
+ frameInputService;
2662
2861
  // Network tracking for waitForLoadState('networkidle')
2663
2862
  inflightRequests = /* @__PURE__ */ new Set();
2664
2863
  networkIdleResolve = null;
@@ -2721,6 +2920,11 @@ var XBPageImpl = class _XBPageImpl {
2721
2920
  isClosed: () => this._closed,
2722
2921
  url: () => this.url()
2723
2922
  });
2923
+ this.frameInputService = new FrameInputService({
2924
+ conn,
2925
+ resolveFrameSession: (urlIncludes) => this.runtimeEvaluationService.resolveFrameSession(urlIncludes),
2926
+ evaluateInFrame: (urlIncludes, expression) => this.evaluateInFrame(urlIncludes, expression)
2927
+ });
2724
2928
  this.domQueryService = new DomQueryService({
2725
2929
  conn,
2726
2930
  sessionId,
@@ -2930,8 +3134,9 @@ var XBPageImpl = class _XBPageImpl {
2930
3134
  "Target.getTargetInfo",
2931
3135
  { targetId: this._targetId }
2932
3136
  );
2933
- this._url = info.url;
2934
- if (info.url && info.url !== "about:blank" && info.url !== "") {
3137
+ const curUrl = info.targetInfo?.url ?? info.url;
3138
+ if (curUrl) this._url = curUrl;
3139
+ if (curUrl && curUrl !== "about:blank" && curUrl !== "") {
2935
3140
  this._loadState = { loadFired: true, domContentFired: true, networkIdle: true };
2936
3141
  }
2937
3142
  } catch {
@@ -3055,6 +3260,29 @@ Last error: ${lastError.message}` : "";
3055
3260
  async evaluateInFrame(urlIncludes, expression) {
3056
3261
  return this.runtimeEvaluationService.evaluateInFrame(urlIncludes, expression);
3057
3262
  }
3263
+ /** 浏览器级 iframe target 列表(OOPIF 可见——frames 命令用) */
3264
+ async listFrameTargets() {
3265
+ return this.runtimeEvaluationService.listFrameTargets();
3266
+ }
3267
+ /**
3268
+ * 拿到指定 OOPIF 的鼠标(Input 域派发进 iframe 渲染进程,isTrusted=true)。
3269
+ * 坐标语义:iframe 局部坐标(与该 session 内 getBoundingClientRect 一致)。
3270
+ * session 失效自动重 attach(鼠标位置会重置,需重读目标坐标)。
3271
+ */
3272
+ async frameMouse(urlIncludes, opts) {
3273
+ return this.frameInputService.frameMouse(urlIncludes, opts);
3274
+ }
3275
+ async runFrameMouse(urlIncludes, fn) {
3276
+ return this.frameInputService.runFrameMouse(urlIncludes, fn);
3277
+ }
3278
+ /** 在 OOPIF 内点击选择器命中的元素(轮询等待 + stealth 落点) */
3279
+ async clickInFrame(urlIncludes, selector, opts) {
3280
+ return this.frameInputService.clickInFrame(urlIncludes, selector, opts);
3281
+ }
3282
+ /** 在 OOPIF 内填输入框(点击聚焦 + insertText + 回读) */
3283
+ async fillInFrame(urlIncludes, selector, text, opts) {
3284
+ return this.frameInputService.fillInFrame(urlIncludes, selector, text, opts);
3285
+ }
3058
3286
  async evaluateHandle(script, ...args) {
3059
3287
  return this.runtimeEvaluationService.evaluateHandle(script, ...args);
3060
3288
  }
@@ -3785,6 +4013,16 @@ var XBBrowserImpl = class {
3785
4013
  await this.conn.close();
3786
4014
  this._emitter.emit("disconnected");
3787
4015
  }
4016
+ async disconnect() {
4017
+ if (this._disconnected) return;
4018
+ this._disconnected = true;
4019
+ if (this._exitHandler) {
4020
+ process.removeListener("exit", this._exitHandler);
4021
+ this._exitHandler = null;
4022
+ }
4023
+ await this.conn.close();
4024
+ this._emitter.emit("disconnected");
4025
+ }
3788
4026
  async newContext(opts = {}) {
3789
4027
  if (this.launchOpts?.stealthConfig && !opts.stealthConfig) {
3790
4028
  opts = { ...opts, stealthConfig: this.launchOpts.stealthConfig };
@@ -6156,6 +6394,7 @@ async function createSessionImpl(name, url, options, deps) {
6156
6394
  const isCDP = !!options?.cdpEndpoint;
6157
6395
  let context;
6158
6396
  let page;
6397
+ let reusedExistingPage = false;
6159
6398
  if (isCDP) {
6160
6399
  const targetHostname = url ? (() => {
6161
6400
  try {
@@ -6237,22 +6476,12 @@ async function createSessionImpl(name, url, options, deps) {
6237
6476
  const pUrl0 = await resolvePageUrl(p);
6238
6477
  if (ownedUrls.has(pUrl0)) continue;
6239
6478
  const pUrl = pUrl0;
6240
- if (isRealPageUrl(pUrl) && pUrl.includes(targetHostname)) {
6241
- targetPage = p;
6242
- break;
6479
+ let hostnameMatches = false;
6480
+ try {
6481
+ hostnameMatches = new URL(pUrl).hostname === targetHostname;
6482
+ } catch {
6243
6483
  }
6244
- }
6245
- if (targetPage) break;
6246
- }
6247
- }
6248
- if (!targetPage) {
6249
- for (const ctx of contexts) {
6250
- const pages = ctx.pages();
6251
- for (const p of pages) {
6252
- const pUrl0 = await resolvePageUrl(p);
6253
- if (ownedUrls.has(pUrl0)) continue;
6254
- const pUrl = pUrl0;
6255
- if (isRealPageUrl(pUrl)) {
6484
+ if (isRealPageUrl(pUrl) && hostnameMatches) {
6256
6485
  targetPage = p;
6257
6486
  break;
6258
6487
  }
@@ -6270,9 +6499,15 @@ async function createSessionImpl(name, url, options, deps) {
6270
6499
  } catch {
6271
6500
  }
6272
6501
  }
6273
- const matchTarget = targets.find(
6274
- (t) => t.url && t.url !== "about:blank" && !t.url.startsWith("chrome://") && !t.url.startsWith("chrome-untrusted://") && !t.url.startsWith("chrome-error://") && !ownedUrlsT.has(t.url) && (url ? t.url === url || t.url.includes(new URL(url).hostname) : true)
6275
- );
6502
+ const matchTarget = targets.find((t) => {
6503
+ if (!t.url || !isRealPageUrl(t.url) || ownedUrlsT.has(t.url)) return false;
6504
+ if (!url) return false;
6505
+ try {
6506
+ return new URL(t.url).hostname === targetHostname;
6507
+ } catch {
6508
+ return false;
6509
+ }
6510
+ });
6276
6511
  if (matchTarget && matchTarget.url) {
6277
6512
  const ownedUrls2 = /* @__PURE__ */ new Set();
6278
6513
  for (const ssn of sessions.list()) {
@@ -6305,31 +6540,12 @@ async function createSessionImpl(name, url, options, deps) {
6305
6540
  }
6306
6541
  }
6307
6542
  }
6543
+ const targetPageExisted = !!targetPage;
6308
6544
  if (!targetPage) {
6309
- const pages = context.pages();
6310
- if (pages.length > 1) {
6311
- try {
6312
- const owned = /* @__PURE__ */ new Set();
6313
- for (const ssn of sessions.list()) owned.add(ssn.page);
6314
- for (const p of pages) {
6315
- if (owned.has(p)) continue;
6316
- const vis = await p.evaluate("document.visibilityState").catch(() => "hidden");
6317
- if (vis === "visible") {
6318
- targetPage = p;
6319
- break;
6320
- }
6321
- }
6322
- } catch {
6323
- }
6324
- }
6325
- if (!targetPage && pages.length > 0) {
6326
- targetPage = pages[0];
6327
- }
6328
- if (!targetPage) {
6329
- targetPage = await context.newPage();
6330
- }
6545
+ targetPage = await context.newPage();
6331
6546
  }
6332
6547
  page = targetPage;
6548
+ reusedExistingPage = targetPageExisted;
6333
6549
  if (isCDP) {
6334
6550
  await Promise.resolve(targetPage.bringToFront?.()).catch(() => {
6335
6551
  });
@@ -6348,7 +6564,8 @@ async function createSessionImpl(name, url, options, deps) {
6348
6564
  page,
6349
6565
  browser: b,
6350
6566
  isCDP,
6351
- cdpEndpoint: options?.cdpEndpoint
6567
+ cdpEndpoint: options?.cdpEndpoint,
6568
+ ...isCDP ? { reusedExistingPage } : {}
6352
6569
  });
6353
6570
  deps.logSessionEvent("create_session", `name="${name}" id="${session.id}" url="${url || "(no url)"}" isCDP=${isCDP} cdpEndpoint=${options?.cdpEndpoint || "(none)"}`);
6354
6571
  await deps.installNetworkCapture(page, name);
@@ -6607,8 +6824,14 @@ async function closeSessionByName(name) {
6607
6824
  if (session.name === name || session.id === name) {
6608
6825
  logSessionEvent2("close_session", `name="${session.name}" id="${session.id}" url="${session.page.url()}"`);
6609
6826
  if (session.isCDP) {
6610
- if (session.browser) {
6611
- await session.browser.close().catch(() => {
6827
+ if (session.reusedExistingPage) {
6828
+ logSessionEvent2("close_session", `name="${session.name}" \u2014 reused existing tab, disconnecting only`);
6829
+ await session.browser.disconnect().catch(() => {
6830
+ });
6831
+ } else {
6832
+ await session.page.close().catch(() => {
6833
+ });
6834
+ await session.browser.disconnect().catch(() => {
6612
6835
  });
6613
6836
  }
6614
6837
  } else {
@@ -6631,7 +6854,7 @@ async function closeSessionByName(name) {
6631
6854
  } catch {
6632
6855
  }
6633
6856
  try {
6634
- const { SessionRecorder } = await import("./session-recorder-R3GDGXLJ.js");
6857
+ const { SessionRecorder } = await import("./session-recorder-RUNQOJ7T.js");
6635
6858
  SessionRecorder.cleanup(session.name);
6636
6859
  } catch {
6637
6860
  }
@@ -77,7 +77,8 @@ var ScreencastCapturer = class {
77
77
  if (!cb) return;
78
78
  cdp.send("Page.screencastFrameAck", { sessionId: params.sessionId }).catch(() => {
79
79
  });
80
- const viewport = {
80
+ const logicalViewport = page.viewportSize();
81
+ const viewport = logicalViewport ?? {
81
82
  width: params.metadata?.deviceWidth || this.maxWidth,
82
83
  height: params.metadata?.deviceHeight || this.maxHeight
83
84
  };
@@ -2249,6 +2249,82 @@ async function injectActionScript(page) {
2249
2249
  // src/recorder/summary-builder.ts
2250
2250
  var POST_WINDOW = 5e3;
2251
2251
  var MERGE_WINDOW = 2e3;
2252
+ var COMPACT_MAX_FLOW_ITEMS = 100;
2253
+ var COMPACT_MAX_API_ITEMS = 20;
2254
+ function buildCompactSummary(data, startedAt) {
2255
+ const flow = [];
2256
+ let lastInputKey = "";
2257
+ const events = [
2258
+ ...data.actions.map((action) => ({ timestamp: action.timestamp, action })),
2259
+ ...data.contextChanges.map((contextChange) => ({ timestamp: contextChange.timestamp, contextChange }))
2260
+ ].sort((a, b) => a.timestamp - b.timestamp);
2261
+ for (const event of events) {
2262
+ if (flow.length >= COMPACT_MAX_FLOW_ITEMS) break;
2263
+ if (event.contextChange) {
2264
+ if (event.contextChange.type !== "navigate" || !event.contextChange.url) continue;
2265
+ lastInputKey = "";
2266
+ flow.push({
2267
+ step: flow.length + 1,
2268
+ action: `navigate ${event.contextChange.url}`
2269
+ });
2270
+ continue;
2271
+ }
2272
+ const action = event.action;
2273
+ if (!action) continue;
2274
+ const selector = action.element?.selector || action.element?.ariaLabel || action.element?.placeholder || action.element?.tag || "";
2275
+ const text = (action.element?.text || "").trim();
2276
+ const target = selector || text || action.url || "unknown";
2277
+ let description = "";
2278
+ if (action.type === "input" || action.type === "cdp-fill") {
2279
+ const inputKey = `${action.type}|${target}`;
2280
+ if (inputKey === lastInputKey) continue;
2281
+ lastInputKey = inputKey;
2282
+ description = `fill ${target} value=${truncate(action.value || "", 60)}`;
2283
+ } else if (action.type === "click" || action.type === "cdp-click") {
2284
+ lastInputKey = "";
2285
+ description = `click ${target}${text ? ` text=${truncate(text, 60)}` : ""}`;
2286
+ } else if (action.type === "change") {
2287
+ lastInputKey = "";
2288
+ description = `change ${target} value=${truncate(action.value || "", 60)}`;
2289
+ } else if (action.type === "submit") {
2290
+ lastInputKey = "";
2291
+ description = `submit ${target}`;
2292
+ } else if (action.type === "hover") {
2293
+ lastInputKey = "";
2294
+ description = `hover ${target}`;
2295
+ } else if (action.type === "drag") {
2296
+ lastInputKey = "";
2297
+ description = `drag from (${action.drag?.fromX ?? 0},${action.drag?.fromY ?? 0}) to (${action.drag?.toX ?? 0},${action.drag?.toY ?? 0})`;
2298
+ } else if (action.type === "drop") {
2299
+ lastInputKey = "";
2300
+ description = `drop ${action.files?.count ?? 1} file(s) on ${target}`;
2301
+ } else {
2302
+ continue;
2303
+ }
2304
+ flow.push({ step: flow.length + 1, action: description });
2305
+ }
2306
+ const keyApis = data.network.filter((entry) => (entry.resourceType === "fetch" || entry.resourceType === "xhr") && entry.contentType.toLowerCase().includes("json")).sort((a, b) => b.responseSize - a.responseSize).slice(0, COMPACT_MAX_API_ITEMS).map((entry) => ({
2307
+ method: entry.method,
2308
+ url: entry.url,
2309
+ path: entry.path,
2310
+ status: entry.status,
2311
+ contentType: entry.contentType,
2312
+ responseSize: entry.responseSize,
2313
+ ...entry.requestBody === void 0 ? {} : { requestBody: entry.requestBody }
2314
+ }));
2315
+ return {
2316
+ startUrl: data.startUrl,
2317
+ recordedAt: new Date(startedAt).toISOString(),
2318
+ durationMs: Math.max(0, Date.now() - startedAt),
2319
+ totalActions: data.actions.length,
2320
+ totalNetworkRequests: data.network.length,
2321
+ flow,
2322
+ keyApis
2323
+ };
2324
+ }
2325
+ function truncate(value, maxLength) {
2326
+ return value.length > maxLength ? `${value.slice(0, Math.max(0, maxLength - 3))}...` : value;
2327
+ }
2252
2328
  function buildSummary(data, startedAt) {
2253
2329
  const steps = [];
2254
2330
  const selectorToRef = /* @__PURE__ */ new Map();
@@ -3570,6 +3646,8 @@ var SessionRecorder = class _SessionRecorder {
3570
3646
  const lines = data.actions.map((a) => JSON.stringify(a)).join("\n") + "\n";
3571
3647
  writeFileSync2(jsonlPath, lines, "utf-8");
3572
3648
  writeFileSync2(join3(this.recordingsDir, "summary.json"), JSON.stringify(summary, null, 2), "utf-8");
3649
+ const compactSummary = buildCompactSummary(data, this.startedAt);
3650
+ writeFileSync2(join3(this.recordingsDir, "summary-compact.json"), JSON.stringify(compactSummary, null, 2), "utf-8");
3573
3651
  writeFileSync2(join3(this.recordingsDir, "summary.md"), buildMarkdownSummary(data, summary), "utf-8");
3574
3652
  }
3575
3653
  buildData() {