From c3ebb79e8235442492ba2cde5f18b1e8f89fdee1 Mon Sep 17 00:00:00 2001 From: josc146 Date: Sun, 2 Apr 2023 21:34:58 +0800 Subject: [PATCH] feat: add entry for Independent Panel and Keyboard Shortcuts --- src/_locales/en/main.json | 5 +++- src/_locales/zh-hans/main.json | 5 +++- src/_locales/zh-hant/main.json | 5 +++- src/background/index.mjs | 3 +++ src/content-script/index.jsx | 4 ++- src/content-script/menu-tools/index.mjs | 8 ++++++ src/manifest.json | 10 +++++++- src/manifest.v2.json | 8 ++++++ src/popup/Popup.jsx | 34 ++++++++++++++++++++++++- src/utils/index.mjs | 4 ++- src/utils/is-edge.mjs | 3 +++ src/utils/open-url.mjs | 11 ++++++++ 12 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/utils/is-edge.mjs create mode 100644 src/utils/open-url.mjs diff --git a/src/_locales/en/main.json b/src/_locales/en/main.json index 95f8146c..aaf3e089 100644 --- a/src/_locales/en/main.json +++ b/src/_locales/en/main.json @@ -92,5 +92,8 @@ "Unpin": "Unpin", "Delete Conversation": "Delete Conversation", "Clear conversations": "Clear conversations", - "Settings": "Settings" + "Settings": "Settings", + "Feature Pages": "Feature Pages", + "Keyboard Shortcuts": "Keyboard Shortcuts", + "Open Conversation Page": "Open Conversation Page" } diff --git a/src/_locales/zh-hans/main.json b/src/_locales/zh-hans/main.json index c7657255..d87c10ce 100644 --- a/src/_locales/zh-hans/main.json +++ b/src/_locales/zh-hans/main.json @@ -92,5 +92,8 @@ "Unpin": "收缩侧边", "Delete Conversation": "删除对话", "Clear conversations": "清空记录", - "Settings": "设置" + "Settings": "设置", + "Feature Pages": "功能页", + "Keyboard Shortcuts": "快捷键设置", + "Open Conversation Page": "打开独立对话页" } diff --git a/src/_locales/zh-hant/main.json b/src/_locales/zh-hant/main.json index 2f3e37be..2e796278 100644 --- a/src/_locales/zh-hant/main.json +++ b/src/_locales/zh-hant/main.json @@ -92,5 +92,8 @@ "Unpin": "收縮側邊", "Delete Conversation": "刪除對話", "Clear conversations": "清空記錄", - "Settings": "設置" + "Settings": "設置", + "Feature Pages": "功能頁", + "Keyboard Shortcuts": "快捷鍵設置", + "Open Conversation Page": "打開獨立對話頁" } diff --git a/src/background/index.mjs b/src/background/index.mjs index 7bea14d2..a6d4f440 100644 --- a/src/background/index.mjs +++ b/src/background/index.mjs @@ -156,6 +156,8 @@ Browser.commands.onCommand.addListener(async (command) => { selectionText: '', useMenuPosition: false, } + console.debug('command triggered', message) + if (menuConfig[command].action) menuConfig[command].action() Browser.tabs.sendMessage(currentTab.id, { type: 'CREATE_CHAT', data: message, @@ -208,6 +210,7 @@ function refreshMenu() { useMenuPosition: tab.id === currentTab.id, } console.debug('menu clicked', message) + if (menuConfig[message.itemId].action) menuConfig[message.itemId].action() Browser.tabs.sendMessage(currentTab.id, { type: 'CREATE_CHAT', data: message, diff --git a/src/content-script/index.jsx b/src/content-script/index.jsx index 9be7d0a5..6d573984 100644 --- a/src/content-script/index.jsx +++ b/src/content-script/index.jsx @@ -241,7 +241,9 @@ async function prepareForRightClickMenu() { if (data.itemId in toolsConfig) { prompt = await toolsConfig[data.itemId].genPrompt(data.selectionText) } else if (data.itemId in menuConfig) { - prompt = await menuConfig[data.itemId].genPrompt() + const menuItem = menuConfig[data.itemId] + if (!menuItem.genPrompt) return + else prompt = await menuItem.genPrompt() if (prompt) prompt = cropText(`Reply in ${await getPreferredLanguage()}.\n` + prompt) } diff --git a/src/content-script/menu-tools/index.mjs b/src/content-script/menu-tools/index.mjs index 0ee810a2..e5b01fad 100644 --- a/src/content-script/menu-tools/index.mjs +++ b/src/content-script/menu-tools/index.mjs @@ -1,4 +1,6 @@ import { getCoreContentText } from '../../utils/get-core-content-text' +import { openUrl } from '../../utils/open-url' +import Browser from 'webextension-polyfill' export const config = { newChat: { @@ -13,4 +15,10 @@ export const config = { return `The following is the text content of a web page, analyze the core content and summarize:\n${getCoreContentText()}` }, }, + openConversationPage: { + label: 'Open Conversation Page', + action: async () => { + openUrl(Browser.runtime.getURL('IndependentPanel.html')) + }, + }, } diff --git a/src/manifest.json b/src/manifest.json index 83aef5dd..21cc85c4 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -17,7 +17,8 @@ "commands", "cookies", "storage", - "contextMenus" + "contextMenus", + "unlimitedStorage" ], "background": { "service_worker": "background.js" @@ -69,6 +70,13 @@ "mac": "Alt+B" }, "description": "Summarize this page" + }, + "openConversationPage": { + "suggested_key": { + "default": "Ctrl+Shift+H", + "mac": "MacCtrl+Shift+H" + }, + "description": "Open the independent conversation page" } } } \ No newline at end of file diff --git a/src/manifest.v2.json b/src/manifest.v2.json index cf593367..784680c1 100644 --- a/src/manifest.v2.json +++ b/src/manifest.v2.json @@ -14,6 +14,7 @@ "cookies", "storage", "contextMenus", + "unlimitedStorage", "https://*.openai.com/", "https://*.bing.com/" ], @@ -62,6 +63,13 @@ "mac": "Alt+B" }, "description": "Summarize this page" + }, + "openConversationPage": { + "suggested_key": { + "default": "Ctrl+Shift+H", + "mac": "MacCtrl+Shift+H" + }, + "description": "Open the independent conversation page" } } } \ No newline at end of file diff --git a/src/popup/Popup.jsx b/src/popup/Popup.jsx index 75388fdd..e5df1b65 100644 --- a/src/popup/Popup.jsx +++ b/src/popup/Popup.jsx @@ -24,7 +24,7 @@ import wechatpay from './donation/wechatpay.jpg' import bugmeacoffee from './donation/bugmeacoffee.png' import { useWindowTheme } from '../hooks/use-window-theme.mjs' import { languageList } from '../config/language.mjs' -import { isMobile, isSafari } from '../utils/index.mjs' +import { isEdge, isFirefox, isMobile, isSafari, openUrl } from '../utils/index.mjs' import { useTranslation } from 'react-i18next' function GeneralPart({ config, updateConfig }) { @@ -256,6 +256,34 @@ GeneralPart.propTypes = { updateConfig: PropTypes.func.isRequired, } +function FeaturePages() { + const { t } = useTranslation() + + return ( +
+ {!isMobile() && !isFirefox() && !isSafari() && ( + + )} + +
+ ) +} + function AdvancedPart({ config, updateConfig }) { const { t } = useTranslation() @@ -514,6 +542,7 @@ function Popup() { {t('General')} + {t('Feature Pages')} {t('Selection Tools')} {t('Sites')} {t('Advanced')} @@ -523,6 +552,9 @@ function Popup() { + + + diff --git a/src/utils/index.mjs b/src/utils/index.mjs index 126c84b4..de7cda47 100644 --- a/src/utils/index.mjs +++ b/src/utils/index.mjs @@ -8,10 +8,12 @@ export * from './get-conversation-pairs' export * from './get-core-content-text' export * from './get-possible-element-by-query-selector' export * from './init-session' -export * from './is-firefox.mjs' +export * from './is-edge' +export * from './is-firefox' export * from './is-mobile' export * from './is-safari' export * from './limited-fetch' +export * from './open-url' export * from './set-element-position-in-viewport' export * from './stream-async-iterable' export * from './update-ref-height' diff --git a/src/utils/is-edge.mjs b/src/utils/is-edge.mjs new file mode 100644 index 00000000..e09e9d7f --- /dev/null +++ b/src/utils/is-edge.mjs @@ -0,0 +1,3 @@ +export function isEdge() { + return navigator.userAgent.toLowerCase().includes('edg') +} diff --git a/src/utils/open-url.mjs b/src/utils/open-url.mjs new file mode 100644 index 00000000..4e4d257a --- /dev/null +++ b/src/utils/open-url.mjs @@ -0,0 +1,11 @@ +import Browser from 'webextension-polyfill' + +export function openUrl(url) { + Browser.tabs.query({ url }).then((tabs) => { + if (tabs.length > 0) { + Browser.tabs.update(tabs[0].id, { active: true }) + } else { + Browser.tabs.create({ url }) + } + }) +}