-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (83 loc) · 2.77 KB
/
Copy pathscript.js
File metadata and controls
92 lines (83 loc) · 2.77 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
let btn = document.querySelector("#btn");
let content = document.querySelector("#content");
let voice = document.querySelector("#voice");
function speak(text) {
let text_speak = new SpeechSynthesisUtterance(text);
text_speak.rate = 1;
text_speak.pitch = 1;
text_speak.volume = 1;
text_speak.lang = "en-GB";
window.speechSynthesis.speak(text_speak);
}
function wishMe() {
let day = new Date();
let hours = day.getHours();
if (hours >= 0 && hours < 12) {
speak("Good Morning Sir");
}
else if (hours >= 12 && hours < 16) {
speak("Good Afternoon Sir");
}
else {
speak("Good Evening Sir");
}
}
let SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
let recognition = new SpeechRecognition();
recognition.continuous = false;
recognition.interimResults = false;
recognition.onresult = (event) => {
let currentIndex = event.resultIndex;
let transcript = event.results[currentIndex][0].transcript;
content.innerText = transcript;
takeCommand(transcript.toLowerCase());
};
recognition.onend = () => {
btn.style.display = "flex";
voice.style.display = "none";
};
btn.addEventListener("click", () => {
recognition.start();
btn.style.display = "none";
voice.style.display = "block";
});
function takeCommand(message) {
btn.style.display = "flex";
voice.style.display = "none";
if (message.includes("hello") || message.includes("hey")) {
speak("Hello sir, what can I help you with?");
}
else if (message.includes("who are you")) {
speak("I am your virtual assistant, created by Aditi Srivastava.");
}
else if (message.includes("open youtube")) {
speak("Opening YouTube...");
window.open("https://youtube.com/", "_blank");
}
else if (message.includes("open google")) {
speak("Opening Google...");
window.open("https://google.com/", "_blank");
}
else if (message.includes("open calculator")) {
speak("Opening calculator...");
window.open("calculator://");
}
else if (message.includes("open whatsapp")) {
speak("Opening WhatsApp...");
window.open("whatsapp://");
}
else if (message.includes("time")) {
let time = new Date().toLocaleString(undefined, { hour: "numeric", minute: "numeric" });
speak(time);
}
else if (message.includes("date")) {
let date = new Date().toLocaleString(undefined, { day: "numeric", month: "short" });
speak(date);
}
else {
let query = message.replace("shipra", "").replace("shifra", "");
let finalText = "This is what I found on the internet regarding " + query;
speak(finalText);
window.open(`https://www.bing.com/search?q=${query}`, "_blank");
}
}