-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (100 loc) · 3.71 KB
/
app.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
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
121
122
123
124
125
126
127
const btn = document.querySelector('.talk');
const content = document.querySelector('.content');
function speak(sentence) {
const text_speak = new SpeechSynthesisUtterance(sentence);
text_speak.rate = 1;
text_speak.pitch = 1;
window.speechSynthesis.speak(text_speak);
}
function wishMe() {
var day = new Date();
var hr = day.getHours();
if(hr >= 0 && hr < 12) {
speak("Good Morning Boss");
}
else if(hr == 12) {
speak("Good noon Boss");
}
else if(hr > 12 && hr <= 17) {
speak("Good Afternoon Boss");
}
else {
speak("Good Evening Boss");
}
}
window.addEventListener('load', ()=>{
speak("Activating Jarvis");
speak("Going online");
wishMe();
})
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.onresult = (event) => {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
content.textContent = transcript;
speakThis(transcript.toLowerCase());
}
btn.addEventListener('click', ()=>{
recognition.start();
})
function speakThis(message) {
const speech = new SpeechSynthesisUtterance();
speech.text = "I did not understand what you said please try again";
if(message.includes('hey') || message.includes('hello')||message.includes('hi')) {
const finalText = "Hello Boss";
speech.text = finalText;
}
else if(message.includes('how are you')) {
const finalText = "I am fine boss Always Ready To Go";
speech.text = finalText;
}
else if(message.includes('name')) {
const finalText = "My name is Jarvis";
speech.text = finalText;
}
else if(message.includes('open google')) {
window.open("https://google.com", "_blank");
const finalText = "Opening Google";
speech.text = finalText;
}
else if(message.includes('open instagram')) {
window.open("https://instagram.com", "_blank");
const finalText = "Opening instagram";
speech.text = finalText;
}
else if(message.includes('what is') || message.includes('who is') || message.includes('what are')) {
window.open(`https://www.google.com/search?q=${message.replace(" ", "+")}`, "_blank");
const finalText = "This are desired result for " + message;
speech.text = finalText;
}
else if(message.includes('wikipedia')||message.includes('wiki')) {
window.open(`https://en.wikipedia.org/wiki/${message.replace("wikipedia", "")}`, "_blank");
const finalText = "This is what i found on wikipedia regarding " + message;
speech.text = finalText;
}
else if(message.includes('time')) {
const time = new Date().toLocaleString(undefined, {hour: "numeric", minute: "numeric"})
const finalText = time;
speech.text = finalText;
}
else if(message.includes('date')) {
const date = new Date().toLocaleString(undefined, {month: "short", day: "numeric"})
const finalText = date;
speech.text = finalText;
}
else if(message.includes('calculator')) {
window.open('Calculator:///')
const finalText = "Opening Calculator";
speech.text = finalText;
}
else {
window.open(`https://www.google.com/search?q=${message.replace(" ", "+")}`, "_blank");
const finalText = "I found some information for " + message + " on google";
speech.text = finalText;
}
speech.volume = 1;
speech.pitch = 10;
speech.rate = 1;
window.speechSynthesis.speak(speech);
}