-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
47 lines (40 loc) · 1.3 KB
/
popup.js
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
41
42
43
44
45
46
47
const startBtn = document.getElementById('start-btn');
const stopBtn = document.getElementById('stop-btn');
const transcriptionDiv = document.getElementById('transcription');
let recognition;
if ('webkitSpeechRecognition' in window) {
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = 'en-US';
recognition.onstart = () => {
transcriptionDiv.textContent = "Listening...";
startBtn.disabled = true;
stopBtn.disabled = false;
};
recognition.onresult = (event) => {
let interimTranscription = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
transcriptionDiv.textContent += event.results[i][0].transcript;
} else {
interimTranscription += event.results[i][0].transcript;
}
}
transcriptionDiv.textContent = interimTranscription;
};
recognition.onerror = (event) => {
console.error("Error occurred in recognition: ", event.error);
};
recognition.onend = () => {
transcriptionDiv.textContent = "Press 'Start' and begin speaking...";
startBtn.disabled = false;
stopBtn.disabled = true;
};
}
startBtn.onclick = () => {
recognition.start();
};
stopBtn.onclick = () => {
recognition.stop();
};