@shgroup/dsh-serenity-hooks 1.129.5 → 1.130.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/dsh.plugin.json +1 -1
- package/lib/browser/ax.d.ts +91 -0
- package/lib/browser/cdp.d.ts +200 -0
- package/lib/browser/keys.d.ts +43 -0
- package/lib/browser/service.d.ts +103 -1
- package/lib/browser/wait-spec.d.ts +39 -0
- package/lib/index.js +1076 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -20282,6 +20282,460 @@ function resolveShotPath(cccRoot, relOrAbs) {
|
|
|
20282
20282
|
if (abs !== rootResolved && !abs.startsWith(rootResolved + sep)) throw new Error(`截图落点越界(必须在 CCC 根内):${relOrAbs}`);
|
|
20283
20283
|
return abs;
|
|
20284
20284
|
}
|
|
20285
|
+
/**
|
|
20286
|
+
* 页面级连接的高阶封装:**一次连接、多次调用、必关**。
|
|
20287
|
+
*
|
|
20288
|
+
* 🔴 为什么需要它(新能力全靠它):既有 10 个动作每个都自己 `attach(wsConnect(...))` +
|
|
20289
|
+
* `finally close()`。新动作若照抄,会把「连接生命周期」复制十几份 ——
|
|
20290
|
+
* 而那正是本轮要避免的:**同一件事只该有一个实现**(D314 同族教训)。
|
|
20291
|
+
*
|
|
20292
|
+
* @param fn 收到一个 `send(method, params, timeoutMs)`,它就是本连接上的 `cdpRaw`
|
|
20293
|
+
* @param opts.events 要订阅的 CDP 事件名(例如 `Page.javascriptDialogOpening`)
|
|
20294
|
+
* @param opts.onEvent 事件回调(**同步**执行;不要在里面 await CDP,那会卡住消息循环)
|
|
20295
|
+
*/
|
|
20296
|
+
async function withPage(slot, targetId, fn, opts = {}) {
|
|
20297
|
+
const conn = attach(await wsConnect(await pageWsUrl(slot, targetId)));
|
|
20298
|
+
if (opts.events && opts.events.length > 0) {
|
|
20299
|
+
const want = new Set(opts.events);
|
|
20300
|
+
const prev = conn.ws.onmessage;
|
|
20301
|
+
conn.ws.onmessage = (ev) => {
|
|
20302
|
+
prev?.call(conn.ws, ev);
|
|
20303
|
+
try {
|
|
20304
|
+
const msg = JSON.parse(typeof ev.data === "string" ? ev.data : String(ev.data));
|
|
20305
|
+
if (msg.method && want.has(msg.method)) opts.onEvent?.(msg.method, msg.params ?? {});
|
|
20306
|
+
} catch {}
|
|
20307
|
+
};
|
|
20308
|
+
}
|
|
20309
|
+
try {
|
|
20310
|
+
return await fn((method, params, timeoutMs) => cdpRaw(conn, method, params, timeoutMs));
|
|
20311
|
+
} finally {
|
|
20312
|
+
conn.ws.close();
|
|
20313
|
+
}
|
|
20314
|
+
}
|
|
20315
|
+
/**
|
|
20316
|
+
* 发一个按键(三事件 `keyDown` → `char`(仅可打印)→ `keyUp`)。
|
|
20317
|
+
*
|
|
20318
|
+
* 🔴 为什么必须是三事件、且 `char` 只在可打印键上发:
|
|
20319
|
+
* 只发 `keyDown`/`keyUp`(`rawKeyDown` 形态)⇒ 大多数页面收得到事件,但**输入框里不会有字**;
|
|
20320
|
+
* 而只发 `char` ⇒ 有字但**没有 keydown**,于是「回车提交」「Esc 关闭」这类**监听 keydown 的逻辑全不触发**。
|
|
20321
|
+
* 两者都缺的表现都是「脚本成功、界面没反应」—— 没有报错。
|
|
20322
|
+
*
|
|
20323
|
+
* ⚠️ 可打印字符走 `Input.insertText` 更稳(不猜键位);本函数只在**按键语义**(Enter/Esc/方向键/快捷键)用。
|
|
20324
|
+
*/
|
|
20325
|
+
async function pressKey(slot, targetId, stroke, times = 1) {
|
|
20326
|
+
let sent = 0;
|
|
20327
|
+
await withPage(slot, targetId, async (send) => {
|
|
20328
|
+
for (let i = 0; i < times; i++) {
|
|
20329
|
+
const base = {
|
|
20330
|
+
key: stroke.key,
|
|
20331
|
+
code: stroke.code,
|
|
20332
|
+
modifiers: stroke.modifiers,
|
|
20333
|
+
windowsVirtualKeyCode: stroke.windowsVirtualKeyCode ?? 0,
|
|
20334
|
+
nativeVirtualKeyCode: stroke.windowsVirtualKeyCode ?? 0
|
|
20335
|
+
};
|
|
20336
|
+
await send("Input.dispatchKeyEvent", {
|
|
20337
|
+
type: "keyDown",
|
|
20338
|
+
...base
|
|
20339
|
+
});
|
|
20340
|
+
if ([...stroke.key].length === 1) await send("Input.dispatchKeyEvent", {
|
|
20341
|
+
type: "char",
|
|
20342
|
+
...base,
|
|
20343
|
+
text: stroke.key,
|
|
20344
|
+
unmodifiedText: stroke.key
|
|
20345
|
+
});
|
|
20346
|
+
await send("Input.dispatchKeyEvent", {
|
|
20347
|
+
type: "keyUp",
|
|
20348
|
+
...base
|
|
20349
|
+
});
|
|
20350
|
+
sent++;
|
|
20351
|
+
}
|
|
20352
|
+
});
|
|
20353
|
+
return sent;
|
|
20354
|
+
}
|
|
20355
|
+
/**
|
|
20356
|
+
* 往「当前焦点」打字(`Input.insertText`)。
|
|
20357
|
+
*
|
|
20358
|
+
* 🔴 与 `fill` 的分工(⛔ 别互相顶替):
|
|
20359
|
+
* · `insertText` = **模拟人打字**:走浏览器的文本输入路径 ⇒ 受控组件(React/Vue)**会收到**真事件。
|
|
20360
|
+
* 但它只作用于**当前有焦点**的元素,且**不触发 change**(要 blur 才触发)。
|
|
20361
|
+
* · `fill` = **设值**:清空 + 设值 + 派发 input/change,一步到位,且不依赖焦点。
|
|
20362
|
+
* ⇒ 要「像人一样逐字输入、触发每次 keydown」用 `type`;要「把这个字段设成这个值」用 `fill`。
|
|
20363
|
+
*/
|
|
20364
|
+
async function insertText(slot, targetId, text) {
|
|
20365
|
+
await withPage(slot, targetId, async (send) => {
|
|
20366
|
+
await send("Input.insertText", { text });
|
|
20367
|
+
});
|
|
20368
|
+
}
|
|
20369
|
+
/**
|
|
20370
|
+
* 滚轮滚动(`Input.dispatchMouseEvent` wheel)。
|
|
20371
|
+
*
|
|
20372
|
+
* 🔴 判据:滚动**必须走真实 wheel 事件**,⛔ 不用 `window.scrollTo`:
|
|
20373
|
+
* 前者会触发懒加载/无限滚动/吸顶 header 的监听;后者只移动视口 ⇒ 页面上的虚拟列表**不加载新行**,
|
|
20374
|
+
* 而读数看起来「滚动成功」(scrollY 变了)。
|
|
20375
|
+
*/
|
|
20376
|
+
async function scrollAt(slot, targetId, x, y, deltaX, deltaY) {
|
|
20377
|
+
await withPage(slot, targetId, async (send) => {
|
|
20378
|
+
await send("Input.dispatchMouseEvent", {
|
|
20379
|
+
type: "mouseWheel",
|
|
20380
|
+
x,
|
|
20381
|
+
y,
|
|
20382
|
+
deltaX,
|
|
20383
|
+
deltaY,
|
|
20384
|
+
pointerType: "mouse"
|
|
20385
|
+
});
|
|
20386
|
+
});
|
|
20387
|
+
}
|
|
20388
|
+
/**
|
|
20389
|
+
* 拖拽(`mousePressed` → 多次 `mouseMoved` → `mouseReleased`)。
|
|
20390
|
+
*
|
|
20391
|
+
* 🔴 为什么必须**分多步移动**、且每步之间要 `await`:
|
|
20392
|
+
* HTML5 拖放与大多数拖拽库(dnd-kit / sortablejs / 原生 draggable)都监听 `mousemove` 序列来算方向与阈值。
|
|
20393
|
+
* **一次 `mouseMoved` 直接跳到终点** ⇒ 它们收到的是一个「瞬移」,判定为不构成拖拽 ⇒ 什么都不发生,
|
|
20394
|
+
* 而回执与真拖拽一模一样。steps 默认 12 是实测下限(再少 sortablejs 偶发不认)。
|
|
20395
|
+
*/
|
|
20396
|
+
async function dragFromTo(slot, targetId, from, to, steps = 12) {
|
|
20397
|
+
const n = Math.max(2, Math.min(64, Math.round(steps)));
|
|
20398
|
+
await withPage(slot, targetId, async (send) => {
|
|
20399
|
+
await send("Input.dispatchMouseEvent", {
|
|
20400
|
+
type: "mouseMoved",
|
|
20401
|
+
x: from.x,
|
|
20402
|
+
y: from.y
|
|
20403
|
+
});
|
|
20404
|
+
await send("Input.dispatchMouseEvent", {
|
|
20405
|
+
type: "mousePressed",
|
|
20406
|
+
x: from.x,
|
|
20407
|
+
y: from.y,
|
|
20408
|
+
button: "left",
|
|
20409
|
+
clickCount: 1
|
|
20410
|
+
});
|
|
20411
|
+
for (let i = 1; i <= n; i++) {
|
|
20412
|
+
const t = i / n;
|
|
20413
|
+
await send("Input.dispatchMouseEvent", {
|
|
20414
|
+
type: "mouseMoved",
|
|
20415
|
+
x: Math.round(from.x + (to.x - from.x) * t),
|
|
20416
|
+
y: Math.round(from.y + (to.y - from.y) * t),
|
|
20417
|
+
button: "left",
|
|
20418
|
+
buttons: 1
|
|
20419
|
+
});
|
|
20420
|
+
}
|
|
20421
|
+
await send("Input.dispatchMouseEvent", {
|
|
20422
|
+
type: "mouseReleased",
|
|
20423
|
+
x: to.x,
|
|
20424
|
+
y: to.y,
|
|
20425
|
+
button: "left",
|
|
20426
|
+
clickCount: 1
|
|
20427
|
+
});
|
|
20428
|
+
});
|
|
20429
|
+
}
|
|
20430
|
+
/**
|
|
20431
|
+
* 在页面里跑的**设值器**:给一个 backendNodeId 与该设什么值,返回设完之后的读数。
|
|
20432
|
+
*
|
|
20433
|
+
* 🔴🔴 这是本轮最容易被做错的一格,判据有三条:
|
|
20434
|
+
*
|
|
20435
|
+
* ① **必须用原型上的原生 setter**(`Object.getOwnPropertyDescriptor(proto,'value').set.call(el,v)`)。
|
|
20436
|
+
* 直接 `el.value = 'x'` 在 React 下**不触发 onChange** ⇒ 界面上显示新值,但**提交的仍是旧值**。
|
|
20437
|
+
* 这是最贵的一种错:读数(界面上有字)与真相(state 没变)相反,而且**永不报错**。
|
|
20438
|
+
* ② **必须派发 `input` 和 `change` 两个事件**,且 `input` 用 `InputEvent`(带 `inputType`)。
|
|
20439
|
+
* 只发 `change` ⇒ 监听 input 的组件不更新;只发 `input` ⇒ 提交时校验不认。
|
|
20440
|
+
* ③ **contenteditable 走另一条路**(`textContent` 无 setter 钩子)⇒ 用 `execCommand('insertText')`
|
|
20441
|
+
* (唯一能让富文本编辑器(ProseMirror/Quill/Slate)真正更新的路径;它们监听 beforeinput)。
|
|
20442
|
+
*
|
|
20443
|
+
* ⚠️ 返回值**只报读回来的值**,⛔ 不报「设置成功」 —— 判据:设值这个动作没有可靠的成功信号,
|
|
20444
|
+
* 唯一能说的是「读回来是什么」;两者不同就是失败,而那正是调用方要看见的东西。
|
|
20445
|
+
*/
|
|
20446
|
+
/**
|
|
20447
|
+
* 设值:CDP 侧 `DOM.resolveNode` 拿 objectId,再 `Runtime.callFunctionOn` 跑设值器。
|
|
20448
|
+
*
|
|
20449
|
+
* 🔴 为什么不用页面内的 `eval` 直接 querySelector:
|
|
20450
|
+
* ref 是 backendNodeId(AX 快照给的),它**不告诉你 CSS 选择器** ——
|
|
20451
|
+
* 要把 backendNodeId 变回可操作的 JS 对象,**唯一正确通道就是 DOM.resolveNode**。
|
|
20452
|
+
* ⛔ 不要试图「猜一个选择器」(那是猜,猜错就设到别的元素上,且静默)。
|
|
20453
|
+
*/
|
|
20454
|
+
async function fillByBackendId(slot, targetId, backendNodeId, value) {
|
|
20455
|
+
return await withPage(slot, targetId, async (send) => {
|
|
20456
|
+
await send("DOM.enable");
|
|
20457
|
+
const objectId = (await send("DOM.resolveNode", { backendNodeId })).object?.objectId;
|
|
20458
|
+
if (!objectId) return {
|
|
20459
|
+
ok: false,
|
|
20460
|
+
tag: "",
|
|
20461
|
+
readBack: "",
|
|
20462
|
+
why: "resolveNode 没有给出 objectId(节点可能已失效)"
|
|
20463
|
+
};
|
|
20464
|
+
const r = await send("Runtime.callFunctionOn", {
|
|
20465
|
+
objectId,
|
|
20466
|
+
functionDeclaration: FILL_FN,
|
|
20467
|
+
arguments: [{ value }],
|
|
20468
|
+
returnByValue: true,
|
|
20469
|
+
awaitPromise: true
|
|
20470
|
+
});
|
|
20471
|
+
const ex = r.exceptionDetails;
|
|
20472
|
+
if (ex) return {
|
|
20473
|
+
ok: false,
|
|
20474
|
+
tag: "",
|
|
20475
|
+
readBack: "",
|
|
20476
|
+
why: (ex.text ?? "") + " " + (ex.exception?.description ?? "")
|
|
20477
|
+
};
|
|
20478
|
+
const v = r.result?.value;
|
|
20479
|
+
return {
|
|
20480
|
+
ok: v?.ok === true,
|
|
20481
|
+
tag: String(v?.tag ?? ""),
|
|
20482
|
+
readBack: String(v?.readBack ?? ""),
|
|
20483
|
+
...v?.why ? { why: String(v.why) } : {}
|
|
20484
|
+
};
|
|
20485
|
+
});
|
|
20486
|
+
}
|
|
20487
|
+
/**
|
|
20488
|
+
* 页面里跑的设值函数(`this` = 目标元素)。
|
|
20489
|
+
*
|
|
20490
|
+
* 三条判据见 `fillByBackendId` 的头注释;这里只补两条实现细节:
|
|
20491
|
+
* · `input` 事件优先用 `InputEvent`,不支持时降级 `Event`(老引擎/某些自定义元素)
|
|
20492
|
+
* · `input type=file` **不走这条路**(它的 value 是只读的设在 C:\fakepath)⇒ 明确拒绝并指路 `upload`
|
|
20493
|
+
*/
|
|
20494
|
+
const FILL_FN = `function (value) {
|
|
20495
|
+
const el = this;
|
|
20496
|
+
const tag = (el.tagName || '').toLowerCase();
|
|
20497
|
+
if (tag === 'input' && (el.getAttribute('type') || '').toLowerCase() === 'file') {
|
|
20498
|
+
return { ok: false, tag, readBack: '', why: 'input[type=file] 不能用设值 —— 用 browser_slot upload(走 DOM.setFileInputFiles)' };
|
|
20499
|
+
}
|
|
20500
|
+
if (el.isContentEditable) {
|
|
20501
|
+
el.focus();
|
|
20502
|
+
const sel = window.getSelection();
|
|
20503
|
+
const range = document.createRange();
|
|
20504
|
+
range.selectNodeContents(el);
|
|
20505
|
+
sel.removeAllRanges();
|
|
20506
|
+
sel.addRange(range);
|
|
20507
|
+
let ok = false;
|
|
20508
|
+
try { ok = document.execCommand('insertText', false, value); } catch { ok = false; }
|
|
20509
|
+
if (!ok) { el.textContent = value; }
|
|
20510
|
+
el.dispatchEvent(new InputEvent('input', { bubbles: true, inputType: 'insertText', data: value }));
|
|
20511
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
20512
|
+
return { ok: true, tag, readBack: (el.innerText || el.textContent || '') + '' };
|
|
20513
|
+
}
|
|
20514
|
+
const proto = tag === 'textarea' ? window.HTMLTextAreaElement.prototype : tag === 'select' ? window.HTMLSelectElement.prototype : window.HTMLInputElement.prototype;
|
|
20515
|
+
const desc = Object.getOwnPropertyDescriptor(proto, 'value');
|
|
20516
|
+
if (!desc || !desc.set) return { ok: false, tag, readBack: '', why: '这个元素没有 value setter(不是表单控件)' };
|
|
20517
|
+
el.focus();
|
|
20518
|
+
desc.set.call(el, value);
|
|
20519
|
+
let ev;
|
|
20520
|
+
try { ev = new InputEvent('input', { bubbles: true, inputType: 'insertText', data: value }); }
|
|
20521
|
+
catch { ev = new Event('input', { bubbles: true }); }
|
|
20522
|
+
el.dispatchEvent(ev);
|
|
20523
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
20524
|
+
return { ok: true, tag, readBack: (el.value === undefined ? '' : el.value) + '' };
|
|
20525
|
+
}`;
|
|
20526
|
+
/**
|
|
20527
|
+
* AX 快照:拿整棵无障碍树,交给 `ax.ts` 的纯判定去剪。
|
|
20528
|
+
*
|
|
20529
|
+
* 🔴 为什么先 `Accessibility.enable`:不启用时 `getFullAXTree` 给的是**空数组**(不是报错)。
|
|
20530
|
+
* 那是典型的恒空读数 —— 与「页面上没有可交互元素」同形。
|
|
20531
|
+
*/
|
|
20532
|
+
async function captureAx(slot, targetId) {
|
|
20533
|
+
return await withPage(slot, targetId, async (send) => {
|
|
20534
|
+
await send("Accessibility.enable");
|
|
20535
|
+
const nodes = (await send("Accessibility.getFullAXTree", {}, 2e4)).nodes;
|
|
20536
|
+
return Array.isArray(nodes) ? nodes : [];
|
|
20537
|
+
});
|
|
20538
|
+
}
|
|
20539
|
+
/**
|
|
20540
|
+
* 把 backendNodeId 变成一个可点的坐标(`DOM.getBoxModel` 的中心点)。
|
|
20541
|
+
*
|
|
20542
|
+
* 🔴 为什么不用 `Runtime.callFunctionOn` + `getBoundingClientRect`:
|
|
20543
|
+
* `getBoundingClientRect` 是**相对视口且受 transform 影响**的读数,而 `DOM.getBoxModel`
|
|
20544
|
+
* 给的是**布局盒**坐标(同样视口系)且会把 padding/border 算进去;
|
|
20545
|
+
* 更关键的是:`getBoxModel` 对 `display:none` / 零尺寸元素**会明确失败**,
|
|
20546
|
+
* 而 rect 会给一个全 0 的「成功」读数 ⇒ 点 (0,0)。前者报错,后者点错地方。
|
|
20547
|
+
*
|
|
20548
|
+
* ⚠️ 元素**在视口外**时盒模型仍在(会被滚动条挡住)⇒ 返回值额外带 `inViewport`,
|
|
20549
|
+
* 调用方据此决定要不要先 `scroll`。⛔ 这里不代劳滚动:滚动本身有副作用(懒加载)。
|
|
20550
|
+
*/
|
|
20551
|
+
async function boxCenterByBackendId(slot, targetId, backendNodeId) {
|
|
20552
|
+
return await withPage(slot, targetId, async (send) => {
|
|
20553
|
+
await send("DOM.enable");
|
|
20554
|
+
const box = await send("DOM.getBoxModel", { backendNodeId });
|
|
20555
|
+
const quad = box.model?.border ?? box.model?.content;
|
|
20556
|
+
if (!quad || quad.length < 8) throw new Error("这个 ref 所在元素没有可见盒(可能 display:none / 零尺寸)");
|
|
20557
|
+
const xs = [
|
|
20558
|
+
quad[0],
|
|
20559
|
+
quad[2],
|
|
20560
|
+
quad[4],
|
|
20561
|
+
quad[6]
|
|
20562
|
+
];
|
|
20563
|
+
const ys = [
|
|
20564
|
+
quad[1],
|
|
20565
|
+
quad[3],
|
|
20566
|
+
quad[5],
|
|
20567
|
+
quad[7]
|
|
20568
|
+
];
|
|
20569
|
+
const minX = Math.min(...xs);
|
|
20570
|
+
const maxX = Math.max(...xs);
|
|
20571
|
+
const minY = Math.min(...ys);
|
|
20572
|
+
const maxY = Math.max(...ys);
|
|
20573
|
+
const vp = await send("Runtime.evaluate", {
|
|
20574
|
+
expression: "JSON.stringify({w:innerWidth,h:innerHeight})",
|
|
20575
|
+
returnByValue: true
|
|
20576
|
+
});
|
|
20577
|
+
let vw = 0;
|
|
20578
|
+
let vh = 0;
|
|
20579
|
+
try {
|
|
20580
|
+
const o = JSON.parse(String(vp.result?.value ?? "{}"));
|
|
20581
|
+
vw = Number(o.w ?? 0);
|
|
20582
|
+
vh = Number(o.h ?? 0);
|
|
20583
|
+
} catch {}
|
|
20584
|
+
const cx = (minX + maxX) / 2;
|
|
20585
|
+
const cy = (minY + maxY) / 2;
|
|
20586
|
+
return {
|
|
20587
|
+
x: Math.round(cx),
|
|
20588
|
+
y: Math.round(cy),
|
|
20589
|
+
w: Math.round(maxX - minX),
|
|
20590
|
+
h: Math.round(maxY - minY),
|
|
20591
|
+
inViewport: vw > 0 && vh > 0 && cx >= 0 && cy >= 0 && cx <= vw && cy <= vh
|
|
20592
|
+
};
|
|
20593
|
+
});
|
|
20594
|
+
}
|
|
20595
|
+
/**
|
|
20596
|
+
* 上传文件:`DOM.setFileInputFiles`。
|
|
20597
|
+
*
|
|
20598
|
+
* 🔴🔴 **这是本轮唯一一个能把本地文件送进网页的动作**,所以守卫写在这里而不是工具层:
|
|
20599
|
+
*
|
|
20600
|
+
* ① **路径必须在 CCC 根内**(`insideRoot` 由调用方传入已判定的结果 ——
|
|
20601
|
+
* ⛔ 本函数不自己算根:那会造出第二个「根在哪」的判定点,D307 同族)。
|
|
20602
|
+
* ② **文件必须真实存在且是普通文件**(目录/符号链接/设备文件一律拒)——
|
|
20603
|
+
* 判据:`DOM.setFileInputFiles` 对不存在的路径**静默成功**(回执 OK,但表单里是空的),
|
|
20604
|
+
* 而那个空上传在页面上看起来与「上传中」一样。
|
|
20605
|
+
* ③ **必须同时显式给 target 的 ref/坐标**:不给就变成「上传到页面上任意 file input」,
|
|
20606
|
+
* 而真实页面常有多个(头像/附件/水印)⇒ 传错的那个不会报错,会成功。
|
|
20607
|
+
*/
|
|
20608
|
+
async function uploadFiles(slot, targetId, backendNodeId, absPaths) {
|
|
20609
|
+
return await withPage(slot, targetId, async (send) => {
|
|
20610
|
+
await send("DOM.enable");
|
|
20611
|
+
await send("DOM.setFileInputFiles", {
|
|
20612
|
+
files: [...absPaths],
|
|
20613
|
+
backendNodeId
|
|
20614
|
+
});
|
|
20615
|
+
return { files: [...absPaths] };
|
|
20616
|
+
});
|
|
20617
|
+
}
|
|
20618
|
+
/**
|
|
20619
|
+
* 摄取下载:把 `Browser.setDownloadBehavior` 指向一个固定目录,然后**读那个目录**。
|
|
20620
|
+
*
|
|
20621
|
+
* 🔴 为什么必须显式设行为(而不是「点了下载就完事」):
|
|
20622
|
+
* 默认行为是弹系统保存框 / 落到用户 `~/Downloads`。前者会把自动化**卡死**(等人点);
|
|
20623
|
+
* 后者让文件落在**我们看不见的地方**,而回执一切正常。
|
|
20624
|
+
*
|
|
20625
|
+
* 🔴 判据:**落点只有一个**(调用方给,必须在 CCC 根内)。⛔ 不给「用默认目录」这个选项 ——
|
|
20626
|
+
* 那会让「这个文件哪来的」在下一次读数里无法重建。
|
|
20627
|
+
*/
|
|
20628
|
+
async function enableDownloads(slot, targetId, absDir) {
|
|
20629
|
+
await withPage(slot, targetId, async (send) => {
|
|
20630
|
+
await send("Page.setDownloadBehavior", {
|
|
20631
|
+
behavior: "allow",
|
|
20632
|
+
downloadPath: absDir
|
|
20633
|
+
}, 1e4);
|
|
20634
|
+
});
|
|
20635
|
+
}
|
|
20636
|
+
/**
|
|
20637
|
+
* 列目录(下载捕获用;只读、不递归)。
|
|
20638
|
+
*
|
|
20639
|
+
* ⚠️ 返回**文件名与大小**,⛔ 不读内容:读内容会让一个「下载了 40 MB 视频」的动作
|
|
20640
|
+
* 把 40 MB 塞进上下文。要看内容由调用方自己决定怎么读。
|
|
20641
|
+
*/
|
|
20642
|
+
function listDownloaded(absDir) {
|
|
20643
|
+
let names;
|
|
20644
|
+
try {
|
|
20645
|
+
names = readdirSync(absDir);
|
|
20646
|
+
} catch {
|
|
20647
|
+
return [];
|
|
20648
|
+
}
|
|
20649
|
+
const out = [];
|
|
20650
|
+
for (const n of names) try {
|
|
20651
|
+
const st = statSync(join(absDir, n));
|
|
20652
|
+
if (st.isFile()) out.push({
|
|
20653
|
+
name: n,
|
|
20654
|
+
bytes: st.size
|
|
20655
|
+
});
|
|
20656
|
+
} catch {}
|
|
20657
|
+
return out.sort((a, b) => a.name.localeCompare(b.name));
|
|
20658
|
+
}
|
|
20659
|
+
/**
|
|
20660
|
+
* 处理原生 dialog(`alert` / `confirm` / `prompt` / `beforeunload`)。
|
|
20661
|
+
*
|
|
20662
|
+
* 🔴🔴 **不处理就会把整个页面卡死** —— `alert`/`confirm` 是**同步阻塞**的,
|
|
20663
|
+
* 页面脚本停在那一行,之后所有 `Runtime.evaluate` 全部超时。而**没有 dialog 时调用这个
|
|
20664
|
+
* 又会报错**(Chrome:`No dialog is showing`)⇒ 所以它必须是「先探、再处理」。
|
|
20665
|
+
*
|
|
20666
|
+
* 判据:**默认 dismiss**(等于点「取消」)。⛔ 不默认 accept ——
|
|
20667
|
+
* 「确定」在真实站点上常意味着**删除/支付/发布**这类不可逆动作,
|
|
20668
|
+
* 而对话框正文由页面控制(可以写任何话)。要接受必须由调用方显式要求。
|
|
20669
|
+
*/
|
|
20670
|
+
async function handleDialog(slot, targetId, accept, promptText) {
|
|
20671
|
+
let seen = {
|
|
20672
|
+
hadDialog: false,
|
|
20673
|
+
type: "",
|
|
20674
|
+
message: ""
|
|
20675
|
+
};
|
|
20676
|
+
await withPage(slot, targetId, async (send) => {
|
|
20677
|
+
await send("Page.enable");
|
|
20678
|
+
try {
|
|
20679
|
+
await send("Page.handleJavaScriptDialog", {
|
|
20680
|
+
accept,
|
|
20681
|
+
...promptText !== void 0 ? { promptText } : {}
|
|
20682
|
+
});
|
|
20683
|
+
seen = {
|
|
20684
|
+
hadDialog: true,
|
|
20685
|
+
type: "",
|
|
20686
|
+
message: ""
|
|
20687
|
+
};
|
|
20688
|
+
} catch (e) {
|
|
20689
|
+
const msg = String(e?.message ?? e);
|
|
20690
|
+
if (/no dialog|No dialog is showing/i.test(msg)) return;
|
|
20691
|
+
throw e;
|
|
20692
|
+
}
|
|
20693
|
+
}, {
|
|
20694
|
+
events: ["Page.javascriptDialogOpening"],
|
|
20695
|
+
onEvent: (_m, p) => {
|
|
20696
|
+
seen = {
|
|
20697
|
+
hadDialog: true,
|
|
20698
|
+
type: String(p.type ?? ""),
|
|
20699
|
+
message: String(p.message ?? "")
|
|
20700
|
+
};
|
|
20701
|
+
}
|
|
20702
|
+
});
|
|
20703
|
+
return {
|
|
20704
|
+
...seen,
|
|
20705
|
+
accepted: seen.hadDialog ? accept : false
|
|
20706
|
+
};
|
|
20707
|
+
}
|
|
20708
|
+
/**
|
|
20709
|
+
* 在页面里等一个条件成立(`wait` 动作)。
|
|
20710
|
+
*
|
|
20711
|
+
* 🔴🔴 为什么条件表达式在**页面内轮询**,而不是在宿主侧每次 `eval` 一次:
|
|
20712
|
+
* 宿主侧轮询 = 每 200ms 一次 CDP 往返 ⇒ 等 30s 就是 150 次往返,
|
|
20713
|
+
* 而**每次往返都可能因为导航而失败**(页面对到了一个死连接),于是「等」本身成了不稳定源。
|
|
20714
|
+
* 页面内轮询 = 一次调用、一次往返,条件是同一段 JS 的连续求值。
|
|
20715
|
+
*
|
|
20716
|
+
* 🔴 判据(超时必须说清等的是什么):
|
|
20717
|
+
* 超时返回 `{ ok:false, waitedMs, last }` —— `last` 是**最后一次求值的原始结果**,
|
|
20718
|
+
* ⛔ 不只给一句「超时」。没有 `last` 时,读者无法区分:
|
|
20719
|
+
* ① 选择器写错了(`last` = null/false)② 元素出现了但条件是别的东西(`last` = 具体值)
|
|
20720
|
+
* 这一格是本轨迹的老账(D252 同族:读了数 ≠ 读的是那个数)。
|
|
20721
|
+
*/
|
|
20722
|
+
async function waitForInPage(slot, targetId, expression, timeoutMs, pollMs = 200) {
|
|
20723
|
+
const started = Date.now();
|
|
20724
|
+
const budget = Math.max(50, Math.round(timeoutMs));
|
|
20725
|
+
const poll = Math.max(50, Math.round(pollMs));
|
|
20726
|
+
return await withPage(slot, targetId, async (send) => {
|
|
20727
|
+
const v = (await send("Runtime.evaluate", {
|
|
20728
|
+
expression: "(async () => { const deadline = Date.now() + " + String(budget) + "; let last = null; while (Date.now() < deadline) { try { last = await (" + expression + "); } catch (e) { last = \"ERR: \" + (e && e.message ? e.message : String(e)); } if (last === true) return { ok: true, last }; if (last && typeof last === \"object\" && last.ok === true) return { ok: true, last }; await new Promise(r => setTimeout(r, " + String(poll) + ")); } return { ok: false, last };})()",
|
|
20729
|
+
awaitPromise: true,
|
|
20730
|
+
returnByValue: true
|
|
20731
|
+
}, budget + 1e4)).result?.value;
|
|
20732
|
+
return {
|
|
20733
|
+
ok: v?.ok === true,
|
|
20734
|
+
waitedMs: Date.now() - started,
|
|
20735
|
+
last: v?.last ?? null
|
|
20736
|
+
};
|
|
20737
|
+
});
|
|
20738
|
+
}
|
|
20285
20739
|
//#endregion
|
|
20286
20740
|
//#region src/browser/extend.ts
|
|
20287
20741
|
/**
|
|
@@ -20694,6 +21148,317 @@ function renderClickables(list, max = 15) {
|
|
|
20694
21148
|
return lines.join("\n");
|
|
20695
21149
|
}
|
|
20696
21150
|
//#endregion
|
|
21151
|
+
//#region src/browser/keys.ts
|
|
21152
|
+
/**
|
|
21153
|
+
* browser/keys.ts — 键盘按键的**纯判定**("Meta+A" → CDP dispatchKeyEvent 参数)。
|
|
21154
|
+
*
|
|
21155
|
+
* 🔴 为什么单独一个文件、且必须纯:**按键是最容易"看起来成功"的动作**。
|
|
21156
|
+
* 发错一个字段,页面只是"没反应" —— 没有报错、没有异常、也没有任何读数与"按对了但没效果"区分。
|
|
21157
|
+
* 所以这张映射表必须能被逐条单测,且键名解析失败必须**拒绝**而不是猜。
|
|
21158
|
+
*
|
|
21159
|
+
* ⛔ 刻意不做的事:
|
|
21160
|
+
* · ⛔ 不支持"任意字符 → 键码"的猜法(Unicode 文本走 `Input.insertText`,不猜键位)
|
|
21161
|
+
* · ⛔ 不接受裸字符串走 `key` 字段却不给 `code`(Chrome 对 `code` 缺失的按键行为随版本漂)
|
|
21162
|
+
*/
|
|
21163
|
+
/** CDP `Input.dispatchKeyEvent` 的修饰位(与 DOM 的 KeyboardEvent.modifier 一致) */
|
|
21164
|
+
const MODIFIERS = {
|
|
21165
|
+
Alt: 1,
|
|
21166
|
+
Control: 2,
|
|
21167
|
+
Meta: 4,
|
|
21168
|
+
Shift: 8
|
|
21169
|
+
};
|
|
21170
|
+
/** 常用键:键名(大小写照收)→ code + VK。⛔ 只收"有明确 VK 且会触发默认行为"的那些。 */
|
|
21171
|
+
const NAMED = {
|
|
21172
|
+
enter: {
|
|
21173
|
+
code: "Enter",
|
|
21174
|
+
vk: 13,
|
|
21175
|
+
cdkey: "Enter"
|
|
21176
|
+
},
|
|
21177
|
+
return: {
|
|
21178
|
+
code: "Enter",
|
|
21179
|
+
vk: 13,
|
|
21180
|
+
cdkey: "Enter"
|
|
21181
|
+
},
|
|
21182
|
+
tab: {
|
|
21183
|
+
code: "Tab",
|
|
21184
|
+
vk: 9,
|
|
21185
|
+
cdkey: "Tab"
|
|
21186
|
+
},
|
|
21187
|
+
escape: {
|
|
21188
|
+
code: "Escape",
|
|
21189
|
+
vk: 27,
|
|
21190
|
+
cdkey: "Escape"
|
|
21191
|
+
},
|
|
21192
|
+
esc: {
|
|
21193
|
+
code: "Escape",
|
|
21194
|
+
vk: 27,
|
|
21195
|
+
cdkey: "Escape"
|
|
21196
|
+
},
|
|
21197
|
+
backspace: {
|
|
21198
|
+
code: "Backspace",
|
|
21199
|
+
vk: 8,
|
|
21200
|
+
cdkey: "Backspace"
|
|
21201
|
+
},
|
|
21202
|
+
delete: {
|
|
21203
|
+
code: "Delete",
|
|
21204
|
+
vk: 46,
|
|
21205
|
+
cdkey: "Delete"
|
|
21206
|
+
},
|
|
21207
|
+
space: {
|
|
21208
|
+
code: "Space",
|
|
21209
|
+
vk: 32,
|
|
21210
|
+
cdkey: " "
|
|
21211
|
+
},
|
|
21212
|
+
arrowup: {
|
|
21213
|
+
code: "ArrowUp",
|
|
21214
|
+
vk: 38,
|
|
21215
|
+
cdkey: "ArrowUp"
|
|
21216
|
+
},
|
|
21217
|
+
arrowdown: {
|
|
21218
|
+
code: "ArrowDown",
|
|
21219
|
+
vk: 40,
|
|
21220
|
+
cdkey: "ArrowDown"
|
|
21221
|
+
},
|
|
21222
|
+
arrowleft: {
|
|
21223
|
+
code: "ArrowLeft",
|
|
21224
|
+
vk: 37,
|
|
21225
|
+
cdkey: "ArrowLeft"
|
|
21226
|
+
},
|
|
21227
|
+
arrowright: {
|
|
21228
|
+
code: "ArrowRight",
|
|
21229
|
+
vk: 39,
|
|
21230
|
+
cdkey: "ArrowRight"
|
|
21231
|
+
},
|
|
21232
|
+
home: {
|
|
21233
|
+
code: "Home",
|
|
21234
|
+
vk: 36,
|
|
21235
|
+
cdkey: "Home"
|
|
21236
|
+
},
|
|
21237
|
+
end: {
|
|
21238
|
+
code: "End",
|
|
21239
|
+
vk: 35,
|
|
21240
|
+
cdkey: "End"
|
|
21241
|
+
},
|
|
21242
|
+
pageup: {
|
|
21243
|
+
code: "PageUp",
|
|
21244
|
+
vk: 33,
|
|
21245
|
+
cdkey: "PageUp"
|
|
21246
|
+
},
|
|
21247
|
+
pagedown: {
|
|
21248
|
+
code: "PageDown",
|
|
21249
|
+
vk: 34,
|
|
21250
|
+
cdkey: "PageDown"
|
|
21251
|
+
}
|
|
21252
|
+
};
|
|
21253
|
+
/**
|
|
21254
|
+
* 解析一个按键描述:`Enter` / `Meta+A` / `Control+Shift+Enter` / `a`。
|
|
21255
|
+
*
|
|
21256
|
+
* 判据(每条都是"宁可拒绝"):
|
|
21257
|
+
* ① 修饰名只认 `Alt` `Control` `Meta` `Shift`(大小写不敏感);⛔ 不认 `Cmd`/`Ctrl` 这类别名 ——
|
|
21258
|
+
* 别名一旦猜错,`Cmd+A` 会变成无修饰的 `A`(全选变打字),**而那没有任何信号**。
|
|
21259
|
+
* ② 单字符 ⇒ 字母/数字才认(`a`→`KeyA` / `1`→`Digit1`);其余单字符(中文/emoji/标点)
|
|
21260
|
+
* ⇒ **拒绝**并指路 `--text`(走 insertText)。判据:键位表里没有的东西,"按"它只是碰运气。
|
|
21261
|
+
* ③ 解析不出 ⇒ 返回 `null`(调用方据此拒绝并列出可用键名),⛔ 不回落成"`key` 直接用原始串"。
|
|
21262
|
+
*/
|
|
21263
|
+
function parseKeyStroke(desc) {
|
|
21264
|
+
const raw = desc.trim();
|
|
21265
|
+
if (!raw) return null;
|
|
21266
|
+
const parts = raw.split("+").map((s) => s.trim()).filter((s) => s !== "");
|
|
21267
|
+
if (parts.length === 0) return null;
|
|
21268
|
+
const mainRaw = parts[parts.length - 1];
|
|
21269
|
+
let modifiers = 0;
|
|
21270
|
+
for (const mod of parts.slice(0, -1)) {
|
|
21271
|
+
const name = Object.keys(MODIFIERS).find((m) => m.toLowerCase() === mod.toLowerCase());
|
|
21272
|
+
if (!name) return null;
|
|
21273
|
+
modifiers |= MODIFIERS[name];
|
|
21274
|
+
}
|
|
21275
|
+
const named = NAMED[mainRaw.toLowerCase()];
|
|
21276
|
+
if (named) return {
|
|
21277
|
+
key: named.cdkey ?? mainRaw,
|
|
21278
|
+
code: named.code,
|
|
21279
|
+
modifiers,
|
|
21280
|
+
...named.vk !== void 0 ? { windowsVirtualKeyCode: named.vk } : {}
|
|
21281
|
+
};
|
|
21282
|
+
if ([...mainRaw].length === 1) {
|
|
21283
|
+
const ch = mainRaw;
|
|
21284
|
+
if (/^[a-zA-Z]$/.test(ch)) return {
|
|
21285
|
+
key: ch,
|
|
21286
|
+
code: `Key${ch.toUpperCase()}`,
|
|
21287
|
+
modifiers
|
|
21288
|
+
};
|
|
21289
|
+
if (/^[0-9]$/.test(ch)) return {
|
|
21290
|
+
key: ch,
|
|
21291
|
+
code: `Digit${ch}`,
|
|
21292
|
+
modifiers
|
|
21293
|
+
};
|
|
21294
|
+
return null;
|
|
21295
|
+
}
|
|
21296
|
+
return null;
|
|
21297
|
+
}
|
|
21298
|
+
//#endregion
|
|
21299
|
+
//#region src/browser/ax.ts
|
|
21300
|
+
/**
|
|
21301
|
+
* 视为「可交互」的角色白名单。
|
|
21302
|
+
*
|
|
21303
|
+
* 🔴 为什么是**白名单**而不是黑名单:AX 角色有 80+ 个,新的会随 Chrome 版本加。
|
|
21304
|
+
* 黑名单会**静默漏掉新角色**(那些控件在快照里消失,而读者以为页面上没有它);
|
|
21305
|
+
* 白名单漏掉时表现为「看得少了」,比「以为不存在」安全。
|
|
21306
|
+
* ⇒ 缺角色就往这里加 —— 这是唯一需要维护的清单。
|
|
21307
|
+
*/
|
|
21308
|
+
const INTERACTIVE_ROLES = [
|
|
21309
|
+
"button",
|
|
21310
|
+
"link",
|
|
21311
|
+
"textbox",
|
|
21312
|
+
"searchbox",
|
|
21313
|
+
"combobox",
|
|
21314
|
+
"listbox",
|
|
21315
|
+
"option",
|
|
21316
|
+
"checkbox",
|
|
21317
|
+
"radio",
|
|
21318
|
+
"switch",
|
|
21319
|
+
"slider",
|
|
21320
|
+
"spinbutton",
|
|
21321
|
+
"tab",
|
|
21322
|
+
"menuitem",
|
|
21323
|
+
"menuitemcheckbox",
|
|
21324
|
+
"menuitemradio",
|
|
21325
|
+
"treeitem",
|
|
21326
|
+
"gridcell",
|
|
21327
|
+
"columnheader",
|
|
21328
|
+
"rowheader",
|
|
21329
|
+
"scrollbar",
|
|
21330
|
+
"dialog",
|
|
21331
|
+
"alertdialog",
|
|
21332
|
+
"menu",
|
|
21333
|
+
"toolbar",
|
|
21334
|
+
"disclosuretriangle"
|
|
21335
|
+
];
|
|
21336
|
+
/** 会出现在 flags 里的属性(只报真值;false 不占位置) */
|
|
21337
|
+
const FLAG_PROPS = [
|
|
21338
|
+
"checked",
|
|
21339
|
+
"selected",
|
|
21340
|
+
"expanded",
|
|
21341
|
+
"pressed",
|
|
21342
|
+
"required",
|
|
21343
|
+
"readonly",
|
|
21344
|
+
"invalid"
|
|
21345
|
+
];
|
|
21346
|
+
/** 读一个 AX 值字段为字符串 */
|
|
21347
|
+
function val(v) {
|
|
21348
|
+
if (!v) return "";
|
|
21349
|
+
const x = v.value;
|
|
21350
|
+
return typeof x === "string" ? x : typeof x === "number" || typeof x === "boolean" ? String(x) : "";
|
|
21351
|
+
}
|
|
21352
|
+
/**
|
|
21353
|
+
* 把 CDP 的 AX 节点数组剪成「可操作清单」。
|
|
21354
|
+
*
|
|
21355
|
+
* 判据:
|
|
21356
|
+
* ① ignored === true ⇒ 丢掉(aria-hidden / inert / 视觉隐藏都在这一档)
|
|
21357
|
+
* ② 角色不在白名单 ⇒ 丢掉
|
|
21358
|
+
* ③ **没有名字的可交互节点保留**(图标按钮就是这样:role=button、name 空、有无障碍标签在 description 里)
|
|
21359
|
+
* —— ⛔ 不许因为「名字为空」就丢:那会把最常见的图标按钮整类漏掉
|
|
21360
|
+
* ④ 没有 backendDOMNodeId 的节点 ⇒ 丢掉(没有它就点不到,留着只会让人以为能点)
|
|
21361
|
+
*/
|
|
21362
|
+
function pruneAx(nodes, limit = 200) {
|
|
21363
|
+
const out = [];
|
|
21364
|
+
for (const n of nodes) {
|
|
21365
|
+
if (out.length >= limit) break;
|
|
21366
|
+
if (n.ignored === true) continue;
|
|
21367
|
+
const role = val(n.role).toLowerCase();
|
|
21368
|
+
if (!INTERACTIVE_ROLES.includes(role)) continue;
|
|
21369
|
+
const id = n.backendDOMNodeId;
|
|
21370
|
+
if (typeof id !== "number" || id <= 0) continue;
|
|
21371
|
+
const name = val(n.name);
|
|
21372
|
+
const desc = val(n.description);
|
|
21373
|
+
const flags = [];
|
|
21374
|
+
let disabled = false;
|
|
21375
|
+
for (const p of n.properties ?? []) {
|
|
21376
|
+
const pv = val(p.value);
|
|
21377
|
+
if (p.name === "disabled" && (pv === "true" || pv === "1")) disabled = true;
|
|
21378
|
+
if (FLAG_PROPS.includes(p.name) && pv !== "" && pv !== "false") flags.push(p.name + "=" + pv);
|
|
21379
|
+
}
|
|
21380
|
+
out.push({
|
|
21381
|
+
ref: "e" + String(id),
|
|
21382
|
+
role,
|
|
21383
|
+
name: name || (desc ? "(" + desc + ")" : ""),
|
|
21384
|
+
value: val(n.value),
|
|
21385
|
+
disabled,
|
|
21386
|
+
flags
|
|
21387
|
+
});
|
|
21388
|
+
}
|
|
21389
|
+
return out;
|
|
21390
|
+
}
|
|
21391
|
+
/** 从 ref 里解出 backendNodeId;格式不对 ⇒ null(⛔ 不猜) */
|
|
21392
|
+
function refToBackendId(ref) {
|
|
21393
|
+
const m = /^e([1-9][0-9]{0,15})$/.exec(ref.trim());
|
|
21394
|
+
if (!m) return null;
|
|
21395
|
+
const n = Number(m[1]);
|
|
21396
|
+
return Number.isSafeInteger(n) && n > 0 ? n : null;
|
|
21397
|
+
}
|
|
21398
|
+
/** 渲染成「能照着 ref 用」的几行 */
|
|
21399
|
+
function renderAx(lines, total) {
|
|
21400
|
+
return lines.map((l) => {
|
|
21401
|
+
const v = l.value ? " =" + JSON.stringify(l.value.slice(0, 40)) : "";
|
|
21402
|
+
const f = l.flags.length > 0 ? " [" + l.flags.join(" ") + "]" : "";
|
|
21403
|
+
const d = l.disabled ? " [disabled]" : "";
|
|
21404
|
+
return " " + l.ref.padEnd(9) + " " + l.role.padEnd(14) + " " + (l.name || "(无名)").slice(0, 60) + v + f + d;
|
|
21405
|
+
}).join("\n") + (typeof total === "number" && total > lines.length ? "\n …另有 " + String(total - lines.length) + " 个(用 --limit 调大)" : "");
|
|
21406
|
+
}
|
|
21407
|
+
//#endregion
|
|
21408
|
+
//#region src/browser/wait-spec.ts
|
|
21409
|
+
/**
|
|
21410
|
+
* 解析 `--for` 参数。
|
|
21411
|
+
*
|
|
21412
|
+
* 判据:
|
|
21413
|
+
* ① 没有前缀的裸值 ⇒ **拒绝**(既有习惯是「给个选择器」,但裸值有歧义:
|
|
21414
|
+
* 它可能是选择器、可能是文本、也可能是 URL 片段 —— 猜错会等一个**永不到来**的条件,
|
|
21415
|
+
* 而超时只说「等到了头」,不告诉你是猜错了形态)
|
|
21416
|
+
* ② `js:` 形态**照原样**拼(这是唯一接受任意代码的一档,且调用方已经明确要执行代码)
|
|
21417
|
+
* ③ 其余形态的参数一律 `JSON.stringify` 成字面量,⛔ 不拼原文
|
|
21418
|
+
*/
|
|
21419
|
+
function parseWaitSpec(raw) {
|
|
21420
|
+
const s = raw.trim();
|
|
21421
|
+
if (!s) return null;
|
|
21422
|
+
const i = s.indexOf(":");
|
|
21423
|
+
if (i <= 0) return null;
|
|
21424
|
+
const kind = s.slice(0, i).toLowerCase();
|
|
21425
|
+
const arg = s.slice(i + 1);
|
|
21426
|
+
if (!arg) return null;
|
|
21427
|
+
if (kind === "js") return {
|
|
21428
|
+
kind: "js",
|
|
21429
|
+
arg,
|
|
21430
|
+
expression: "(" + arg + ")"
|
|
21431
|
+
};
|
|
21432
|
+
const lit = JSON.stringify(arg);
|
|
21433
|
+
if (kind === "selector") return {
|
|
21434
|
+
kind: "selector",
|
|
21435
|
+
arg,
|
|
21436
|
+
expression: "document.querySelector(" + lit + ") !== null"
|
|
21437
|
+
};
|
|
21438
|
+
if (kind === "gone") return {
|
|
21439
|
+
kind: "gone",
|
|
21440
|
+
arg,
|
|
21441
|
+
expression: "document.querySelector(" + lit + ") === null"
|
|
21442
|
+
};
|
|
21443
|
+
if (kind === "text") return {
|
|
21444
|
+
kind: "text",
|
|
21445
|
+
arg,
|
|
21446
|
+
expression: "((document.body && document.body.innerText) || \"\").includes(" + lit + ")"
|
|
21447
|
+
};
|
|
21448
|
+
if (kind === "url") return {
|
|
21449
|
+
kind: "url",
|
|
21450
|
+
arg,
|
|
21451
|
+
expression: "location.href.includes(" + lit + ")"
|
|
21452
|
+
};
|
|
21453
|
+
return null;
|
|
21454
|
+
}
|
|
21455
|
+
/** 把超时结果翻成人话(含「等的是什么」与「最后一次读到什么」) */
|
|
21456
|
+
function explainWaitTimeout(spec, waitedMs, last) {
|
|
21457
|
+
const what = spec.kind === "selector" ? "元素出现:" + spec.arg : spec.kind === "gone" ? "元素消失:" + spec.arg : spec.kind === "text" ? "正文包含「" + spec.arg + "」" : spec.kind === "url" ? "URL 包含「" + spec.arg + "」" : "表达式为真:" + spec.arg;
|
|
21458
|
+
const lastText = last === null || last === void 0 ? "(什么都没读到)" : typeof last === "string" ? last : JSON.stringify(last).slice(0, 200);
|
|
21459
|
+
return "⛔ 等待超时(" + String(Math.round(waitedMs / 100) / 10) + "s)—— 等的是:" + what + "\n 最后一次读数:" + lastText + "\n ⇒ 若最后一次读数是 null/false,多半是条件写错了(形态:" + spec.kind + ");若是别的值,说明条件在往别的方向走 —— 用 eval 看一眼实际状态再重试。";
|
|
21460
|
+
}
|
|
21461
|
+
//#endregion
|
|
20697
21462
|
//#region src/browser/service.ts
|
|
20698
21463
|
/**
|
|
20699
21464
|
* browser/service.ts — 槽位管理器的动作实现(ACC 级)
|
|
@@ -20737,6 +21502,16 @@ const SLOT_ACTIONS = [
|
|
|
20737
21502
|
"reload",
|
|
20738
21503
|
"close",
|
|
20739
21504
|
"click",
|
|
21505
|
+
"ax",
|
|
21506
|
+
"press",
|
|
21507
|
+
"type",
|
|
21508
|
+
"fill",
|
|
21509
|
+
"scroll",
|
|
21510
|
+
"wait",
|
|
21511
|
+
"drag",
|
|
21512
|
+
"upload",
|
|
21513
|
+
"downloads",
|
|
21514
|
+
"dialog",
|
|
20740
21515
|
"ext-list",
|
|
20741
21516
|
"ext"
|
|
20742
21517
|
];
|
|
@@ -21311,6 +22086,164 @@ async function slotClose(root, slot, sessionId, targetId, all = false) {
|
|
|
21311
22086
|
pageTouched(root, slot);
|
|
21312
22087
|
return closed;
|
|
21313
22088
|
}
|
|
22089
|
+
/**
|
|
22090
|
+
* 把 `--ref` 解成 backendNodeId。**格式不对或已失效 ⇒ 明确拒绝**,⛔ 不回落。
|
|
22091
|
+
*
|
|
22092
|
+
* 🔴 判据(与 D311 的 `--match` 同族,但后果更重):
|
|
22093
|
+
* ref 失效(页面导航后、节点被删后)时,如果**悄悄重取一次快照再按顺序找**,
|
|
22094
|
+
* 很可能命中「另一个元素」—— 于是点击落在一个你看不见的地方,而回执完全正常。
|
|
22095
|
+
* ⇒ 唯一允许的是**报错 + 指路**:重跑 `ax` 拿新 ref。
|
|
22096
|
+
*/
|
|
22097
|
+
function needBackendId(args) {
|
|
22098
|
+
const ref = typeof args.ref === "string" ? args.ref : "";
|
|
22099
|
+
if (!ref) throw new Error("⛔ 需要 --ref=<eNNN>(先跑 `browser_slot ax` 拿 ref)。\n ⇒ ref 来自无障碍树快照;⛔ 不是 DOM 选择器,也不是 CSS 路径。");
|
|
22100
|
+
const id = refToBackendId(ref);
|
|
22101
|
+
if (id === null) throw new Error(`⛔ --ref 格式不对:${JSON.stringify(ref)}(应形如 e12345,来自 \`ax\` 的输出)。\n ⛔ 不接受 CSS 选择器/文本 —— 那需要猜,而猜错会操作到另一个元素且没有任何信号。`);
|
|
22102
|
+
return id;
|
|
22103
|
+
}
|
|
22104
|
+
/** AX 快照 */
|
|
22105
|
+
async function slotAx(root, slot, sessionId, targetId, limit = 200) {
|
|
22106
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22107
|
+
const nodes = await captureAx(slot, targetId);
|
|
22108
|
+
const lines = pruneAx(nodes, limit);
|
|
22109
|
+
pageTouched(root, slot);
|
|
22110
|
+
return {
|
|
22111
|
+
lines,
|
|
22112
|
+
total: pruneAx(nodes, Number.MAX_SAFE_INTEGER).length
|
|
22113
|
+
};
|
|
22114
|
+
}
|
|
22115
|
+
/** 按键(语义键 + 快捷键);返回实际发出的次数 */
|
|
22116
|
+
async function slotPress(root, slot, sessionId, targetId, keyDesc, times = 1) {
|
|
22117
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22118
|
+
const stroke = parseKeyStroke(keyDesc);
|
|
22119
|
+
if (!stroke) throw new Error(`⛔ 解析不出按键:${JSON.stringify(keyDesc)}。\n ⇒ 可用形态:Enter / Escape / Tab / ArrowUp/Down/Left/Right / Home / End / PageUp / PageDown / Space / Backspace / Delete;修饰用 + 连接(Control+A / Meta+Shift+Enter);单字符只认 a-z 0-9。\n ⇒ 要「把一段文字打进输入框」用 type(走 Input.insertText,不猜键位):--text=<原文>`);
|
|
22120
|
+
const sent = await pressKey(slot, targetId, stroke, times);
|
|
22121
|
+
pageTouched(root, slot);
|
|
22122
|
+
return {
|
|
22123
|
+
stroke,
|
|
22124
|
+
sent
|
|
22125
|
+
};
|
|
22126
|
+
}
|
|
22127
|
+
/** 打字(走浏览器文本输入路径;作用于当前焦点) */
|
|
22128
|
+
async function slotType(root, slot, sessionId, targetId, text) {
|
|
22129
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22130
|
+
await insertText(slot, targetId, text);
|
|
22131
|
+
pageTouched(root, slot);
|
|
22132
|
+
}
|
|
22133
|
+
/**
|
|
22134
|
+
* 填值(按 ref)。
|
|
22135
|
+
*
|
|
22136
|
+
* 🔴 回执**只报读回来的值**,⛔ 不报「设置成功」—— 见 `cdp.ts` FILL_FN 的判据。
|
|
22137
|
+
* 调用方要判成功,必须自己比 `readBack === 期望值`(或再 `eval` 读一次)。
|
|
22138
|
+
*/
|
|
22139
|
+
async function slotFill(root, slot, sessionId, targetId, backendNodeId, value) {
|
|
22140
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22141
|
+
const r = await fillByBackendId(slot, targetId, backendNodeId, value);
|
|
22142
|
+
pageTouched(root, slot);
|
|
22143
|
+
return r;
|
|
22144
|
+
}
|
|
22145
|
+
/**
|
|
22146
|
+
* 滚动:`--ref`(滚到元素可见)/ `--xy`(在该点滚)/ `--to top|bottom`(整页)。
|
|
22147
|
+
*
|
|
22148
|
+
* 🔴 判据:滚到元素时**先量盒模型再滚**,⛔ 不用 `scrollIntoView` ——
|
|
22149
|
+
* 后者会改页面的滚动容器与动画状态,且**在虚拟列表里不触发懒加载**(它只移动视口);
|
|
22150
|
+
* 而这里走的是**真实 wheel 事件**,浏览器自己会做该做的事。
|
|
22151
|
+
*/
|
|
22152
|
+
async function slotScroll(root, slot, sessionId, targetId, opts) {
|
|
22153
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22154
|
+
let at = opts.xy ?? {
|
|
22155
|
+
x: 600,
|
|
22156
|
+
y: 400
|
|
22157
|
+
};
|
|
22158
|
+
if (opts.ref !== void 0) {
|
|
22159
|
+
const b = await boxCenterByBackendId(slot, targetId, opts.ref);
|
|
22160
|
+
at = {
|
|
22161
|
+
x: b.x,
|
|
22162
|
+
y: b.y
|
|
22163
|
+
};
|
|
22164
|
+
}
|
|
22165
|
+
const before = Number(await evalInPage(slot, targetId, "String(window.scrollY || 0)"));
|
|
22166
|
+
if (opts.to === "top") await scrollAt(slot, targetId, at.x, at.y, 0, -1e5);
|
|
22167
|
+
else if (opts.to === "bottom") await scrollAt(slot, targetId, at.x, at.y, 0, 1e5);
|
|
22168
|
+
else await scrollAt(slot, targetId, at.x, at.y, 0, opts.deltaY ?? 600);
|
|
22169
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
22170
|
+
const after = Number(await evalInPage(slot, targetId, "String(window.scrollY || 0)"));
|
|
22171
|
+
pageTouched(root, slot);
|
|
22172
|
+
return { scrolledY: Number.isFinite(after - before) ? after - before : 0 };
|
|
22173
|
+
}
|
|
22174
|
+
/** 等待条件成立(条件由 `wait-spec.ts` 解析) */
|
|
22175
|
+
async function slotWait(root, slot, sessionId, targetId, raw, timeoutMs) {
|
|
22176
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22177
|
+
const spec = parseWaitSpec(raw);
|
|
22178
|
+
if (!spec) throw new Error(`⛔ 解析不出等待条件:${JSON.stringify(raw)}。\n ⇒ 可用形态(前缀必填,裸值有歧义会等到永不到来的条件):\n selector:<CSS> text:<子串> url:<子串> gone:<CSS> js:<表达式>`);
|
|
22179
|
+
const r = await waitForInPage(slot, targetId, spec.expression, timeoutMs);
|
|
22180
|
+
pageTouched(root, slot);
|
|
22181
|
+
if (!r.ok) throw new Error(explainWaitTimeout(spec, r.waitedMs, r.last));
|
|
22182
|
+
return {
|
|
22183
|
+
spec,
|
|
22184
|
+
waitedMs: r.waitedMs
|
|
22185
|
+
};
|
|
22186
|
+
}
|
|
22187
|
+
/** 拖拽(坐标;判定在调用方,执行在 cdp.ts) */
|
|
22188
|
+
async function slotDrag(root, slot, sessionId, targetId, from, to, steps = 12) {
|
|
22189
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22190
|
+
await dragFromTo(slot, targetId, from, to, steps);
|
|
22191
|
+
pageTouched(root, slot);
|
|
22192
|
+
}
|
|
22193
|
+
/**
|
|
22194
|
+
* 上传(路径必须在 CCC 根内 —— 这是本轮唯一的「把本地文件送进网页」动作)。
|
|
22195
|
+
*
|
|
22196
|
+
* 🔴 守卫三条(判据在 `cdp.ts` uploadFiles 头注释;这里做**路径侧**那两条):
|
|
22197
|
+
* ① 每个路径都过 `resolveInside`(CCC 根外一律拒)
|
|
22198
|
+
* ② 必须是**真实存在的普通文件**(`DOM.setFileInputFiles` 对不存在的路径**静默成功**)
|
|
22199
|
+
*/
|
|
22200
|
+
function resolveUploadPath(cccRoot, relOrAbs) {
|
|
22201
|
+
const abs = resolveInside(cccRoot, relOrAbs);
|
|
22202
|
+
let st;
|
|
22203
|
+
try {
|
|
22204
|
+
st = statSync(abs);
|
|
22205
|
+
} catch {
|
|
22206
|
+
throw new Error(`⛔ 文件不存在:${relOrAbs}(解析为 ${abs})。\n ⚠️ 这条拒绝是必要的:DOM.setFileInputFiles 对不存在的路径**静默成功**,表单里会是空的,而页面上看起来与「上传中」一样。`);
|
|
22207
|
+
}
|
|
22208
|
+
if (!st.isFile()) throw new Error(`⛔ 不是普通文件(目录/设备/符号链接):${relOrAbs}`);
|
|
22209
|
+
return abs;
|
|
22210
|
+
}
|
|
22211
|
+
async function slotUpload(root, slot, sessionId, targetId, backendNodeId, absPaths) {
|
|
22212
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22213
|
+
const r = await uploadFiles(slot, targetId, backendNodeId, absPaths);
|
|
22214
|
+
pageTouched(root, slot);
|
|
22215
|
+
return r;
|
|
22216
|
+
}
|
|
22217
|
+
/**
|
|
22218
|
+
* 下载:把捕获目录指向 `<CCC 根>/_tmp/browser-downloads/b<slot>/` 并列出其中文件。
|
|
22219
|
+
*
|
|
22220
|
+
* 🔴 为什么目录**由本函数决定**而不是调用方传:
|
|
22221
|
+
* 下载落点是「这个文件哪来的」的一部分。让人随便传 ⇒ 下一次读数时
|
|
22222
|
+
* 没人能重建「这个文件是这次下载的,还是上次留下的」。⇒ 固定目录 + 前缀不清理。
|
|
22223
|
+
* ⚠️ **不自动清理**:清理掉的是**证据**。要清理由人来做。
|
|
22224
|
+
*/
|
|
22225
|
+
const DOWNLOAD_DIR_REL = "_tmp/browser-downloads";
|
|
22226
|
+
function downloadDirFor(root, slot) {
|
|
22227
|
+
return join(root, DOWNLOAD_DIR_REL, slotLabel(slot));
|
|
22228
|
+
}
|
|
22229
|
+
async function slotDownloads(root, slot, sessionId, targetId) {
|
|
22230
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22231
|
+
const dir = downloadDirFor(root, slot);
|
|
22232
|
+
mkdirSync(dir, { recursive: true });
|
|
22233
|
+
await enableDownloads(slot, targetId, dir);
|
|
22234
|
+
pageTouched(root, slot);
|
|
22235
|
+
return {
|
|
22236
|
+
dir: `${DOWNLOAD_DIR_REL}/${slotLabel(slot)}`,
|
|
22237
|
+
files: listDownloaded(dir)
|
|
22238
|
+
};
|
|
22239
|
+
}
|
|
22240
|
+
/** dialog:默认 dismiss(见 cdp.ts handleDialog 判据) */
|
|
22241
|
+
async function slotDialog(root, slot, sessionId, targetId, accept, promptText) {
|
|
22242
|
+
assertSlotOwned(root, slot, sessionId);
|
|
22243
|
+
const r = await handleDialog(slot, targetId, accept, promptText);
|
|
22244
|
+
pageTouched(root, slot);
|
|
22245
|
+
return r;
|
|
22246
|
+
}
|
|
21314
22247
|
/** 列本 CCC 的扩展 */
|
|
21315
22248
|
function slotExtList(root) {
|
|
21316
22249
|
return listExtensions(root);
|
|
@@ -21568,6 +22501,95 @@ async function run(ctx, root, sessionId, trajectory, action, args) {
|
|
|
21568
22501
|
});
|
|
21569
22502
|
return xy ? `[browser_slot] clicked @${xy.x},${xy.y}(坐标直给;未做命中校验)` : `[browser_slot] clicked <${picked.tag}> "${picked.text.slice(0, 60)}" @${picked.x},${picked.y}`;
|
|
21570
22503
|
}
|
|
22504
|
+
case "ax": {
|
|
22505
|
+
const n = needSlot(args);
|
|
22506
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22507
|
+
const { lines, total } = await slotAx(root, n, sessionId, target, num(args.limit) ?? 200);
|
|
22508
|
+
if (lines.length === 0) return `[browser_slot] ax —— 无障碍树里没有可交互元素(target=${target})。\n ⇒ 可能页面还没渲染完(先 wait 一下),或这个页面真的只有静态文本(用 text 读)。`;
|
|
22509
|
+
return `[browser_slot] ax —— ${lines.length} 个可交互元素(共 ${total} 个,ref 跨快照稳定)\n${renderAx(lines, total)}`;
|
|
22510
|
+
}
|
|
22511
|
+
case "press": {
|
|
22512
|
+
const n = needSlot(args);
|
|
22513
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22514
|
+
const k = str(args.key);
|
|
22515
|
+
if (!k) return "⛔ 需要 --key=<按键>(例如 Enter / Escape / Control+A / Meta+Shift+Enter)";
|
|
22516
|
+
const { stroke, sent } = await slotPress(root, n, sessionId, target, k, num(args.times) ?? 1);
|
|
22517
|
+
return `[browser_slot] press ${stroke.key}(code=${stroke.code},modifiers=${stroke.modifiers})× ${sent}\n ⚠️ 回执只说「发了几个按键」,⛔ 不说「界面响应了」 —— 效果请用 ax / eval 再读一次。`;
|
|
22518
|
+
}
|
|
22519
|
+
case "type": {
|
|
22520
|
+
const n = needSlot(args);
|
|
22521
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22522
|
+
const text = typeof args.text === "string" ? args.text : void 0;
|
|
22523
|
+
if (text === void 0 || text === "") return "⛔ 需要 --text=<要打的原文>(作用于当前焦点;要设值用 fill)";
|
|
22524
|
+
await slotType(root, n, sessionId, target, text);
|
|
22525
|
+
return `[browser_slot] type ${JSON.stringify(text.slice(0, 40))}(已插入 ${[...text].length} 个字符;⛔ 未触发 change —— blur 才触发)`;
|
|
22526
|
+
}
|
|
22527
|
+
case "fill": {
|
|
22528
|
+
const n = needSlot(args);
|
|
22529
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22530
|
+
const id = needBackendId(args);
|
|
22531
|
+
const value = typeof args.value === "string" ? args.value : void 0;
|
|
22532
|
+
if (value === void 0) return "⛔ 需要 --value=<要设成的值>(空串可以:--value=)";
|
|
22533
|
+
const r = await slotFill(root, n, sessionId, target, id, value);
|
|
22534
|
+
if (!r.ok) return `⛔ fill 失败(<${r.tag || "?"}>):${r.why ?? "未知原因"}`;
|
|
22535
|
+
const same = r.readBack === value;
|
|
22536
|
+
return `[browser_slot] fill <${r.tag}> 读回=${JSON.stringify(r.readBack.slice(0, 80))}` + (same ? " ✓ 与给的值一致" : " ⚠️ 与给的值**不一致** —— 该控件可能做了格式化/拒绝");
|
|
22537
|
+
}
|
|
22538
|
+
case "scroll": {
|
|
22539
|
+
const n = needSlot(args);
|
|
22540
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22541
|
+
const refStr = str(args.ref);
|
|
22542
|
+
const xy = parseXY(str(args.xy));
|
|
22543
|
+
const toRaw = str(args.to);
|
|
22544
|
+
const to = toRaw === "top" || toRaw === "bottom" ? toRaw : void 0;
|
|
22545
|
+
if (!refStr && !xy && !to) return "⛔ 需要 --ref=<eNNN> / --xy=<x,y> / --to=<top|bottom> 之一";
|
|
22546
|
+
const { scrolledY } = await slotScroll(root, n, sessionId, target, {
|
|
22547
|
+
ref: refStr ? needBackendId(args) : void 0,
|
|
22548
|
+
xy,
|
|
22549
|
+
to,
|
|
22550
|
+
deltaY: num(args.delta)
|
|
22551
|
+
});
|
|
22552
|
+
return `[browser_slot] scroll → scrollY 变化 ${scrolledY > 0 ? "+" : ""}${scrolledY}px`;
|
|
22553
|
+
}
|
|
22554
|
+
case "wait": {
|
|
22555
|
+
const n = needSlot(args);
|
|
22556
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22557
|
+
const forRaw = str(args.for);
|
|
22558
|
+
if (!forRaw) return "⛔ 需要 --for=<selector:…|text:…|url:…|gone:…|js:…>(前缀必填)";
|
|
22559
|
+
const { spec, waitedMs } = await slotWait(root, n, sessionId, target, forRaw, num(args.timeout) ?? 15e3);
|
|
22560
|
+
return `[browser_slot] wait ${spec.kind} 成立(等了 ${waitedMs}ms)`;
|
|
22561
|
+
}
|
|
22562
|
+
case "drag": {
|
|
22563
|
+
const n = needSlot(args);
|
|
22564
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22565
|
+
const from = parseXY(str(args.from));
|
|
22566
|
+
const to = parseXY(str(args.to));
|
|
22567
|
+
if (!from || !to) return "⛔ 需要 --from=<x,y> 与 --to=<x,y>(先 ax 拿 ref,再用 scroll/eval 量坐标)";
|
|
22568
|
+
await slotDrag(root, n, sessionId, target, from, to, num(args.steps) ?? 12);
|
|
22569
|
+
return `[browser_slot] drag ${from.x},${from.y} → ${to.x},${to.y}(分 ${num(args.steps) ?? 12} 步;⛔ 回执不说拖成功)`;
|
|
22570
|
+
}
|
|
22571
|
+
case "upload": {
|
|
22572
|
+
const n = needSlot(args);
|
|
22573
|
+
const target = await needTarget(root, n, sessionId, args);
|
|
22574
|
+
const id = needBackendId(args);
|
|
22575
|
+
const list = Array.isArray(args.file) ? args.file : typeof args.file === "string" ? [args.file] : [];
|
|
22576
|
+
if (list.length === 0) return "⛔ 需要 --file=<CCC 内路径>(可重复;⛔ 不接受 CCC 根外路径)";
|
|
22577
|
+
const abs = list.filter((x) => typeof x === "string" && x !== "").map((f) => resolveUploadPath(root, f));
|
|
22578
|
+
if (abs.length === 0) return "⛔ --file 里没有可用路径";
|
|
22579
|
+
const r = await slotUpload(root, n, sessionId, target, id, abs);
|
|
22580
|
+
return `[browser_slot] upload ${r.files.length} 个文件 → ref ${str(args.ref) ?? ""}\n ${r.files.map((f) => f.replace(root + "/", "")).join("\n ")}`;
|
|
22581
|
+
}
|
|
22582
|
+
case "downloads": {
|
|
22583
|
+
const n = needSlot(args);
|
|
22584
|
+
const { dir, files } = await slotDownloads(root, n, sessionId, await needTarget(root, n, sessionId, args));
|
|
22585
|
+
return `[browser_slot] 下载捕获目录:${dir}\n${files.length === 0 ? "(目录是空的)" : files.map((f) => ` ${f.name} ${Math.round(f.bytes / 1024)} KB`).join("\n")}\n ⇒ 触发下载用 click(点带 download 的链接会被拒)或 eval;之后**再调一次 downloads** 看新文件。\n ⚠️ 目录不自动清理 —— 清掉的是证据。`;
|
|
22586
|
+
}
|
|
22587
|
+
case "dialog": {
|
|
22588
|
+
const n = needSlot(args);
|
|
22589
|
+
const r = await slotDialog(root, n, sessionId, await needTarget(root, n, sessionId, args), args.accept === true, str(args.prompt));
|
|
22590
|
+
if (!r.hadDialog) return "[browser_slot] dialog —— 此刻没有弹窗(正常状态,不报错)";
|
|
22591
|
+
return `[browser_slot] dialog ${r.type || "(未知类型)"}:${JSON.stringify(r.message.slice(0, 120))}\n ${r.accepted ? "✓ 已接受(确定)" : "✓ 已拒绝(取消)"}`;
|
|
22592
|
+
}
|
|
21571
22593
|
case "close": {
|
|
21572
22594
|
const n = needSlot(args);
|
|
21573
22595
|
const all = args.all === true;
|
|
@@ -21591,7 +22613,7 @@ async function run(ctx, root, sessionId, trajectory, action, args) {
|
|
|
21591
22613
|
function createBrowserSlotTool(ctx) {
|
|
21592
22614
|
return defineTool({
|
|
21593
22615
|
name: "browser_slot",
|
|
21594
|
-
description: "Browser slot manager (ACC-level, max 20 Chrome slots). Each slot = one isolated Chrome process with its own profile dir, fixed resolution, and a CDP port (9301+). Slots carry state: free / leased / stale (idle too long, CPU or memory over threshold, lease expired) / orphaned (process died). Actions: acquire (lease a slot; reuses your existing lease instead of leasing a second one) / release (REQUIRED when done — a background watchman nudges you if you forget) / list (all slots + occupancy) / status --slot <bN> / reap --slot <bN> (force-reclaim an abandoned slot, records a reason) / doctor (self-check, read-only). 🔴 Lease before you drive a browser for evidence, and ALWAYS release when finished — an unreleased slot keeps a Chrome process alive and burns power.",
|
|
22616
|
+
description: "Browser slot manager (ACC-level, max 20 Chrome slots). Each slot = one isolated Chrome process with its own profile dir, fixed resolution, and a CDP port (9301+). Slots carry state: free / leased / stale (idle too long, CPU or memory over threshold, lease expired) / orphaned (process died). Actions: acquire (lease a slot; reuses your existing lease instead of leasing a second one) / release (REQUIRED when done — a background watchman nudges you if you forget) / list (all slots + occupancy) / status --slot <bN> / reap --slot <bN> (force-reclaim an abandoned slot, records a reason) / doctor (self-check, read-only). PAGE: targets / open / goto / eval / text / html / shot / reload / close / click (text or xy, real mouse events). HIGH-LEVEL (v1.130.0): ax (accessibility-tree snapshot -> stable `ref` like e12345; the only honest answer to \"is this control clickable\") / press (semantic keys & chords: Enter, Escape, Control+A; non-ASCII text must use `type`) / type (browser text input at the focused element) / fill --ref --value (native setter + input/change so React/Vue actually see it; receipt reports the READ-BACK value) / scroll (real wheel events, so lazy-loading and virtual lists fire) / wait --for <selector:|text:|url:|gone:|js:> (in-page polling; timeout says WHAT it waited for and what it last read) / drag --from --to (multi-step mouseMoved; one-jump drags are ignored by sortablejs & friends) / upload --ref --file (paths must stay inside the CCC root) / downloads (pins the capture dir to _tmp/browser-downloads/<slot>) / dialog (default DISMISS — OK often means delete/pay/publish). 🔴 Lease before you drive a browser for evidence, and ALWAYS release when finished — an unreleased slot keeps a Chrome process alive and burns power.",
|
|
21595
22617
|
parameters: {
|
|
21596
22618
|
action: {
|
|
21597
22619
|
type: "string",
|
|
@@ -21663,6 +22685,59 @@ function createBrowserSlotTool(ctx) {
|
|
|
21663
22685
|
script: {
|
|
21664
22686
|
type: "string",
|
|
21665
22687
|
description: "reload: a script to run after the reload completes"
|
|
22688
|
+
},
|
|
22689
|
+
ref: {
|
|
22690
|
+
type: "string",
|
|
22691
|
+
description: "fill / scroll / upload: the element ref from `ax` (e.g. e12345 = an AX backendDOMNodeId). ⛔ NOT a CSS selector — refs are identities and re-snapshot when stale"
|
|
22692
|
+
},
|
|
22693
|
+
value: {
|
|
22694
|
+
type: "string",
|
|
22695
|
+
description: "fill: the value to set (empty string allowed). Drives the native value setter + input/change so controlled components (React/Vue) see it. Receipt reports the READ-BACK value, not \"success\""
|
|
22696
|
+
},
|
|
22697
|
+
key: {
|
|
22698
|
+
type: "string",
|
|
22699
|
+
description: "press: key or chord — Enter / Escape / Tab / ArrowUp / Home / Space / Backspace / Delete / Control+A / Meta+Shift+Enter / single a-z 0-9. Non-ASCII text must use `type`"
|
|
22700
|
+
},
|
|
22701
|
+
times: {
|
|
22702
|
+
type: "number",
|
|
22703
|
+
description: "press: how many times to send the key (default 1)"
|
|
22704
|
+
},
|
|
22705
|
+
to: {
|
|
22706
|
+
type: "string",
|
|
22707
|
+
description: "scroll: \"top\" | \"bottom\" (whole-page jump). Otherwise give ref or xy plus optional delta"
|
|
22708
|
+
},
|
|
22709
|
+
delta: {
|
|
22710
|
+
type: "number",
|
|
22711
|
+
description: "scroll: wheel deltaY in px (default 600; negative scrolls up)"
|
|
22712
|
+
},
|
|
22713
|
+
for: {
|
|
22714
|
+
type: "string",
|
|
22715
|
+
description: "wait: REQUIRED PREFIX — selector:<css> | text:<substring> | url:<substring> | gone:<css> | js:<expr>. A bare value is refused because \"which form\" would be a guess"
|
|
22716
|
+
},
|
|
22717
|
+
from: {
|
|
22718
|
+
type: "string",
|
|
22719
|
+
description: "drag: \"x,y\" start point in viewport coords"
|
|
22720
|
+
},
|
|
22721
|
+
steps: {
|
|
22722
|
+
type: "number",
|
|
22723
|
+
description: "drag: number of intermediate mouseMoved steps (default 12; sortablejs needs a real move sequence)"
|
|
22724
|
+
},
|
|
22725
|
+
file: {
|
|
22726
|
+
type: "array",
|
|
22727
|
+
items: { type: "string" },
|
|
22728
|
+
description: "upload: repeatable path INSIDE the CCC root. Paths outside are refused, as are missing files (DOM.setFileInputFiles silently succeeds on them)"
|
|
22729
|
+
},
|
|
22730
|
+
accept: {
|
|
22731
|
+
type: "boolean",
|
|
22732
|
+
description: "dialog: accept (OK) instead of the default dismiss (Cancel). Default is dismiss because \"OK\" often means delete/pay/publish"
|
|
22733
|
+
},
|
|
22734
|
+
prompt: {
|
|
22735
|
+
type: "string",
|
|
22736
|
+
description: "dialog: text to type into a prompt() dialog (only meaningful with accept)"
|
|
22737
|
+
},
|
|
22738
|
+
limit: {
|
|
22739
|
+
type: "number",
|
|
22740
|
+
description: "ax: max interactive rows to return (default 200)"
|
|
21666
22741
|
}
|
|
21667
22742
|
},
|
|
21668
22743
|
output: {
|