Skip to content

Commit ea51c09

Browse files
committed
feat: customizable clicking icon action (#291)
1 parent 96f31a3 commit ea51c09

File tree

9 files changed

+97
-25
lines changed

9 files changed

+97
-25
lines changed

src/_locales/en/main.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@
115115
"API Modes": "API Modes",
116116
"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",
117117
"Display selection tools next to input box to avoid blocking": "Display selection tools next to input box to avoid blocking",
118-
"Close All Chats In This Page": "Close All Chats In This Page"
118+
"Close All Chats In This Page": "Close All Chats In This Page",
119+
"When Icon Clicked": "When Icon Clicked",
120+
"Open Settings": "Open Settings"
119121
}

src/_locales/zh-hans/main.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@
115115
"API Modes": "API模式",
116116
"Disable web mode history for better privacy protection, but it will result in unavailable conversations after a period of time": "禁用网页版模式历史记录以获得更好的隐私保护, 但会导致对话在一段时间后不可用",
117117
"Display selection tools next to input box to avoid blocking": "将选择浮动工具显示在输入框旁边以避免遮挡",
118-
"Close All Chats In This Page": "关闭本页所有聊天"
118+
"Close All Chats In This Page": "关闭本页所有聊天",
119+
"When Icon Clicked": "当图标被点击时",
120+
"Open Settings": "打开设置"
119121
}

src/_locales/zh-hant/main.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@
115115
"API Modes": "API 模式",
116116
"Disable web mode history for better privacy protection, but it will result in unavailable conversations after a period of time": "停用網頁版模式歷史記錄以提升隱私保護,但會導致對話記錄在一段時間後無法使用",
117117
"Display selection tools next to input box to avoid blocking": "將選擇浮動工具顯示在輸入框旁邊以避免遮擋",
118-
"Close All Chats In This Page": "關閉本頁所有對話"
118+
"Close All Chats In This Page": "關閉本頁所有對話",
119+
"When Icon Clicked": "當圖示被點擊時",
120+
"Open Settings": "開啟設定"
119121
}

