Skip to content

Commit ca2a4d7

Browse files
committed
feat: shortcut keys (#81, #28)
1 parent d308fad commit ca2a4d7

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

src/background/index.mjs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ Browser.runtime.onMessage.addListener(async (message) => {
126126
}
127127
})
128128

129+
Browser.commands.onCommand.addListener(async (command) => {
130+
const currentTab = (await Browser.tabs.query({ active: true, currentWindow: true }))[0]
131+
const message = {
132+
itemId: command,
133+
selectionText: '',
134+
useMenuPosition: false,
135+
}
136+
Browser.tabs.sendMessage(currentTab.id, {
137+
type: 'CREATE_CHAT',
138+
data: message,
139+
})
140+
})
141+
129142
function refreshMenu() {
130143
Browser.contextMenus.removeAll().then(() => {
131144
const menuId = 'ChatGPTBox-Menu'
@@ -164,10 +177,11 @@ function refreshMenu() {
164177
const message = {
165178
itemId: info.menuItemId.replace(menuId, ''),
166179
selectionText: info.selectionText,
180+
useMenuPosition: true,
167181
}
168182
console.debug('menu clicked', message)
169183
Browser.tabs.sendMessage(tab.id, {
170-
type: 'CREATE_MENU',
184+
type: 'CREATE_CHAT',
171185
data: message,
172186
})
173187
})

src/content-script/index.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ async function prepareForRightClickMenu() {
218218
})
219219

220220
Browser.runtime.onMessage.addListener(async (message) => {
221-
if (message.type === 'CREATE_MENU') {
221+
if (message.type === 'CREATE_CHAT') {
222222
const data = message.data
223223
let prompt = ''
224224
if (data.itemId in toolsConfig) {
@@ -228,7 +228,9 @@ async function prepareForRightClickMenu() {
228228
if (prompt) prompt = cropText(`Reply in ${await getPreferredLanguage()}.\n` + prompt)
229229
}
230230

231-
const position = { x: menuX, y: menuY }
231+
const position = data.useMenuPosition
232+
? { x: menuX, y: menuY }
233+
: { x: window.innerWidth / 2 - 300, y: window.innerHeight / 2 - 200 }
232234
const container = createElementAtPosition(position.x, position.y)
233235
container.className = 'chatgptbox-toolbar-container-not-queryable'
234236
render(

src/manifest.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"https://*.bing.com/"
1515
],
1616
"permissions": [
17+
"commands",
1718
"cookies",
1819
"storage",
1920
"contextMenus"
@@ -47,5 +48,21 @@
4748
"<all_urls>"
4849
]
4950
}
50-
]
51+
],
52+
"commands": {
53+
"newChat": {
54+
"suggested_key": {
55+
"default": "Ctrl+B",
56+
"mac": "MacCtrl+B"
57+
},
58+
"description": "Create a new chat"
59+
},
60+
"summarizePage": {
61+
"suggested_key": {
62+
"default": "Alt+B",
63+
"mac": "Alt+B"
64+
},
65+
"description": "Summarize this page"
66+
}
67+
}
5168
}

src/manifest.v2.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"128": "logo.png"
1111
},
1212
"permissions": [
13+
"commands",
1314
"cookies",
1415
"storage",
1516
"contextMenus",
@@ -40,5 +41,21 @@
4041
],
4142
"web_accessible_resources": [
4243
"*.png"
43-
]
44+
],
45+
"commands": {
46+
"newChat": {
47+
"suggested_key": {
48+
"default": "Ctrl+B",
49+
"mac": "MacCtrl+B"
50+
},
51+
"description": "Create a new chat"
52+
},
53+
"summarizePage": {
54+
"suggested_key": {
55+
"default": "Alt+B",
56+
"mac": "Alt+B"
57+
},
58+
"description": "Summarize this page"
59+
}
60+
}
4461
}

0 commit comments

Comments
 (0)