-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (39 loc) · 1.35 KB
/
app.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const stopBtn = document.querySelector("#stop-btn");
const speakBtn = document.querySelector("#speak-btn");
const msg = new SpeechSynthesisUtterance();
const voiceDropdown = document.querySelector("[name='voice']");
const options = document.querySelectorAll("[type = 'range'],[name='text'] ");
let voices = [];
msg.text = document.querySelector("[name='text']").value;
function populateVoices() {
voices = this.getVoices();
// console.log(voice);
const voiceOptions = voices
.map(v => `<option value ="${v.name}"> ${v.name} (${v.lang}) </option>`).join("")
// console.log(voiceOptions);
voiceDropdown.innerHTML = voiceOptions;
}
function setVoice() {
msg.voice = voices.find(v => v.name === this.value );
// console.log(msg.voice);
start_stop()
}
function start_stop (startOver = true) {
speechSynthesis.cancel();
if(startOver){
speechSynthesis.speak(msg);
}
}
function setOptions(){
console.log(this.name,this.value ) ;
msg[this.name] = this.value;
start_stop();
}
speechSynthesis.addEventListener("voiceschanged", populateVoices);
voiceDropdown.addEventListener("change",setVoice );
options.forEach(items =>items.addEventListener("change",setOptions ) );
speakBtn.addEventListener("click",start_stop );
stopBtn.addEventListener("click",() =>{
start_stop(false)
} )
// console.log(voices);