Spaces:
Sleeping
Sleeping
Update frontend/app.js
Browse files- frontend/app.js +27 -7
frontend/app.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
// Persist a session_id
|
| 4 |
function getSessionId() {
|
| 5 |
let sid = localStorage.getItem("session_id");
|
| 6 |
if (!sid) {
|
|
@@ -25,6 +26,12 @@ function disableInput(reason = "Input disabled.") {
|
|
| 25 |
document.getElementById("status").textContent = reason;
|
| 26 |
}
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
async function moderateText(text) {
|
| 29 |
const sid = getSessionId();
|
| 30 |
const res = await fetch(`${API_BASE}/moderate`, {
|
|
@@ -36,7 +43,7 @@ async function moderateText(text) {
|
|
| 36 |
body: JSON.stringify({ text })
|
| 37 |
});
|
| 38 |
if (!res.ok) throw new Error(`Moderate failed: ${res.status}`);
|
| 39 |
-
return res.json(); // {allowed
|
| 40 |
}
|
| 41 |
|
| 42 |
async function sendChat(text) {
|
|
@@ -50,10 +57,10 @@ async function sendChat(text) {
|
|
| 50 |
body: JSON.stringify({ text })
|
| 51 |
});
|
| 52 |
if (!res.ok) throw new Error(`Chat failed: ${res.status}`);
|
| 53 |
-
return res.json(); // {reply
|
| 54 |
}
|
| 55 |
|
| 56 |
-
|
| 57 |
const nameInput = document.getElementById("name-input");
|
| 58 |
const name = (nameInput.value || "").trim();
|
| 59 |
if (!name) return;
|
|
@@ -61,9 +68,9 @@ document.getElementById("name-submit").addEventListener("click", async () => {
|
|
| 61 |
document.getElementById("chat-section").classList.remove("hidden");
|
| 62 |
addMessage(`Welcome, ${name}!`, "bot");
|
| 63 |
addMessage(name, "user"); // immediately show name in chat
|
| 64 |
-
}
|
| 65 |
|
| 66 |
-
|
| 67 |
const input = document.getElementById("chat-input");
|
| 68 |
const text = (input.value || "").trim();
|
| 69 |
if (!text) return;
|
|
@@ -84,4 +91,17 @@ document.getElementById("send-btn").addEventListener("click", async () => {
|
|
| 84 |
} finally {
|
| 85 |
input.value = "";
|
| 86 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Same-origin API calls
|
| 2 |
+
const API_BASE = "";
|
| 3 |
|
| 4 |
+
// Persist a session_id to let backend track violations
|
| 5 |
function getSessionId() {
|
| 6 |
let sid = localStorage.getItem("session_id");
|
| 7 |
if (!sid) {
|
|
|
|
| 26 |
document.getElementById("status").textContent = reason;
|
| 27 |
}
|
| 28 |
|
| 29 |
+
function clearChat() {
|
| 30 |
+
const win = document.getElementById("chat-window");
|
| 31 |
+
win.innerHTML = "";
|
| 32 |
+
document.getElementById("status").textContent = "";
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
async function moderateText(text) {
|
| 36 |
const sid = getSessionId();
|
| 37 |
const res = await fetch(`${API_BASE}/moderate`, {
|
|
|
|
| 43 |
body: JSON.stringify({ text })
|
| 44 |
});
|
| 45 |
if (!res.ok) throw new Error(`Moderate failed: ${res.status}`);
|
| 46 |
+
return res.json(); // {allowed, banned, lock, reason?}
|
| 47 |
}
|
| 48 |
|
| 49 |
async function sendChat(text) {
|
|
|
|
| 57 |
body: JSON.stringify({ text })
|
| 58 |
});
|
| 59 |
if (!res.ok) throw new Error(`Chat failed: ${res.status}`);
|
| 60 |
+
return res.json(); // {reply}
|
| 61 |
}
|
| 62 |
|
| 63 |
+
function submitName() {
|
| 64 |
const nameInput = document.getElementById("name-input");
|
| 65 |
const name = (nameInput.value || "").trim();
|
| 66 |
if (!name) return;
|
|
|
|
| 68 |
document.getElementById("chat-section").classList.remove("hidden");
|
| 69 |
addMessage(`Welcome, ${name}!`, "bot");
|
| 70 |
addMessage(name, "user"); // immediately show name in chat
|
| 71 |
+
}
|
| 72 |
|
| 73 |
+
async function submitMessage() {
|
| 74 |
const input = document.getElementById("chat-input");
|
| 75 |
const text = (input.value || "").trim();
|
| 76 |
if (!text) return;
|
|
|
|
| 91 |
} finally {
|
| 92 |
input.value = "";
|
| 93 |
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// Wire up buttons and Enter key
|
| 97 |
+
document.getElementById("name-submit").addEventListener("click", submitName);
|
| 98 |
+
document.getElementById("name-input").addEventListener("keydown", (e) => {
|
| 99 |
+
if (e.key === "Enter") submitName();
|
| 100 |
});
|
| 101 |
+
|
| 102 |
+
document.getElementById("send-btn").addEventListener("click", submitMessage);
|
| 103 |
+
document.getElementById("chat-input").addEventListener("keydown", (e) => {
|
| 104 |
+
if (e.key === "Enter") submitMessage();
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
document.getElementById("clear-btn").addEventListener("click", clearChat);
|