-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (31 loc) · 1.13 KB
/
script.js
File metadata and controls
45 lines (31 loc) · 1.13 KB
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
39
40
41
42
43
44
let speech = new SpeechSynthesisUtterance();
let voices = [];
let voiceSelect = document.querySelector("select");
function updateVoices() {
voices = window.speechSynthesis.getVoices();
speech.voice = voices[0];
voiceSelect.innerHTML = '';
voices.forEach((voice, i) => {
const option = new Option(voice.name, i);
voiceSelect.appendChild(option);
});
}
document.querySelector("button").addEventListener("click", () => {
const inputText = document.querySelector("textarea").value;
const russianRegex = /[а-яА-Я]/;
if (russianRegex.test(inputText) && speech.voice.lang !== 'ru-RU') {
alert("Incorrect language format...");
} else {
speech.text = inputText;
window.speechSynthesis.speak(speech);
}
});
window.speechSynthesis.addEventListener('voiceschanged', updateVoices);
updateVoices();
voiceSelect.addEventListener("change", () => {
speech.voice = voices[voiceSelect.value];
});
document.querySelector("button").addEventListener("click", () => {
speech.text = document.querySelector("textarea").value;
window.speechSynthesis.speak(speech);
});