forked from Sivamani1611/MSPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
120 lines (109 loc) · 3.59 KB
/
content.js
File metadata and controls
120 lines (109 loc) · 3.59 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const statusEl = document.getElementById("mspl_status_page");
const inputEl = document.getElementById("mspl_homepage");
function speak(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = "en-US";
speechSynthesis.speak(utterance);
}
navigator.getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
navigator.getUserMedia(
{ audio: true },
(stream) => {
function closeWindow() {
if (inputEl && inputEl.value === "mspl_homepage") {
window.close();
}
}
function statusChanger(text) {
if (statusEl) {
statusEl.innerText = text;
}
console.log(text);
}
speak("Voice activated Say start mspl");
statusChanger("Status: Voice Activated");
const SpeechRecognition =
window.SpeechRecognition ||
window.webkitSpeechRecognition ||
window.mozSpeechRecognition ||
window.msSpeechRecognition ||
window.oSpeechRecognition;
let activatedVoice = false;
let speechResult = "";
const recognition = new SpeechRecognition();
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.start();
recognition.onresult = (event) => {
speechResult = event.results[0][0].transcript;
console.log(speechResult);
};
recognition.onend = async () => {
if (
speechResult.toLowerCase().slice(0, 10) === "start mspl" &&
!activatedVoice
) {
activatedVoice = true;
speak("Welcome to the mspl-voice assistant. How can I assist you?");
statusChanger("Status: Started MSPL Voice-Assistant");
} else if (
speechResult.toLowerCase().slice(0, 10) === "start mspl" &&
activatedVoice
) {
speak(
"mspl-voice assistant is already activated. How can I assist you?"
);
statusChanger("Status: Started MSPL Voice-Assistant");
} else if (
(speechResult.toLowerCase().slice(0, 9) === "stop mspl" ||
speechResult.toLowerCase().slice(0, 8) === "end mspl") &&
!activatedVoice
) {
speak("mspl-voice assistant is already terminated.");
statusChanger("Status: MSPL Voice-Assistant Stopped.");
} else if (
(speechResult.toLowerCase().slice(0, 9) === "stop mspl" ||
speechResult.toLowerCase().slice(0, 8) === "end mspl") &&
activatedVoice
) {
activatedVoice = false;
speak("mspl-voice assistant is stopped, Say start mspl");
statusChanger("Status: MSPL Voice-Assistant Stopped.");
} else if (
speechResult.includes("stop") ||
speechResult.includes("exit") ||
speechResult.includes("terminate")
) {
activatedVoice = false;
speak("Voice terminated.");
statusChanger("Status: Voice Terminated.");
stream.getTracks().forEach((track) => track.stop());
closeWindow();
return;
} else if (
speechResult.toLowerCase().slice(0, 4) === "open" &&
activatedVoice
) {
const command = await chrome.runtime.sendMessage({
text: speechResult.split("open ")[1],
});
if (command && command.message) {
speak(command.message);
speechResult = "";
}
}
speechResult = "";
recognition.start();
};
recognition.onerror = (event) => {
console.log(event.error);
};
},
(error) => {
console.log(error);
}
);