mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39:29 +00:00
fix: potential fix for the speech synthesis
Refs: OPS-56
This commit is contained in:
parent
b2cf4a0a72
commit
6baaee8d9a
1 changed files with 31 additions and 36 deletions
|
|
@ -29,36 +29,31 @@
|
||||||
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) {
|
||||||
|
msg.voice = voices[0]; // Use the first available voice
|
||||||
|
} else {
|
||||||
|
console.warn("No voices available!");
|
||||||
}
|
}
|
||||||
|
|
||||||
speakButton.addEventListener('click', function() {
|
window.speechSynthesis.cancel(); // Stop any previous speech
|
||||||
// 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);
|
window.speechSynthesis.speak(msg);
|
||||||
|
}
|
||||||
|
|
||||||
// Visual feedback
|
if ('speechSynthesis' in window) {
|
||||||
speakButton.classList.add('bg-violet-800');
|
speechSynthesis.onvoiceschanged = () => {
|
||||||
msg.onend = function() {
|
console.log("Voices loaded:", speechSynthesis.getVoices());
|
||||||
speakButton.classList.remove('bg-violet-800');
|
speakButton.addEventListener('click', speakText);
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
speakButton.style.display = 'none';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue