()
| 57 | safari: false, |
| 58 | }, |
| 59 | main() { |
| 60 | const isContextMenuSupported = !!browser.contextMenus |
| 61 | |
| 62 | // 创建右键菜单项 |
| 63 | if (isContextMenuSupported) { |
| 64 | try { |
| 65 | // 创建父菜单 |
| 66 | browser.contextMenus.create({ |
| 67 | id: 'fluentread-parent', |
| 68 | title: 'FluentRead', |
| 69 | contexts: ['page', 'selection'], |
| 70 | }); |
| 71 | |
| 72 | // 创建全文翻译子菜单 |
| 73 | browser.contextMenus.create({ |
| 74 | id: CONTEXT_MENU_IDS.TRANSLATE_FULL_PAGE, |
| 75 | title: '全文翻译', |
| 76 | parentId: 'fluentread-parent', |
| 77 | contexts: ['page', 'selection'], |
| 78 | }); |
| 79 | |
| 80 | // 创建撤销翻译子菜单 |
| 81 | browser.contextMenus.create({ |
| 82 | id: CONTEXT_MENU_IDS.RESTORE_ORIGINAL, |
| 83 | title: '撤销翻译', |
| 84 | parentId: 'fluentread-parent', |
| 85 | contexts: ['page', 'selection'], |
| 86 | enabled: false, // 初始状态为禁用 |
| 87 | }); |
| 88 | |
| 89 | // 监听右键菜单点击事件 |
| 90 | browser.contextMenus.onClicked.addListener((info: any, tab: any) => { |
| 91 | if (!tab?.id) return; |
| 92 | |
| 93 | if (info.menuItemId === CONTEXT_MENU_IDS.TRANSLATE_FULL_PAGE) { |
| 94 | // 发送消息到内容脚本触发全文翻译 |
| 95 | browser.tabs.sendMessage(tab.id, { |
| 96 | type: 'contextMenuTranslate', |
| 97 | action: 'fullPage' |
| 98 | }).then(() => { |
| 99 | // 更新翻译状态 |
| 100 | translationStateMap.set(tab.id!, true); |
| 101 | updateContextMenus(tab.id!); |
| 102 | }).catch((error: any) => { |
| 103 | console.error('Failed to send message to content script:', error); |
| 104 | }); |
| 105 | } else if (info.menuItemId === CONTEXT_MENU_IDS.RESTORE_ORIGINAL) { |
| 106 | // 发送消息到内容脚本撤销翻译 |
| 107 | browser.tabs.sendMessage(tab.id, { |
| 108 | type: 'contextMenuTranslate', |
| 109 | action: 'restore' |
| 110 | }).then(() => { |
| 111 | // 更新翻译状态 |
| 112 | translationStateMap.set(tab.id!, false); |
| 113 | updateContextMenus(tab.id!); |
| 114 | }).catch((error: any) => { |
| 115 | console.error('Failed to send message to content script:', error); |
| 116 | }); |
nothing calls this directly
no test coverage detected