Skip to content
Closed
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
33 changes: 33 additions & 0 deletions src/app/service/service_worker/popup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,39 @@ describe("PopupService 删除脚本后 Popup 菜单残留清理", () => {
});
});

describe("PopupService 菜单点击分发", () => {
it("同一 document 重复注册的同组菜单只触发最新监听,跨 document 仍分别触发", async () => {
const { service, runtime } = createService();
const menu = {
groupKey: "translate-group",
name: "翻译网页/显示原文",
options: {},
tabId: 1,
frameId: 0,
documentId: "main-document",
};

await service.menuClick({
uuid: "immersive-translate",
menus: [
{ ...menu, key: "main-1" },
{ ...menu, key: "main-2" },
{ ...menu, key: "frame-1", frameId: 2, documentId: "frame-document" },
],
});

expect(runtime.emitEventToTab).toHaveBeenCalledTimes(2);
expect(runtime.emitEventToTab).toHaveBeenCalledWith(
expect.objectContaining({ documentId: "main-document" }),
expect.objectContaining({ eventId: "main-2" })
);
expect(runtime.emitEventToTab).toHaveBeenCalledWith(
expect.objectContaining({ documentId: "frame-document" }),
expect.objectContaining({ eventId: "frame-1" })
);
});
});

// ─────────────────────────────────────────────────────────────────────────────

describe("PopupService addScriptRunNumber 页面脚本执行计数", () => {
Expand Down
9 changes: 7 additions & 2 deletions src/app/service/service_worker/popup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type IMessageQueue } from "@Packages/message/message_queue";
import { type Group } from "@Packages/message/server";
import { type RuntimeService } from "./runtime";
import type { ScriptMenu, TPopupPageStatus, TPopupScript } from "./types";
import type { ScriptMenu, ScriptMenuItem, TPopupPageStatus, TPopupScript } from "./types";
import type { GetPopupDataReq, GetPopupDataRes, MenuClickParams } from "./client";
import { cacheInstance } from "@App/app/cache";
import type { ScriptDAO } from "@App/app/repo/scripts";
Expand Down Expand Up @@ -740,8 +740,13 @@ export class PopupService {

// 触发目标 tab/frame 的「menuClick」事件;key 为菜单唯一键以定位对应 listener。
async menuClick({ uuid, menus, inputValue }: MenuClickParams) {
const targets = new Map<string, ScriptMenuItem>();
for (const menu of menus) {
const documentKey = menu.documentId || `frame:${menu.frameId || 0}`;
targets.set(`${menu.tabId}:${documentKey}:${menu.groupKey}`, menu);
}
await Promise.allSettled(
menus.map((menu) =>
[...targets.values()].map((menu) =>
this.runtime.emitEventToTab(
{
tabId: menu.tabId,
Expand Down
Loading