diff --git a/assets/js/speak.js b/assets/js/speak.js index 9503fc9..23bc120 100644 --- a/assets/js/speak.js +++ b/assets/js/speak.js @@ -1,13 +1,14 @@ "use strict"; -let synth = window.speechSynthesis; -let voice; -if (synth) { - voice = synth.getVoices().filter(voice => voice.lang.startsWith(document.querySelector('html').lang))[0]; +function getVoice() { + if (window.speechSynthesis) { + return window.speechSynthesis.getVoices().filter(voice => voice.lang.startsWith(document.querySelector('html').lang))[0]; + } + return false; } function initSpeak() { - if (voice) { + if (window.speechSynthesis) { let speakBtn = document.querySelector('#speakBtn'); speakBtn.style.display = ''; speakBtn.innerHTML = "Read to me, please."; @@ -17,19 +18,23 @@ function initSpeak() { function speak() { console.log("Start speaking") document.querySelector('#speakBtn').innerHTML = "Stop speaking!"; - 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); - utterThis.voice = voice; + utterThis.voice = getVoice(); utterThis.onerror = stopSpeak; utterThis.onend = stopSpeak; - synth.speak(utterThis); + window.speechSynthesis.speak(utterThis); } function stopSpeak() { console.log("Stop speaking") - synth.cancel(); + window.speechSynthesis.cancel(); document.querySelector('#speakBtn').innerHTML = "Read to me, please."; } -initSpeak(); -window.onbeforeunload = function () { stopSpeak(); } \ No newline at end of file +window.onbeforeunload = function () { + stopSpeak(); +} +initSpeak(); \ No newline at end of file