docs: add async eval .then() fix design spec
🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
47
docs/superpowers/specs/2026-04-13-async-eval-then-fix.md
Normal file
47
docs/superpowers/specs/2026-04-13-async-eval-then-fix.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# 修复 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`,查看浏览器控制台日志
|
||||
Reference in New Issue
Block a user