-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
52 lines (40 loc) · 1.27 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { app } = require('electron')
const { clipboard } = require('electron')
const { globalShortcut } = require('electron')
const { Notification } = require('electron')
const axios = require('axios')
const setting = require('./setting')
let notification = null
let apiKey = null
function accessClipboard () {
// Allow selection is only available on Linux,
// this API fallback to clipboard on Windows automatically.
return clipboard.readText('selection')
}
async function translate (unknown, code, key) {
const url = 'https://translation.googleapis.com/language/translate/v2' +
`?q=${unknown}&target=${code}&key=${key}`
const response = await axios.get(url)
const body = response.data
return body.data.translations[0].translatedText
}
function showText (text) {
notification.body = text
notification.show()
}
async function launchTranslationJob () {
const unknownText = accessClipboard()
const translatedText = await translate(unknownText, 'zh-TW', apiKey)
showText(translatedText)
}
async function run () {
notification = new Notification({
title: 'Translation'
})
apiKey = process.env.API_KEY || await setting.getApiKey()
globalShortcut.register('Alt+T', launchTranslationJob)
}
async function main () {
await run()
}
app.whenReady().then(main)