Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

No description provided.

doc.addEventListener("DOMNodeInserted", handler, false);
doc.addEventListener("DOMContentLoaded", handler, false);

doc = null; // 释放引用,便于 GC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有点夸张了,虽然无所谓

Copy link
Collaborator Author

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对了 TM 那边好像没有用 DOMNodeInserted 了
不过我不动你原本的做法了。
只是手㾗改一下

Copy link
Contributor

Copilot AI left a 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:

  • 新增并导出 waitBodysrc/app/service/content/utils.ts,用于等待 document.body 就绪后执行回调
  • ScriptExecutor 改为使用公共 waitBody,并整理变量命名(execexecScript
  • 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 绑定稳定

Comment on lines +14 to +16
// 参考了tm的实现
export const waitBody = (callback: () => void) => {
// 只读取一次 document,避免重复访问 getter
Copy link

Copilot AI Feb 12, 2026

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 后执行、以及确保只触发一次/正确移除监听器。

Copilot uses AI. Check for mistakes.
Comment on lines +150 to 154
waitBody(execScript.exec);
} else {
try {
exec.exec();
execScript.exec();
} catch {
Copy link

Copilot AI Feb 12, 2026

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)。

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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();
Copy link

Copilot AI Feb 12, 2026

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(保持吞错策略一致)。

Suggested change
callback();
const result = callback();
// 如果 callback 返回 Promise/thenable,则显式捕获其 reject,保持与同步 try/catch 一致的吞错策略
if (result && typeof (result as any).then === "function") {
(result as Promise<unknown>).catch(() => {
// 屏蔽错误,防止脚本报错导致后续脚本无法执行
});
}

Copilot uses AI. Check for mistakes.
@CodFrm CodFrm merged commit d12232b into scriptscat:release/v1.3 Feb 12, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants