diff --git a/senju/templates/haiku.html b/senju/templates/haiku.html index 10cae42..a259f8f 100644 --- a/senju/templates/haiku.html +++ b/senju/templates/haiku.html @@ -25,40 +25,35 @@ + document.addEventListener('DOMContentLoaded', function () { + const speakButton = document.getElementById('speak-button'); + const haikuText = document.getElementById('haiku-text'); + + function speakText() { + const textContent = haikuText.innerText.trim(); + console.log("Speaking text:", textContent); + + const msg = new SpeechSynthesisUtterance(textContent); + const voices = window.speechSynthesis.getVoices(); + + if (voices.length > 0) { + msg.voice = voices[0]; // Use the first available voice + } else { + console.warn("No voices available!"); + } + + window.speechSynthesis.cancel(); // Stop any previous speech + window.speechSynthesis.speak(msg); + } + + if ('speechSynthesis' in window) { + speechSynthesis.onvoiceschanged = () => { + console.log("Voices loaded:", speechSynthesis.getVoices()); + speakButton.addEventListener('click', speakText); + }; + } else { + speakButton.style.display = 'none'; + } +}); + {% endblock %}