Skip to content

Commit

Permalink
feat: add entry for Independent Panel and Keyboard Shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Apr 2, 2023
1 parent d9bd4bf commit c3ebb79
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/_locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 4 additions & 1 deletion src/_locales/zh-hans/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@
"Unpin": "收缩侧边",
"Delete Conversation": "删除对话",
"Clear conversations": "清空记录",
"Settings": "设置"
"Settings": "设置",
"Feature Pages": "功能页",
"Keyboard Shortcuts": "快捷键设置",
"Open Conversation Page": "打开独立对话页"
}
5 changes: 4 additions & 1 deletion src/_locales/zh-hant/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@
"Unpin": "收縮側邊",
"Delete Conversation": "刪除對話",
"Clear conversations": "清空記錄",
"Settings": "設置"
"Settings": "設置",
"Feature Pages": "功能頁",
"Keyboard Shortcuts": "快捷鍵設置",
"Open Conversation Page": "打開獨立對話頁"
}
3 changes: 3 additions & 0 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions src/content-script/menu-tools/index.mjs
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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'))
},
},
}
10 changes: 9 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"commands",
"cookies",
"storage",
"contextMenus"
"contextMenus",
"unlimitedStorage"
],
"background": {
"service_worker": "background.js"
Expand Down Expand Up @@ -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"
}
}
}
8 changes: 8 additions & 0 deletions src/manifest.v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cookies",
"storage",
"contextMenus",
"unlimitedStorage",
"https://*.openai.com/",
"https://*.bing.com/"
],
Expand Down Expand Up @@ -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"
}
}
}
34 changes: 33 additions & 1 deletion src/popup/Popup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down Expand Up @@ -256,6 +256,34 @@ GeneralPart.propTypes = {
updateConfig: PropTypes.func.isRequired,
}

function FeaturePages() {
const { t } = useTranslation()

return (
<div style="display:flex;flex-direction:column;align-items:center;">
{!isMobile() && !isFirefox() && !isSafari() && (
<button
type="button"
onClick={() => {
if (isEdge()) openUrl('edge://extensions/shortcuts')
else openUrl('chrome://extensions/shortcuts')
}}
>
{t('Keyboard Shortcuts')}
</button>
)}
<button
type="button"
onClick={() => {
openUrl(Browser.runtime.getURL('IndependentPanel.html'))
}}
>
{t('Open Conversation Page')}
</button>
</div>
)
}

function AdvancedPart({ config, updateConfig }) {
const { t } = useTranslation()

Expand Down Expand Up @@ -514,6 +542,7 @@ function Popup() {
<Tabs selectedTabClassName="popup-tab--selected">
<TabList>
<Tab className="popup-tab">{t('General')}</Tab>
<Tab className="popup-tab">{t('Feature Pages')}</Tab>
<Tab className="popup-tab">{t('Selection Tools')}</Tab>
<Tab className="popup-tab">{t('Sites')}</Tab>
<Tab className="popup-tab">{t('Advanced')}</Tab>
Expand All @@ -523,6 +552,9 @@ function Popup() {
<TabPanel>
<GeneralPart config={config} updateConfig={updateConfig} />
</TabPanel>
<TabPanel>
<FeaturePages />
</TabPanel>
<TabPanel>
<SelectionTools config={config} updateConfig={updateConfig} />
</TabPanel>
Expand Down
4 changes: 3 additions & 1 deletion src/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
3 changes: 3 additions & 0 deletions src/utils/is-edge.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isEdge() {
return navigator.userAgent.toLowerCase().includes('edg')
}
11 changes: 11 additions & 0 deletions src/utils/open-url.mjs
Original file line number Diff line number Diff line change
@@ -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 })
}
})
}

0 comments on commit c3ebb79

Please sign in to comment.