Visitar URL original
Added Speech Detection (#20) · JeffDess/JavaScript30@b7cc4b9 · GitHub
Skip to content

Commit b7cc4b9

Browse files
committed
Added Speech Detection (wesbos#20)
1 parent 8e8caa1 commit b7cc4b9

1 file changed

Lines changed: 43 additions & 15 deletions

File tree

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,76 @@
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+
17+
const recog = new SpeechRecognition();
18+
recog.interimResults = true;
19+
20+
let p = document.createElement('p');
21+
const words = document.querySelector('.words');
22+
words.appendChild(p);
23+
24+
recog.addEventListener('result', e => {
25+
const transcript = [...e.results]
26+
.map(r => r[0])
27+
.map(r => r.transcript)
28+
.join('');
1429

30+
p.textContent = transcript;
31+
if (e.results[0].isFinal) {
32+
p = document.createElement('p');
33+
words.appendChild(p);
34+
}
35+
if (transcript.includes('get the weather')) {
36+
console.log('Getting the weather...');
37+
}
38+
});
1539

16-
</script>
40+
recog.addEventListener('end', recog.start);
41+
recog.start();
42+
</script>
1743

1844

1945
<style>
2046
html {
2147
font-size: 10px;
2248
}
23-
49+
2450
body {
25-
background:#ffc600;
51+
background: #ffc600;
2652
font-family: 'helvetica neue';
2753
font-weight: 200;
2854
font-size: 20px;
2955
}
30-
56+
3157
.words {
32-
max-width:500px;
33-
margin:50px auto;
34-
background:white;
35-
border-radius:5px;
36-
box-shadow:10px 10px 0 rgba(0,0,0,0.1);
37-
padding:1rem 2rem 1rem 5rem;
58+
max-width: 500px;
59+
margin: 50px auto;
60+
background: white;
61+
border-radius: 5px;
62+
box-shadow: 10px 10px 0 rgba(0, 0, 0, 0.1);
63+
padding: 1rem 2rem 1rem 5rem;
3864
background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
3965
background-size: 100% 3rem;
4066
position: relative;
41-
line-height:3rem;
67+
line-height: 3rem;
4268
}
69+
4370
p {
4471
margin: 0 0 3rem;
4572
}
46-
73+
4774
.words:before {
4875
content: '';
4976
position: absolute;
@@ -57,4 +84,5 @@
5784
</style>
5885

5986
</body>
60-
</html>
87+
88+
</html>

0 commit comments

Comments
 (0)