Conversation
There was a problem hiding this comment.
Pull request overview
该 PR 旨在通过压缩 compileScriptCodeByResource 生成的包装代码(try/catch + with + async IIFE)的额外换行数,减少 DevTools 中 console.log/debug 与堆栈行号相对用户脚本源码的偏移,提升调试体验。
Changes:
- 抽出
addTryCatch(),并对 try/catch 包装模板进行“去换行 + 压缩空白”处理以减少引入行数 - 将脚本主体拼接逻辑改为数组 join,便于控制换行位置
- 更新单测中对生成代码片段的断言以匹配新的输出格式
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/app/service/content/utils.ts | 引入 addTryCatch 并调整脚本拼接方式,以减少包装层新增行数 |
| src/app/service/content/utils.test.ts | 更新断言以匹配 return(async function(){ 的新输出 |
| if (e.message && e.stack) { | ||
| console.error("ERROR: Execution of script '" + arguments[1] + "' failed! " + e.message); | ||
| console.log(e.stack); |
There was a problem hiding this comment.
catch 分支里直接访问 e.message / e.stack 在 throw null / undefined / 原始类型时会二次抛出 TypeError,导致原始异常被吞掉且日志缺失。建议先判断 e 是否为对象且非 null,再读取 message/stack(或用可选链 e?.message / e?.stack 并配合 typeof)。
| if (e.message && e.stack) { | |
| console.error("ERROR: Execution of script '" + arguments[1] + "' failed! " + e.message); | |
| console.log(e.stack); | |
| if (e && typeof e === "object" && "message" in e && "stack" in e) { | |
| console.error("ERROR: Execution of script '" + arguments[1] + "' failed! " + (e as any).message); | |
| console.log((e as any).stack); |
| const joinedCode = [ | ||
| "with(arguments[0]||this.$){", | ||
| `${preCode}`, | ||
| "return(async function(){", | ||
| `${code}`, | ||
| "}).call(this);}", | ||
| ] | ||
| .filter(Boolean) | ||
| .join("\n"); | ||
| const codeBody = addTryCatch(joinedCode); | ||
| return `${codeBody}${sourceMapTo(`${resource.name}.user.js`)}\n`; |
There was a problem hiding this comment.
这个 PR 的目标是减少包装代码引入的额外行数以改善 console 输出的行号,但当前测试只校验了部分片段字符串。建议补充针对“try/catch 包装被压缩成单行”的断言(例如校验 try 后立刻跟 with、以及 with 结束后紧跟 catch),以防后续格式化/重构又把多余换行加回来。
|
我跑了一下,一些脚本有问题啊,你是不是都没怎么做过测试呀 |
有什么问题? |
|
呀,我好像知道问题了 |
行数影响 console.log console.debug 那个行数显示
跟 userscript 写的行数相差很远
Before
After