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
15 changes: 4 additions & 11 deletions src/app/service/service_worker/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import type {
ScriptMenuItem,
SearchType,
TBatchUpdateListAction,
TScriptMenuItemKey,
} from "./types";
import { Client } from "@Packages/message/client";
import type { ExtMessageSender, MessageSend } from "@Packages/message/types";
import type { MessageSend } from "@Packages/message/types";
import type PermissionVerify from "./permission_verify";
import { type UserConfirm } from "./permission_verify";
import { type FileSystemType } from "@Packages/filesystem/factory";
Expand Down Expand Up @@ -280,8 +279,7 @@ export type GetPopupDataRes = {

export type MenuClickParams = {
uuid: string;
key: TScriptMenuItemKey;
sender: ExtMessageSender;
menus: ScriptMenuItem[];
inputValue?: any;
};

Expand All @@ -294,16 +292,11 @@ export class PopupClient extends Client {
return this.doThrow("getPopupData", data);
}

menuClick(uuid: string, data: ScriptMenuItem, inputValue?: any) {
menuClick(uuid: string, menus: ScriptMenuItem[], inputValue?: any) {
return this.do("menuClick", {
uuid,
key: data.key,
menus,
inputValue,
sender: {
tabId: data.tabId,
frameId: data.frameId,
documentId: data.documentId,
},
} as MenuClickParams);
}
}
Expand Down
46 changes: 22 additions & 24 deletions src/app/service/service_worker/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,24 @@ export class PopupService {
}

// 触发目标 tab/frame 的「menuClick」事件;key 为菜单唯一键以定位对应 listener。
async menuClick({ uuid, key, sender, inputValue }: MenuClickParams) {
// 菜单点击事件
await this.runtime.emitEventToTab(sender, {
uuid,
event: "menuClick",
eventId: `${key}`,
data: inputValue,
});
return true;
async menuClick({ uuid, menus, inputValue }: MenuClickParams) {
await Promise.allSettled(
menus.map((menu) =>
this.runtime.emitEventToTab(
{
tabId: menu.tabId,
frameId: menu.frameId || 0,
documentId: menu.documentId || "",
},
{
uuid,
event: "menuClick",
eventId: `${menu.key}`,
data: inputValue,
}
)
)
);
}

async updateBadgeIcon() {
Expand Down Expand Up @@ -677,29 +686,18 @@ export class PopupService {
// 寻找menu信息
const menu = await this.getScriptMenu(tab!.id!);
let script = menu.find((item) => item.uuid === uuid);
let bgscript = false;
if (!script) {
// 从后台脚本中寻找
const backgroundMenu = await this.getScriptMenu(-1);
script = backgroundMenu.find((item) => item.uuid === uuid);
bgscript = true;
}
if (script) {
// 仅触发「非输入型」且 groupKey 相符的项目;同 groupKey 可能代表多个 frame 来源,一次性全部触发。
const menuItems = script.menus.filter((item) => item.groupKey === groupKey && !item.options?.inputType);
await Promise.allSettled(
menuItems.map((menuItem) =>
this.menuClick({
uuid: script.uuid,
key: menuItem.key,
sender: {
tabId: bgscript ? -1 : tab!.id!,
frameId: menuItem.frameId || 0,
documentId: menuItem.documentId || "",
},
} as MenuClickParams)
)
);
await this.menuClick({
uuid: script.uuid,
menus: menuItems,
} as MenuClickParams);
return;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/pages/components/ScriptMenuList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ const CollapseItem = Collapse.Item;

const sendMenuAction = (
uuid: string,
name: string,
options: ScriptMenuItemOption | undefined,
menus: ScriptMenuItem[],
inputValue?: any
) => {
Promise.allSettled(menus.map((menu) => popupClient.menuClick(uuid, menu, inputValue))).then(() => {
popupClient.menuClick(uuid, menus, inputValue).then(() => {
options?.autoClose !== false && window.close();
});
};
Expand Down Expand Up @@ -86,7 +85,7 @@ const MenuItem = React.memo(({ menuItems, uuid }: MenuItemProps) => {
autoComplete="off"
onSubmit={(v) => {
const inputValue = v.inputValue;
sendMenuAction(uuid, name, options, menuItems, inputValue);
sendMenuAction(uuid, options, menuItems, inputValue);
}}
>
<Button
Expand Down Expand Up @@ -406,10 +405,10 @@ const ScriptMenuList = React.memo(
if (!checkItems.size) return;
const sharedKeyPressListner = (e: KeyboardEvent) => {
const keyUpper = e.key.toUpperCase();
checkItems.forEach(([uuid, accessKeyUpper, name, menuItems]) => {
checkItems.forEach(([uuid, accessKeyUpper, _name, menuItems]) => {
if (keyUpper === accessKeyUpper) {
// 快速键触发不需传递 options(autoClose 由 sendMenuAction 内部处理)。
sendMenuAction(uuid, name, {}, menuItems);
sendMenuAction(uuid, {}, menuItems);
}
});
};
Expand Down
Loading