@@ -35,6 +35,37 @@ <h1>The Voiceinator 5000</h1>
35
35
const options = document . querySelectorAll ( '[type="range"], [name="text"]' ) ;
36
36
const speakButton = document . querySelector ( '#speak' ) ;
37
37
const stopButton = document . querySelector ( '#stop' ) ;
38
+
39
+ msg . text = document . querySelector ( '[name="text"]' ) . value ;
40
+
41
+ function populateVoices ( ) {
42
+ voices = this . getVoices ( ) ;
43
+ const voiceOptions = voices
44
+ . filter ( voice => voice . lang . includes ( 'en' ) )
45
+ . map ( voice => `<option value="${ voice . name } ">${ voice . name } (${ voice . lang } )</option>` )
46
+ . join ( '' ) ;
47
+ voicesDropdown . innerHTML = voiceOptions ;
48
+ }
49
+ function setVoice ( ) {
50
+ msg . voice = voices . find ( voice => voice . name === this . value ) ;
51
+ toggle ( ) ;
52
+ }
53
+ function toggle ( startOver = true ) {
54
+ speechSynthesis . cancel ( ) ;
55
+ if ( startOver ) {
56
+ speechSynthesis . speak ( msg ) ;
57
+ }
58
+ }
59
+ function setOption ( e ) {
60
+ console . log ( this . name , this . value ) ;
61
+ msg [ this . name ] = this . value ;
62
+ toggle ( ) ;
63
+ }
64
+ speechSynthesis . addEventListener ( 'voiceschanged' , populateVoices ) ;
65
+ voicesDropdown . addEventListener ( 'change' , setVoice ) ;
66
+ options . forEach ( option => option . addEventListener ( 'change' , setOption ) ) ;
67
+ speakButton . addEventListener ( 'click' , toggle ) ;
68
+ stopButton . addEventListener ( 'click' , ( ) => toggle ( false ) ) ;
38
69
</ script >
39
70
40
71
</ body >
0 commit comments