11<!DOCTYPE html>
22< html lang ="en ">
3+
34< head >
45 < meta charset ="UTF-8 ">
56 < title > Speech Detection</ title >
67</ head >
8+
79< body >
810
911 < div class ="words " contenteditable >
1012 </ div >
1113
12- < script >
13- window . SpeechRecognition = window . SpeechRecognition || window . webkitSpeechRecognition ;
14+ < script >
15+ window . SpeechRecognition = window . SpeechRecognition || window . webkitSpeechRecognition ;
16+ const recognition = new SpeechRecognition ;
17+ recognition . interimResults = true ;
1418
19+ let p = document . createElement ( 'p' ) ;
20+ const words = document . querySelector ( '.words' ) ;
21+ words . appendChild ( p ) ;
22+
23+ recognition . addEventListener ( 'result' , e => {
24+ const transcript = Array . from ( e . results )
25+ . map ( result => result [ 0 ] )
26+ . map ( result => result . transcript )
27+ . join ( '' ) ;
28+ p . textContent = transcript ;
29+ if ( e . results [ 0 ] . isFinal ) {
30+ p = document . createElement ( 'p' ) ;
31+ words . appendChild ( p ) ;
32+ }
33+ console . log ( transcript )
34+ } ) ;
35+ recognition . addEventListener ( 'end' , recognition . start ) ;
36+ recognition . start ( ) ;
37+
1538
16- </ script >
39+ </ script >
1740
1841
1942 < style >
3356 margin : 50px auto;
3457 background : white;
3558 border-radius : 5px ;
36- box-shadow : 10px 10px 0 rgba (0 , 0 , 0 , 0.1 );
59+ box-shadow : 10px 10px 0 rgba (0 , 0 , 0 , 0.1 );
3760 padding : 1rem 2rem 1rem 5rem ;
3861 background : -webkit-gradient (linear, 0 0 , 0 100% , from (# d9eaf3 ), color-stop (4% , # fff )) 0 4px ;
3962 background-size : 100% 3rem ;
4063 position : relative;
4164 line-height : 3rem ;
4265 }
43-
66+
4467 p {
4568 margin : 0 0 3rem ;
4669 }
5881 </ style >
5982
6083</ body >
61- </ html >
84+
85+ </ html >
0 commit comments