@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.
- package/README.md +2 -2
- package/dist/{browser-XD3IOU5I.js → browser-6PFTHZI3.js} +2 -2
- package/dist/{browser-2K3XYAKM.js → browser-I2NLJN2A.js} +1 -1
- package/dist/{browser-OTEMG5NL.js → browser-KOUA4ZDA.js} +2 -2
- package/dist/{cdp-driver-AAS55KT2.js → cdp-driver-3ETNFQA4.js} +1 -1
- package/dist/{cdp-driver-XTPY7EXP.js → cdp-driver-U7HDQMQ5.js} +257 -19
- package/dist/{cdp-driver-O5I7SJ24.js → cdp-driver-WBFJNOLD.js} +1 -1
- package/dist/{chunk-SCYBNSOG.js → chunk-3K57RYVC.js} +378 -115
- package/dist/{chunk-FNXA6FDM.js → chunk-BND5O5KC.js} +2 -1
- package/dist/{chunk-P33R7F2D.js → chunk-BOZTIIBT.js} +30 -45
- package/dist/{chunk-GDKKVT2P.js → chunk-KPTD3SNJ.js} +379 -116
- package/dist/{chunk-CO2ZVOVT.js → chunk-NINETOM4.js} +30 -45
- package/dist/{chunk-SOPN4XDK.js → chunk-O5PASNI7.js} +2 -1
- package/dist/{chunk-NQPXZ74W.js → chunk-OCECE22C.js} +257 -19
- package/dist/{chunk-LFSLNHW5.js → chunk-OSIDUT4H.js} +286 -63
- package/dist/{chunk-HBMEFSTB.js → chunk-QCV2PI7U.js} +2 -1
- package/dist/{chunk-UZ6XPUBD.js → chunk-RMYKVZIZ.js} +78 -0
- package/dist/{chunk-N5XTLNKS.js → chunk-SPNWUUHA.js} +257 -19
- package/dist/{chunk-PGKV24QN.js → chunk-TBJUSUKF.js} +1 -1
- package/dist/{chunk-FCPVQFTQ.js → chunk-VYTQLZAX.js} +78 -0
- package/dist/cli.js +45 -16
- package/dist/daemon-main.js +581 -157
- package/dist/{human-interaction-CTZ4IWXJ.js → human-interaction-ISMMOJOK.js} +2 -2
- package/dist/{human-interaction-HJCK45SL.js → human-interaction-STTZZZ5N.js} +1 -1
- package/dist/{human-interaction-V3VDGHHI.js → human-interaction-UXFYV7DU.js} +1 -1
- package/dist/index.d.ts +106 -12
- package/dist/index.js +195 -46
- package/dist/{recovery-EBKORYEU.js → recovery-JFGV3DBX.js} +1 -1
- package/dist/{recovery-5C4FBA5O.js → recovery-ORHJSBDN.js} +2 -2
- package/dist/{recovery-IMZUGWSS.js → recovery-XDKN63O5.js} +1 -1
- package/dist/{server-ZTI2K5YJ.js → server-TG47W7MV.js} +3 -3
- package/dist/{server-LNZTDAKT.js → server-U3YAX5MJ.js} +2 -2
- package/dist/{session-recorder-DF2YGH2E.js → session-recorder-N7VEKJVW.js} +1 -1
- package/dist/{session-recorder-R3GDGXLJ.js → session-recorder-RUNQOJ7T.js} +1 -1
- package/dist/{session-replayer-CLTZ2URU.js → session-replayer-XLWQ47BB.js} +1 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -286,8 +286,8 @@ xbrowser --cdp auto "goto https://example.com , title"
|
|
|
286
286
|
| `screenshot` | 截取页面截图 | `xbrowser screenshot` |
|
|
287
287
|
| `screenshot --full-page` | 全页截图 | `xbrowser screenshot --full-page` |
|
|
288
288
|
| `screenshot --type jpeg` | 指定格式 | `xbrowser screenshot --type jpeg` |
|
|
289
|
-
| `snapshot` |
|
|
290
|
-
| `snapshot --
|
|
289
|
+
| `snapshot` | 获取可操作交互元素 `@ref` 快照 | `xbrowser snapshot` |
|
|
290
|
+
| `snapshot --type aria` | 获取页面大纲 / accessibility tree | `xbrowser snapshot --type aria` |
|
|
291
291
|
|
|
292
292
|
### DOM 结构
|
|
293
293
|
|
|
@@ -21,8 +21,8 @@ import {
|
|
|
21
21
|
saveSessionDiskMeta,
|
|
22
22
|
setActivePage,
|
|
23
23
|
touchSession
|
|
24
|
-
} from "./chunk-
|
|
25
|
-
import "./chunk-
|
|
24
|
+
} from "./chunk-BOZTIIBT.js";
|
|
25
|
+
import "./chunk-SPNWUUHA.js";
|
|
26
26
|
import "./chunk-SMYSNO4M.js";
|
|
27
27
|
import "./chunk-2KPI5GKP.js";
|
|
28
28
|
import "./chunk-GDKLH7ZY.js";
|
|
@@ -21,8 +21,8 @@ import {
|
|
|
21
21
|
saveSessionDiskMeta,
|
|
22
22
|
setActivePage,
|
|
23
23
|
touchSession
|
|
24
|
-
} from "./chunk-
|
|
25
|
-
import "./chunk-
|
|
24
|
+
} from "./chunk-NINETOM4.js";
|
|
25
|
+
import "./chunk-OCECE22C.js";
|
|
26
26
|
import "./chunk-A7NEA4PA.js";
|
|
27
27
|
import "./chunk-SMYSNO4M.js";
|
|
28
28
|
import "./chunk-2KPI5GKP.js";
|
|
@@ -31,12 +31,16 @@ import { EventEmitter as EventEmitter2 } from "events";
|
|
|
31
31
|
import { EventEmitter } from "events";
|
|
32
32
|
|
|
33
33
|
// src/cdp-driver/mouse.ts
|
|
34
|
+
function buttonBit(button) {
|
|
35
|
+
return button === "left" ? 1 : button === "right" ? 2 : 4;
|
|
36
|
+
}
|
|
34
37
|
var XBMouseImpl = class {
|
|
35
38
|
conn;
|
|
36
39
|
sessionId;
|
|
37
40
|
_x = 0;
|
|
38
41
|
_y = 0;
|
|
39
42
|
_button = "none";
|
|
43
|
+
_buttons = 0;
|
|
40
44
|
constructor(conn, sessionId) {
|
|
41
45
|
this.conn = conn;
|
|
42
46
|
this.sessionId = sessionId;
|
|
@@ -67,7 +71,7 @@ var XBMouseImpl = class {
|
|
|
67
71
|
_truncated = true;
|
|
68
72
|
break;
|
|
69
73
|
}
|
|
70
|
-
await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: p.x, y: p.y, button: this._button });
|
|
74
|
+
await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: p.x, y: p.y, button: this._button, buttons: this._buttons });
|
|
71
75
|
this._x = p.x;
|
|
72
76
|
this._y = p.y;
|
|
73
77
|
await sleep(p.delay);
|
|
@@ -75,7 +79,7 @@ var XBMouseImpl = class {
|
|
|
75
79
|
this._x = tx;
|
|
76
80
|
this._y = ty;
|
|
77
81
|
if (_truncated) {
|
|
78
|
-
await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: tx, y: ty, button: this._button });
|
|
82
|
+
await this.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: tx, y: ty, button: this._button, buttons: this._buttons });
|
|
79
83
|
}
|
|
80
84
|
await sleep(rand(...DEFAULT_STEALTH_CONFIG.aimPause));
|
|
81
85
|
} else {
|
|
@@ -87,7 +91,7 @@ var XBMouseImpl = class {
|
|
|
87
91
|
const ry = stealth ? this._y + rand(...DEFAULT_STEALTH_CONFIG.releaseDrift) * (Math.random() < 0.5 ? -1 : 1) : this._y;
|
|
88
92
|
this._x = rx;
|
|
89
93
|
this._y = ry;
|
|
90
|
-
await this.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: rx, y: ry, button, clickCount: opts.clickCount ?? 1 });
|
|
94
|
+
await this.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: rx, y: ry, button, buttons: this._buttons, clickCount: opts.clickCount ?? 1 });
|
|
91
95
|
for (let i = 1; i < (opts.clickCount ?? 1); i++) {
|
|
92
96
|
if (opts.delay) await sleep(opts.delay);
|
|
93
97
|
await this.down({ button });
|
|
@@ -100,11 +104,13 @@ var XBMouseImpl = class {
|
|
|
100
104
|
async down(opts = {}) {
|
|
101
105
|
const button = opts.button ?? "left";
|
|
102
106
|
this._button = button;
|
|
107
|
+
this._buttons |= buttonBit(button);
|
|
103
108
|
await this.send("Input.dispatchMouseEvent", {
|
|
104
109
|
type: "mousePressed",
|
|
105
110
|
x: this._x,
|
|
106
111
|
y: this._y,
|
|
107
112
|
button,
|
|
113
|
+
buttons: this._buttons,
|
|
108
114
|
// r23: press 必须与 release 声明同一 clickCount——Chrome 按序列计数
|
|
109
115
|
// 合成 click/dblclick,press 缺声明会把计数器重置(dblclick 永不合成)
|
|
110
116
|
clickCount: opts.clickCount ?? 1
|
|
@@ -118,8 +124,11 @@ var XBMouseImpl = class {
|
|
|
118
124
|
x: this._x,
|
|
119
125
|
y: this._y,
|
|
120
126
|
button,
|
|
127
|
+
// release 先带位发送再清位(Puppeteer 同序——DOM mouseup 的 buttons 反映释放后状态)
|
|
128
|
+
buttons: this._buttons,
|
|
121
129
|
clickCount: opts.clickCount ?? 1
|
|
122
130
|
});
|
|
131
|
+
this._buttons &= ~buttonBit(button);
|
|
123
132
|
}
|
|
124
133
|
async move(x, y, opts = {}) {
|
|
125
134
|
const stealth = opts.stealth ?? process.env.XBROWSER_STEALTH !== "off";
|
|
@@ -137,7 +146,8 @@ var XBMouseImpl = class {
|
|
|
137
146
|
type: "mouseMoved",
|
|
138
147
|
x: this._x,
|
|
139
148
|
y: this._y,
|
|
140
|
-
button: this._button
|
|
149
|
+
button: this._button,
|
|
150
|
+
buttons: this._buttons
|
|
141
151
|
});
|
|
142
152
|
}
|
|
143
153
|
this._x = x;
|
|
@@ -157,9 +167,11 @@ var XBMouseImpl = class {
|
|
|
157
167
|
x: this._x,
|
|
158
168
|
y: this._y,
|
|
159
169
|
button: "left",
|
|
170
|
+
buttons: 1,
|
|
160
171
|
clickCount: 1
|
|
161
172
|
});
|
|
162
173
|
this._button = "left";
|
|
174
|
+
this._buttons |= 1;
|
|
163
175
|
const steps = opts.steps ?? Math.max(10, Math.min(24, Math.round(Math.hypot(x - this._x, y - this._y) / 20)));
|
|
164
176
|
const fx = this._x, fy = this._y;
|
|
165
177
|
for (let i = 1; i <= steps; i++) {
|
|
@@ -171,7 +183,8 @@ var XBMouseImpl = class {
|
|
|
171
183
|
type: "mouseMoved",
|
|
172
184
|
x: this._x,
|
|
173
185
|
y: this._y,
|
|
174
|
-
button: this._button
|
|
186
|
+
button: this._button,
|
|
187
|
+
buttons: this._buttons
|
|
175
188
|
});
|
|
176
189
|
}
|
|
177
190
|
this._x = x;
|
|
@@ -182,15 +195,22 @@ var XBMouseImpl = class {
|
|
|
182
195
|
x,
|
|
183
196
|
y,
|
|
184
197
|
button: "left",
|
|
198
|
+
buttons: this._buttons,
|
|
185
199
|
clickCount: 1
|
|
186
200
|
});
|
|
187
201
|
this._button = "none";
|
|
202
|
+
this._buttons &= ~1;
|
|
203
|
+
}
|
|
204
|
+
async wheelAt(x, y, deltaX, deltaY) {
|
|
205
|
+
await this.move(x, y);
|
|
206
|
+
await this.wheel(deltaX, deltaY);
|
|
188
207
|
}
|
|
189
208
|
async wheel(deltaX, deltaY) {
|
|
190
209
|
await this.send("Input.dispatchMouseEvent", {
|
|
191
210
|
type: "mouseWheel",
|
|
192
211
|
x: this._x,
|
|
193
212
|
y: this._y,
|
|
213
|
+
buttons: this._buttons,
|
|
194
214
|
deltaX,
|
|
195
215
|
deltaY
|
|
196
216
|
});
|
|
@@ -1452,7 +1472,7 @@ var MediaService = class {
|
|
|
1452
1472
|
y: Math.round(opts.clip.y),
|
|
1453
1473
|
width: Math.round(Math.max(1, opts.clip.width)),
|
|
1454
1474
|
height: Math.round(Math.max(1, opts.clip.height)),
|
|
1455
|
-
scale: 1
|
|
1475
|
+
scale: opts.clip.scale ?? 1
|
|
1456
1476
|
};
|
|
1457
1477
|
params.clip = clip;
|
|
1458
1478
|
}
|
|
@@ -2449,14 +2469,13 @@ var RuntimeEvaluationService = class {
|
|
|
2449
2469
|
}
|
|
2450
2470
|
return result.result?.value;
|
|
2451
2471
|
};
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2472
|
+
const attachedA = await this.tryAttachViaTargets(urlIncludes);
|
|
2473
|
+
if (attachedA) {
|
|
2474
|
+
try {
|
|
2475
|
+
return await evalIn(attachedA);
|
|
2476
|
+
} finally {
|
|
2477
|
+
this.detachQuietly(attachedA);
|
|
2458
2478
|
}
|
|
2459
|
-
} catch {
|
|
2460
2479
|
}
|
|
2461
2480
|
const tree = await this.options.conn.send("Page.getFrameTree", void 0, this.options.sessionId);
|
|
2462
2481
|
const all = [];
|
|
@@ -2484,6 +2503,87 @@ var RuntimeEvaluationService = class {
|
|
|
2484
2503
|
const ctx = contexts.find((c) => c.frameId === target.id);
|
|
2485
2504
|
if (ctx) return evalIn(this.options.sessionId, ctx.id);
|
|
2486
2505
|
}
|
|
2506
|
+
const attachedC = await this.tryAttachViaAutoAttach(urlIncludes);
|
|
2507
|
+
if (attachedC) {
|
|
2508
|
+
try {
|
|
2509
|
+
return await evalIn(attachedC);
|
|
2510
|
+
} finally {
|
|
2511
|
+
this.detachQuietly(attachedC);
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2514
|
+
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`);
|
|
2515
|
+
}
|
|
2516
|
+
/**
|
|
2517
|
+
* 输入专用 frame session 解析(mouse/click/fill --frame)。
|
|
2518
|
+
*
|
|
2519
|
+
* 与 evaluateInFrame 的区别:必须拿到**真实 target session**(Input 域
|
|
2520
|
+
* 事件派发到该 session 才会进入 iframe 的渲染进程——主页面 session 上
|
|
2521
|
+
* 派发的输入进不去 OOPIF,qqmail 验证码 4 次页面级拖动全败的实证)。
|
|
2522
|
+
* 因此只走 A(getTargets attach)/ C(auto-attach)两路;同进程 frame
|
|
2523
|
+
* 没有 session,但 locator 层的坐标偏移本就支持同源 iframe,直接用普通
|
|
2524
|
+
* click/fill 即可。
|
|
2525
|
+
*
|
|
2526
|
+
* 坐标语义:返回的 session 上派发 Input 事件用 **iframe 局部坐标**
|
|
2527
|
+
* (与该 session 内 getBoundingClientRect 一致)。
|
|
2528
|
+
*/
|
|
2529
|
+
async resolveFrameSession(urlIncludes) {
|
|
2530
|
+
if (this.options.isClosed()) throw new Error("Page is closed");
|
|
2531
|
+
const viaTargets = await this.tryAttachViaTargets(urlIncludes);
|
|
2532
|
+
if (viaTargets) return viaTargets;
|
|
2533
|
+
const viaAuto = await this.tryAttachViaAutoAttach(urlIncludes);
|
|
2534
|
+
if (viaAuto) return viaAuto;
|
|
2535
|
+
try {
|
|
2536
|
+
const tree = await this.options.conn.send("Page.getFrameTree", void 0, this.options.sessionId);
|
|
2537
|
+
const urls = [];
|
|
2538
|
+
const walk = (node) => {
|
|
2539
|
+
urls.push(node.frame.url);
|
|
2540
|
+
for (const child of node.childFrames || []) walk(child);
|
|
2541
|
+
};
|
|
2542
|
+
walk(tree.frameTree);
|
|
2543
|
+
if (urls.some((u) => u.includes(urlIncludes))) {
|
|
2544
|
+
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`);
|
|
2545
|
+
}
|
|
2546
|
+
} catch (err) {
|
|
2547
|
+
if (err instanceof Error && err.message.includes("\u540C\u8FDB\u7A0B")) throw err;
|
|
2548
|
+
}
|
|
2549
|
+
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`);
|
|
2550
|
+
}
|
|
2551
|
+
/** 浏览器级 iframe target 列表(OOPIF 可见性——Page.getFrameTree 看不到它们) */
|
|
2552
|
+
async listFrameTargets() {
|
|
2553
|
+
try {
|
|
2554
|
+
const tg = await this.options.conn.send("Target.getTargets", void 0);
|
|
2555
|
+
return (tg.targetInfos || []).filter((t) => t.type === "iframe").map((t) => ({ url: t.url || "", targetId: t.targetId }));
|
|
2556
|
+
} catch {
|
|
2557
|
+
return [];
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
/** 路径 A:浏览器级 Target.getTargets 平铺 iframe target,按 URL 子串 attach。
|
|
2561
|
+
* 多次导航后列表里可能残留正在销毁的陈旧 target——attach 后用 location.href
|
|
2562
|
+
* 验证候选确实是活目标,不匹配则换下一个(验证码 iframe 重建场景实测会撞上)。 */
|
|
2563
|
+
async tryAttachViaTargets(urlIncludes) {
|
|
2564
|
+
try {
|
|
2565
|
+
const tg = await this.options.conn.send("Target.getTargets", void 0);
|
|
2566
|
+
const candidates = (tg.targetInfos || []).filter((t) => t.type === "iframe" && (t.url || "").includes(urlIncludes));
|
|
2567
|
+
for (const hit of candidates) {
|
|
2568
|
+
try {
|
|
2569
|
+
const att = await this.options.conn.send("Target.attachToTarget", { targetId: hit.targetId, flatten: true });
|
|
2570
|
+
const probe = await this.options.conn.send("Runtime.evaluate", { expression: "location.href", returnByValue: true }, att.sessionId);
|
|
2571
|
+
const href = typeof probe.result?.value === "string" ? probe.result.value : "";
|
|
2572
|
+
if (probe.exceptionDetails || !href.includes(urlIncludes)) {
|
|
2573
|
+
this.detachQuietly(att.sessionId);
|
|
2574
|
+
continue;
|
|
2575
|
+
}
|
|
2576
|
+
return att.sessionId;
|
|
2577
|
+
} catch {
|
|
2578
|
+
continue;
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
} catch {
|
|
2582
|
+
}
|
|
2583
|
+
return null;
|
|
2584
|
+
}
|
|
2585
|
+
/** 路径 C:Target.setAutoAttach 监听后续 attach 的 iframe target(兜底) */
|
|
2586
|
+
async tryAttachViaAutoAttach(urlIncludes) {
|
|
2487
2587
|
const attached = [];
|
|
2488
2588
|
const onAttach = (raw) => {
|
|
2489
2589
|
const ev = raw;
|
|
@@ -2505,10 +2605,11 @@ var RuntimeEvaluationService = class {
|
|
|
2505
2605
|
});
|
|
2506
2606
|
}
|
|
2507
2607
|
const hit = attached.find((a) => a.url.includes(urlIncludes));
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2608
|
+
return hit ? hit.sessionId : null;
|
|
2609
|
+
}
|
|
2610
|
+
detachQuietly(sessionId) {
|
|
2611
|
+
this.options.conn.send("Target.detachFromTarget", { sessionId }).catch(() => {
|
|
2612
|
+
});
|
|
2512
2613
|
}
|
|
2513
2614
|
/** evaluateHandle — evaluates fn and returns a handle for element bounding box */
|
|
2514
2615
|
async evaluateHandle(fn, ...args) {
|
|
@@ -2560,6 +2661,103 @@ var RuntimeEvaluationService = class {
|
|
|
2560
2661
|
}
|
|
2561
2662
|
};
|
|
2562
2663
|
|
|
2664
|
+
// src/cdp-driver/page-frame-input.ts
|
|
2665
|
+
function isSessionDeadError(err) {
|
|
2666
|
+
return /session (with given id )?not found|no session with given id/i.test(errMsg(err));
|
|
2667
|
+
}
|
|
2668
|
+
var FrameInputService = class {
|
|
2669
|
+
constructor(deps) {
|
|
2670
|
+
this.deps = deps;
|
|
2671
|
+
}
|
|
2672
|
+
deps;
|
|
2673
|
+
/** urlIncludes → 已 attach 的 session + 鼠标实例(跨命令保持位置状态) */
|
|
2674
|
+
cache = /* @__PURE__ */ new Map();
|
|
2675
|
+
/**
|
|
2676
|
+
* 拿到指定 OOPIF 的鼠标(Input 域派发进 iframe 渲染进程,isTrusted=true)。
|
|
2677
|
+
* 缓存的 session 失效(iframe 重建/刷新)时自动重 attach;重 attach 会
|
|
2678
|
+
* 重置鼠标位置——调用方需重新读取目标元素坐标。
|
|
2679
|
+
*/
|
|
2680
|
+
async frameMouse(urlIncludes, opts = {}) {
|
|
2681
|
+
if (opts.fresh) this.cache.delete(urlIncludes);
|
|
2682
|
+
const cached = this.cache.get(urlIncludes);
|
|
2683
|
+
if (cached) {
|
|
2684
|
+
try {
|
|
2685
|
+
await this.deps.conn.send("Runtime.evaluate", { expression: "1", returnByValue: true }, cached.sessionId);
|
|
2686
|
+
return cached.mouse;
|
|
2687
|
+
} catch {
|
|
2688
|
+
this.cache.delete(urlIncludes);
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
const sessionId = await this.deps.resolveFrameSession(urlIncludes);
|
|
2692
|
+
const mouse = new XBMouseImpl(this.deps.conn, sessionId);
|
|
2693
|
+
this.cache.set(urlIncludes, { sessionId, mouse });
|
|
2694
|
+
return mouse;
|
|
2695
|
+
}
|
|
2696
|
+
/**
|
|
2697
|
+
* 带重试的 frame 鼠标操作:Chrome 异步销毁旧 OOPIF target 的窗口期里
|
|
2698
|
+
* (连续导航/验证码 iframe 重建),probe 通过后 dispatch 仍可能撞上死
|
|
2699
|
+
* session——session 失效时强制重 attach 再做一次。
|
|
2700
|
+
*/
|
|
2701
|
+
async runFrameMouse(urlIncludes, fn) {
|
|
2702
|
+
try {
|
|
2703
|
+
return await fn(await this.frameMouse(urlIncludes));
|
|
2704
|
+
} catch (err) {
|
|
2705
|
+
if (!isSessionDeadError(err)) throw err;
|
|
2706
|
+
this.cache.delete(urlIncludes);
|
|
2707
|
+
return fn(await this.frameMouse(urlIncludes, { fresh: true }));
|
|
2708
|
+
}
|
|
2709
|
+
}
|
|
2710
|
+
/** 在 OOPIF 内点击选择器命中的元素(frame 局部坐标,stealth 落点偏移继承) */
|
|
2711
|
+
async clickInFrame(urlIncludes, selector, opts = {}) {
|
|
2712
|
+
const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
|
|
2713
|
+
const x = rect.x + rect.width / 2;
|
|
2714
|
+
const y = rect.y + rect.height / 2;
|
|
2715
|
+
await this.runFrameMouse(urlIncludes, (mouse) => mouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height }));
|
|
2716
|
+
return { x, y };
|
|
2717
|
+
}
|
|
2718
|
+
/** 在 OOPIF 内填输入框:点击聚焦 → el.select() 全选 → insertText 整体替换 → 回读验证 */
|
|
2719
|
+
async fillInFrame(urlIncludes, selector, text, opts = {}) {
|
|
2720
|
+
try {
|
|
2721
|
+
return await this.fillInFrameInner(urlIncludes, selector, text, opts);
|
|
2722
|
+
} catch (err) {
|
|
2723
|
+
if (!isSessionDeadError(err)) throw err;
|
|
2724
|
+
this.cache.delete(urlIncludes);
|
|
2725
|
+
return this.fillInFrameInner(urlIncludes, selector, text, opts);
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
async fillInFrameInner(urlIncludes, selector, text, opts = {}) {
|
|
2729
|
+
await this.clickInFrame(urlIncludes, selector, opts);
|
|
2730
|
+
const cached = this.cache.get(urlIncludes);
|
|
2731
|
+
if (!cached) throw new Error(`frame session lost for "${urlIncludes}"`);
|
|
2732
|
+
await this.deps.evaluateInFrame(
|
|
2733
|
+
urlIncludes,
|
|
2734
|
+
`(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (el && typeof el.select === 'function') el.select(); return true; })()`
|
|
2735
|
+
);
|
|
2736
|
+
await this.deps.conn.send("Input.insertText", { text }, cached.sessionId);
|
|
2737
|
+
const readback = await this.deps.evaluateInFrame(
|
|
2738
|
+
urlIncludes,
|
|
2739
|
+
`(() => { const el = document.querySelector(${JSON.stringify(selector)}); return el ? String(el.value ?? '') : ''; })()`
|
|
2740
|
+
);
|
|
2741
|
+
return { value: readback ?? "" };
|
|
2742
|
+
}
|
|
2743
|
+
/** 轮询 OOPIF 内元素 rect(等待元素出现/可测量) */
|
|
2744
|
+
async pollFrameRect(urlIncludes, selector, timeoutMs) {
|
|
2745
|
+
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 }; })()`;
|
|
2746
|
+
const deadline = Date.now() + timeoutMs;
|
|
2747
|
+
let lastErr;
|
|
2748
|
+
while (Date.now() < deadline) {
|
|
2749
|
+
try {
|
|
2750
|
+
const rect = await this.deps.evaluateInFrame(urlIncludes, expr);
|
|
2751
|
+
if (rect) return rect;
|
|
2752
|
+
} catch (err) {
|
|
2753
|
+
lastErr = err;
|
|
2754
|
+
}
|
|
2755
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
2756
|
+
}
|
|
2757
|
+
throw new Error(`[frame ${urlIncludes}] element "${selector}" not found/visible within ${timeoutMs}ms${lastErr instanceof Error ? `\uFF08last: ${lastErr.message}\uFF09` : ""}`);
|
|
2758
|
+
}
|
|
2759
|
+
};
|
|
2760
|
+
|
|
2563
2761
|
// src/cdp-driver/page.ts
|
|
2564
2762
|
var XBPageImpl = class _XBPageImpl {
|
|
2565
2763
|
conn;
|
|
@@ -2586,6 +2784,7 @@ var XBPageImpl = class _XBPageImpl {
|
|
|
2586
2784
|
navigationService;
|
|
2587
2785
|
domQueryService;
|
|
2588
2786
|
runtimeEvaluationService;
|
|
2787
|
+
frameInputService;
|
|
2589
2788
|
// Network tracking for waitForLoadState('networkidle')
|
|
2590
2789
|
inflightRequests = /* @__PURE__ */ new Set();
|
|
2591
2790
|
networkIdleResolve = null;
|
|
@@ -2648,6 +2847,11 @@ var XBPageImpl = class _XBPageImpl {
|
|
|
2648
2847
|
isClosed: () => this._closed,
|
|
2649
2848
|
url: () => this.url()
|
|
2650
2849
|
});
|
|
2850
|
+
this.frameInputService = new FrameInputService({
|
|
2851
|
+
conn,
|
|
2852
|
+
resolveFrameSession: (urlIncludes) => this.runtimeEvaluationService.resolveFrameSession(urlIncludes),
|
|
2853
|
+
evaluateInFrame: (urlIncludes, expression) => this.evaluateInFrame(urlIncludes, expression)
|
|
2854
|
+
});
|
|
2651
2855
|
this.domQueryService = new DomQueryService({
|
|
2652
2856
|
conn,
|
|
2653
2857
|
sessionId,
|
|
@@ -2857,8 +3061,9 @@ var XBPageImpl = class _XBPageImpl {
|
|
|
2857
3061
|
"Target.getTargetInfo",
|
|
2858
3062
|
{ targetId: this._targetId }
|
|
2859
3063
|
);
|
|
2860
|
-
|
|
2861
|
-
if (
|
|
3064
|
+
const curUrl = info.targetInfo?.url ?? info.url;
|
|
3065
|
+
if (curUrl) this._url = curUrl;
|
|
3066
|
+
if (curUrl && curUrl !== "about:blank" && curUrl !== "") {
|
|
2862
3067
|
this._loadState = { loadFired: true, domContentFired: true, networkIdle: true };
|
|
2863
3068
|
}
|
|
2864
3069
|
} catch {
|
|
@@ -2982,6 +3187,29 @@ Last error: ${lastError.message}` : "";
|
|
|
2982
3187
|
async evaluateInFrame(urlIncludes, expression) {
|
|
2983
3188
|
return this.runtimeEvaluationService.evaluateInFrame(urlIncludes, expression);
|
|
2984
3189
|
}
|
|
3190
|
+
/** 浏览器级 iframe target 列表(OOPIF 可见——frames 命令用) */
|
|
3191
|
+
async listFrameTargets() {
|
|
3192
|
+
return this.runtimeEvaluationService.listFrameTargets();
|
|
3193
|
+
}
|
|
3194
|
+
/**
|
|
3195
|
+
* 拿到指定 OOPIF 的鼠标(Input 域派发进 iframe 渲染进程,isTrusted=true)。
|
|
3196
|
+
* 坐标语义:iframe 局部坐标(与该 session 内 getBoundingClientRect 一致)。
|
|
3197
|
+
* session 失效自动重 attach(鼠标位置会重置,需重读目标坐标)。
|
|
3198
|
+
*/
|
|
3199
|
+
async frameMouse(urlIncludes, opts) {
|
|
3200
|
+
return this.frameInputService.frameMouse(urlIncludes, opts);
|
|
3201
|
+
}
|
|
3202
|
+
async runFrameMouse(urlIncludes, fn) {
|
|
3203
|
+
return this.frameInputService.runFrameMouse(urlIncludes, fn);
|
|
3204
|
+
}
|
|
3205
|
+
/** 在 OOPIF 内点击选择器命中的元素(轮询等待 + stealth 落点) */
|
|
3206
|
+
async clickInFrame(urlIncludes, selector, opts) {
|
|
3207
|
+
return this.frameInputService.clickInFrame(urlIncludes, selector, opts);
|
|
3208
|
+
}
|
|
3209
|
+
/** 在 OOPIF 内填输入框(点击聚焦 + insertText + 回读) */
|
|
3210
|
+
async fillInFrame(urlIncludes, selector, text, opts) {
|
|
3211
|
+
return this.frameInputService.fillInFrame(urlIncludes, selector, text, opts);
|
|
3212
|
+
}
|
|
2985
3213
|
async evaluateHandle(script, ...args) {
|
|
2986
3214
|
return this.runtimeEvaluationService.evaluateHandle(script, ...args);
|
|
2987
3215
|
}
|
|
@@ -3712,6 +3940,16 @@ var XBBrowserImpl = class {
|
|
|
3712
3940
|
await this.conn.close();
|
|
3713
3941
|
this._emitter.emit("disconnected");
|
|
3714
3942
|
}
|
|
3943
|
+
async disconnect() {
|
|
3944
|
+
if (this._disconnected) return;
|
|
3945
|
+
this._disconnected = true;
|
|
3946
|
+
if (this._exitHandler) {
|
|
3947
|
+
process.removeListener("exit", this._exitHandler);
|
|
3948
|
+
this._exitHandler = null;
|
|
3949
|
+
}
|
|
3950
|
+
await this.conn.close();
|
|
3951
|
+
this._emitter.emit("disconnected");
|
|
3952
|
+
}
|
|
3715
3953
|
async newContext(opts = {}) {
|
|
3716
3954
|
if (this.launchOpts?.stealthConfig && !opts.stealthConfig) {
|
|
3717
3955
|
opts = { ...opts, stealthConfig: this.launchOpts.stealthConfig };
|