File size: 2,414 Bytes
d265ba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = `<b>${speaker}:</b> <span class="message-text"></span>`;
  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 = `<div class="bubble-shodan"><b>SHODAN:</b> ${data.message}</div>`;
    });
});