Spaces:
Sleeping
Sleeping
Update custom.js
Browse files
custom.js
CHANGED
|
@@ -3,42 +3,56 @@ const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognit
|
|
| 3 |
recognition.continuous = false;
|
| 4 |
recognition.lang = "en-US";
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
if (!apiKey) {
|
| 9 |
alert("Please enter your OpenAI API key first!");
|
| 10 |
return;
|
| 11 |
}
|
| 12 |
|
| 13 |
recognition.start();
|
| 14 |
-
document.getElementById("micButton")
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
recognition.onresult = (event) => {
|
| 18 |
const transcript = event.results[0][0].transcript;
|
| 19 |
-
document.querySelector("#voiceInput input")
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
};
|
| 22 |
|
| 23 |
recognition.onend = () => {
|
| 24 |
-
document.getElementById("micButton")
|
|
|
|
| 25 |
};
|
| 26 |
|
| 27 |
recognition.onerror = (event) => {
|
| 28 |
console.error("Speech recognition error", event.error);
|
| 29 |
-
document.getElementById("micButton")
|
|
|
|
| 30 |
alert("Speech recognition error: " + event.error);
|
| 31 |
};
|
| 32 |
|
| 33 |
-
//
|
| 34 |
-
|
| 35 |
-
const chatBox = document.getElementById("chatBox");
|
| 36 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
// Observe chat box changes for auto-scroll
|
| 40 |
-
const observer = new MutationObserver(scrollChat);
|
| 41 |
-
observer.observe(document.getElementById("chatBox"), {
|
| 42 |
-
childList: true,
|
| 43 |
-
subtree: true
|
| 44 |
-
});
|
|
|
|
| 3 |
recognition.continuous = false;
|
| 4 |
recognition.lang = "en-US";
|
| 5 |
|
| 6 |
+
// Initialize chat box scrolling
|
| 7 |
+
function scrollChat() {
|
| 8 |
+
const chatBox = document.getElementById("chatBox");
|
| 9 |
+
if (chatBox) {
|
| 10 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
// Observe chat box changes for auto-scroll
|
| 15 |
+
const observer = new MutationObserver(scrollChat);
|
| 16 |
+
if (document.getElementById("chatBox")) {
|
| 17 |
+
observer.observe(document.getElementById("chatBox"), {
|
| 18 |
+
childList: true,
|
| 19 |
+
subtree: true
|
| 20 |
+
});
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// Expose function to global scope
|
| 24 |
+
window.startListening = function() {
|
| 25 |
+
const apiKey = document.querySelector("#apiKeyInput input")?.value;
|
| 26 |
if (!apiKey) {
|
| 27 |
alert("Please enter your OpenAI API key first!");
|
| 28 |
return;
|
| 29 |
}
|
| 30 |
|
| 31 |
recognition.start();
|
| 32 |
+
const micButton = document.getElementById("micButton");
|
| 33 |
+
if (micButton) micButton.textContent = "🔴 Listening...";
|
| 34 |
}
|
| 35 |
|
| 36 |
recognition.onresult = (event) => {
|
| 37 |
const transcript = event.results[0][0].transcript;
|
| 38 |
+
const voiceInput = document.querySelector("#voiceInput input");
|
| 39 |
+
if (voiceInput) {
|
| 40 |
+
voiceInput.value = transcript;
|
| 41 |
+
voiceInput.dispatchEvent(new Event("change"));
|
| 42 |
+
}
|
| 43 |
};
|
| 44 |
|
| 45 |
recognition.onend = () => {
|
| 46 |
+
const micButton = document.getElementById("micButton");
|
| 47 |
+
if (micButton) micButton.textContent = "🎤 Speak";
|
| 48 |
};
|
| 49 |
|
| 50 |
recognition.onerror = (event) => {
|
| 51 |
console.error("Speech recognition error", event.error);
|
| 52 |
+
const micButton = document.getElementById("micButton");
|
| 53 |
+
if (micButton) micButton.textContent = "🎤 Speak";
|
| 54 |
alert("Speech recognition error: " + event.error);
|
| 55 |
};
|
| 56 |
|
| 57 |
+
// Initial scroll
|
| 58 |
+
setTimeout(scrollChat, 500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|