-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (38 loc) · 1.16 KB
/
script.js
File metadata and controls
42 lines (38 loc) · 1.16 KB
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
function startListening() {
const recognition = new (window.SpeechRecognition ||
window.webkitSpeechRecognition)();
recognition.lang = "en-US";
recognition.start();
recognition.onresult = function (event) {
const voiceCommand = event.results[0][0].transcript;
document.getElementById("voiceText").innerText =
"You said: " + voiceCommand;
fetchCode(voiceCommand);
};
recognition.onerror = function (event) {
alert("Speech recognition error: " + event.error);
};
}
function fetchCode(prompt) {
fetch("/generate-code", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt }),
})
.then((res) => res.json())
.then((data) => {
document.getElementById("codeOutput").innerText =
data.code || data.response || "No code found.";
})
.catch((err) => {
document.getElementById("codeOutput").innerText =
"Error generating code.";
});
}
function copyCode() {
const code = document.getElementById("codeOutput").innerText;
navigator.clipboard
.writeText(code)
.then(() => alert("Code copied!"))
.catch(() => alert("Failed to copy."));
}