Update script.js
Browse files
script.js
CHANGED
|
@@ -58,49 +58,49 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
| 58 |
|
| 59 |
// Check if the browser supports the Web Speech API
|
| 60 |
if (!('webkitSpeechRecognition' in window)) {
|
| 61 |
-
|
| 62 |
} else {
|
| 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 |
// Add an event listener to the input field to detect the Enter key
|
|
@@ -116,41 +116,41 @@ userInput.addEventListener('keydown', function(event) {
|
|
| 116 |
|
| 117 |
|
| 118 |
function toggleHistory() {
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
}
|
| 131 |
|
| 132 |
|
| 133 |
function toggleMenu() {
|
| 134 |
-
|
| 135 |
-
|
| 136 |
}
|
| 137 |
|
| 138 |
function startNewChat() {
|
| 139 |
-
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
}
|
| 155 |
|
| 156 |
|
|
|
|
| 58 |
|
| 59 |
// Check if the browser supports the Web Speech API
|
| 60 |
if (!('webkitSpeechRecognition' in window)) {
|
| 61 |
+
alert("Your browser doesn't support speech recognition. Please use Google Chrome.");
|
| 62 |
} else {
|
| 63 |
+
// Initialize speech recognition
|
| 64 |
+
const recognition = new webkitSpeechRecognition();
|
| 65 |
+
recognition.continuous = false;
|
| 66 |
+
recognition.interimResults = false;
|
| 67 |
+
recognition.lang = 'en-IN'; // Default language (can be 'hi-IN' for Hindi)
|
| 68 |
+
|
| 69 |
+
const micButton = document.getElementById('micButton'); // Microphone icon
|
| 70 |
+
const userInput = document.getElementById('userInput'); // Text input field
|
| 71 |
+
const chatBody = document.getElementById('chatBody'); // Chat history container
|
| 72 |
+
|
| 73 |
+
// Voice Input Button Click Event
|
| 74 |
+
micButton.addEventListener('click', () => {
|
| 75 |
+
if (micButton.classList.contains('active')) {
|
| 76 |
+
recognition.stop(); // Stop if already active
|
| 77 |
+
} else {
|
| 78 |
+
recognition.start(); // Start listening
|
| 79 |
+
}
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
// When speech recognition starts
|
| 83 |
+
recognition.onstart = () => {
|
| 84 |
+
micButton.classList.add('active'); // Change mic color to green
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
// When speech recognition ends
|
| 88 |
+
recognition.onend = () => {
|
| 89 |
+
micButton.classList.remove('active'); // Reset mic color to red
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
// When speech recognition result is detected
|
| 93 |
+
recognition.onresult = (event) => {
|
| 94 |
+
const transcript = event.results[0][0].transcript;
|
| 95 |
+
userInput.value = transcript; // Fill the input with transcribed text
|
| 96 |
+
sendMessage(); // Call sendMessage after getting the input
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
// Handle errors during speech recognition
|
| 100 |
+
recognition.onerror = (event) => {
|
| 101 |
+
alert('Speech recognition error: ' + event.error);
|
| 102 |
+
micButton.classList.remove('active'); // Reset mic color to red if error occurs
|
| 103 |
+
};
|
| 104 |
}
|
| 105 |
|
| 106 |
// Add an event listener to the input field to detect the Enter key
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
function toggleHistory() {
|
| 119 |
+
const historyPanel = document.querySelector('.history-panel');
|
| 120 |
+
const chatHistoryText = document.querySelector('.chat-history-text');
|
| 121 |
+
|
| 122 |
+
// Toggle the width of the history panel
|
| 123 |
+
if (historyPanel.style.width === '0px') {
|
| 124 |
+
historyPanel.style.width = '250px'; // Open the panel
|
| 125 |
+
chatHistoryText.style.display = 'block'; // Show "Chat History" text
|
| 126 |
+
} else {
|
| 127 |
+
historyPanel.style.width = '0px'; // Close the panel
|
| 128 |
+
chatHistoryText.style.display = 'none'; // Hide "Chat History" text
|
| 129 |
+
}
|
| 130 |
}
|
| 131 |
|
| 132 |
|
| 133 |
function toggleMenu() {
|
| 134 |
+
const menu = document.querySelector('.three-dot-menu .menu-options');
|
| 135 |
+
menu.classList.toggle('active');
|
| 136 |
}
|
| 137 |
|
| 138 |
function startNewChat() {
|
| 139 |
+
// Select the chat body element
|
| 140 |
+
const chatBody = document.getElementById('chatBody');
|
| 141 |
|
| 142 |
+
// Remove all chat messages
|
| 143 |
+
chatBody.innerHTML = ''; // This clears all existing messages
|
| 144 |
|
| 145 |
+
// Optionally, add the default welcome message again
|
| 146 |
+
const welcomeMessage = document.createElement('div');
|
| 147 |
+
welcomeMessage.classList.add('bot-message');
|
| 148 |
+
welcomeMessage.textContent = "Hello! How can I assist you today?";
|
| 149 |
|
| 150 |
+
chatBody.appendChild(welcomeMessage);
|
| 151 |
|
| 152 |
+
// Scroll to the top after resetting chat
|
| 153 |
+
chatBody.scrollTop = chatBody.scrollHeight;
|
| 154 |
}
|
| 155 |
|
| 156 |
|