Skip to content

Commit 32a9fe2

Browse files
authored
🐛 修正 content.js 没有 UserAgentData 问题 (#1183)
* 修正 content.js 没有 UserAgentData 问题 * fix * 修订 initEnvInfo 生成 * 注释 及 ts 修正
1 parent cc2729d commit 32a9fe2

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed

src/app/service/content/script_executor.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@ export type ExecScriptEntry = {
1717
scriptFunc: any;
1818
};
1919

20-
export let initEnvInfo: GMInfoEnv;
21-
22-
try {
23-
initEnvInfo = {
24-
userAgentData: UserAgentData, // 从全局变量获取
25-
sandboxMode: "raw", // 预留字段,当前固定为 raw
26-
isIncognito: false, // inject 环境下无法判断,固定为 false
27-
};
28-
} catch {
29-
// 如果 UserAgentData 不存在,可能是在非inject/content环境下运行
30-
initEnvInfo = {
31-
userAgentData: {},
32-
sandboxMode: "raw",
33-
isIncognito: false,
34-
};
35-
}
20+
export const initEnvInfo = {
21+
/** userAgentData - 从全局变量获取 */
22+
userAgentData: typeof UserAgentData === "object" ? UserAgentData : {},
23+
/** sandboxMode - 预留字段,当前固定为 raw */
24+
sandboxMode: "raw",
25+
/** isIncognito - inject/content 环境下无法判断,固定为 false */
26+
/** 使用者可透过 「 await navigator.storage.persisted() 」来判断,但ScriptCat不会主动执行此代码来判断 */
27+
isIncognito: false,
28+
} satisfies GMInfoEnv;
3629

3730
// 脚本执行器
3831
export class ScriptExecutor {

src/app/service/service_worker/runtime.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -825,16 +825,9 @@ export class RuntimeService {
825825
}
826826

827827
let retContent: chrome.scripting.RegisteredContentScript[] = [];
828-
let retInject: chrome.userScripts.RegisteredUserScript[] = [];
829-
// inject.js
830-
const injectJs = await this.getInjectJsCode();
831-
if (injectJs) {
832-
// 构建inject.js的脚本注册信息
833-
retInject = this.compileInjectUserScript(injectJs, {
834-
excludeMatches,
835-
excludeGlobs,
836-
});
837-
}
828+
const retInject: chrome.userScripts.RegisteredUserScript[] = [];
829+
830+
// ------ scripting.js ------
838831
// Note: Chrome does not support file.js?query
839832
// 注意:Chrome 不支持 file.js?query
840833
retContent = [
@@ -848,20 +841,41 @@ export class RuntimeService {
848841
} satisfies chrome.scripting.RegisteredContentScript,
849842
];
850843

844+
// ------ inject.js & content.js ------
845+
const jsonUAD = JSON.stringify(this.userAgentData);
846+
const injectJs = await this.getInjectJsCode();
847+
if (injectJs) {
848+
// 构建inject.js的脚本注册信息
849+
const codeBody = `(function (UserAgentData) {\n${injectJs}\n})(${jsonUAD})`;
850+
const code = `${codeBody}${sourceMapTo("scriptcat-inject.js")}\n`;
851+
const script = {
852+
id: "scriptcat-inject",
853+
js: [{ code }],
854+
matches: ["<all_urls>"],
855+
allFrames: true,
856+
runAt: "document_start",
857+
excludeMatches: excludeMatches,
858+
excludeGlobs: excludeGlobs,
859+
world: "MAIN",
860+
} satisfies chrome.userScripts.RegisteredUserScript;
861+
retInject.push(script);
862+
}
851863
const contentJs = await this.getContentJsCode();
852864
if (contentJs) {
853-
const codeBody = `(function () {\n${contentJs}\n})()`;
865+
// 构建 content.js 的脚本注册信息
866+
const codeBody = `(function (UserAgentData) {\n${contentJs}\n})(${jsonUAD})`;
854867
const code = `${codeBody}${sourceMapTo("scriptcat-content.js")}\n`;
855-
retInject.push({
868+
const script = {
856869
id: "scriptcat-content",
857870
js: [{ code }],
858871
matches: ["<all_urls>"],
859872
allFrames: true,
860873
runAt: "document_start",
861-
world: "USER_SCRIPT",
862874
excludeMatches,
863875
excludeGlobs,
864-
} satisfies chrome.userScripts.RegisteredUserScript);
876+
world: "USER_SCRIPT",
877+
} satisfies chrome.userScripts.RegisteredUserScript;
878+
retInject.push(script);
865879
}
866880

867881
return { content: retContent, inject: retInject };
@@ -1305,27 +1319,6 @@ export class RuntimeService {
13051319
return await runScript(this.msgSender, res);
13061320
}
13071321

1308-
compileInjectUserScript(
1309-
injectJs: string,
1310-
{ excludeMatches, excludeGlobs }: { excludeMatches: string[] | undefined; excludeGlobs: string[] | undefined }
1311-
) {
1312-
// 构建inject.js的脚本注册信息
1313-
const codeBody = `(function (UserAgentData) {\n${injectJs}\n})(${JSON.stringify(this.userAgentData)})`;
1314-
const code = `${codeBody}${sourceMapTo("scriptcat-inject.js")}\n`;
1315-
const script: chrome.userScripts.RegisteredUserScript = {
1316-
id: "scriptcat-inject",
1317-
js: [{ code }],
1318-
matches: ["<all_urls>"],
1319-
allFrames: true,
1320-
world: "MAIN",
1321-
runAt: "document_start",
1322-
excludeMatches: excludeMatches,
1323-
excludeGlobs: excludeGlobs,
1324-
};
1325-
1326-
return [script] as chrome.userScripts.RegisteredUserScript[];
1327-
}
1328-
13291322
scriptMatchEntry(
13301323
scriptRes: ScriptRunResource,
13311324
o: {

src/types/main.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ interface FileSystemObserverInstance {
2828
observe(handle: FileSystemFileHandle | FileSystemDirectoryHandle | FileSystemSyncAccessHandle): Promise<void>;
2929
}
3030

31-
declare const UserAgentData: typeof GM_info.userAgentData;
31+
declare const UserAgentData: typeof GM_info.userAgentData | undefined;
3232

3333
// 可以让content与inject环境交换携带dom的对象
34-
declare let cloneInto: ((detail: any, view: any) => any) | undefined;
34+
declare let cloneInto: ((obj: object, targetScope: object, options?: object) => object) | undefined;
3535

3636
declare namespace GMSend {
3737
interface XHRDetails {

0 commit comments

Comments
 (0)