Offline Notepad View raw

Shared snapshot

script2

This JavaScript code is for creating a voice-controlled virtual assistant that can perform actions like greeting, opening websites, and telling the time or date based on voice commands. Let's break it down line by line:

1. Select DOM Elements:

let btn = document.querySelector("#btn");
let content = document.querySelector("#content");
let voice = document.querySelector("#voice");

2. speak() Function:

function speak(text) {
    let text_speak = new SpeechSynthesisUtterance(text);
    text_speak.rate = 1;
    text_speak.pitch = 2;
    text_speak.volume = 2;
    text_speak.lang = "en-GB";
    window.speechSynthesis.speak(text_speak);
}

This function converts text to speech:

3. wishMe() Function:

function wishMe() {
    if (!sessionStorage.getItem("greeted")) {
        let day = new Date();
        let hours = day.getHours();
        if (hours >= 0 && hours < 12) {
            speak("Good Morning, It's a pleasure to have you here!");
        } else if (hours >= 12 && hours < 16) {
            speak("Good afternoon, It's a pleasure to have you here!");
        } else {
            speak("Good Evening, It's a pleasure to have you here!");
        }
        sessionStorage.setItem("greeted", "true");
    }
}

This function greets the user based on the time of day:

4. window.addEventListener('load',()=>{ wishMe() })

5. Speech Recognition Setup:

let speechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
let recognition = new speechRecognition();

6. Handle Speech Recognition Results:

recognition.onresult = (event) => {
    let currentIndex = event.resultIndex;
    let transcript = event.results[currentIndex][0].transcript;
    content.innerText = "";
    takeCommand(transcript.toLowerCase());
}

7. Start Speech Recognition on Button Click:

btn1.addEventListener("click", () => {
    recognition.start();
    voice.style.display = "block";
    btn1.style.display = "none";
    btn2.style.display = "none";
});

8. Process Voice Command:

function takeCommand(message) {
    voice.style.display = "none";
    btn1.style.display = "flex";
    if (message.includes("hello") || message.includes("hey")) {
        speak("Hello!, How can I assist you?");
    } else if (message.includes("who are you")) {
        speak("I am a virtual assistant, created by Seema, Naila, and others.");
    } 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 facebook")) {
        speak("Opening Facebook...");
        window.open("https://facebook.com/", "_blank");
    } else if (message.includes("open instagram")) {
        speak("Opening Instagram...");
        window.open("https://instagram.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 finalText = "This is what I found on the internet regarding " + message.replace("shipra", "") || message.replace("shifra", "");
        speak(finalText);
        window.open(`https://www.google.com/search?q=${message.replace("shipra", "")}`, "_blank");
    }
    btn2.style.display = "flex";
}

This function processes the voice message (received as message) and performs actions based on it:

9. Show "Stop Listening" Button:

btn2.style.display = "flex";

Conclusion:

This script combines speech recognition and speech synthesis to create a virtual assistant that can listen for voice commands and respond with actions or spoken replies. The assistant can greet the user, open websites, and provide real-time information like time and date based on the user's input.