-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (44 loc) · 1.26 KB
/
script.js
File metadata and controls
53 lines (44 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
41
42
43
44
45
46
47
48
49
50
51
52
53
const warning = document.getElementById("warning");
const toggleButton = document.getElementById("toggle-button");
const toggleLanguage = document.getElementById("toggle-language");
let recognition = {},
isRecognizing;
try {
recognition = new webkitSpeechRecognition();
} catch {
warning.textContent = "Please use a chrome-based browser on laptop/desktop";
}
const initialOptions = {
lang: "id-ID",
continuous: true,
interimResults: true,
onend: reset(),
};
Object.assign(recognition, initialOptions);
recognition.onresult = function (event) {
for (var i = event.resultIndex; i < event.results.length; ++i) {
//only display the final transcript
textarea.value = [...event.results].map((result) => result[0].transcript);
}
};
toggleLanguage.onchange = function (event) {
const language = event.target.value;
recognition.lang = language;
toggleStartStop();
};
function reset() {
isRecognizing = false;
toggleButton.textContent = "Click to Speak";
}
function toggleStartStop() {
if (isRecognizing) {
toggleButton.classList.remove("is-active");
recognition.stop();
reset();
} else {
toggleButton.classList.add("is-active");
recognition.start();
isRecognizing = true;
toggleButton.textContent = "Click to Stop";
}
}