Krishnavamshithumma commited on
Commit
ddc9384
·
verified ·
1 Parent(s): c53823f

Create custom.js

Browse files
Files changed (1) hide show
  1. custom.js +44 -0
custom.js ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // custom.js
2
+ const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
3
+ recognition.continuous = false;
4
+ recognition.lang = "en-US";
5
+
6
+ function startListening() {
7
+ const apiKey = document.querySelector("#apiKeyInput input").value;
8
+ if (!apiKey) {
9
+ alert("Please enter your OpenAI API key first!");
10
+ return;
11
+ }
12
+
13
+ recognition.start();
14
+ document.getElementById("micButton").textContent = "🔴 Listening...";
15
+ }
16
+
17
+ recognition.onresult = (event) => {
18
+ const transcript = event.results[0][0].transcript;
19
+ document.querySelector("#voiceInput input").value = transcript;
20
+ document.querySelector("#voiceInput input").dispatchEvent(new Event("change"));
21
+ };
22
+
23
+ recognition.onend = () => {
24
+ document.getElementById("micButton").textContent = "🎤 Speak";
25
+ };
26
+
27
+ recognition.onerror = (event) => {
28
+ console.error("Speech recognition error", event.error);
29
+ document.getElementById("micButton").textContent = "🎤 Speak";
30
+ alert("Speech recognition error: " + event.error);
31
+ };
32
+
33
+ // Initialize chat box scrolling
34
+ function scrollChat() {
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
+ });