-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
103 lines (85 loc) · 2.84 KB
/
Script.js
File metadata and controls
103 lines (85 loc) · 2.84 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("service-worker.js").then(() => {
console.log("Service Worker registered");
});
}
const micBtn = document.getElementById("micBtn");
const input = document.getElementById("thoughtInput");
const sigPytbtn = document.getElementById("sigPytbtn");
const message = document.getElementById("messageArea");
//const api ="https://localhost:5001/api/Pyt";
const api = "https://bustrackerserver-fcd5hra6drazczce.northeurope-01.azurewebsites.net/api/Pyt" // Azure backend URL
document.addEventListener("touchstart", function () {}, true);
let recognition;
if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SpeechRecognition();
recognition.lang = 'da-DK'; // Dansk
recognition.continuous = false;
recognition.interimResults = false;
recognition.onstart = function() {
micBtn.classList.add('recording');
micBtn.title = 'Optager… klik igen for at stoppe';
};
recognition.onend = function() {
micBtn.classList.remove('recording');
micBtn.title = 'Tal dine tanker';
};
recognition.onresult = function(event) {
const transcript = event.results[0][0].transcript;
input.value = transcript; // Bare indsæt – ikke send
clearThought();
};
recognition.onerror = function(event) {
micBtn.classList.remove('recording');
console.error("Talegenkendelse fejl:", event.error);
};
micBtn.addEventListener("click", () => {
if (micBtn.classList.contains('recording')) {
recognition.stop();
} else {
recognition.start();
}
});
}
else {
micBtn.disabled = true;
micBtn.title = "Talegenkendelse understøttes ikke i denne browser.";
}
async function clearThought() {
message.style.display = "block";
if (!input.value.trim()) {
message.textContent = "Pyt med det !"
sigPytbtn.click(); // sigPyt();
return;
}
message.textContent = "Tænker...";
const response = await fetch(api, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": "sk-pyt-1234567890abcdef"
},
body: JSON.stringify({ text: input.value })
});
const data = await response.json();
input.value = "";
message.style.opacity = 0;
setTimeout(() => {
message.textContent = data.message;
message.style.opacity = 1;
//sigPyt();
}, 300);
}
// Enter-genvej til knappen
input.addEventListener("keydown", function (e) {
if (e.key === "Enter") {
e.preventDefault();
clearThought();
}
});
function sigPyt(){
const sound = document.getElementById("pytlyd");
sound.currentTime = 0;
sound.play();
}