-
Notifications
You must be signed in to change notification settings - Fork 315
[v1.3] 改良代码 - waitBody #1215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v1.3] 改良代码 - waitBody #1215
Conversation
| doc.addEventListener("DOMNodeInserted", handler, false); | ||
| doc.addEventListener("DOMContentLoaded", handler, false); | ||
|
|
||
| doc = null; // 释放引用,便于 GC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有点夸张了,虽然无所谓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个PR是在搞 bind 不 bind 看到顺便改一下.
主要是 waitBody 的执行在页面,又有一堆 document-start 的脚本在搞来搞去
所以这个东西要写得保险一点
那些 addEventListener 很常被 hack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对了 TM 那边好像没有用 DOMNodeInserted 了
不过我不动你原本的做法了。
只是手㾗改一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
该 PR 针对 content 环境的 document-body 运行时机等待逻辑进行改良,将原先 ScriptExecutor 内部的 waitBody 提取为通用工具函数,并调整脚本执行入口以便安全传递回调。
Changes:
- 新增并导出
waitBody到src/app/service/content/utils.ts,用于等待document.body就绪后执行回调 ScriptExecutor改为使用公共waitBody,并整理变量命名(exec→execScript)- 将
ExecScript.exec()改为实例箭头函数属性,便于直接传递为回调且保持this绑定
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/app/service/content/utils.ts | 新增 waitBody 工具函数并在 content 工具集中复用 |
| src/app/service/content/script_executor.ts | 使用 waitBody 处理 document-body 执行时机,移除类内实现 |
| src/app/service/content/exec_script.ts | 将 exec 改为箭头函数属性以便回调传递与 this 绑定稳定 |
| // 参考了tm的实现 | ||
| export const waitBody = (callback: () => void) => { | ||
| // 只读取一次 document,避免重复访问 getter |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
新增的 waitBody 没有对应单测覆盖;当前仓库对 utils.ts 有较完整的 utils.test.ts。建议补充用例覆盖:body 已存在直接执行、body 通过 DOMContentLoaded/DOMNodeInserted 后执行、以及确保只触发一次/正确移除监听器。
| waitBody(execScript.exec); | ||
| } else { | ||
| try { | ||
| exec.exec(); | ||
| execScript.exec(); | ||
| } catch { |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里直接调用/传递 execScript.exec,但未处理其可能返回的 Promise。若脚本是 async 且发生 reject,会产生未处理的 unhandledrejection。建议统一把返回值保存下来:如果是 Promise 就 .catch(或在 waitBody 内部支持 async callback 并处理返回 Promise)。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CodFrm
对了 这个 try catch 没 await 的话其实是捕捉不到 this.scriptFunc.cal(...) 里面的 error
虽然这里这样写没问题
但Copilot 很迷惑 (明明加了 try catch 但又不加 .catch)
其他部份尽量 async 就对应 await 会比较好
| // body 已存在,直接执行回调 | ||
| if (doc.body) { | ||
| try { | ||
| callback(); |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
waitBody 里对 callback 只做了同步 try/catch;但这里 callback 可能返回 Promise(例如执行 async 脚本),Promise reject 时会变成未处理的 unhandledrejection。建议捕获 callback 的返回值并在是 Promise/thenable 时显式 .catch(保持吞错策略一致)。
| callback(); | |
| const result = callback(); | |
| // 如果 callback 返回 Promise/thenable,则显式捕获其 reject,保持与同步 try/catch 一致的吞错策略 | |
| if (result && typeof (result as any).then === "function") { | |
| (result as Promise<unknown>).catch(() => { | |
| // 屏蔽错误,防止脚本报错导致后续脚本无法执行 | |
| }); | |
| } |
No description provided.