Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/app/service/content/exec_script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ function setExecCode(exec: ExecScript, script: ScriptLoadInfo, code: string): vo
exec.scriptFunc = compileScript(compileScriptCode(script));
}

function makeExec(code: string, grant?: string[]): { exec: ExecScript; script: ScriptLoadInfo } {
const script = makeScript({ code, metadata: { grant, version: ["1.0.0"] } });
function makeExec(
code: string,
grant?: string[],
overrides: Partial<ScriptLoadInfo> = {}
): { exec: ExecScript; script: ScriptLoadInfo } {
const script = makeScript({ code, metadata: { grant, version: ["1.0.0"] }, ...overrides });
const message = {} as Message;
const exec = new ExecScript(script, {
envPrefix: "scripting",
Expand Down Expand Up @@ -68,6 +72,15 @@ describe.concurrent("GM_info", () => {
expect(ret.GM_info.script.version).toEqual("1.0.0");
expect(ret._this).not.toEqual(global);
});
it.concurrent.each([
["none", ["none"]],
["sandbox", undefined],
])("GM_info.uuid 与 GM.info.uuid 返回当前脚本 UUID(%s)", async (_mode, grant) => {
const { exec } = makeExec("return {GM_info, GM};", grant, { uuid: "4b1c2d3e-5f60-4718-8a9b-0c1d2e3f4a5b" });
const ret = await exec.exec();
expect(ret.GM_info.uuid).toBe("4b1c2d3e-5f60-4718-8a9b-0c1d2e3f4a5b");
expect(ret.GM.info.uuid).toBe("4b1c2d3e-5f60-4718-8a9b-0c1d2e3f4a5b");
});
});

describe.concurrent("unsafeWindow", () => {
Expand Down
2 changes: 2 additions & 0 deletions src/app/service/content/gm_api/gm_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export function evaluateGMInfo(envInfo: GMInfoEnv, script: TScriptInfo) {
scriptMetaStr: script.metadataStr,
userConfig: script.userConfig,
userConfigStr: script.userConfigStr,
// 与 Violentmonkey 一致,在顶层暴露当前脚本 UUID
uuid: script.uuid,
version: ExtVersion,
script: {
// TODO: 更多完整的信息(为了兼容Tampermonkey,后续待定)
Expand Down
1 change: 1 addition & 0 deletions src/template/scriptcat.d.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare const GM_info: {
scriptMetaStr?: string;
userConfig?: UserConfig;
userConfigStr?: string;
uuid: string;
isIncognito: boolean;
sandboxMode: "raw"; // "js" | "raw" | "none";
userAgentData: {
Expand Down
2 changes: 2 additions & 0 deletions src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ declare const GM_info: {
scriptMetaStr?: string;
userConfig?: UserConfig;
userConfigStr?: string;
/** UUID of the current script, stable across updates (same as Violentmonkey). */
uuid: string;
/** Whether running in an incognito/private window. */
isIncognito: boolean;
/** Sandbox mode (ScriptCat always uses `"raw"`). */
Expand Down
2 changes: 2 additions & 0 deletions src/types/scriptcat.zh-CN.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ declare const GM_info: {
scriptMetaStr?: string;
userConfig?: UserConfig;
userConfigStr?: string;
/** 当前脚本的 UUID,脚本更新后保持不变(与 Violentmonkey 一致)。 */
uuid: string;
/** 是否在隐私/无痕窗口中运行。 */
isIncognito: boolean;
/** 沙箱模式(ScriptCat 始终使用 `"raw"`)。 */
Expand Down
Loading