Skip to content

Commit bbc632f

Browse files
committed
Proj 20: finish.
1 parent 2e57a92 commit bbc632f

File tree

1 file changed

+68
-41
lines changed

1 file changed

+68
-41
lines changed
Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,87 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
5-
<title>Speech Detection</title>
4+
<meta charset="UTF-8">
5+
<title>Speech Detection</title>
66
</head>
77
<body>
88

9-
<div class="words" contenteditable>
10-
</div>
9+
<div class="words" contenteditable></div>
1110

1211
<script>
13-
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
1412

13+
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
1514

16-
</script>
15+
const recog = new SpeechRecognition();
16+
recog.interimResults = true;
1717

18+
let p = document.createElement("p");
19+
const words = document.querySelector(".words");
20+
words.appendChild(p);
1821

19-
<style>
20-
html {
21-
font-size: 10px;
22+
recog.addEventListener("result", e => {
23+
// console.log(e.results);
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);
2232
}
33+
console.log(transcript);
34+
})
2335

24-
body {
25-
background:#ffc600;
26-
font-family: 'helvetica neue';
27-
font-weight: 200;
28-
font-size: 20px;
29-
}
36+
recog.addEventListener("end", recog.start);
3037

31-
.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;
38-
background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
39-
background-size: 100% 3rem;
40-
position: relative;
41-
line-height:3rem;
42-
}
43-
p {
44-
margin: 0 0 3rem 0;
45-
}
38+
recog.start();
4639

47-
.words:before {
48-
content: '';
49-
position: absolute;
50-
width: 4px;
51-
top: 0;
52-
left: 30px;
53-
bottom: 0;
54-
border: 1px solid;
55-
border-color: transparent #efe4e4;
56-
}
57-
</style>
40+
</script>
41+
42+
43+
<style>
44+
45+
html {
46+
font-size: 10px;
47+
}
48+
49+
body {
50+
background:#ffc600;
51+
font-family: 'helvetica neue';
52+
font-weight: 200;
53+
font-size: 20px;
54+
}
55+
56+
.words {
57+
max-width:500px;
58+
margin:50px auto;
59+
background:white;
60+
border-radius:5px;
61+
box-shadow:10px 10px 0 rgba(0,0,0,0.1);
62+
padding:1rem 2rem 1rem 5rem;
63+
background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
64+
background-size: 100% 3rem;
65+
position: relative;
66+
line-height:3rem;
67+
}
68+
69+
p {
70+
margin: 0 0 3rem 0;
71+
}
72+
73+
.words:before {
74+
content: '';
75+
position: absolute;
76+
width: 4px;
77+
top: 0;
78+
left: 30px;
79+
bottom: 0;
80+
border: 1px solid;
81+
border-color: transparent #efe4e4;
82+
}
83+
84+
</style>
5885

5986
</body>
6087
</html>

0 commit comments

Comments
 (0)