mirror of https://github.com/jlelse/GoBlog
Rewrite speak.js to make it smaller when compressed
This commit is contained in:
parent
561d656f81
commit
a1297e72c1
|
@ -1,37 +1,38 @@
|
||||||
"use strict";
|
(function () {
|
||||||
|
window.onbeforeunload = stopSpeak
|
||||||
|
|
||||||
let sb = document.getElementById('speakBtn')
|
let speakButton = query('#speakBtn')
|
||||||
let s = window.speechSynthesis
|
let speech = window.speechSynthesis
|
||||||
|
|
||||||
function gv() {
|
if (getVoice()) {
|
||||||
return s ? s.getVoices().filter(voice => voice.lang.startsWith(document.querySelector('html').lang))[0] : false
|
speakButton.classList.remove('hide')
|
||||||
}
|
speakButton.onclick = startSpeak
|
||||||
|
speakButton.textContent = speakButton.dataset.speak
|
||||||
function is() {
|
|
||||||
if (s) {
|
|
||||||
sb.classList.remove('hide')
|
|
||||||
sb.onclick = sp
|
|
||||||
sb.textContent = sb.dataset.speak
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function sp() {
|
function query(selector) {
|
||||||
sb.onclick = ssp
|
return document.querySelector(selector)
|
||||||
sb.textContent = sb.dataset.stopspeak
|
}
|
||||||
let ut = new SpeechSynthesisUtterance(
|
|
||||||
((document.querySelector('article .p-name')) ? document.querySelector('article .p-name').innerText + "\n\n" : '') + document.querySelector('article .e-content').innerText
|
|
||||||
)
|
|
||||||
ut.voice = gv()
|
|
||||||
ut.onerror = ssp
|
|
||||||
ut.onend = ssp
|
|
||||||
s.speak(ut)
|
|
||||||
}
|
|
||||||
|
|
||||||
function ssp() {
|
function getVoice() {
|
||||||
s.cancel()
|
return speech ? speech.getVoices().filter(voice => voice.lang.startsWith(query('html').lang))[0] : false
|
||||||
sb.onclick = sp
|
}
|
||||||
sb.textContent = sb.dataset.speak
|
|
||||||
}
|
|
||||||
|
|
||||||
window.onbeforeunload = ssp
|
function startSpeak() {
|
||||||
is()
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopSpeak() {
|
||||||
|
speech.cancel()
|
||||||
|
speakButton.onclick = startSpeak
|
||||||
|
speakButton.textContent = speakButton.dataset.speak
|
||||||
|
}
|
||||||
|
})()
|
Loading…
Reference in New Issue