2021-02-18 08:17:23 +00:00
|
|
|
(function () {
|
|
|
|
window.onbeforeunload = stopSpeak
|
2020-11-01 17:37:21 +00:00
|
|
|
|
2021-02-18 08:17:23 +00:00
|
|
|
let speakButton = query('#speakBtn')
|
|
|
|
let speech = window.speechSynthesis
|
2021-02-16 20:27:52 +00:00
|
|
|
|
2021-02-18 08:26:55 +00:00
|
|
|
if (speech) {
|
2021-02-18 08:17:23 +00:00
|
|
|
speakButton.onclick = startSpeak
|
|
|
|
speakButton.textContent = speakButton.dataset.speak
|
2021-09-07 20:16:28 +00:00
|
|
|
speakButton.classList.remove('hide')
|
2021-02-18 08:17:23 +00:00
|
|
|
}
|
2020-11-01 17:37:21 +00:00
|
|
|
|
2021-02-18 08:17:23 +00:00
|
|
|
function query(selector) {
|
|
|
|
return document.querySelector(selector)
|
2020-11-01 17:37:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 08:17:23 +00:00
|
|
|
function getVoice() {
|
|
|
|
return speech ? speech.getVoices().filter(voice => voice.lang.startsWith(query('html').lang))[0] : false
|
|
|
|
}
|
2020-11-01 17:37:21 +00:00
|
|
|
|
2021-02-18 08:17:23 +00:00
|
|
|
function startSpeak() {
|
|
|
|
speakButton.onclick = stopSpeak
|
|
|
|
speakButton.textContent = speakButton.dataset.stopspeak
|
|
|
|
let ut = new SpeechSynthesisUtterance(
|
|
|
|
((query('article .p-name')) ? query('article .p-name').innerText + "\n\n" : '') + query('article .e-content').innerText
|
|
|
|
)
|
|
|
|
ut.voice = getVoice()
|
|
|
|
ut.onerror = stopSpeak
|
|
|
|
ut.onend = stopSpeak
|
|
|
|
speech.speak(ut)
|
|
|
|
}
|
2020-11-01 17:37:21 +00:00
|
|
|
|
2021-02-18 08:17:23 +00:00
|
|
|
function stopSpeak() {
|
|
|
|
speech.cancel()
|
|
|
|
speakButton.onclick = startSpeak
|
|
|
|
speakButton.textContent = speakButton.dataset.speak
|
|
|
|
}
|
|
|
|
})()
|