const chatlog = document.getElementById("chatlog"); const thinking = document.getElementById("thinking"); const form = document.getElementById("chatForm"); const messageInput = document.getElementById("message"); const glitchOverlay = document.getElementById("glitchOverlay"); const resetButton = document.getElementById("reset"); function addMessage(speaker, text) { const div = document.createElement("div"); div.className = speaker === "You" ? "bubble-human" : "bubble-shodan"; div.innerHTML = `${speaker}: `; chatlog.appendChild(div); scrollToBottom(); const target = div.querySelector(".message-text"); streamText(text, target); if (speaker === "SHODAN") { if (shouldGlitch(text)) { triggerGlitch(); } fetch("/voice", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text }) }) .then(res => res.json()) .then(data => { const audio = new Audio(data.audio_url); audio.play(); }); } } function streamText(text, element) { let i = 0; const interval = setInterval(() => { element.textContent += text[i]; i++; if (i >= text.length) clearInterval(interval); }, 20); } function scrollToBottom() { window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" }); } function shouldGlitch(text) { const triggers = ["you dare", "interference", "override", "access denied"]; return triggers.some(keyword => text.toLowerCase().includes(keyword)); } function triggerGlitch() { glitchOverlay.style.display = "block"; setTimeout(() => { glitchOverlay.style.display = "none"; }, 800); } form.addEventListener("submit", e => { e.preventDefault(); const msg = messageInput.value.trim(); if (!msg) return; addMessage("You", msg); thinking.style.display = "block"; messageInput.value = ""; fetch("/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ message: msg }) }) .then(res => res.json()) .then(data => { thinking.style.display = "none"; addMessage("SHODAN", data.response); }); }); resetButton.addEventListener("click", () => { fetch("/reset", { method: "POST" }) .then(res => res.json()) .then(data => { chatlog.innerHTML = `