From ea51c093f72dd3b90a84ba4d6d6c783f3b192875 Mon Sep 17 00:00:00 2001 From: josc146 Date: Sat, 13 May 2023 14:36:53 +0800 Subject: [PATCH] feat: customizable clicking icon action (#291) --- src/_locales/en/main.json | 4 +++- src/_locales/zh-hans/main.json | 4 +++- src/_locales/zh-hant/main.json | 4 +++- src/background/index.mjs | 16 +++++++++++++ src/config/index.mjs | 1 + src/content-script/menu-tools/index.mjs | 25 ++++++++------------ src/popup/index.jsx | 31 ++++++++++++++++++++++++- src/popup/sections/FeaturePages.jsx | 15 +++++++----- src/popup/sections/GeneralPart.jsx | 22 ++++++++++++++++++ 9 files changed, 97 insertions(+), 25 deletions(-) diff --git a/src/_locales/en/main.json b/src/_locales/en/main.json index 51698a3..337aca7 100644 --- a/src/_locales/en/main.json +++ b/src/_locales/en/main.json @@ -115,5 +115,7 @@ "API Modes": "API Modes", "Disable web mode history for better privacy protection, but it will result in unavailable conversations after a period of time": "Disable web mode history for better privacy protection, but it will result in unavailable conversations after a period of time", "Display selection tools next to input box to avoid blocking": "Display selection tools next to input box to avoid blocking", - "Close All Chats In This Page": "Close All Chats In This Page" + "Close All Chats In This Page": "Close All Chats In This Page", + "When Icon Clicked": "When Icon Clicked", + "Open Settings": "Open Settings" } diff --git a/src/_locales/zh-hans/main.json b/src/_locales/zh-hans/main.json index bb9694b..310fa1f 100644 --- a/src/_locales/zh-hans/main.json +++ b/src/_locales/zh-hans/main.json @@ -115,5 +115,7 @@ "API Modes": "API模式", "Disable web mode history for better privacy protection, but it will result in unavailable conversations after a period of time": "禁用网页版模式历史记录以获得更好的隐私保护, 但会导致对话在一段时间后不可用", "Display selection tools next to input box to avoid blocking": "将选择浮动工具显示在输入框旁边以避免遮挡", - "Close All Chats In This Page": "关闭本页所有聊天" + "Close All Chats In This Page": "关闭本页所有聊天", + "When Icon Clicked": "当图标被点击时", + "Open Settings": "打开设置" } diff --git a/src/_locales/zh-hant/main.json b/src/_locales/zh-hant/main.json index db9b2fa..bafafd3 100644 --- a/src/_locales/zh-hant/main.json +++ b/src/_locales/zh-hant/main.json @@ -115,5 +115,7 @@ "API Modes": "API 模式", "Disable web mode history for better privacy protection, but it will result in unavailable conversations after a period of time": "停用網頁版模式歷史記錄以提升隱私保護,但會導致對話記錄在一段時間後無法使用", "Display selection tools next to input box to avoid blocking": "將選擇浮動工具顯示在輸入框旁邊以避免遮擋", - "Close All Chats In This Page": "關閉本頁所有對話" + "Close All Chats In This Page": "關閉本頁所有對話", + "When Icon Clicked": "當圖示被點擊時", + "Open Settings": "開啟設定" } diff --git a/src/background/index.mjs b/src/background/index.mjs index d7862d8..56edf07 100644 --- a/src/background/index.mjs +++ b/src/background/index.mjs @@ -20,6 +20,7 @@ import { chatgptWebModelKeys, customApiModelKeys, defaultConfig, + getUserConfig, githubThirdPartyApiModelKeys, gptApiModelKeys, Models, @@ -156,6 +157,21 @@ Browser.runtime.onMessage.addListener(async (message, sender) => { case 'OPEN_URL': openUrl(message.data.url) break + case 'OPEN_CHAT_WINDOW': { + const config = await getUserConfig() + const url = Browser.runtime.getURL('IndependentPanel.html') + const tabs = await Browser.tabs.query({ url: url, windowType: 'popup' }) + if (!config.alwaysCreateNewConversationWindow && tabs.length > 0) + await Browser.windows.update(tabs[0].windowId, { focused: true }) + else + await Browser.windows.create({ + url: url, + type: 'popup', + width: 500, + height: 650, + }) + break + } case 'REFRESH_MENU': refreshMenu() break diff --git a/src/config/index.mjs b/src/config/index.mjs index a52ab5b..a96cba6 100644 --- a/src/config/index.mjs +++ b/src/config/index.mjs @@ -90,6 +90,7 @@ export const defaultConfig = { modelName: 'chatgptFree35', preferredLanguage: getNavigatorLanguage(), + clickIconAction: 'popup', insertAtTop: isMobile(), lockWhenAnswer: false, autoRegenAfterSwitchModel: false, diff --git a/src/content-script/menu-tools/index.mjs b/src/content-script/menu-tools/index.mjs index 18b994a..6bb15df 100644 --- a/src/content-script/menu-tools/index.mjs +++ b/src/content-script/menu-tools/index.mjs @@ -1,7 +1,5 @@ import { getCoreContentText } from '../../utils/get-core-content-text' -import { openUrl } from '../../utils/open-url' import Browser from 'webextension-polyfill' -import { getUserConfig } from '../../config/index.mjs' export const config = { newChat: { @@ -19,24 +17,21 @@ export const config = { openConversationPage: { label: 'Open Conversation Page', action: async () => { - openUrl(Browser.runtime.getURL('IndependentPanel.html')) + Browser.runtime.sendMessage({ + type: 'OPEN_URL', + data: { + url: Browser.runtime.getURL('IndependentPanel.html'), + }, + }) }, }, openConversationWindow: { label: 'Open Conversation Window', action: async () => { - const config = await getUserConfig() - const url = Browser.runtime.getURL('IndependentPanel.html') - const tabs = await Browser.tabs.query({ url: url, windowType: 'popup' }) - if (!config.alwaysCreateNewConversationWindow && tabs.length > 0) - await Browser.windows.update(tabs[0].windowId, { focused: true }) - else - await Browser.windows.create({ - url: url, - type: 'popup', - width: 500, - height: 650, - }) + Browser.runtime.sendMessage({ + type: 'OPEN_CHAT_WINDOW', + data: {}, + }) }, }, closeAllChats: { diff --git a/src/popup/index.jsx b/src/popup/index.jsx index 159b7ea..50d5c01 100644 --- a/src/popup/index.jsx +++ b/src/popup/index.jsx @@ -1,5 +1,34 @@ import { render } from 'preact' import Popup from './Popup' import '../_locales/i18n-react' +import { getUserConfig } from '../config/index.mjs' +import { config as menuConfig } from '../content-script/menu-tools/index.mjs' +import Browser from 'webextension-polyfill' -render(, document.getElementById('app')) +getUserConfig().then(async (config) => { + if (config.clickIconAction === 'popup' || (window.innerWidth > 100 && window.innerHeight > 100)) { + render(, document.getElementById('app')) + } else { + const message = { + itemId: config.clickIconAction, + selectionText: '', + useMenuPosition: false, + } + console.debug('custom icon action triggered', message) + + if (config.clickIconAction in menuConfig) { + if (menuConfig[config.clickIconAction].action) { + menuConfig[config.clickIconAction].action() + } + + if (menuConfig[config.clickIconAction].genPrompt) { + const currentTab = (await Browser.tabs.query({ active: true, currentWindow: true }))[0] + Browser.tabs.sendMessage(currentTab.id, { + type: 'CREATE_CHAT', + data: message, + }) + } + } + window.close() + } +}) diff --git a/src/popup/sections/FeaturePages.jsx b/src/popup/sections/FeaturePages.jsx index 3a4878e..30aedc8 100644 --- a/src/popup/sections/FeaturePages.jsx +++ b/src/popup/sections/FeaturePages.jsx @@ -34,7 +34,12 @@ export function FeaturePages({ config, updateConfig }) {