src/background/index.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
chatgptWebModelKeys,
2121
customApiModelKeys,
2222
defaultConfig,
23+
getUserConfig,
2324
githubThirdPartyApiModelKeys,
2425
gptApiModelKeys,
2526
Models,
@@ -156,6 +157,21 @@ Browser.runtime.onMessage.addListener(async (message, sender) => {
156157
case 'OPEN_URL':
157158
openUrl(message.data.url)
158159
break
160+
case 'OPEN_CHAT_WINDOW': {
161+
const config = await getUserConfig()
162+
const url = Browser.runtime.getURL('IndependentPanel.html')
163+
const tabs = await Browser.tabs.query({ url: url, windowType: 'popup' })
164+
if (!config.alwaysCreateNewConversationWindow && tabs.length > 0)
165+
await Browser.windows.update(tabs[0].windowId, { focused: true })
166+
else
167+
await Browser.windows.create({
168+
url: url,
169+
type: 'popup',
170+
width: 500,
171+
height: 650,
172+
})
173+
break
174+
}
159175
case 'REFRESH_MENU':
160176
refreshMenu()
161177
break

src/config/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const defaultConfig = {
9090
modelName: 'chatgptFree35',
9191

9292
preferredLanguage: getNavigatorLanguage(),
93+
clickIconAction: 'popup',
9394
insertAtTop: isMobile(),
9495
lockWhenAnswer: false,
9596
autoRegenAfterSwitchModel: false,

src/content-script/menu-tools/index.mjs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { getCoreContentText } from '../../utils/get-core-content-text'
2-
import { openUrl } from '../../utils/open-url'
32
import Browser from 'webextension-polyfill'
4-
import { getUserConfig } from '../../config/index.mjs'
53

64
export const config = {
75
newChat: {
@@ -19,24 +17,21 @@ export const config = {
1917
openConversationPage: {
2018
label: 'Open Conversation Page',
2119
action: async () => {
22-
openUrl(Browser.runtime.getURL('IndependentPanel.html'))
20+
Browser.runtime.sendMessage({
21+
type: 'OPEN_URL',
22+
data: {
23+
url: Browser.runtime.getURL('IndependentPanel.html'),
24+
},
25+
})
2326
},
2427
},
2528
openConversationWindow: {
2629
label: 'Open Conversation Window',
2730
action: async () => {
28-
const config = await getUserConfig()
29-
const url = Browser.runtime.getURL('IndependentPanel.html')
30-
const tabs = await Browser.tabs.query({ url: url, windowType: 'popup' })
31-
if (!config.alwaysCreateNewConversationWindow && tabs.length > 0)
32-
await Browser.windows.update(tabs[0].windowId, { focused: true })
33-
else
34-
await Browser.windows.create({
35-
url: url,
36-
type: 'popup',
37-
width: 500,
38-
height: 650,
39-
})
31+
Browser.runtime.sendMessage({
32+
type: 'OPEN_CHAT_WINDOW',
33+
data: {},
34+
})
4035
},
4136
},
4237
closeAllChats: {

src/popup/index.jsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
import { render } from 'preact'
22
import Popup from './Popup'
33
import '../_locales/i18n-react'
4+
import { getUserConfig } from '../config/index.mjs'
5+
import { config as menuConfig } from '../content-script/menu-tools/index.mjs'
6+
import Browser from 'webextension-polyfill'
47

5-
render(<Popup />, document.getElementById('app'))
8+
getUserConfig().then(async (config) => {
9+
if (config.clickIconAction === 'popup' || (window.innerWidth > 100 && window.innerHeight > 100)) {
10+
render(<Popup />, document.getElementById('app'))
11+
} else {
12+
const message = {
13+
itemId: config.clickIconAction,
14+
selectionText: '',
15+
useMenuPosition: false,
16+
}
17+
console.debug('custom icon action triggered', message)
18+
19+
if (config.clickIconAction in menuConfig) {
20+
if (menuConfig[config.clickIconAction].action) {
21+
menuConfig[config.clickIconAction].action()
22+
}
23+
24+
if (menuConfig[config.clickIconAction].genPrompt) {
25+
const currentTab = (await Browser.tabs.query({ active: true, currentWindow: true }))[0]
26+
Browser.tabs.sendMessage(currentTab.id, {
27+
type: 'CREATE_CHAT',
28+
data: message,
29+
})
30+
}
31+
}
32+
window.close()
33+
}
34+
})

src/popup/sections/FeaturePages.jsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ export function FeaturePages({ config, updateConfig }) {
3434
<button
3535
type="button"
3636
onClick={() => {
37-
openUrl(Browser.runtime.getURL('IndependentPanel.html'))
37+
Browser.runtime.sendMessage({
38+
type: 'OPEN_URL',
39+
data: {
40+
url: Browser.runtime.getURL('IndependentPanel.html'),
41+
},
42+
})
3843
}}
3944
>
4045
{t('Open Conversation Page')}
@@ -43,11 +48,9 @@ export function FeaturePages({ config, updateConfig }) {
4348
<button
4449
type="button"
4550
onClick={() => {
46-
Browser.windows.create({
47-
url: Browser.runtime.getURL('IndependentPanel.html'),
48-
type: 'popup',
49-
width: 500,
50-
height: 650,
51+
Browser.runtime.sendMessage({
52+
type: 'OPEN_CHAT_WINDOW',
53+
data: {},
5154
})
5255
}}
5356
>

src/popup/sections/GeneralPart.jsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import Browser from 'webextension-polyfill'
1717
import { languageList } from '../../config/language.mjs'
1818
import PropTypes from 'prop-types'
19+
import { config as menuConfig } from '../../content-script/menu-tools'
1920

2021
GeneralPart.propTypes = {
2122
config: PropTypes.object.isRequired,
@@ -283,6 +284,27 @@ export function GeneralPart({ config, updateConfig }) {
283284
})}
284285
</select>
285286
</label>
287+
<label>
288+
<legend>{t('When Icon Clicked')}</legend>
289+
<select
290+
required
291+
onChange={(e) => {
292+
const mode = e.target.value
293+
updateConfig({ clickIconAction: mode })
294+
}}
295+
>
296+
<option value="popup" key="popup" selected={config.clickIconAction === 'popup'}>
297+
{t('Open Settings')}
298+
</option>
299+
{Object.entries(menuConfig).map(([k, v]) => {
300+
return (
301+
<option value={k} key={k} selected={k === config.clickIconAction}>
302+
{t(v.label)}
303+
</option>
304+
)
305+
})}
306+
</select>
307+
</label>
286308
<label>
287309
<input
288310
type="checkbox"

0 commit comments

Comments
 (0)