Skip to content

Commit f70f9fd

Browse files
committed
🐛 修复注入代码构建问题
1 parent 7a8c0b3 commit f70f9fd

6 files changed

Lines changed: 12 additions & 13 deletions

File tree

src/app/service/content/content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class ContentRuntime {
7676
let parentNode: EventTarget | undefined;
7777
// 判断是不是content脚本发过来的
7878
let msg: CustomEventMessage;
79-
if (this.contentScript.has(data.uuid) || this.scriptExecutor.execList.has(data.uuid)) {
79+
if (this.contentScript.has(data.uuid) || this.scriptExecutor.execMap.has(data.uuid)) {
8080
msg = this.scriptExecutorMsg;
8181
} else {
8282
msg = this.senderToInject;

src/app/service/content/script_executor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type ExecScriptEntry = {
1414

1515
// 脚本执行器
1616
export class ScriptExecutor {
17-
execList: Map<string, ExecScript> = new Map();
17+
execMap: Map<string, ExecScript> = new Map();
1818

1919
envInfo: GMInfoEnv | undefined;
2020
earlyScriptFlag: string[] = [];
@@ -27,15 +27,15 @@ export class ScriptExecutor {
2727

2828
emitEvent(data: EmitEventRequest) {
2929
// 转发给脚本
30-
const exec = this.execList.get(data.uuid);
30+
const exec = this.execMap.get(data.uuid);
3131
if (exec) {
3232
exec.emitEvent(data.event, data.eventId, data.data);
3333
}
3434
}
3535

3636
valueUpdate(data: ValueUpdateDataEncoded) {
3737
const { uuid, storageName } = data;
38-
for (const val of this.execList.values()) {
38+
for (const val of this.execMap.values()) {
3939
if (val.scriptRes.uuid === uuid || getStorageName(val.scriptRes) === storageName) {
4040
val.valueUpdate(data);
4141
}
@@ -55,7 +55,7 @@ export class ScriptExecutor {
5555
const flag = script.flag;
5656
// 如果是EarlyScriptFlag,处理沙盒环境
5757
if (this.earlyScriptFlag.includes(flag)) {
58-
for (const val of this.execList.values()) {
58+
for (const val of this.execMap.values()) {
5959
if (val.scriptRes.flag === flag) {
6060
// 处理早期脚本的沙盒环境
6161
val.updateEarlyScriptGMInfo(this.envInfo!);
@@ -92,7 +92,7 @@ export class ScriptExecutor {
9292
const { scriptLoadInfo, scriptFunc, envInfo } = scriptEntry;
9393

9494
const exec = new ExecScript(scriptLoadInfo, "content", this.msg, scriptFunc, envInfo);
95-
this.execList.set(scriptLoadInfo.uuid, exec);
95+
this.execMap.set(scriptLoadInfo.uuid, exec);
9696
const metadata = scriptLoadInfo.metadata || {};
9797
const resource = scriptLoadInfo.resource;
9898
// 注入css

src/app/service/content/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function compilePreInjectScript(
106106
const autoDeleteMountCode = autoDeleteMountFunction ? `try{delete window['${script.flag}']}catch(e){}` : "";
107107
return `window['${script.flag}'] = {
108108
scriptInfo: ${JSON.stringify(script)},
109-
func: function(){${autoDeleteMountCode}${compileScriptCode(script, scriptCode)}}
109+
func: function(){${autoDeleteMountCode}${scriptCode}}
110110
}`;
111111
}
112112

src/app/service/service_worker/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export class RuntimeClient extends Client {
257257
return this.do("stopScript", uuid);
258258
}
259259

260-
pageLoad(): Promise<{ flag: string; scripts: ScriptLoadInfo[]; envInfo: GMInfoEnv }> {
260+
pageLoad(): Promise<{ scripts: ScriptLoadInfo[]; envInfo: GMInfoEnv }> {
261261
return this.doThrow("pageLoad");
262262
}
263263

src/app/service/service_worker/runtime.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,6 @@ export class RuntimeService {
972972
return { flag: "", scripts: [] };
973973
}
974974

975-
const scriptFlag = this.getMessageFlag();
976-
977975
// 匹配当前页面的脚本(只包含有效脚本。自定义排除了的不包含)
978976
const matchingResult = this.getPageScriptMatchingResultByUrl(chromeSender.url!, false, false);
979977

@@ -1180,7 +1178,6 @@ export class RuntimeService {
11801178
}
11811179

11821180
return {
1183-
flag: scriptFlag,
11841181
scripts: enableScript,
11851182
envInfo: {
11861183
sandboxMode: "raw",

src/app/service/service_worker/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ScriptLoadInfo, ScriptMatchInfo } from "./types";
55
import {
66
compileInjectScript,
77
compilePreInjectScript,
8+
compileScriptCode,
89
getScriptFlag,
910
isEarlyStartScript,
1011
isInjectIntoContent,
@@ -181,10 +182,11 @@ export function parseScriptLoadInfo(script: ScriptRunResource): ScriptLoadInfo {
181182
export function compileInjectionCode(scriptRes: ScriptRunResource, scriptCode: string) {
182183
const preDocumentStartScript = isEarlyStartScript(scriptRes.metadata);
183184
let scriptInjectCode;
185+
scriptCode = compileScriptCode(scriptRes, scriptCode);
184186
if (preDocumentStartScript) {
185-
scriptInjectCode = compilePreInjectScript(parseScriptLoadInfo(scriptRes), scriptCode, true);
187+
scriptInjectCode = compilePreInjectScript(parseScriptLoadInfo(scriptRes), scriptCode);
186188
} else {
187-
scriptInjectCode = compileInjectScript(scriptRes, scriptCode, true);
189+
scriptInjectCode = compileInjectScript(scriptRes, scriptCode);
188190
}
189191
return scriptInjectCode;
190192
}

0 commit comments

Comments
 (0)