Files
claw/docs/superpowers/specs/2026-04-13-async-eval-then-fix.md
2026-04-13 18:32:05 +08:00

48 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 修复 build_eval_js 异步支持 + validatePageContext 诊断日志
## 问题描述
1. `collect_lineloss.js``buildBrowserEntrypointResult` 是 async 函数,返回 Promise
2. 当前同步版 `build_eval_js``JSON.stringify(Promise)` = `"{}"`
3. 之前的 async IIFE 方案导致 `page_context_unavailable`(原因待排查)
## 方案
### 修改1: build_eval_js 使用 .then() 分支
文件:`src/browser/callback_backend.rs` - `build_eval_js` 函数
逻辑:
1. 外层 IIFE 保持同步(兼容 C++ 注入层)
2. 将回调发送逻辑提取为 `_s` 函数
3. 如果返回值是 Promise`.then` 方法),用 `.then(_s)` 异步等待结果
4. 否则直接同步调用 `_s(v)`
```javascript
(function(){try{
var v=(function(){return {script}})();
function _s(v){
var t=(typeof v==='string')?v:JSON.stringify(v);
try{callBackJsToCpp(...);}catch(_){}
var j=JSON.stringify({...});
try{XHR...}catch(_){}
try{sendBeacon...}catch(_){}
}
if(v&&typeof v.then==='function'){v.then(_s).catch(function(){});}
else{_s(v);}
}catch(e){}})()
```
### 修改2: validatePageContext 添加诊断日志
文件:`D:\data\ideaSpace\rust\sgClaw\claw\claw\skills\skill_staging\skills\tq-lineloss-report\scripts\collect_lineloss.js`
`validatePageContext` 每个检查点添加 console.log记录 host、expected_domain、mac 状态。
## 验证
1. `cargo test` 通过
2. 编译后拷贝 exe 到线上
3. 执行 skill确认不再返回 `{}`
4. 如果出现 `page_context_unavailable`,查看浏览器控制台日志