|
|
|
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); |
|
recognition.continuous = false; |
|
recognition.lang = "en-US"; |
|
|
|
|
|
function scrollChat() { |
|
const chatBox = document.getElementById("chatBox"); |
|
if (chatBox) { |
|
chatBox.scrollTop = chatBox.scrollHeight; |
|
} |
|
} |
|
|
|
|
|
const observer = new MutationObserver(scrollChat); |
|
if (document.getElementById("chatBox")) { |
|
observer.observe(document.getElementById("chatBox"), { |
|
childList: true, |
|
subtree: true |
|
}); |
|
} |
|
|
|
|
|
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); |
|
}; |
|
|
|
|
|
setTimeout(scrollChat, 500); |