fix: potential fix for the speech synthesis

Refs: OPS-56
This commit is contained in:
0xjrx 2025-03-21 14:16:32 +01:00
parent b2cf4a0a72
commit 6baaee8d9a
No known key found for this signature in database
GPG key ID: 61C53033867D0271

View file

@ -25,40 +25,35 @@
</div> </div>
</div> </div>
<script> <script>
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function () {
const speakButton = document.getElementById('speak-button'); const speakButton = document.getElementById('speak-button');
const haikuText = document.getElementById('haiku-text'); const haikuText = document.getElementById('haiku-text');
// Check if speech synthesis is supported function speakText() {
if (!('speechSynthesis' in window)) { const textContent = haikuText.innerText.trim();
speakButton.disabled = true; console.log("Speaking text:", textContent);
speakButton.title = "Speech synthesis not supported in your browser";
speakButton.classList.add('opacity-50'); const msg = new SpeechSynthesisUtterance(textContent);
console.error("Speech synthesis not supported"); const voices = window.speechSynthesis.getVoices();
}
if (voices.length > 0) {
speakButton.addEventListener('click', function() { msg.voice = voices[0]; // Use the first available voice
// Get the haiku text without the <br> tags } else {
const textContent = haikuText.innerText.trim(); console.warn("No voices available!");
}
// Create a new speech synthesis instance
const msg = new SpeechSynthesisUtterance(); window.speechSynthesis.cancel(); // Stop any previous speech
msg.text = textContent; window.speechSynthesis.speak(msg);
msg.rate = 0.9; // Slightly slower for better haiku rhythm }
msg.pitch = 1.0;
if ('speechSynthesis' in window) {
// Stop any ongoing speech speechSynthesis.onvoiceschanged = () => {
window.speechSynthesis.cancel(); console.log("Voices loaded:", speechSynthesis.getVoices());
speakButton.addEventListener('click', speakText);
// Start speaking };
window.speechSynthesis.speak(msg); } else {
speakButton.style.display = 'none';
// Visual feedback }
speakButton.classList.add('bg-violet-800'); });
msg.onend = function() {
speakButton.classList.remove('bg-violet-800');
};
});
});
</script>
{% endblock %} {% endblock %}