@@ -2,6 +2,7 @@ import type { SCMetadata, ScriptRunResource, TScriptInfo } from "@App/app/repo/s
22import type { ScriptFunc } from "./types" ;
33import type { ScriptLoadInfo } from "../service_worker/types" ;
44import { DefinedFlags } from "../service_worker/runtime.consts" ;
5+ import { sourceMapTo } from "@App/pkg/utils/utils" ;
56
67export type CompileScriptCodeResource = {
78 name : string ;
@@ -41,17 +42,16 @@ export function compileScriptCode(scriptRes: ScriptRunResource, scriptCode?: str
4142}
4243
4344export function compileScriptCodeByResource ( resource : CompileScriptCodeResource ) : string {
44- const sourceURL = `//# sourceURL=${ chrome . runtime . getURL ( `/${ encodeURI ( resource . name ) } .user.js` ) } ` ;
4545 const requireCode = resource . require . map ( ( r ) => r . content ) . join ( "\n;" ) ;
4646 const preCode = requireCode ; // 不需要 async 封装
47- const code = [ resource . code , sourceURL ] . join ( "\n" ) ; // 需要 async 封装, 可top-level await
47+ const code = resource . code ; // 需要 async 封装, 可top-level await
4848 // context 和 name 以unnamed arguments方式导入。避免代码能直接以变量名存取
4949 // this = context: globalThis
5050 // arguments = [named: Object, scriptName: string]
5151 // 使用sandboxContext时,arguments[0]为undefined, this.$则为一次性Proxy变量,用于全域拦截context
5252 // 非沙盒环境时,先读取 arguments[0],因此不会读取页面环境的 this.$
5353 // 在UserScripts API中,由于执行不是在物件导向里呼叫,使用arrow function的话会把this改变。须使用 .call(this) [ 或 .bind(this)() ]
54- return `try {
54+ const codeBody = `try {
5555 with(arguments[0]||this.$){
5656${ preCode }
5757 return (async function(){
@@ -66,6 +66,7 @@ ${code}
6666 console.error(e);
6767 }
6868}` ;
69+ return `${ codeBody } ${ sourceMapTo ( `${ resource . name } .user.js` ) } \n` ;
6970}
7071
7172// 通过脚本代码编译脚本函数
0 commit comments