@glittering0717/ai-browser 2.3.1 → 2.4.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 +7 -3
- package/build/icon-dock.png +0 -0
- package/build/icon.icns +0 -0
- package/build/icon.png +0 -0
- package/llms-install.md +3 -3
- package/package.json +7 -3
- package/scripts/build_dmg.cjs +51 -1
- package/scripts/check_icon.cjs +161 -0
- package/scripts/ensure_running.cjs +3 -1
- package/scripts/icon_geometry.cjs +139 -0
- package/scripts/install_app.cjs +18 -4
- package/scripts/render_icon.cjs +49 -0
- package/skills/ai-browser/references/tabs.md +3 -0
- package/skills/ai-browser/references/tasks.md +3 -3
- package/src/main/axExtractor.js +1 -1
- package/src/main/canvas_hook_source.js +136 -2
- package/src/main/capability_probe.js +27 -2
- package/src/main/index.js +103 -12
- package/src/main/mcp_server.js +16 -4
- package/src/main/mcp_tools.js +18 -18
- package/src/main/page_manager.js +109 -3
- package/src/main/snapshot_diff.js +15 -0
- package/src/main/ws_server.js +5 -0
- package/src/preload/actions.cjs +6 -0
- package/src/preload/extractor.cjs +11 -1
- package/src/shared/find_ref.cjs +8 -0
- package/src/shared/page_probe.js +215 -18
- package/src/shared/version.js +1 -1
- package/src/shared/window_show.js +82 -0
package/src/main/page_manager.js
CHANGED
|
@@ -425,7 +425,26 @@ class PageManager {
|
|
|
425
425
|
if (!token) return;
|
|
426
426
|
const sourceTabId = this._tabIdForContents(_event.sender);
|
|
427
427
|
const url = typeof p.url === 'string' ? p.url : '';
|
|
428
|
-
|
|
428
|
+
// Bug 1 修复:setWindowOpenHandler 那条路(v2.3.0 已补 group + show)之后,
|
|
429
|
+
// window.open 的真正生效路径是这条 IPC(垫片主 world -> bridge -> IPC),
|
|
430
|
+
// 这边此前漏掉 opts。现在两条路必须行为一致:
|
|
431
|
+
// - group:派生 tab 跟 opening 者走(agent 后台页弹窗仍进 agent 组,而不是人组)
|
|
432
|
+
// - show:opener 是后台页时不显示(登录/预览/下载弹窗不抢视野)
|
|
433
|
+
const parentGroup = sourceTabId !== null ? this.groupOfTab(sourceTabId) : tabGroup.HUMAN_GROUP;
|
|
434
|
+
const show = shouldShowChildTab({ openerIsVisible: sourceTabId !== null && sourceTabId === this.activeTab });
|
|
435
|
+
// 弹窗把组撑满时不能拒绝(站点的脚本在等这个窗口),只能牺牲一页:
|
|
436
|
+
// 最久没用、不在人眼前、没有挂着对话框的那一页。
|
|
437
|
+
if (!this.canOpenInGroup(parentGroup).allowed) {
|
|
438
|
+
const victim = this.pickEvictableInGroup(parentGroup, sourceTabId);
|
|
439
|
+
if (victim !== null) {
|
|
440
|
+
const deadUrl = (this._getView(victim) || {}).webContents
|
|
441
|
+
? this._getView(victim).webContents.getURL() : '';
|
|
442
|
+
this.evictions.push({ tab: victim, url: deadUrl, at: Date.now(), reason: 'group-full' });
|
|
443
|
+
if (this.evictions.length > 20) this.evictions.shift();
|
|
444
|
+
this.closeTab(victim);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const childId = this.newTab(url && url !== 'about:blank' ? url : null, { show, group: parentGroup });
|
|
429
448
|
this._windowOpenTokens.set(token, { tabId: childId, sourceTabId });
|
|
430
449
|
// 与 setWindowOpenHandler 路径共用 _openedTabs:executeAction 的
|
|
431
450
|
// auto-follow(点击后 Agent 视角跟到新 tab)对两种打开方式一致。
|
|
@@ -1831,6 +1850,28 @@ class PageManager {
|
|
|
1831
1850
|
// 动作前先拍一张。为什么拍在 withDeadline 外面:deadline 只应该盖住动作
|
|
1832
1851
|
// 本身,探针有它自己的 1.5s 上限,混进同一个预算会让"动作超时"和
|
|
1833
1852
|
// "探针慢"两种完全不同的故障共用一个错误码。
|
|
1853
|
+
// canvas 页要先量一次"空闲时画布在不在动、动的时候内容一样不一样"。
|
|
1854
|
+
//
|
|
1855
|
+
// 采**三个**样本(仍在 IDLE_WINDOW_MS 附近,不是三倍等待),因为只看指纹差
|
|
1856
|
+
// 分不清两件事:页面在画**同一个**画面(稳定的动画循环),还是在画**不同**的
|
|
1857
|
+
// 画面(时钟指针、协作光标、直播)。前者用完整帧哈希照样能判"变了没有",
|
|
1858
|
+
// 后者只能老实说"不可判"。判据在 page_probe.canvasVerdict 里,输入就是这三个样本。
|
|
1859
|
+
//
|
|
1860
|
+
// 代价只在有 canvas 指纹的页面上发生(普通页面一次都不多等):这一层存在的
|
|
1861
|
+
// 意义是**不把"点了没反应"变成永远为真**——那是 page_probe 的原始动机。
|
|
1862
|
+
let idleSamples = [];
|
|
1863
|
+
if (pageProbe.shouldProbe(action)) {
|
|
1864
|
+
const first = await this._pageProbe(tid, action);
|
|
1865
|
+
if (first && typeof first.c === 'string') {
|
|
1866
|
+
idleSamples = [first];
|
|
1867
|
+
const gap = Math.max(20, Math.round(pageProbe.IDLE_WINDOW_MS / 3));
|
|
1868
|
+
for (let i = 0; i < 2; i++) {
|
|
1869
|
+
await new Promise((r) => setTimeout(r, gap));
|
|
1870
|
+
const one = await this._pageProbe(tid, action);
|
|
1871
|
+
if (one) idleSamples.push(one);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1834
1875
|
const probeBefore = await this._pageProbe(tid, action);
|
|
1835
1876
|
try {
|
|
1836
1877
|
const res = await withDeadline(
|
|
@@ -1845,7 +1886,17 @@ class PageManager {
|
|
|
1845
1886
|
if (probeBefore) {
|
|
1846
1887
|
await new Promise((r) => setTimeout(r, pageProbe.settleMs(action)));
|
|
1847
1888
|
const probeAfter = await this._pageProbe(tid, action);
|
|
1848
|
-
|
|
1889
|
+
// 动作之后再采一次。画面在动作结束后还在自己变 → 这一页会自漂,画布的
|
|
1890
|
+
// 变化不能归因给我的动作。为什么必须补这一刀:动作前的空闲窗口可能正好
|
|
1891
|
+
// 落在页面被后台节流冻住的时刻(Chromium 对不可见标签页把 rAF 降到 ~1fps),
|
|
1892
|
+
// 那时"空闲期没动"不代表"它不会动",而动作本身会唤醒页面。
|
|
1893
|
+
// 只有画布页付这 150ms;其它页面的 probeAfter.c 是 null,直接跳过。
|
|
1894
|
+
let postBaseline = null;
|
|
1895
|
+
if (probeAfter && typeof probeAfter.c === 'string') {
|
|
1896
|
+
await new Promise((r) => setTimeout(r, pageProbe.IDLE_WINDOW_MS));
|
|
1897
|
+
postBaseline = await this._pageProbe(tid, action);
|
|
1898
|
+
}
|
|
1899
|
+
out = pageProbe.applyProbe(out, probeBefore, probeAfter, action, { idleSamples, postBaseline });
|
|
1849
1900
|
}
|
|
1850
1901
|
const dlg = this._dialogSince(tid, startedAt);
|
|
1851
1902
|
if (dlg) out = { ...out, dialog: dlg };
|
|
@@ -3357,9 +3408,37 @@ return {ok:true,name:file.name,size:file.size,via:via,host_sel:hostSel,clicked:c
|
|
|
3357
3408
|
`var editable=(ed.contentEditable==='true')||(ed.tagName==='TEXTAREA')||(ed.tagName==='INPUT');` +
|
|
3358
3409
|
`if(!editable)return {ok:false,err:'not-editable'};` +
|
|
3359
3410
|
`ed.focus();` +
|
|
3360
|
-
|
|
3411
|
+
// focus() 到底有没有生效,必须当场读,不能假设。
|
|
3412
|
+
// 实测(Notion,2026-10-30):空段落的 contenteditable 拿到 focus() 之后
|
|
3413
|
+
// document.activeElement 仍是 BODY —— 它要一次真实点击才进入编辑态。
|
|
3414
|
+
// 于是后续的受信按键全打到页面上,而校验读到"没写进去",报 Input not verified。
|
|
3415
|
+
// 那不是"打字失败",是"没坐上光标"。
|
|
3416
|
+
`var a=document.activeElement;` +
|
|
3417
|
+
`var took=(a===ed)||(ed.contains&&ed.contains(a))||(a&&a.contains&&a.contains(ed));` +
|
|
3418
|
+
`var isCE=(ed.contentEditable==='true');` +
|
|
3419
|
+
`return {ok:true,focus_took:!!took,kind:isCE?'ce':'input'};`
|
|
3361
3420
|
);
|
|
3362
3421
|
if (!prep || !prep.ok) return null;
|
|
3422
|
+
// contenteditable **一律**先来一次真实指针点击,不能只 focus()。
|
|
3423
|
+
//
|
|
3424
|
+
// 实测(Notion,2026-10-30):空段落的 contenteditable 调 focus() 之后
|
|
3425
|
+
// document.activeElement 确实等于它(所以"焦点有没有坐上"这个判据看不出问题),
|
|
3426
|
+
// 但编辑器**没进入编辑态** —— 随后的受信按键全部落空,校验读到空,报
|
|
3427
|
+
// Input not verified。真人点一下之后 activeElement 会交给编辑器自己的容器,
|
|
3428
|
+
// 按键才生效。也就是说:对这一类块编辑器,"焦点"不等于"可输入"。
|
|
3429
|
+
//
|
|
3430
|
+
// ⚠️ 点完**不要**再对叶子 focus()。踩过:点开之后补一个 ed.focus() 把焦点从
|
|
3431
|
+
// 编辑器容器抢回叶子,编辑态当场被打断,症状与没点一模一样(实测两次)。
|
|
3432
|
+
// 点完保持原样,让编辑器自己放光标;type 的 at 定位在后面的选区块里做。
|
|
3433
|
+
//
|
|
3434
|
+
// input/textarea 不加这一步:原生控件 focus 就够,多点一下反而会把光标
|
|
3435
|
+
// 挪到中段(type 的 at:'end' 语义会变)。
|
|
3436
|
+
if (prep.kind === 'ce' || prep.focus_took === false) {
|
|
3437
|
+
try {
|
|
3438
|
+
await this._cdpPointerTarget(view, 'click', aiId, tid);
|
|
3439
|
+
await new Promise((r) => setTimeout(r, 150)); // 等编辑器切进编辑态
|
|
3440
|
+
} catch (e) { /* 点不到就按原路径继续,后面的校验会如实报错 */ }
|
|
3441
|
+
}
|
|
3363
3442
|
// 清空收尾:拆掉残留的空块外壳。selectAll + 受信 Delete 只删文本不删外层块
|
|
3364
3443
|
// 标签,浏览器会保留至少一个空块承载光标 —— 清一个 H2 段落得到的是
|
|
3365
3444
|
// `<h2><br></h2>` 而不是空。残留外壳会让随后的排版一层层嵌进旧外壳(实测出现
|
|
@@ -3524,6 +3603,33 @@ return {ok:true,name:file.name,size:file.size,via:via,host_sel:hostSel,clicked:c
|
|
|
3524
3603
|
if (ok) {
|
|
3525
3604
|
return { success: true, method: 'cdp-input', chars: (after && after.text) ? after.text.length : 0 };
|
|
3526
3605
|
}
|
|
3606
|
+
// 焦点复核:**焦点不等于我们写的那个编辑器**。
|
|
3607
|
+
//
|
|
3608
|
+
// 实测(Notion,2026-10-30):清空一个空段落之后,Notion 把焦点交还给页面
|
|
3609
|
+
// (activeElement 变回 BODY),于是 deepActive 读到的是整页文本;而目标块其实
|
|
3610
|
+
// 已经空了 —— 明明成功却报 Input not verified。这是**假阴性**,比失败更危险:
|
|
3611
|
+
// agent 会重试,对 type 就是写入两份重复内容。
|
|
3612
|
+
//
|
|
3613
|
+
// 所以失败时再按"我们解析出来的那个可编辑元素"读一次。命中就如实给成功并标明
|
|
3614
|
+
// 依据来源。它不会掩盖真错位:文字若落到别的输入框,目标元素里就不会有它。
|
|
3615
|
+
try {
|
|
3616
|
+
const tgt = await run(
|
|
3617
|
+
`var el=${findRefExpr(JSON.stringify(aiId))};if(!el)return {ok:false};` +
|
|
3618
|
+
`var ed=el;if(ed.querySelector){var inner=${findDescExpr('ed', JSON.stringify('[contenteditable=true],input,textarea'))};if(inner)ed=inner;}` +
|
|
3619
|
+
`return {ok:true,text:(ed.innerText||ed.value||'')};`
|
|
3620
|
+
);
|
|
3621
|
+
const tText = norm(tgt && tgt.text);
|
|
3622
|
+
const okTgt = tgt && tgt.ok && (want ? tText.includes(want) : skeletal(tText) === '');
|
|
3623
|
+
if (okTgt) {
|
|
3624
|
+
return {
|
|
3625
|
+
success: true,
|
|
3626
|
+
method: 'cdp-input',
|
|
3627
|
+
verified_by: 'target-element',
|
|
3628
|
+
chars: (tgt && tgt.text) ? tgt.text.length : 0,
|
|
3629
|
+
note: 'verified against the target element because focus moved away after the input (common in block editors after clear).',
|
|
3630
|
+
};
|
|
3631
|
+
}
|
|
3632
|
+
} catch (e) { /* 复核只是加分项,出错不影响下面的如实报错 */ }
|
|
3527
3633
|
// 诊断:忽略全部空白后再比一次。相等说明东西其实打进去了、只是被编辑器
|
|
3528
3634
|
// 重新排了版;不等说明大概率压根没落上去。两者的处置方式完全相反。
|
|
3529
3635
|
const landed = want ? skeletal(after && after.text) === skeletal(text) : false;
|
|
@@ -263,6 +263,20 @@ export function safeByteLength(value) {
|
|
|
263
263
|
* Reading is strictly read-only: `loadGraphData` is a LOADER and is never
|
|
264
264
|
* called, and a throwing `serialize()` or a circular `_nodes` is caught and
|
|
265
265
|
* simply yields no fingerprint.
|
|
266
|
+
*
|
|
267
|
+
* 5. THE CANVAS DRAW SEQUENCE IS FINGERPRINTED TOO (property 4 only covers
|
|
268
|
+
* apps that HANG A MODEL OFF WINDOW).
|
|
269
|
+
* Measured 2026-10-29 on Excalidraw: a whiteboard whose entire document is
|
|
270
|
+
* canvas-drawn and whose window exposes no model at all. Dragging out a
|
|
271
|
+
* rectangle changed zero DOM bytes, so a model-less canvas app fell straight
|
|
272
|
+
* through every signal above and `ui.diff` answered "unchanged" to a write
|
|
273
|
+
* that visibly happened. The canvas hook already records every draw call, so
|
|
274
|
+
* it also maintains a per-frame sequence hash (see canvas_hook_source.js
|
|
275
|
+
* `push`) and exposes it as `window.__aiCanvasFp`. Reading it costs ~110
|
|
276
|
+
* chars here and gives the diff its only view into a model-less canvas.
|
|
277
|
+
* `on:0` / absent means the hook is not installed (it is only installed
|
|
278
|
+
* after first load, and can be switched off) — absent is reported as
|
|
279
|
+
* "no canvas signal", never as "canvas unchanged".
|
|
266
280
|
*/
|
|
267
281
|
export const DEFAULT_SNAPSHOT_JS = `(function(){
|
|
268
282
|
try {
|
|
@@ -364,6 +378,7 @@ export const DEFAULT_SNAPSHOT_JS = `(function(){
|
|
|
364
378
|
out.model = { global: p.k, kind: p.f.kind, count: p.f.count, fp: p.f.fp };
|
|
365
379
|
}
|
|
366
380
|
} catch (_) {}
|
|
381
|
+
try { var cfp = window.__aiCanvasFp; if (typeof cfp === 'function') { var cv = cfp(); if (cv) out.canvas = cv.fp; } } catch (_) {}
|
|
367
382
|
return out;
|
|
368
383
|
} catch (e) {
|
|
369
384
|
return { __error: String(e && e.message || e) };
|
package/src/main/ws_server.js
CHANGED
|
@@ -590,6 +590,11 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null)
|
|
|
590
590
|
// 窗口状态:后台运行时要能问到"现在到底是不是隐藏的",否则
|
|
591
591
|
// "我没看到窗口却以为它在显示"这类问题只能靠猜。
|
|
592
592
|
...(pageManager.windowState ? { window: pageManager.windowState() } : {}),
|
|
593
|
+
// 应用图标:source 表明这次 Dock 图标来自 bundle 的 icns 还是运行时
|
|
594
|
+
// dock.setIcon(源码启动)。content_ratio 是实际设进去那张图的内容
|
|
595
|
+
// 占比 —— macOS 的图标网格约 0.867;两条路径不一致就会"一会大一会
|
|
596
|
+
// 小",所以这里要能直接判定,而不是靠肉眼比。
|
|
597
|
+
...(pageManager.appIcon ? { icon: pageManager.appIcon } : {}),
|
|
593
598
|
// 这个连接的工作标签页(多 agent 时用来核对自己绑到了哪一页)
|
|
594
599
|
...(pageManager.sessionTabOf ? { session_tab: pageManager.sessionTabOf(sessionId) } : {}),
|
|
595
600
|
},
|
package/src/preload/actions.cjs
CHANGED
|
@@ -17,6 +17,12 @@ function findRef(root, elementId) {
|
|
|
17
17
|
var el = null;
|
|
18
18
|
try { el = root.querySelector('[data-ai-id="' + elementId + '"]'); } catch (e) { el = null; }
|
|
19
19
|
if (el) return el;
|
|
20
|
+
// 原生 id 兜底。抽取器在没有 data-ai-id 时用元素自己的 id 当句柄
|
|
21
|
+
// (`existingAiId || nativeId`),所以报出去的 id 有时就是 el.id;而 data-ai-id 只是
|
|
22
|
+
// 页面里的一个普通属性,React 类应用重渲染子树时会被一起抹掉,元素却还在。
|
|
23
|
+
// 实测(Notion,2026-10-30):空段落叶子 id=":r6:"、data-ai-id 刚写就被抹成空,
|
|
24
|
+
// 于是 get_tree 报 :r6:、type 报 "Target not found for ref: :r6:" —— 读得到、打不进。
|
|
25
|
+
try { if (root.getElementById) { var byId = root.getElementById(elementId); if (byId) return byId; } } catch (e) {}
|
|
20
26
|
var all;
|
|
21
27
|
try { all = root.querySelectorAll("*"); } catch (e) { return null; }
|
|
22
28
|
for (var i = 0; i < all.length; i++) {
|
|
@@ -797,8 +797,18 @@ function extractTree() {
|
|
|
797
797
|
// success,却打在了别的东西上(静默错打;实测插入 1 个元素后 4/4 元素 id 全变)。
|
|
798
798
|
// 修法两条:① 元素已有 data-ai-id 就沿用,id 不再随遍历顺序变化;
|
|
799
799
|
// ② 新元素用模块级只增不减的序号,保证生成出来的 id 不重号。
|
|
800
|
+
//
|
|
801
|
+
// ①的优先级必须高于元素自己的 id 属性。曾经写成 `nativeId || existingAiId`,
|
|
802
|
+
// 与上面这句注释正好相反,后果在 Figma 的 Mirror DOM 上实测到(2026-10-30):
|
|
803
|
+
// 那类元素**同时**带 `id="3:1858"` 和 `data-ai-id="ax-4293"`(后者由 AX 路径
|
|
804
|
+
// 的 ensureHandles 写入)。抽取器按元素 id 把它改写成 `3:1858`,下一次 AX
|
|
805
|
+
// 路径又改回 `ax-…` —— 两条路径轮流改写同一个属性,各自报出的 ref 都被对方
|
|
806
|
+
// 作废。现象不是报错,而是 `ui.act` 拿 get_tree 给的 id 去点,得到
|
|
807
|
+
// "Target not found for ref",或者更糟:拿 AX 那次读到的 id 去点另一棵树。
|
|
808
|
+
// data-ai-id 一旦写进 DOM 就是**权威句柄**;元素自己的 id 只是"第一次分配时
|
|
809
|
+
// 的偏好"。冷读(页面没被我们碰过)两者等价,所以这个改动对普通网页行为不变。
|
|
800
810
|
var existingAiId = el.getAttribute("data-ai-id");
|
|
801
|
-
var aiId =
|
|
811
|
+
var aiId = existingAiId || nativeId || "e:" + el.tagName.toLowerCase() + "-" + (AI_ID_SEQ++);
|
|
802
812
|
// ③ 只在值**真的变了**时才写。
|
|
803
813
|
//
|
|
804
814
|
// 实测(tools/_oneoff/mutation_microtest.cjs,真实 Chromium):setAttribute 把属性
|
package/src/shared/find_ref.cjs
CHANGED
|
@@ -20,6 +20,14 @@
|
|
|
20
20
|
function findRefExpr(idJsExpr) {
|
|
21
21
|
return "(function(id){var f=function(r){var e=null;"
|
|
22
22
|
+ "try{e=r.querySelector('[data-ai-id=\"'+id+'\"]')}catch(x){e=null}"
|
|
23
|
+
// 原生 id 兜底:抽取器在没有 data-ai-id 时用元素自己的 id 当句柄(`existingAiId ||
|
|
24
|
+
// nativeId`),所以我们**报出过**的 id 有时就是 el.id。而 data-ai-id 是页面里的一个
|
|
25
|
+
// 普通属性 —— React 类应用重渲染那个子树时会把它一起抹掉,元素本身还在(id 还在)。
|
|
26
|
+
// 实测(Notion,2026-10-30):空段落叶子 id=":r6:"、data-ai-id 读到后立刻变空,
|
|
27
|
+
// 于是 get_tree 报 :r6: 而 type 报 "Target not found for ref: :r6:" —— 读得到、打不进。
|
|
28
|
+
// getElementById 不受特殊字符影响(`:r6:` 用属性选择器也行,但元素 id 里带引号就不行),
|
|
29
|
+
// 且 ShadowRoot 也有这个方法。找不到就是 null,不会误命中。
|
|
30
|
+
+ "if(!e){try{if(r.getElementById)e=r.getElementById(id)}catch(x){}}"
|
|
23
31
|
+ "if(e)return e;"
|
|
24
32
|
+ "var a;try{a=r.querySelectorAll('*')}catch(x){return null}"
|
|
25
33
|
+ "for(var i=0;i<a.length;i++){var s=a[i].shadowRoot;if(!s)continue;var h=f(s);if(h)return h}"
|
package/src/shared/page_probe.js
CHANGED
|
@@ -40,6 +40,16 @@
|
|
|
40
40
|
// 最该抓的场景(点了没反应)全丢掉。实测于 2026-10-22 的 fixture 回归。
|
|
41
41
|
// n = DOM 元素总数(粗粒度结构变化:弹窗、列表插入)
|
|
42
42
|
// u = location.href(SPA 路由跳转)
|
|
43
|
+
// c = canvas 帧指纹 "fp:n:frames",来自页面里的 canvas hook(canvas_hook_source.js
|
|
44
|
+
// 暴露的 window.__aiCanvasFp)。null = 这个页面没装 hook / hook 关着。
|
|
45
|
+
//
|
|
46
|
+
// 为什么必须加 c:上面四个信号全部只看 DOM,而**纯 canvas 应用的写入在 DOM 上
|
|
47
|
+
// 常常是零字节**。实测(2026-10-29,Excalidraw):在白板上拖出一个矩形,DOM 节点
|
|
48
|
+
// 数与文本哈希都不变(画布是 <canvas>,图元不是 DOM 节点),于是 page_changed
|
|
49
|
+
// 只能靠"选中图元后右侧属性面板出现"这种副作用偶然为真;一旦动作不产生面板
|
|
50
|
+
// 变化(移动已存在的图元、在画布空白处涂色、连续调参),就会报 false,并附上一句
|
|
51
|
+
// "payload 很可能没落地" —— 把一次成功的写入说成失败,比不报更糟。
|
|
52
|
+
// 同一个指纹也进了 DEFAULT_SNAPSHOT_JS,所以 ui.diff 对画布写入同样能报 changed。
|
|
43
53
|
export const PAGE_PROBE_JS = `(function(){
|
|
44
54
|
try{
|
|
45
55
|
var b=document.body||document.documentElement;
|
|
@@ -59,7 +69,13 @@ export const PAGE_PROBE_JS = `(function(){
|
|
|
59
69
|
}catch(_e){}
|
|
60
70
|
var n=0;
|
|
61
71
|
try{ n=document.getElementsByTagName('*').length; }catch(_e){}
|
|
62
|
-
|
|
72
|
+
var cf=null,cn=null;
|
|
73
|
+
var cl=null;
|
|
74
|
+
var cg=0;
|
|
75
|
+
try{ var fpFn=window.__aiCanvasFp; if(typeof fpFn==='function'){var v=fpFn(); if(v){cf=String(v.fp);cn=v.n;cl=(v.last==null?null:v.last);cg=(v.g===1?1:0);}} }catch(_e){}
|
|
76
|
+
var vis='';
|
|
77
|
+
try{ vis=String(document.visibilityState||''); }catch(_e){}
|
|
78
|
+
return {t:t.length,h:(h>>>0).toString(36),d:(dv||'').replace(/\\s+/g,'').length,n:n,u:String(location.href),c:cf,k:cn,v:vis,l:cl,g:cg};
|
|
63
79
|
}catch(e){ return {__err:String((e&&e.message)||e)}; }
|
|
64
80
|
})()`;
|
|
65
81
|
|
|
@@ -101,24 +117,178 @@ const HINT_CLICK =
|
|
|
101
117
|
+ 'expected inside an out-of-process iframe, this probe cannot see it — verify with '
|
|
102
118
|
+ 'get_tree/screenshot instead.';
|
|
103
119
|
|
|
120
|
+
// canvas 侧的证据分两种,措辞必须分开 —— 它们的下一步动作完全相反。
|
|
121
|
+
//
|
|
122
|
+
// 可归因(动作前 150ms 空闲窗口里指纹没动过):这就是 canvas 应用上"写入成功"
|
|
123
|
+
// 的**常态**外观,图元不是 DOM 节点,画一笔只会让 <canvas> 重绘一次。
|
|
124
|
+
// 而调用方最容易犯的错恰恰是"为了确认再画一次",于是画布上多了一个形状。
|
|
125
|
+
const HINT_CANVAS =
|
|
126
|
+
'page_changed=true by canvas only: the DOM is byte-identical and the canvas draw sequence '
|
|
127
|
+
+ 'changed — the normal shape of a successful write on a canvas surface (shapes are not DOM '
|
|
128
|
+
+ 'nodes). Do NOT redo the action to "make sure": a second drag/paste duplicates the result. '
|
|
129
|
+
+ 'Read content back via the route in ui.capabilities, not ui.canvas_read — full-frame '
|
|
130
|
+
+ 'renderers (Excalidraw-class) log no fillText, so an empty texts[] does NOT mean empty canvas.';
|
|
131
|
+
|
|
132
|
+
// 不可归因(空闲窗口里指纹自己在动):这一页会自己重绘(时钟、光标、动画、直播)。
|
|
133
|
+
// 此时"画布变了"什么都证明不了。把它算进 page_changed 尤其危险 —— 那会把
|
|
134
|
+
// page_probe 最初要抓的那件事(点了没反应)重新变回**永远为真**。
|
|
135
|
+
const HINT_CANVAS_NOISY =
|
|
136
|
+
'canvas signal unusable on this page: the canvas repainted during a 150ms idle window before '
|
|
137
|
+
+ 'the action, so a canvas change cannot be attributed to your action. page_changed reflects '
|
|
138
|
+
+ 'the DOM only here. If you need certainty, call ui.snapshot twice with NO action in between '
|
|
139
|
+
+ 'and compare snapshot.canvas yourself — if it moves on its own, trust the app\'s own state '
|
|
140
|
+
+ 'route (ui.capabilities) instead of the canvas.';
|
|
141
|
+
|
|
142
|
+
// **判不了**(证据不足):页面可能被后台节流冻住、也可能是拿不到完整帧的增量渲染器。
|
|
143
|
+
// 此时"画布没变"这句话不能说 —— 我们并没有真的看见它没变。这是最容易被误读成
|
|
144
|
+
// "什么都没发生"的一种状态,必须明说"这个信号在当页不可用"。
|
|
145
|
+
const HINT_CANVAS_UNKNOWN =
|
|
146
|
+
'canvas signal unusable on this page: not enough evidence to tell whether the canvas changed '
|
|
147
|
+
+ '(the page was hidden/throttled during the idle check, or it repaints without whole frames). '
|
|
148
|
+
+ 'page_changed reflects the DOM only here. If you need certainty, call ui.snapshot twice with '
|
|
149
|
+
+ 'NO action in between and compare snapshot.canvas yourself; if it moves on its own, trust the '
|
|
150
|
+
+ 'app state route in ui.capabilities instead of the canvas.';
|
|
151
|
+
|
|
152
|
+
// **看不见看得见的东西**:页面用 WebGPU/WebGL 画,而我们的钩子只挂得到 2D 上下文。
|
|
153
|
+
// 实测(Figma,2026-10-30):DOM 里 1 张 canvas 是 webgpu、2D 画布 0 张,而钩子照样
|
|
154
|
+
// 记到 41 帧 —— 那些来自页面自己建的**离屏** 2D 画布(缩略图/排版缓存),用户看不见。
|
|
155
|
+
// 于是一次真实的 100→200 宽度修改,fp 一动不动,回执报 canvas_changed:false。
|
|
156
|
+
// 那是**假阴性**:写入成功了却说"证明没变",比 null 坏得多,所以这里宁可全给 null。
|
|
157
|
+
const HINT_CANVAS_GPU =
|
|
158
|
+
'canvas signal unusable on this page: the visible surface is drawn with WebGPU/WebGL, which the '
|
|
159
|
+
+ 'draw hook cannot see (it only wraps 2D contexts; the frames it counts come from offscreen 2D '
|
|
160
|
+
+ 'canvases). canvas_changed/canvas_idle are null here — that means "cannot be judged", NOT '
|
|
161
|
+
+ '"nothing happened". page_changed reflects the DOM only. Verify the write by reading the app\'s '
|
|
162
|
+
+ 'own state (ui.capabilities): a property panel, a layer list or a mirror DOM, not pixels.';
|
|
163
|
+
|
|
164
|
+
// 动作前用于标定"这一页画布会不会自己动"的空闲窗口。150ms 覆盖 60fps 下 9 帧,
|
|
165
|
+
// 足够看出持续重绘;再长就会让 canvas 页上每次动作都多等一截。只在页面确实有
|
|
166
|
+
// canvas 指纹时才等(见 page_manager.executeAction)。
|
|
167
|
+
export const IDLE_WINDOW_MS = 150;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 这一页的画布处于哪种状态 —— 决定了"画布变了"能不能当成证据。
|
|
171
|
+
*
|
|
172
|
+
* 四种,各有各的判据(**不要**拿一个量去回答两个问题):
|
|
173
|
+
*
|
|
174
|
+
* static 空闲窗口里一次都没画过。用**前缀哈希 fp** 判变化:静态页只画一帧,
|
|
175
|
+
* 下一帧边界永远不来,所以只有 fp 是即时的。
|
|
176
|
+
* repeat 在画,但每次画的**完整帧**一样(稳定的动画循环 / 同内容重绘)。
|
|
177
|
+
* 用**完整帧哈希 last** 判变化:它与采样相位无关,因此这一页终于可判了。
|
|
178
|
+
* drift 在画,且完整帧每帧都不同(时钟指针、协作光标、视频)。
|
|
179
|
+
* 此时"画布变了"无法归因给动作 —— 只能回答"不可判"。
|
|
180
|
+
* unknown 证据不足:空闲窗口一个样本都没有、拿不到完整帧(增量渲染器)、
|
|
181
|
+
* 或页面在后台被节流(那时"没画"不等于"不会画")。
|
|
182
|
+
*
|
|
183
|
+
* 为什么不能用"空闲期采到几个不同的指纹"来区分 repeat 与 drift:稳定重绘的页面
|
|
184
|
+
* 也会因为采样落在帧内不同位置而给出几个不同的**前缀**哈希。实测踩过这个坑,
|
|
185
|
+
* 于是改成"数帧而不是数相位"——完整帧哈希只有内容变了才会变。
|
|
186
|
+
*/
|
|
187
|
+
function canvasVerdict(before, after, opts) {
|
|
188
|
+
const num = (v) => Number(v) || 0;
|
|
189
|
+
const samples = Array.isArray(opts.idleSamples)
|
|
190
|
+
? opts.idleSamples.filter((x) => x && typeof x.c === 'string')
|
|
191
|
+
: [];
|
|
192
|
+
// 没做标定(调用方没给样本):退回"只看指纹差",与加这套判据之前完全一致。
|
|
193
|
+
// 生产路径上不会走到这里 —— page_manager 见到 canvas 指纹就会采三份。
|
|
194
|
+
if (!samples.length) return { mode: 'static', active: false };
|
|
195
|
+
|
|
196
|
+
const idleDrew = num(before.k) - num(samples[0].k);
|
|
197
|
+
// 指纹在空闲窗口里变过 = 这一页**确实画过**(c 只在真有绘制时变)。这与计数器
|
|
198
|
+
// 是两条独立证据:慢速动画(1fps)可能一次计数差都攒不够,但指纹已经变了。
|
|
199
|
+
//
|
|
200
|
+
// 端点必须含 before:窗口是从**第一个**样本到**动作前那一刻**,只比样本之间会漏掉
|
|
201
|
+
// 最后一段(实测只有一个样本时这条恒为 false,自漂页当场被当成静止页)。
|
|
202
|
+
const cbNow = typeof before.c === 'string' ? before.c : null;
|
|
203
|
+
const idleVaried = samples.some((x) => x.c !== samples[0].c)
|
|
204
|
+
|| (cbNow !== null && samples[samples.length - 1].c !== cbNow);
|
|
205
|
+
const post = opts.postBaseline;
|
|
206
|
+
const postBusy = !!(post && typeof post.c === 'string'
|
|
207
|
+
&& (post.c !== after.c || num(post.k) > num(after.k)));
|
|
208
|
+
const active = idleDrew >= 2 || idleVaried || postBusy;
|
|
209
|
+
|
|
210
|
+
if (!active) {
|
|
211
|
+
// 空闲期真的没画。但后台标签页被 Chromium 把 rAF 降到约 1fps 甚至暂停,
|
|
212
|
+
// 那时"没画"不是"不会画"的证据 —— 隐藏页上只能给 unknown。
|
|
213
|
+
const m2 = String(before.v || '') === 'visible' ? 'static' : 'unknown';
|
|
214
|
+
return { mode: m2, active: false };
|
|
215
|
+
}
|
|
216
|
+
// 在画。能不能判,取决于拿不拿得到**完整帧**(与采样相位无关的那个值)。
|
|
217
|
+
const lasts = new Set();
|
|
218
|
+
for (const s2 of samples) if (s2.l !== null && s2.l !== undefined) lasts.add(String(s2.l));
|
|
219
|
+
if (lasts.size > 1) return { mode: 'drift', active: true }; // 每帧内容都不同 → 不可归因
|
|
220
|
+
if (lasts.size === 1 && samples.length >= 2) return { mode: 'repeat', active: true }; // 每帧一样 → 可判
|
|
221
|
+
return { mode: 'unknown', active: true }; // 在画但没有完整帧(增量渲染器)
|
|
222
|
+
}
|
|
223
|
+
|
|
104
224
|
/**
|
|
105
225
|
* 比较前后两次探针。纯函数,无 Electron 依赖,可直接单测。
|
|
106
|
-
*
|
|
107
|
-
*
|
|
226
|
+
*
|
|
227
|
+
* @param {object[]} opts.idleSamples 动作之前连续采的几次(≥2 才能判 repeat)。
|
|
228
|
+
* @param {object} opts.postBaseline 动作之后再隔 IDLE_WINDOW_MS 采的一次。
|
|
229
|
+
* @returns {{changed, delta, domChanged, canvasChanged, canvasIdle, canvasRepeat, mode, hasCanvas}}
|
|
230
|
+
* canvasChanged: true=变了 / false=没变 / null=不可判(drift、unknown)
|
|
231
|
+
* canvasIdle: true=静止 / false=自己在画 / null=不可判
|
|
232
|
+
* canvasRepeat: 这一页在重绘但每帧内容相同(此时 canvasChanged 仍然可信)
|
|
233
|
+
* hasCanvas: 两次采样里有没有指纹(决定回执要不要带这四个字段)
|
|
108
234
|
*/
|
|
109
|
-
export function diffProbe(before, after) {
|
|
235
|
+
export function diffProbe(before, after, opts = {}) {
|
|
110
236
|
const ok = (p) => p && typeof p === 'object' && !p.__err && typeof p.h === 'string';
|
|
111
|
-
|
|
237
|
+
const empty = {
|
|
238
|
+
changed: null, delta: null, domChanged: false,
|
|
239
|
+
canvasChanged: null, canvasIdle: null, canvasRepeat: false, mode: 'unknown', hasCanvas: false,
|
|
240
|
+
};
|
|
241
|
+
if (!ok(before) || !ok(after)) return empty;
|
|
242
|
+
|
|
112
243
|
const delta = {
|
|
113
244
|
text_chars: (Number(after.t) || 0) - (Number(before.t) || 0),
|
|
114
245
|
dom_nodes: (Number(after.n) || 0) - (Number(before.n) || 0),
|
|
115
246
|
focused_chars: (Number(after.d) || 0) - (Number(before.d) || 0),
|
|
116
247
|
};
|
|
117
|
-
const
|
|
248
|
+
const domChanged = before.h !== after.h
|
|
118
249
|
|| (Number(before.n) || 0) !== (Number(after.n) || 0)
|
|
119
250
|
|| (Number(before.d) || 0) !== (Number(after.d) || 0)
|
|
120
251
|
|| before.u !== after.u;
|
|
121
|
-
|
|
252
|
+
|
|
253
|
+
// 只比**帧指纹本身**,绝不比它周围的计数器。
|
|
254
|
+
//
|
|
255
|
+
// 这里踩过一次,代价很大:接入时把探针字段写成 `fp:n:frames` 的拼接串,而 n 与
|
|
256
|
+
// frames 是单调递增的计数器 —— 于是"内容一模一样地重绘了一遍"(时钟、光标、
|
|
257
|
+
// ECharts 动画区间、canvas 视频)也会让字符串变化,page_changed 变成**永远为真**,
|
|
258
|
+
// 恰好把 page_probe 存在的意义(抓"点了没反应")反过来废掉。
|
|
259
|
+
// 教训:**单测证明了 A,产品用的是 A 的包装 B** —— 两边各自都"对",合起来是错的。
|
|
260
|
+
const cb = typeof before.c === 'string' ? before.c : null;
|
|
261
|
+
const ca = typeof after.c === 'string' ? after.c : null;
|
|
262
|
+
const hasCanvas = !!(cb && ca);
|
|
263
|
+
|
|
264
|
+
const verdict = hasCanvas ? canvasVerdict(before, after, opts) : { mode: 'unknown', active: false };
|
|
265
|
+
const mode0 = verdict.mode;
|
|
266
|
+
// GPU 绘制层:钩子记到的是**离屏 2D 画布**,不是用户看的那张(Figma 实测)。
|
|
267
|
+
// 这种页面上 fp 与可见画布无关,任何结论(包括 false)都是编的 —— 一律给 null。
|
|
268
|
+
// 注意方向:这里唯一可接受的错是"多说几个不知道",绝不允许再说 false。
|
|
269
|
+
const gpuBlind = !!(hasCanvas && (before.g === 1 || after.g === 1));
|
|
270
|
+
const mode = gpuBlind ? 'gpu' : mode0;
|
|
271
|
+
let canvasChanged = null;
|
|
272
|
+
if (hasCanvas && !gpuBlind) {
|
|
273
|
+
if (mode === 'static') {
|
|
274
|
+
canvasChanged = cb !== ca;
|
|
275
|
+
} else if (mode === 'repeat') {
|
|
276
|
+
const lb = before.l == null ? null : String(before.l);
|
|
277
|
+
const la = after.l == null ? null : String(after.l);
|
|
278
|
+
canvasChanged = (lb !== null && la !== null) ? lb !== la : null;
|
|
279
|
+
}
|
|
280
|
+
// drift / unknown:变化**不可归因**,给 null 而不是 true —— 一个"可能是我干的"
|
|
281
|
+
// 结论比"我不知道"更容易被误用。
|
|
282
|
+
}
|
|
283
|
+
// canvas_idle: true=静止 / false=自己在画 / null=判不了。'unknown' 既可能是
|
|
284
|
+
// "在画但拿不到完整帧",也可能是"根本没证据"(隐藏页),所以下面按 active 给值 ——
|
|
285
|
+
// 让一个 50% 把握的结论长得像 100% 把握的结论,是这类字段最常见的坏法。
|
|
286
|
+
const canvasIdle = gpuBlind ? null : (mode === 'static' ? true : (verdict.active ? false : null));
|
|
287
|
+
const canvasRepeat = gpuBlind ? false : (mode === 'repeat');
|
|
288
|
+
return {
|
|
289
|
+
changed: domChanged || canvasChanged === true,
|
|
290
|
+
delta, domChanged, canvasChanged, canvasIdle, canvasRepeat, mode, hasCanvas,
|
|
291
|
+
};
|
|
122
292
|
}
|
|
123
293
|
|
|
124
294
|
/**
|
|
@@ -126,24 +296,42 @@ export function diffProbe(before, after) {
|
|
|
126
296
|
*
|
|
127
297
|
* 只在 changed === false 时附加 page_delta 和 effect_hint:
|
|
128
298
|
* 正常路径(changed:true)不多说一个字,避免每步回执都多两行噪音。
|
|
299
|
+
* 例外是"仅 canvas 变化"(成功写入的样子,调用方看不出落点)与"信号不可用"
|
|
300
|
+
* (必须说清不是"没变化",而是"这里判不了")。
|
|
129
301
|
*/
|
|
130
|
-
export function applyProbe(res, before, after, action) {
|
|
302
|
+
export function applyProbe(res, before, after, action, opts = {}) {
|
|
131
303
|
if (!res || typeof res !== 'object') return res;
|
|
132
|
-
const d = diffProbe(before, after);
|
|
304
|
+
const d = diffProbe(before, after, opts);
|
|
133
305
|
if (d.changed === null) return res;
|
|
134
306
|
const out = { ...res, page_changed: d.changed };
|
|
135
|
-
|
|
307
|
+
|
|
308
|
+
// canvas 字段**只要页面确实有指纹**就发,哪怕结论是"不可判"。曾经只在 true 时写,
|
|
309
|
+
// 于是"没变""不可判""没有画布"三种情况在回执里长得一模一样 —— 一个读不出含义
|
|
310
|
+
// 的字段比没有这个字段更糟。没有画布的页面继续不给字段,与加这个信号之前逐字节一致。
|
|
311
|
+
if (d.hasCanvas) {
|
|
312
|
+
out.canvas_changed = d.canvasChanged;
|
|
313
|
+
out.dom_changed = d.domChanged;
|
|
314
|
+
out.canvas_idle = d.canvasIdle;
|
|
315
|
+
out.canvas_repeat = d.canvasRepeat;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (d.changed) {
|
|
319
|
+
if (d.canvasChanged === true && !d.domChanged && !out.effect_hint) {
|
|
320
|
+
out.effect_hint = HINT_CANVAS;
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
|
|
136
325
|
out.page_delta = d.delta;
|
|
137
326
|
const isWrite = MUST_HAVE_EFFECT.has(String(action || ''));
|
|
138
327
|
if (!out.effect_hint) {
|
|
139
|
-
out.effect_hint =
|
|
328
|
+
if (d.mode === 'gpu') out.effect_hint = HINT_CANVAS_GPU;
|
|
329
|
+
else if (d.mode === 'drift') out.effect_hint = HINT_CANVAS_NOISY;
|
|
330
|
+
else if (d.mode === 'unknown' && d.hasCanvas) out.effect_hint = HINT_CANVAS_UNKNOWN;
|
|
331
|
+
else out.effect_hint = isWrite ? HINT_WRITE : HINT_CLICK;
|
|
140
332
|
}
|
|
141
|
-
// 写类动作
|
|
142
|
-
//
|
|
143
|
-
// 等于告诉 agent"这一手已经生效,别重试",而观测说它没生效。
|
|
144
|
-
//
|
|
145
|
-
// 为什么是 unknown 而不是 none:探针只看主文档,OOPIF 子帧里的编辑器
|
|
146
|
-
// (B站 read-editor)变化它看不见。unknown + verify_first 是唯一诚实的选择。
|
|
333
|
+
// 写类动作"什么都没变":工具无法证明它落地了,只能降级成"先验证"。
|
|
334
|
+
// 它看不见的东西(OOPIF、原生弹窗、跨进程编辑)太多,unknown + verify_first 是唯一诚实的选择。
|
|
147
335
|
if (isWrite && out.success === true) {
|
|
148
336
|
out.effect_state = 'unknown';
|
|
149
337
|
out.retry = 'verify_first';
|
|
@@ -151,4 +339,13 @@ export function applyProbe(res, before, after, action) {
|
|
|
151
339
|
return out;
|
|
152
340
|
}
|
|
153
341
|
|
|
154
|
-
|
|
342
|
+
// 默认导出是**另一个对象**,不是命名空间本身。page_manager 用的是
|
|
343
|
+
// `import pageProbe from ...`,于是这里少列一个名字,调用方拿到的就是 undefined
|
|
344
|
+
// 而不会报错 —— IDLE_WINDOW_MS 就漏过一次:`setTimeout(r, undefined)` 等于 0ms,
|
|
345
|
+
// "空闲标定"退化成"立刻再采一次",两次采样的计数完全相同,页面被误判成静止,
|
|
346
|
+
// 于是动画页上的"画布变了"被算成动作生效。症状在很远的地方(回执里 canvas_idle
|
|
347
|
+
// 恒为 true),根因在这里。
|
|
348
|
+
// 由 tests/test_page_probe.js 的 PP-30 逐键核对,不再靠人记得。
|
|
349
|
+
export default {
|
|
350
|
+
PAGE_PROBE_JS, shouldProbe, settleMs, diffProbe, applyProbe, IDLE_WINDOW_MS,
|
|
351
|
+
};
|
package/src/shared/version.js
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
// Kept in JS (not imported from package.json) to avoid Node import-attribute
|
|
3
3
|
// (JSON module) compatibility issues inside Electron. package.json "version"
|
|
4
4
|
// is manually synced to this value; tests assert they match.
|
|
5
|
-
export const VERSION = '2.
|
|
5
|
+
export const VERSION = '2.4.1';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// shared/window_show.js — 窗口"显示那一刻"的自述口径。
|
|
2
|
+
//
|
|
3
|
+
// 为什么单独一个模块:这里连续出过两次同一类问题,都是**记录比事实好看**。
|
|
4
|
+
//
|
|
5
|
+
// 第一次(2026-10-14):`waited_for_dom` 恒为 true —— 3s 兜底显示时也报 true,
|
|
6
|
+
// 于是"等到了 dom-ready"和"我们主动选择先显示"混成同一种结果,白屏缺陷从
|
|
7
|
+
// 记录里看不出来。修法是把"为什么现在显示"记成 show_reason。
|
|
8
|
+
//
|
|
9
|
+
// 第二次(2026-10-29,就是本模块):那次修复只区分了 timeout,**漏掉了占位页**。
|
|
10
|
+
// 2026-10-24 起新标签页先加载 `loading.html`(把白屏变成"正在打开 <url>"),
|
|
11
|
+
// 而这个占位页自己会发 dom-ready —— 于是 `show_reason: 'dom-ready'` +
|
|
12
|
+
// `waited_for_dom: true` 照旧成立,只不过等到的 DOM 是**我们自己的**占位页。
|
|
13
|
+
// 实测:起始页是 baidu,外网不通时窗口照样报"等到了 dom-ready",而此刻页面
|
|
14
|
+
// 是我们的占位页(body 578 字符),1.5s 后仍是 578。
|
|
15
|
+
//
|
|
16
|
+
// 所以口径必须按"谁的 DOM"来分:
|
|
17
|
+
// dom-ready / renderer-ready + 目标文档 → 真等到了,waited_for_dom = true
|
|
18
|
+
// dom-ready / renderer-ready + 占位页 → show_reason = 'placeholder',
|
|
19
|
+
// waited_for_dom = false(我们没等到目标页)
|
|
20
|
+
// timeout → false
|
|
21
|
+
// fail-load → false(目标文档**失败**了,随后由自绘错误页接手)
|
|
22
|
+
//
|
|
23
|
+
// ⚠️ 这个模块**不改显示时机**。占位页一就绪就显示,是 2026-10-24 的决定
|
|
24
|
+
// (人先看到"正在打开 <url>"比看到白屏好)。这里只负责把那一刻记录成事实。
|
|
25
|
+
// 本文件是 ESM(主进程直接 import),占位页判定复用 CJS 模块。
|
|
26
|
+
import placeholder from './placeholder.cjs';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 调用方(showOnce)能传进来的**事件名**。与输出枚举分开:
|
|
30
|
+
* 'placeholder' 是**折算出来的结论**,不是事件 —— 把它混进输入侧会让"输出在枚举内"
|
|
31
|
+
* 这条不变量变成自证(输入什么就输出什么)。
|
|
32
|
+
*/
|
|
33
|
+
export const SHOW_EVENT_REASONS = ['dom-ready', 'renderer-ready', 'fail-load', 'timeout'];
|
|
34
|
+
|
|
35
|
+
/** show_reason 的合法取值(输出侧)。新增一个必须同时改这里和 NV-A3b 的枚举断言。 */
|
|
36
|
+
export const SHOW_REASONS = [
|
|
37
|
+
'dom-ready', 'renderer-ready', 'fail-load', 'timeout', 'placeholder',
|
|
38
|
+
// 归因不明时保留原样(调用方传了奇怪的字符串)—— 留在枚举里是为了让
|
|
39
|
+
// "输出一定是枚举之一"这条不变量成立,而不是让读的人去猜。
|
|
40
|
+
'unknown',
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/** 这两个原因字面上是"文档就绪",但只有 url 不是占位页时才作数。 */
|
|
44
|
+
const READY_REASONS = new Set(['dom-ready', 'renderer-ready']);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 把"触发显示的事件 + 当时 webContents 的 URL"折算成可对外播报的自述。
|
|
48
|
+
*
|
|
49
|
+
* 纯函数,无 Electron 依赖 —— 这一层必须能被单测钉死,因为它回答的是
|
|
50
|
+
* "我们说的话算不算数",而这类错误在 e2e 里只会表现为一个莫名其妙的红。
|
|
51
|
+
*
|
|
52
|
+
* @param {string} reason 触发显示的事件名:dom-ready / renderer-ready / fail-load / timeout
|
|
53
|
+
* @param {string} url 那一刻 webContents.getURL()(可能为空串)
|
|
54
|
+
* @returns {{show_reason: string, waited_for_dom: boolean, on_placeholder: boolean}}
|
|
55
|
+
*/
|
|
56
|
+
export function classifyShowReason(reason, url) {
|
|
57
|
+
const r = String(reason || 'unknown');
|
|
58
|
+
const ready = READY_REASONS.has(r);
|
|
59
|
+
// 空串也算占位页(导航刚发起时 getURL() 还是空,见 placeholder.cjs 的说明)。
|
|
60
|
+
// 网络页面一律不算 —— 一个恰好叫 loading.html 的站点页面不该被误判。
|
|
61
|
+
let onPlaceholder = false;
|
|
62
|
+
try {
|
|
63
|
+
onPlaceholder = placeholder.isPlaceholderUrl(url);
|
|
64
|
+
} catch (_e) {
|
|
65
|
+
onPlaceholder = false;
|
|
66
|
+
}
|
|
67
|
+
if (ready && onPlaceholder) {
|
|
68
|
+
return { show_reason: 'placeholder', waited_for_dom: false, on_placeholder: true };
|
|
69
|
+
}
|
|
70
|
+
return { show_reason: r, waited_for_dom: ready, on_placeholder: onPlaceholder };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 记录里额外要带的那句"我当时看到的是哪一页"。
|
|
75
|
+
*
|
|
76
|
+
* 只留 origin+路径太长(占位页 URL 里带着目标地址,那正是最有用的一半),
|
|
77
|
+
* 所以整串保留但截断 —— 一条记录被撑到几千字符会让日志无法目视。
|
|
78
|
+
*/
|
|
79
|
+
export function describeShowUrl(url, max = 300) {
|
|
80
|
+
const s = String(url || '');
|
|
81
|
+
return s.length > max ? s.slice(0, max) + '…' : s;
|
|
82
|
+
}
|