Fix speak.js
This commit is contained in:
parent
ca7114a589
commit
d29c669422
|
@ -1,13 +1,14 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
let synth = window.speechSynthesis;
|
function getVoice() {
|
||||||
let voice;
|
if (window.speechSynthesis) {
|
||||||
if (synth) {
|
return window.speechSynthesis.getVoices().filter(voice => voice.lang.startsWith(document.querySelector('html').lang))[0];
|
||||||
voice = synth.getVoices().filter(voice => voice.lang.startsWith(document.querySelector('html').lang))[0];
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function initSpeak() {
|
function initSpeak() {
|
||||||
if (voice) {
|
if (window.speechSynthesis) {
|
||||||
let speakBtn = document.querySelector('#speakBtn');
|
let speakBtn = document.querySelector('#speakBtn');
|
||||||
speakBtn.style.display = '';
|
speakBtn.style.display = '';
|
||||||
speakBtn.innerHTML = "<a onclick=\"speak()\">Read to me, please.</a>";
|
speakBtn.innerHTML = "<a onclick=\"speak()\">Read to me, please.</a>";
|
||||||
|
@ -17,19 +18,23 @@ function initSpeak() {
|
||||||
function speak() {
|
function speak() {
|
||||||
console.log("Start speaking")
|
console.log("Start speaking")
|
||||||
document.querySelector('#speakBtn').innerHTML = "<a onclick=\"stopSpeak()\">Stop speaking!</a>";
|
document.querySelector('#speakBtn').innerHTML = "<a onclick=\"stopSpeak()\">Stop speaking!</a>";
|
||||||
let textContent = document.querySelector('.content').innerText;
|
let textContent =
|
||||||
|
((document.querySelector('article .post-title')) ? document.querySelector('article .post-title').innerText + "\n\n" : "")
|
||||||
|
+ document.querySelector('article .content').innerText;
|
||||||
let utterThis = new SpeechSynthesisUtterance(textContent);
|
let utterThis = new SpeechSynthesisUtterance(textContent);
|
||||||
utterThis.voice = voice;
|
utterThis.voice = getVoice();
|
||||||
utterThis.onerror = stopSpeak;
|
utterThis.onerror = stopSpeak;
|
||||||
utterThis.onend = stopSpeak;
|
utterThis.onend = stopSpeak;
|
||||||
synth.speak(utterThis);
|
window.speechSynthesis.speak(utterThis);
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopSpeak() {
|
function stopSpeak() {
|
||||||
console.log("Stop speaking")
|
console.log("Stop speaking")
|
||||||
synth.cancel();
|
window.speechSynthesis.cancel();
|
||||||
document.querySelector('#speakBtn').innerHTML = "<a onclick=\"speak()\">Read to me, please.</a>";
|
document.querySelector('#speakBtn').innerHTML = "<a onclick=\"speak()\">Read to me, please.</a>";
|
||||||
}
|
}
|
||||||
|
|
||||||
initSpeak();
|
window.onbeforeunload = function () {
|
||||||
window.onbeforeunload = function () { stopSpeak(); }
|
stopSpeak();
|
||||||
|
}
|
||||||
|
initSpeak();
|
Reference in New Issue