Skip to content

Commit de49c50

Browse files
committed
🐛 兼容TM的GM_info #478
1 parent a3adc00 commit de49c50

11 files changed

Lines changed: 97 additions & 22 deletions

File tree

src/app/service/content/content.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ScriptRunResouce } from "@App/app/repo/scripts";
22
import { Client, sendMessage } from "@Packages/message/client";
33
import { CustomEventMessage } from "@Packages/message/custom_event_message";
44
import { forwardMessage, Message, MessageSend, Server } from "@Packages/message/server";
5+
import { GMInfoEnv } from "./gm_api";
56

67
// content页的处理
78
export default class ContentRuntime {
@@ -12,7 +13,7 @@ export default class ContentRuntime {
1213
private msg: Message
1314
) {}
1415

15-
start(scripts: ScriptRunResouce[]) {
16+
start(scripts: ScriptRunResouce[], envInfo: GMInfoEnv) {
1617
this.extServer.on("runtime/emitEvent", (data) => {
1718
// 转发给inject
1819
return sendMessage(this.msg, "inject/runtime/emitEvent", data);
@@ -101,6 +102,6 @@ export default class ContentRuntime {
101102
}
102103
);
103104
const client = new Client(this.msg, "inject");
104-
client.do("pageLoad", { scripts });
105+
client.do("pageLoad", { scripts, envInfo });
105106
}
106107
}

src/app/service/content/exec_script.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { compileScript, compileScriptCode } from "./utils";
44
import { ExtVersion } from "@App/app/const";
55
import { initTestEnv } from "@Tests/utils";
66
import { describe, expect, it } from "vitest";
7+
import { GMInfoEnv } from "./gm_api";
78

89
initTestEnv();
910

@@ -20,9 +21,18 @@ const scriptRes = {
2021
none: true,
2122
},
2223
} as unknown as ScriptRunResouce;
24+
const envInfo: GMInfoEnv = {
25+
sandboxMode: "raw",
26+
userAgentData: {
27+
brands: [],
28+
mobile: false,
29+
platform: "",
30+
},
31+
isIncognito: false,
32+
};
2333

2434
// @ts-ignore
25-
const noneExec = new ExecScript(scriptRes);
35+
const noneExec = new ExecScript(scriptRes, undefined, undefined, undefined, envInfo, undefined);
2636

2737
const scriptRes2 = {
2838
id: 0,
@@ -37,7 +47,7 @@ const scriptRes2 = {
3747
} as unknown as ScriptRunResouce;
3848

3949
// @ts-ignore
40-
const sandboxExec = new ExecScript(scriptRes2);
50+
const sandboxExec = new ExecScript(scriptRes2, undefined, undefined, undefined, envInfo, undefined);
4151

4252
describe("GM_info", () => {
4353
it("none", async () => {

src/app/service/content/exec_script.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import LoggerCore from "@App/app/logger/core";
22
import Logger from "@App/app/logger/logger";
3-
import GMApi from "./gm_api";
3+
import GMApi, { GMInfoEnv } from "./gm_api";
44
import { compileScript, createContext, proxyContext, ScriptFunc } from "./utils";
55
import { Message } from "@Packages/message/server";
66
import { ScriptLoadInfo } from "../service_worker/runtime";
@@ -40,6 +40,7 @@ export default class ExecScript {
4040
envPrefix: "content" | "offscreen",
4141
message: Message,
4242
code: string | ScriptFunc,
43+
envInfo: GMInfoEnv,
4344
thisContext?: { [key: string]: any }
4445
) {
4546
this.scriptRes = scriptRes;
@@ -48,7 +49,7 @@ export default class ExecScript {
4849
uuid: this.scriptRes.uuid,
4950
name: this.scriptRes.name,
5051
});
51-
this.GM_info = GMApi.GM_info(this.scriptRes);
52+
this.GM_info = GMApi.GM_info(envInfo, this.scriptRes);
5253
// 构建脚本资源
5354
if (typeof code === "string") {
5455
this.scriptFunc = compileScript(code);

src/app/service/content/exec_warp.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ScriptRunResouce } from "@App/app/repo/scripts";
21
import ExecScript from "./exec_script";
32
import { Message } from "@Packages/message/server";
43
import { ScriptLoadInfo } from "../service_worker/runtime";
4+
import { GMInfoEnv } from "./gm_api";
55

66
export class CATRetryError {
77
msg: string;
@@ -64,7 +64,16 @@ export class BgExecScriptWarp extends ExecScript {
6464
};
6565
// @ts-ignore
6666
thisContext.CATRetryError = CATRetryError;
67-
super(scriptRes, "offscreen", message, scriptRes.code, thisContext);
67+
let envInfo: GMInfoEnv = {
68+
sandboxMode: "raw",
69+
userAgentData: {
70+
brands: [],
71+
mobile: false,
72+
platform: "",
73+
},
74+
isIncognito: false,
75+
};
76+
super(scriptRes, "offscreen", message, scriptRes.code, envInfo, thisContext);
6877
this.setTimeout = setTimeout;
6978
this.setInterval = setInterval;
7079
}

src/app/service/content/gm_api.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export interface ApiValue {
2121
param: ApiParam;
2222
}
2323

24+
export interface GMInfoEnv {
25+
userAgentData: typeof GM_info.userAgentData;
26+
sandboxMode: typeof GM_info.sandboxMode;
27+
isIncognito: typeof GM_info.isIncognito;
28+
}
29+
2430
export class GMContext {
2531
static apis: Map<string, ApiValue> = new Map();
2632

@@ -119,7 +125,7 @@ export default class GMApi {
119125
}
120126

121127
// 获取脚本信息和管理器信息
122-
static GM_info(script: ScriptLoadInfo) {
128+
static GM_info(envInfo: GMInfoEnv, script: ScriptLoadInfo) {
123129
const options = {
124130
description: script.metadata.description?.[0] || null,
125131
matches: script.metadata.match || [],
@@ -133,12 +139,13 @@ export default class GMApi {
133139
connects: script.metadata.connect || [],
134140
};
135141
return {
136-
downloadMode: "browser",
137-
// isIncognito
142+
downloadMode: "native",
143+
isIncognito: envInfo.isIncognito,
138144
// relaxedCsp
139-
// sandboxMode
145+
sandboxMode: envInfo.sandboxMode,
140146
scriptWillUpdate: true,
141147
scriptHandler: "ScriptCat",
148+
userAgentData: envInfo.userAgentData,
142149
// "" => null
143150
scriptUpdateURL: script.downloadUrl || null,
144151
scriptMetaStr: script.metadataStr,

src/app/service/content/inject.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { getStorageName } from "@App/pkg/utils/utils";
55
import { EmitEventRequest, ScriptLoadInfo } from "../service_worker/runtime";
66
import { ExternalWhitelist } from "@App/app/const";
77
import { sendMessage } from "@Packages/message/client";
8+
import { GMInfoEnv } from "./gm_api";
89

910
export class InjectRuntime {
1011
execList: ExecScript[] = [];
1112

1213
constructor(
1314
private server: Server,
1415
private msg: Message,
15-
private scripts: ScriptLoadInfo[]
16+
private scripts: ScriptLoadInfo[],
17+
private envInfo: GMInfoEnv
1618
) {}
1719

1820
start() {
@@ -84,7 +86,7 @@ export class InjectRuntime {
8486
execScript(script: ScriptLoadInfo, scriptFunc: ScriptFunc) {
8587
// @ts-ignore
8688
delete window[script.flag];
87-
const exec = new ExecScript(script, "content", this.msg, scriptFunc);
89+
const exec = new ExecScript(script, "content", this.msg, scriptFunc, this.envInfo);
8890
this.execList.push(exec);
8991
// 注入css
9092
if (script.metadata["require-css"]) {

src/app/service/service_worker/client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Subscribe } from "@App/app/repo/subscribe";
1313
import { Permission } from "@App/app/repo/permission";
1414
import { ResourceBackup } from "@App/pkg/backup/struct";
1515
import { VSCodeConnect } from "../offscreen/vscode-connect";
16+
import { GMInfoEnv } from "../content/gm_api";
1617

1718
export class ServiceWorkerClient extends Client {
1819
constructor(msg: MessageSend) {
@@ -129,7 +130,7 @@ export class ScriptClient extends Client {
129130
if (urls.length == 0) {
130131
return;
131132
}
132-
const results = await Promise.allSettled(
133+
const results = (await Promise.allSettled(
133134
urls.map(async (url) => {
134135
const formattedResult = await this.formatUrl(url);
135136
if (formattedResult instanceof Object) {
@@ -139,7 +140,7 @@ export class ScriptClient extends Client {
139140
}
140141
})
141142
// this.do 只会resolve 不会reject
142-
) as PromiseFulfilledResult<{ success: boolean; msg: string }>[];
143+
)) as PromiseFulfilledResult<{ success: boolean; msg: string }>[];
143144
const stat = results.reduce(
144145
(obj, result, index) => {
145146
if (result.value.success) {
@@ -201,7 +202,7 @@ export class RuntimeClient extends Client {
201202
return this.do("stopScript", uuid);
202203
}
203204

204-
pageLoad(): Promise<{ flag: string; scripts: ScriptRunResouce[] }> {
205+
pageLoad(): Promise<{ flag: string; scripts: ScriptRunResouce[]; envInfo: GMInfoEnv }> {
205206
return this.do("pageLoad");
206207
}
207208

src/app/service/service_worker/runtime.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ResourceService } from "./resource";
2828
import { LocalStorageDAO } from "@App/app/repo/localStorage";
2929
import Logger from "@App/app/logger/logger";
3030
import { getMetadataStr, getUserConfigStr } from "@App/pkg/utils/script";
31+
import { GMInfoEnv } from "../content/gm_api";
3132

3233
// 为了优化性能,存储到缓存时删除了code、value与resource
3334
export interface ScriptMatchInfo extends ScriptRunResouce {
@@ -58,6 +59,7 @@ export class RuntimeService {
5859

5960
isEnableDeveloperMode = false;
6061
isEnableUserscribe = true;
62+
userAgentData: typeof GM_info.userAgentData = {};
6163

6264
constructor(
6365
private systemConfig: SystemConfig,
@@ -72,6 +74,25 @@ export class RuntimeService {
7274
this.logger = LoggerCore.logger({ component: "runtime" });
7375
}
7476

77+
async initUserAgentData() {
78+
// @ts-ignore
79+
if (navigator.userAgentData) {
80+
// @ts-ignore
81+
this.userAgentData = {
82+
// @ts-ignore
83+
brands: navigator.userAgentData.brands,
84+
// @ts-ignore
85+
mobile: navigator.userAgentData.mobile,
86+
// @ts-ignore
87+
platform: navigator.userAgentData.platform,
88+
};
89+
// 处理architecture和bitness
90+
const platformInfo = await chrome.runtime.getPlatformInfo();
91+
this.userAgentData.architecture = platformInfo.nacl_arch;
92+
this.userAgentData.bitness = platformInfo.arch.includes("64") ? "64" : "32";
93+
}
94+
}
95+
7596
async init() {
7697
// 启动gm api
7798
const permission = new PermissionVerify(this.group.group("permission"), this.mq);
@@ -206,6 +227,8 @@ export class RuntimeService {
206227
});
207228
// 加载黑名单
208229
this.loadBlacklist(await this.systemConfig.getBlacklist());
230+
// 初始化一下userAgentData
231+
this.initUserAgentData();
209232
}
210233

211234
private loadBlacklist(blacklist: string) {
@@ -515,7 +538,15 @@ export class RuntimeService {
515538
scripts: enableScript,
516539
});
517540

518-
return { flag: scriptFlag, scripts: enableScript };
541+
return {
542+
flag: scriptFlag,
543+
scripts: enableScript,
544+
envInfo: {
545+
sandboxMode: "raw",
546+
isIncognito: chrome.extension.inIncognitoContext,
547+
userAgentData: this.userAgentData,
548+
} as GMInfoEnv,
549+
};
519550
}
520551

521552
// 停止脚本

src/content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ client.pageLoad().then((data) => {
2424
const extServer = new Server("content", extMsg);
2525
// 初始化运行环境
2626
const runtime = new ContentRuntime(extServer, server, send, msg);
27-
runtime.start(data.scripts);
27+
runtime.start(data.scripts, data.envInfo);
2828
});

src/inject.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CustomEventMessage } from "@Packages/message/custom_event_message";
44
import { Server } from "@Packages/message/server";
55
import { InjectRuntime } from "./app/service/content/inject";
66
import { ScriptLoadInfo } from "./app/service/service_worker/runtime";
7+
import { GMInfoEnv } from "./app/service/content/gm_api";
78

89
const msg = new CustomEventMessage(MessageFlag, false);
910

@@ -15,9 +16,9 @@ const logger = new LoggerCore({
1516

1617
const server = new Server("inject", msg);
1718

18-
server.on("pageLoad", (data: { scripts: ScriptLoadInfo[] }) => {
19+
server.on("pageLoad", (data: { scripts: ScriptLoadInfo[]; envInfo: GMInfoEnv }) => {
1920
logger.logger().debug("inject start");
2021
// 监听事件
21-
const runtime = new InjectRuntime(server, msg, data.scripts);
22+
const runtime = new InjectRuntime(server, msg, data.scripts, data.envInfo);
2223
runtime.start();
2324
});

0 commit comments

Comments
 (0)