Skip to content

Commit ed741e7

Browse files
authored
🎨 sourceURL 调整,方便debug (#987)
1 parent c50fcf7 commit ed741e7

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/app/service/content/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { SCMetadata, ScriptRunResource, TScriptInfo } from "@App/app/repo/s
22
import type { ScriptFunc } from "./types";
33
import type { ScriptLoadInfo } from "../service_worker/types";
44
import { DefinedFlags } from "../service_worker/runtime.consts";
5+
import { sourceMapTo } from "@App/pkg/utils/utils";
56

67
export type CompileScriptCodeResource = {
78
name: string;
@@ -41,17 +42,16 @@ export function compileScriptCode(scriptRes: ScriptRunResource, scriptCode?: str
4142
}
4243

4344
export 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
// 通过脚本代码编译脚本函数

src/app/service/service_worker/runtime.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
getMetadataStr,
2323
getUserConfigStr,
2424
obtainBlackList,
25+
sourceMapTo,
2526
} from "@App/pkg/utils/utils";
2627
import { cacheInstance } from "@App/app/cache";
2728
import { UrlMatch } from "@App/pkg/utils/match";
@@ -832,9 +833,11 @@ export class RuntimeService {
832833
const retScript: chrome.userScripts.RegisteredUserScript[] = [];
833834
const contentJs = await this.getContentJsCode();
834835
if (contentJs) {
836+
const codeBody = `(function (MessageFlag) {\n${contentJs}\n})('${messageFlag}')`;
837+
const code = `${codeBody}${sourceMapTo("scriptcat-content.js")}\n`;
835838
retScript.push({
836839
id: "scriptcat-content",
837-
js: [{ code: `(function (MessageFlag) {\n${contentJs}\n})('${messageFlag}')` }],
840+
js: [{ code }],
838841
matches: ["<all_urls>"],
839842
allFrames: true,
840843
runAt: "document_start",
@@ -1293,7 +1296,8 @@ export class RuntimeService {
12931296
{ excludeMatches, excludeGlobs }: { excludeMatches: string[] | undefined; excludeGlobs: string[] | undefined }
12941297
) {
12951298
// 构建inject.js的脚本注册信息
1296-
const code = `(function (MessageFlag) {\n${injectJs}\n})('${messageFlag}')`;
1299+
const codeBody = `(function (MessageFlag) {\n${injectJs}\n})('${messageFlag}')`;
1300+
const code = `${codeBody}${sourceMapTo("scriptcat-inject.js")}\n`;
12971301
const script: chrome.userScripts.RegisteredUserScript = {
12981302
id: "scriptcat-inject",
12991303
js: [{ code }],

src/pkg/utils/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ export function cleanFileName(name: string): string {
390390
return name.replace(/[\x00-\x1F\\\/:*?"<>|]+/g, "-").trim();
391391
}
392392

393+
export const sourceMapTo = (scriptName: string) => {
394+
const url = chrome.runtime.getURL(`/${encodeURI(scriptName)}`);
395+
return `\n//# sourceURL=${url}`;
396+
};
397+
393398
export const stringMatching = (main: string, sub: string): boolean => {
394399
// If no wildcards, use simple includes check
395400
if (!sub.includes("*") && !sub.includes("?")) {

0 commit comments

Comments
 (0)