-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (103 loc) · 3.59 KB
/
Copy pathscript.js
File metadata and controls
136 lines (103 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
let btn=document.querySelector("#btn")
let content=document.querySelector("#content")
let voice=document.querySelector("#voice")
// A function to speak by converting text into speech using Web speech AI
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)
}
// A function to greet the user
function wishMe(){
//Fetching the current time using the date object
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")
}
}
// speech recognition support from window or chrome
let speechRecognition= window.SpeechRecognition || window.webkitSpeechRecognition
let recognition =new speechRecognition()
//Event listener for when speech recognition detects a result
recognition.onresult=(event)=>{
let currentIndex=event.resultIndex
//extracting the transcript (spoken words) from the recognition result
let transcript=event.results[currentIndex][0].transcript
//display of recognized speech
content.innerText=transcript
takeCommand(transcript.toLowerCase())
}
// Event listener to start speech recognition when button is clicked
btn.addEventListener("click",()=>{
recognition.start()
voice.style.display="block"
btn.style.display="none"
})
//Function to process user commands based on the recognized speech
function takeCommand(message){
voice.style.display="none"
btn.style.display="flex"
//respond to greeting
if(message.includes("hello")||message.includes("hey")){
speak("hello sir,how can i help you?")
}
//respond to assisstant identity
else if(message.includes("who are you")){
speak("i am virtual assistant ,created by Khushi")
}
//open youtube
else if(message.includes("open youtube")){
speak("opening youtube...")
window.open("https://youtube.com/","_blank")
}
//open google
else if(message.includes("open google")){
speak("opening google...")
window.open("https://google.com/","_blank")
}
//open facebook
else if(message.includes("open facebook")){
speak("opening facebook...")
window.open("https://facebook.com/","_blank")
}
//open instagram
else if(message.includes("open instagram")){
speak("opening instagram...")
window.open("https://instagram.com/","_blank")
}
//open calculator(if supported by the system or browser)
else if(message.includes("open calculator")){
speak("opening calculator..")
window.open("calculator://")
}
//open whatsapp(if supported by the system or browser)
else if(message.includes("open whatsapp")){
speak("opening whatsapp..")
window.open("whatsapp://")
}
// current time response
else if(message.includes("time")){
let time=new Date().toLocaleString(undefined,{hour:"numeric",minute:"numeric"})
speak(time)
}
// current date response
else if(message.includes("date")){
let date=new Date().toLocaleString(undefined,{day:"numeric",month:"short"})
speak(date)
}
//default response
else{
let finalText="this is what i found on internet regarding" + message.replace("googly","") || message.replace("googly","")
speak(finalText)
window.open(`https://www.google.com/search?q=${message.replace("googly","")}`,"_blank")
}
}