-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (31 loc) · 1.26 KB
/
script.js
File metadata and controls
40 lines (31 loc) · 1.26 KB
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
// Query the page for elements
const micBtn = document.getElementById('microphone')
const panelsData = document.getElementById('panels-data')
const transcript = document.getElementById('transcript')
// Set a default SpeechRecognition browser library to use
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
// Build the speech recognition library
const recognition = new SpeechRecognition()
// When button clicked, show the available commands
function onStartListening() {
// Start listening to the microphone
recognition.start()
// Show the available commands
panelsData.classList.add('listening')
}
// When finished speaking, run this function
function onResult(event) {
// Hide the available commands
panelsData.classList.remove('listening')
// Get the words we spoke
const text = event.results[0][0].transcript
// Add what we spoke to the transcript HTML element for visibility
transcript.textContent = `You said: ${text}`
}
// If anything goes wrong (it shouldn't), let us know in the console
function onError(event) {
console.error(event.error)
}
micBtn.addEventListener('click', onStartListening)
recognition.addEventListener('result', onResult)
recognition.addEventListener('error', onError)