feat: shortcut keys (#81, #28)

This commit is contained in:
josc146
2023-03-27 16:48:43 +08:00
parent 9c7c596fb6
commit caddb972a5
4 changed files with 55 additions and 5 deletions
+15 -1
View File
@@ -126,6 +126,19 @@ Browser.runtime.onMessage.addListener(async (message) => {
}
})
Browser.commands.onCommand.addListener(async (command) => {
const currentTab = (await Browser.tabs.query({ active: true, currentWindow: true }))[0]
const message = {
itemId: command,
selectionText: '',
useMenuPosition: false,
}
Browser.tabs.sendMessage(currentTab.id, {
type: 'CREATE_CHAT',
data: message,
})
})
function refreshMenu() {
Browser.contextMenus.removeAll().then(() => {
const menuId = 'ChatGPTBox-Menu'
@@ -164,10 +177,11 @@ function refreshMenu() {
const message = {
itemId: info.menuItemId.replace(menuId, ''),
selectionText: info.selectionText,
useMenuPosition: true,
}
console.debug('menu clicked', message)
Browser.tabs.sendMessage(tab.id, {
type: 'CREATE_MENU',
type: 'CREATE_CHAT',
data: message,
})
})
+4 -2
View File
@@ -218,7 +218,7 @@ async function prepareForRightClickMenu() {
})
Browser.runtime.onMessage.addListener(async (message) => {
if (message.type === 'CREATE_MENU') {
if (message.type === 'CREATE_CHAT') {
const data = message.data
let prompt = ''
if (data.itemId in toolsConfig) {
@@ -228,7 +228,9 @@ async function prepareForRightClickMenu() {
if (prompt) prompt = cropText(`Reply in ${await getPreferredLanguage()}.\n` + prompt)
}
const position = { x: menuX, y: menuY }
const position = data.useMenuPosition
? { x: menuX, y: menuY }
: { x: window.innerWidth / 2 - 300, y: window.innerHeight / 2 - 200 }
const container = createElementAtPosition(position.x, position.y)
container.className = 'chatgptbox-toolbar-container-not-queryable'
render(
+18 -1
View File
@@ -14,6 +14,7 @@
"https://*.bing.com/"
],
"permissions": [
"commands",
"cookies",
"storage",
"contextMenus"
@@ -47,5 +48,21 @@
"<all_urls>"
]
}
]
],
"commands": {
"newChat": {
"suggested_key": {
"default": "Ctrl+B",
"mac": "MacCtrl+B"
},
"description": "Create a new chat"
},
"summarizePage": {
"suggested_key": {
"default": "Alt+B",
"mac": "Alt+B"
},
"description": "Summarize this page"
}
}
}
+18 -1
View File
@@ -10,6 +10,7 @@
"128": "logo.png"
},
"permissions": [
"commands",
"cookies",
"storage",
"contextMenus",
@@ -40,5 +41,21 @@
],
"web_accessible_resources": [
"*.png"
]
],
"commands": {
"newChat": {
"suggested_key": {
"default": "Ctrl+B",
"mac": "MacCtrl+B"
},
"description": "Create a new chat"
},
"summarizePage": {
"suggested_key": {
"default": "Alt+B",
"mac": "Alt+B"
},
"description": "Summarize this page"
}
}
}