forked from heygsc/word-wind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpronunciation.js
38 lines (33 loc) · 1.23 KB
/
pronunciation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// pronunciation.js
document.addEventListener('DOMContentLoaded', function() {
const pronounceButton = document.getElementById('pronounceButton');
const wordElement = document.getElementById('map0');
pronounceButton.addEventListener('click', function() {
const word = wordElement.textContent.trim();
if (word) {
speak(word);
}
});
function speak(text) {
if ('speechSynthesis' in window) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = 'en-US'; // Set language to English
utterance.rate = 0.8; // Slightly slower speed
speechSynthesis.speak(utterance);
} else {
console.log('Text-to-speech not supported in this browser.');
}
}
// Automatically pronounce the word when it changes
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
const newWord = wordElement.textContent.trim();
if (newWord) {
speak(newWord);
}
}
});
});
observer.observe(wordElement, { childList: true });
});