Skip to content
Merged
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
9 changes: 5 additions & 4 deletions src/app/repo/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ export interface ConfigGroup {
[key: string]: Config;
}

export type UserConfig = {
[key: string]: ConfigGroup | { sort: string[] } | undefined;
"#options"?: { sort: string[] };
};
export type UserConfig = Partial<
Record<string, ConfigGroup> & {
"#options": { sort: string[] };
}
>;

// 排除掉 #options
export type UserConfigWithoutOptions = Omit<{ [key: string]: ConfigGroup }, "#options">;
Expand Down
55 changes: 41 additions & 14 deletions src/app/service/content/gm_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,16 +686,16 @@ describe("GM_value", () => {
expect(ret).toEqual({ ret1: { a: 123, b: 456, c: "789" }, ret2: { b: 456 } });
});

it("GM_addValueChangeListener", async () => {
it("GM_addValueChangeListener - remote: false", async () => {
const script = Object.assign({ uuid: uuidv4() }, scriptRes) as ScriptLoadInfo;
script.metadata.grant = ["GM_getValue", "GM_setValue", "GM_addValueChangeListener"];
script.metadata.storageName = ["testStorage"];
script.code = `
return new Promise(resolve=>{
GM_addValueChangeListener("a", (name, oldValue, newValue, remote)=>{
GM_addValueChangeListener("param1", (name, oldValue, newValue, remote)=>{
resolve({name, oldValue, newValue, remote});
});
GM_setValue("a", 123);
GM_setValue("param1", 123);
});
`;
const mockSendMessage = vi.fn().mockResolvedValue({ code: 0 });
Expand All @@ -705,31 +705,54 @@ describe("GM_value", () => {
// @ts-ignore
const exec = new ExecScript(script, "content", mockMessage, nilFn, envInfo);
exec.scriptFunc = compileScript(compileScriptCode(script));
let retPromise = exec.exec();
const retPromise = exec.exec();
expect(mockSendMessage).toHaveBeenCalledTimes(1);
// 模拟值变化
exec.valueUpdate({
id: "123",
entries: encodeMessage([["a", 123, undefined]]),
id: "id-1",
entries: encodeMessage([["param1", 123, undefined]]),
uuid: script.uuid,
storageName: script.uuid,
sender: { runFlag: exec.sandboxContext!.runFlag, tabId: -2 },
valueUpdated: true,
});
const ret = await retPromise;
expect(ret).toEqual({ name: "a", oldValue: undefined, newValue: 123, remote: false });
expect(ret).toEqual({ name: "param1", oldValue: undefined, newValue: 123, remote: false });
});

it("GM_addValueChangeListener - remote: true", async () => {
const script = Object.assign({ uuid: uuidv4() }, scriptRes) as ScriptLoadInfo;
script.metadata.grant = ["GM_getValue", "GM_setValue", "GM_addValueChangeListener"];
script.metadata.storageName = ["testStorage"];
script.code = `
return new Promise(resolve=>{
GM_addValueChangeListener("param2", (name, oldValue, newValue, remote)=>{
resolve({name, oldValue, newValue, remote});
});
GM_setValue("param2", 456);
});
`;
const mockSendMessage = vi.fn().mockResolvedValue({ code: 0 });
const mockMessage = {
sendMessage: mockSendMessage,
} as unknown as Message;
// @ts-ignore
const exec = new ExecScript(script, "content", mockMessage, nilFn, envInfo);
exec.scriptFunc = compileScript(compileScriptCode(script));
// remote = true
retPromise = exec.exec();
expect(mockSendMessage).toHaveBeenCalledTimes(2);
const retPromise = exec.exec();
expect(mockSendMessage).toHaveBeenCalledTimes(1);
// 模拟值变化
exec.valueUpdate({
id: "123",
entries: encodeMessage([["a", 123, undefined]]),
id: "id-2",
entries: encodeMessage([["param2", 456, undefined]]),
uuid: script.uuid,
storageName: "testStorage",
sender: { runFlag: "user", tabId: -2 },
valueUpdated: true,
});
const ret2 = await retPromise;
expect(ret2).toEqual({ name: "a", oldValue: undefined, newValue: 123, remote: true });
expect(ret2).toEqual({ name: "param2", oldValue: undefined, newValue: 456, remote: true });
});
it("异步GM.setValue,等待回调", async () => {
const script = Object.assign({}, scriptRes) as ScriptLoadInfo;
Expand All @@ -750,14 +773,18 @@ describe("GM_value", () => {
expect(mockSendMessage).toHaveBeenCalledTimes(1);
// 获取调用参数
const actualCall = mockSendMessage.mock.calls[0][0];
console.log("actualCall", actualCall.data.params[0]);
const id = actualCall.data.params[0];

expect(id).toBeTypeOf("string");
expect(id.length).greaterThan(0);
// 触发valueUpdate
exec.valueUpdate({
id: actualCall.data.params[0],
id: id,
entries: encodeMessage([["a", 123, undefined]]),
uuid: script.uuid,
storageName: script.uuid,
sender: { runFlag: exec.sandboxContext!.runFlag, tabId: -2 },
valueUpdated: true,
});

const ret = await retPromise;
Expand Down
22 changes: 12 additions & 10 deletions src/app/service/content/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class GM_Base implements IGM_Base {
public valueUpdate(data: ValueUpdateDataEncoded) {
if (!this.scriptRes || !this.valueChangeListener) return;
const scriptRes = this.scriptRes;
const { id, uuid, entries, storageName, sender } = data;
const { id, uuid, entries, storageName, sender, valueUpdated } = data;
if (uuid === scriptRes.uuid || storageName === getStorageName(scriptRes)) {
const valueStore = scriptRes.value;
const remote = sender.runFlag !== this.runFlag;
Expand All @@ -156,17 +156,19 @@ class GM_Base implements IGM_Base {
fn();
}
}
const entries_ = decodeMessage(entries);
for (const [key, value, oldValue] of entries_) {
// 触发,并更新值
if (value === undefined) {
if (valueStore[key] !== undefined) {
delete valueStore[key];
if (valueUpdated) {
const valueChanges = decodeMessage(entries);
for (const [key, value, oldValue] of valueChanges) {
// 触发,并更新值
if (value === undefined) {
if (valueStore[key] !== undefined) {
delete valueStore[key];
}
} else {
valueStore[key] = value;
}
} else {
valueStore[key] = value;
this.valueChangeListener.execute(key, oldValue, value, remote, sender.tabId);
}
this.valueChangeListener.execute(key, oldValue, value, remote, sender.tabId);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/service/content/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type ValueUpdateDataEncoded = {
uuid: string;
storageName: string; // 储存name
sender: ValueUpdateSender;
valueUpdated: boolean;
};

// gm_api.ts
Expand Down
2 changes: 1 addition & 1 deletion src/app/service/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type TEnableScript = { uuid: string; enable: boolean };

export type TScriptRunStatus = { uuid: string; runStatus: SCRIPT_RUN_STATUS };

export type TScriptValueUpdate = { script: Script };
export type TScriptValueUpdate = { script: Script; valueUpdated: boolean };

export type TScriptMenuRegister = {
uuid: string;
Expand Down
12 changes: 7 additions & 5 deletions src/app/service/service_worker/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,13 @@ export class RuntimeService {
});

// 监听脚本值变更
this.mq.subscribe<TScriptValueUpdate>("valueUpdate", async ({ script }: TScriptValueUpdate) => {
if (script.status === SCRIPT_STATUS_ENABLE && isEarlyStartScript(script.metadata)) {
// 如果是预加载脚本,需要更新脚本代码重新注册
// scriptMatchInfo 里的 value 改变 => compileInjectionCode -> injectionCode 改变
await this.updateResourceOnScriptChange(script);
this.mq.subscribe<TScriptValueUpdate>("valueUpdate", async ({ script, valueUpdated }: TScriptValueUpdate) => {
if (valueUpdated) {
if (script.status === SCRIPT_STATUS_ENABLE && isEarlyStartScript(script.metadata)) {
// 如果是预加载脚本,需要更新脚本代码重新注册
// scriptMatchInfo 里的 value 改变 => compileInjectionCode -> injectionCode 改变
await this.updateResourceOnScriptChange(script);
}
}
});

Expand Down
Loading
Loading