-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (53 loc) · 1.84 KB
/
app.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
53
54
55
56
57
58
59
/* eslint-disable no-unused-vars */
chrome.contextMenus.create({
id: `tauphahji-lookup`,
title: 'Liām hōo lín-pē thiann "%s" on 臺語媠聲',
contexts: ['selection'],
})
const SEPARATOR = '--'
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'tauphahji-lookup') {
const url =
'https://suisiann.ithuan.tw/講/' +
encodeURIComponent(info.selectionText)
chrome.tabs.create({ url: url, index: tab.index + 1 })
} else {
const [_, lang, code, name] = info.menuItemId.split(SEPARATOR)
speakIn(info.selectionText, code, name)
}
})
// https://stackoverflow.com/a/59786665
const allVoicesObtained = new Promise((resolve, reject) => {
let voices = chrome.tts.getVoices()
if (voices.length !== 0) {
resolve(voices)
} else {
chrome.tts.onVoicesChanged.addEventListener(() => {
voices = chrome.tts.getVoices()
resolve(voices)
})
}
})
// https://twitter.com/bcrypt/status/1500348547887079424
const speakIn = async (text, lang, name) => {
const voices = await allVoicesObtained
// to pick a new voice
// console.log('available voices')
// console.log(voices.filter((v) => v.lang === lang))
voice = voices.filter(
(v) => v.lang === lang && v.voiceName.includes(name),
)[0]
chrome.tts.speak(text, { lang: lang, voiceName: voice.voiceName })
}
;[
['English', 'en-US', 'Microsoft Aria Online'],
['日本語', 'ja-JP', 'Microsoft Nanami Online'],
['中文', 'zh-TW', 'Microsoft HsiaoChen Online'],
['廣東話', 'zh-HK', 'Microsoft HiuGaai Online'],
].forEach(([lang, code, name]) => {
chrome.contextMenus.create({
id: ['tauphahji-lookup', lang, code, name].join(SEPARATOR),
title: `Speak in ${lang}`,
contexts: ['selection'],
})
})