Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit ccf87a5

Browse files
committed
feat: add context menus
1 parent c26cf83 commit ccf87a5

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/manifest.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"name": "A Translator",
3-
"permissions": ["http://*/*", "https://*/*", "storage", "activeTab"],
3+
"permissions": [
4+
"http://*/*",
5+
"https://*/*",
6+
"storage",
7+
"activeTab",
8+
"contextMenus"
9+
],
410
"options_ui": {
511
"page": "options.html",
612
"chrome_style": false,

src/pages/Background/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ const toggleOCR = (): void => {
3535
})
3636
}
3737

38+
const translateText = (text: string): void => {
39+
cc(chrome.tabs, 'query', { active: true, currentWindow: true })
40+
.then((tabs: chrome.tabs.Tab[]) => {
41+
const client = createClient(tabs[0].id)
42+
43+
client.send('translate_text', {
44+
text,
45+
})
46+
})
47+
.catch((err) => {
48+
logger.error({
49+
err,
50+
})
51+
})
52+
}
53+
3854
chrome.browserAction.onClicked.addListener(openExtension)
3955

4056
chrome.commands.onCommand.addListener(function (command) {
@@ -49,3 +65,38 @@ chrome.commands.onCommand.addListener(function (command) {
4965
// no default
5066
}
5167
})
68+
69+
chrome.contextMenus.create({
70+
id: 'ate',
71+
title: 'A Translator',
72+
contexts: ['page'],
73+
})
74+
75+
chrome.contextMenus.create({
76+
id: 'ate-open_application',
77+
parentId: 'ate',
78+
title: 'Open application',
79+
onclick() {
80+
openExtension()
81+
},
82+
})
83+
84+
chrome.contextMenus.create({
85+
id: 'ate-toggle_ocr',
86+
parentId: 'ate',
87+
title: 'Toggle OCR',
88+
onclick() {
89+
toggleOCR()
90+
},
91+
})
92+
93+
chrome.contextMenus.create({
94+
id: 'ate-translate_selection',
95+
contexts: ['selection'],
96+
title: 'Translate selection',
97+
onclick(payload) {
98+
if (payload.selectionText) {
99+
translateText(payload.selectionText)
100+
}
101+
},
102+
})

src/pages/Content/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ const attachListeners = () => {
181181
}, 50)
182182
}
183183
})
184+
185+
client.on('translate_text', (payload: { text: string }) => {
186+
initApp()
187+
translationStack.push({
188+
type: 'translate',
189+
id: uuid(),
190+
text: payload.text,
191+
})
192+
})
184193
})
185194
}
186195

0 commit comments

Comments
 (0)