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>
<script>
document.addEventListener('DOMContentLoaded', function() {
const speakButton = document.getElementById('speak-button');
const haikuText = document.getElementById('haiku-text');
// Check if speech synthesis is supported
if (!('speechSynthesis' in window)) {
speakButton.disabled = true;
speakButton.title = "Speech synthesis not supported in your browser";
speakButton.classList.add('opacity-50');
console.error("Speech synthesis not supported");
}
speakButton.addEventListener('click', function() {
// Get the haiku text without the <br> tags
const textContent = haikuText.innerText.trim();
// Create a new speech synthesis instance
const msg = new SpeechSynthesisUtterance();
msg.text = textContent;
msg.rate = 0.9; // Slightly slower for better haiku rhythm
msg.pitch = 1.0;
// Stop any ongoing speech
window.speechSynthesis.cancel();
// Start speaking
window.speechSynthesis.speak(msg);
// Visual feedback
speakButton.classList.add('bg-violet-800');
msg.onend = function() {
speakButton.classList.remove('bg-violet-800');
};
});
});
</script>
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 %}