-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,47 @@ export type CompileScriptCodeResource = { | |||||||||||||||||
| require: Array<{ url: string; content: string }>; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| // 参考了tm的实现 | ||||||||||||||||||
| export const waitBody = (callback: () => void) => { | ||||||||||||||||||
| // 只读取一次 document,避免重复访问 getter | ||||||||||||||||||
|
Comment on lines
+14
to
+16
|
||||||||||||||||||
| let doc: Document | null = document; | ||||||||||||||||||
|
|
||||||||||||||||||
| // body 已存在,直接执行回调 | ||||||||||||||||||
| if (doc.body) { | ||||||||||||||||||
| try { | ||||||||||||||||||
| callback(); | ||||||||||||||||||
|
||||||||||||||||||
| callback(); | |
| const result = callback(); | |
| // 如果 callback 返回 Promise/thenable,则显式捕获其 reject,保持与同步 try/catch 一致的吞错策略 | |
| if (result && typeof (result as any).then === "function") { | |
| (result as Promise<unknown>).catch(() => { | |
| // 屏蔽错误,防止脚本报错导致后续脚本无法执行 | |
| }); | |
| } |
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.
这里直接调用/传递 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 会比较好