-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-of-sentences-common.js
107 lines (83 loc) · 3.87 KB
/
list-of-sentences-common.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// import { TongWen } from '/home/srghma/projects/anki-cards-from-pdf/scripts/lib/TongWen'
const TongWen = require('/home/srghma/projects/anki-cards-from-pdf/scripts/lib/TongWen').TongWen
const getAudioGoogleUrl = text => {
return `https://translate.google.com/translate_tts?ie=UTF-8&q=${encodeURIComponent(text)}&tl=zh&total=1&idx=0&textlen=${text.length}&client=tw-ob&prev=input&ttsspeed=0.24`
};
const getAudioBaiduUrl = text => {
// return `https://tts.baidu.com/text2audio?cuid=baike&lan=ZH&ctp=1&pdt=301&vol=9&rate=32&per=4&tex=${encodeURIComponent(text)}`
return `https://fanyi.baidu.com/gettts?lan=zh&text=${encodeURIComponent(text)}&spd=2&source=web`
};
function isHanzi(ch) {
const REGEX_JAPANESE = /[\u3000-\u303f]|[\u3040-\u309f]|[\u30a0-\u30ff]|[\uff00-\uff9f]|[\u4e00-\u9faf]|[\u3400-\u4dbf]/
const REGEX_CHINESE = /[\u4e00-\u9fff]|[\u3400-\u4dbf]|[\u{20000}-\u{2a6df}]|[\u{2a700}-\u{2b73f}]|[\u{2b740}-\u{2b81f}]|[\u{2b820}-\u{2ceaf}]|[\uf900-\ufaff]|[\u3300-\u33ff]|[\ufe30-\ufe4f]|[\uf900-\ufaff]|[\u{2f800}-\u{2fa1f}]/u;
const isSpecialChar = "。?!".includes(ch)
const isJapanese = REGEX_JAPANESE.test(ch)
const isChinese = REGEX_CHINESE.test(ch)
const isHanzi = !isSpecialChar && (isJapanese || isChinese)
return isHanzi
}
const getCurrentSentenceTextContent = () => document.getElementById('currentSentence').textContent
////////////
document.addEventListener("DOMContentLoaded", function(){
const board = new Board({id: "app", background: '#000'})
board.setSize(2)
board.setColor('#fff')
let currentlySelectedElement = null
const googleAudioEl = document.getElementById('google-tts-audio')
const baiduAudioEl = document.getElementById('baidu-tts-audio')
document.getElementById("body").addEventListener('click', function(event) {
event.preventDefault()
let element = event.target
// console.log(element)
if (element.tagName !== 'SENTENCE') {
const parent = element.parentElement
if (parent.tagName === 'SENTENCE') {
element = parent
} else {
return
}
}
if (element === currentlySelectedElement) {
baiduAudioEl.play()
return
}
currentlySelectedElement = element
const text = currentlySelectedElement.textContent
const simplifiedText = [...text].map(x => TongWen.t_2_s[x] || x).join('')
const traditionalText = [...text].map(x => TongWen.s_2_t[x] || x).join('')
;(async function() {
// const array = await fetch('/list-of-known-hanzi').then(x => x.json())
const array = await allHanziAnkiInfoPromise().then(x => Object.keys(x))
const setOfKnownHanzi = new Set(array)
function addLinks(text) {
const colorizer = ch => {
const isKnown = setOfKnownHanzi.has(ch)
const linkclass = isKnown ? ` class="known-hanzi"` : ''
return `<a target="_blank" href="../h.html#${ch}"${linkclass}>${ch}</a>`
}
return [...text].map(ch => isHanzi(ch) ? colorizer(ch) : ch).join('')
}
document.getElementById('currentSentence').innerHTML = addLinks(simplifiedText)
document.getElementById('currentSentenceTraditional').innerHTML = addLinks(traditionalText)
})();
googleAudioEl.src = getAudioGoogleUrl(text)
googleAudioEl.load()
baiduAudioEl.src = getAudioBaiduUrl(text)
baiduAudioEl.load()
baiduAudioEl.play().catch(error => {
console.error(error)
googleAudioEl.play()
})
baiduAudioEl.addEventListener('ended', (event) => {
googleAudioEl.play()
}, { once: true })
}, false);
document.getElementById('pleco').addEventListener('click', function(event) {
event.preventDefault()
window.open(`plecoapi://x-callback-url/s?q=${encodeURIComponent(getCurrentSentenceTextContent())}`, '_blank')
}, false);
document.getElementById('clear-canvas').addEventListener('click', function(event) {
event.preventDefault()
board.clear()
}, false);
});