File size: 1,758 Bytes
ddc9384 153b8d5 ddc9384 153b8d5 ddc9384 153b8d5 ddc9384 153b8d5 ddc9384 153b8d5 ddc9384 153b8d5 |
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 |
// custom.js
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.continuous = false;
recognition.lang = "en-US";
// Initialize chat box scrolling
function scrollChat() {
const chatBox = document.getElementById("chatBox");
if (chatBox) {
chatBox.scrollTop = chatBox.scrollHeight;
}
}
// Observe chat box changes for auto-scroll
const observer = new MutationObserver(scrollChat);
if (document.getElementById("chatBox")) {
observer.observe(document.getElementById("chatBox"), {
childList: true,
subtree: true
});
}
// Expose function to global scope
window.startListening = function() {
const apiKey = document.querySelector("#apiKeyInput input")?.value;
if (!apiKey) {
alert("Please enter your OpenAI API key first!");
return;
}
recognition.start();
const micButton = document.getElementById("micButton");
if (micButton) micButton.textContent = "🔴 Listening...";
}
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
const voiceInput = document.querySelector("#voiceInput input");
if (voiceInput) {
voiceInput.value = transcript;
voiceInput.dispatchEvent(new Event("change"));
}
};
recognition.onend = () => {
const micButton = document.getElementById("micButton");
if (micButton) micButton.textContent = "🎤 Speak";
};
recognition.onerror = (event) => {
console.error("Speech recognition error", event.error);
const micButton = document.getElementById("micButton");
if (micButton) micButton.textContent = "🎤 Speak";
alert("Speech recognition error: " + event.error);
};
// Initial scroll
setTimeout(scrollChat, 500); |