Skip to content

Commit 43572a3

Browse files
cyfung1031CodFrmCopilot
authored
🚑 紧急修正: GM.delete/setValue Promise不fulfill (#865)
* 紧急修正: GM.delete/setValue Promise不fulfill * Update value.test.ts * Update value.test.ts * Update value.test.ts * Update gm_api.test.ts * 修改变量名 * Update src/app/service/content/gm_api.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: 王一之 <yz@ggnb.top> Co-authored-by: wangyizhi <i@xloli.top> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent aa61335 commit 43572a3

8 files changed

Lines changed: 239 additions & 77 deletions

File tree

src/app/repo/scripts.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ export interface ConfigGroup {
4343
[key: string]: Config;
4444
}
4545

46-
export type UserConfig = {
47-
[key: string]: ConfigGroup | { sort: string[] } | undefined;
48-
"#options"?: { sort: string[] };
49-
};
46+
export type UserConfig = Partial<
47+
Record<string, ConfigGroup> & {
48+
"#options": { sort: string[] };
49+
}
50+
>;
5051

5152
// 排除掉 #options
5253
export type UserConfigWithoutOptions = Omit<{ [key: string]: ConfigGroup }, "#options">;

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

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -686,16 +686,16 @@ describe("GM_value", () => {
686686
expect(ret).toEqual({ ret1: { a: 123, b: 456, c: "789" }, ret2: { b: 456 } });
687687
});
688688

689-
it("GM_addValueChangeListener", async () => {
689+
it("GM_addValueChangeListener - remote: false", async () => {
690690
const script = Object.assign({ uuid: uuidv4() }, scriptRes) as ScriptLoadInfo;
691691
script.metadata.grant = ["GM_getValue", "GM_setValue", "GM_addValueChangeListener"];
692692
script.metadata.storageName = ["testStorage"];
693693
script.code = `
694694
return new Promise(resolve=>{
695-
GM_addValueChangeListener("a", (name, oldValue, newValue, remote)=>{
695+
GM_addValueChangeListener("param1", (name, oldValue, newValue, remote)=>{
696696
resolve({name, oldValue, newValue, remote});
697697
});
698-
GM_setValue("a", 123);
698+
GM_setValue("param1", 123);
699699
});
700700
`;
701701
const mockSendMessage = vi.fn().mockResolvedValue({ code: 0 });
@@ -705,31 +705,54 @@ describe("GM_value", () => {
705705
// @ts-ignore
706706
const exec = new ExecScript(script, "content", mockMessage, nilFn, envInfo);
707707
exec.scriptFunc = compileScript(compileScriptCode(script));
708-
let retPromise = exec.exec();
708+
const retPromise = exec.exec();
709709
expect(mockSendMessage).toHaveBeenCalledTimes(1);
710710
// 模拟值变化
711711
exec.valueUpdate({
712-
id: "123",
713-
entries: encodeMessage([["a", 123, undefined]]),
712+
id: "id-1",
713+
entries: encodeMessage([["param1", 123, undefined]]),
714714
uuid: script.uuid,
715715
storageName: script.uuid,
716716
sender: { runFlag: exec.sandboxContext!.runFlag, tabId: -2 },
717+
valueUpdated: true,
717718
});
718719
const ret = await retPromise;
719-
expect(ret).toEqual({ name: "a", oldValue: undefined, newValue: 123, remote: false });
720+
expect(ret).toEqual({ name: "param1", oldValue: undefined, newValue: 123, remote: false });
721+
});
722+
723+
it("GM_addValueChangeListener - remote: true", async () => {
724+
const script = Object.assign({ uuid: uuidv4() }, scriptRes) as ScriptLoadInfo;
725+
script.metadata.grant = ["GM_getValue", "GM_setValue", "GM_addValueChangeListener"];
726+
script.metadata.storageName = ["testStorage"];
727+
script.code = `
728+
return new Promise(resolve=>{
729+
GM_addValueChangeListener("param2", (name, oldValue, newValue, remote)=>{
730+
resolve({name, oldValue, newValue, remote});
731+
});
732+
GM_setValue("param2", 456);
733+
});
734+
`;
735+
const mockSendMessage = vi.fn().mockResolvedValue({ code: 0 });
736+
const mockMessage = {
737+
sendMessage: mockSendMessage,
738+
} as unknown as Message;
739+
// @ts-ignore
740+
const exec = new ExecScript(script, "content", mockMessage, nilFn, envInfo);
741+
exec.scriptFunc = compileScript(compileScriptCode(script));
720742
// remote = true
721-
retPromise = exec.exec();
722-
expect(mockSendMessage).toHaveBeenCalledTimes(2);
743+
const retPromise = exec.exec();
744+
expect(mockSendMessage).toHaveBeenCalledTimes(1);
723745
// 模拟值变化
724746
exec.valueUpdate({
725-
id: "123",
726-
entries: encodeMessage([["a", 123, undefined]]),
747+
id: "id-2",
748+
entries: encodeMessage([["param2", 456, undefined]]),
727749
uuid: script.uuid,
728750
storageName: "testStorage",
729751
sender: { runFlag: "user", tabId: -2 },
752+
valueUpdated: true,
730753
});
731754
const ret2 = await retPromise;
732-
expect(ret2).toEqual({ name: "a", oldValue: undefined, newValue: 123, remote: true });
755+
expect(ret2).toEqual({ name: "param2", oldValue: undefined, newValue: 456, remote: true });
733756
});
734757
it("异步GM.setValue,等待回调", async () => {
735758
const script = Object.assign({}, scriptRes) as ScriptLoadInfo;
@@ -750,14 +773,18 @@ describe("GM_value", () => {
750773
expect(mockSendMessage).toHaveBeenCalledTimes(1);
751774
// 获取调用参数
752775
const actualCall = mockSendMessage.mock.calls[0][0];
753-
console.log("actualCall", actualCall.data.params[0]);
776+
const id = actualCall.data.params[0];
777+
778+
expect(id).toBeTypeOf("string");
779+
expect(id.length).greaterThan(0);
754780
// 触发valueUpdate
755781
exec.valueUpdate({
756-
id: actualCall.data.params[0],
782+
id: id,
757783
entries: encodeMessage([["a", 123, undefined]]),
758784
uuid: script.uuid,
759785
storageName: script.uuid,
760786
sender: { runFlag: exec.sandboxContext!.runFlag, tabId: -2 },
787+
valueUpdated: true,
761788
});
762789

763790
const ret = await retPromise;

src/app/service/content/gm_api.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class GM_Base implements IGM_Base {
145145
public valueUpdate(data: ValueUpdateDataEncoded) {
146146
if (!this.scriptRes || !this.valueChangeListener) return;
147147
const scriptRes = this.scriptRes;
148-
const { id, uuid, entries, storageName, sender } = data;
148+
const { id, uuid, entries, storageName, sender, valueUpdated } = data;
149149
if (uuid === scriptRes.uuid || storageName === getStorageName(scriptRes)) {
150150
const valueStore = scriptRes.value;
151151
const remote = sender.runFlag !== this.runFlag;
@@ -156,17 +156,19 @@ class GM_Base implements IGM_Base {
156156
fn();
157157
}
158158
}
159-
const entries_ = decodeMessage(entries);
160-
for (const [key, value, oldValue] of entries_) {
161-
// 触发,并更新值
162-
if (value === undefined) {
163-
if (valueStore[key] !== undefined) {
164-
delete valueStore[key];
159+
if (valueUpdated) {
160+
const valueChanges = decodeMessage(entries);
161+
for (const [key, value, oldValue] of valueChanges) {
162+
// 触发,并更新值
163+
if (value === undefined) {
164+
if (valueStore[key] !== undefined) {
165+
delete valueStore[key];
166+
}
167+
} else {
168+
valueStore[key] = value;
165169
}
166-
} else {
167-
valueStore[key] = value;
170+
this.valueChangeListener.execute(key, oldValue, value, remote, sender.tabId);
168171
}
169-
this.valueChangeListener.execute(key, oldValue, value, remote, sender.tabId);
170172
}
171173
}
172174
}

src/app/service/content/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export type ValueUpdateDataEncoded = {
3131
uuid: string;
3232
storageName: string; // 储存name
3333
sender: ValueUpdateSender;
34+
valueUpdated: boolean;
3435
};
3536

3637
// gm_api.ts

src/app/service/queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type TEnableScript = { uuid: string; enable: boolean };
3030

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

33-
export type TScriptValueUpdate = { script: Script };
33+
export type TScriptValueUpdate = { script: Script; valueUpdated: boolean };
3434

3535
export type TScriptMenuRegister = {
3636
uuid: string;

src/app/service/service_worker/runtime.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,13 @@ export class RuntimeService {
454454
});
455455

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

0 commit comments

Comments
 (0)