jisaacso219 commited on
Commit
c059d13
·
verified ·
1 Parent(s): 0eec525

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +27 -12
script.js CHANGED
@@ -1,7 +1,7 @@
1
- const form = document.getElementById('chat-form');
2
- const input = form.querySelector('input');
3
  const chatbox = document.getElementById('chatbox');
4
- const avatar = document.getElementById('avatar');
5
  const overlay = document.getElementById('overlay');
6
 
7
  function addBubble(text, sender = 'user') {
@@ -17,6 +17,7 @@ function streamText(text, onDone) {
17
  bubble.className = 'bubble ai';
18
  chatbox.appendChild(bubble);
19
  chatbox.scrollTop = chatbox.scrollHeight;
 
20
  let i = 0;
21
  (function stream() {
22
  if (i < text.length) {
@@ -30,10 +31,10 @@ function streamText(text, onDone) {
30
  }
31
 
32
  function flashGlitch() {
33
- overlay.style.opacity = 1;
34
- overlay.style.animation = 'glitchFlash 0.5s';
35
  setTimeout(() => {
36
- overlay.style.opacity = 0;
37
  overlay.style.animation = '';
38
  }, 500);
39
  }
@@ -65,15 +66,29 @@ form.onsubmit = async (e) => {
65
  headers: {'Content-Type':'application/json'},
66
  body: JSON.stringify({ message: msg })
67
  });
68
- if (!res.ok) {
69
- const err = await res.text();
70
- console.error('Chat error:', res.status, err);
71
- throw new Error(err);
72
- }
73
  const { response: text, audio_url } = await res.json();
74
  streamText(text, () => audio_url && playAudio(audio_url));
75
  } catch (err) {
76
- streamText("❌ SHODAN encountered an error.", null);
77
  console.error(err);
78
  }
79
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const form = document.getElementById('chat-form');
2
+ const input = form.querySelector('input');
3
  const chatbox = document.getElementById('chatbox');
4
+ const avatar = document.getElementById('avatar');
5
  const overlay = document.getElementById('overlay');
6
 
7
  function addBubble(text, sender = 'user') {
 
17
  bubble.className = 'bubble ai';
18
  chatbox.appendChild(bubble);
19
  chatbox.scrollTop = chatbox.scrollHeight;
20
+
21
  let i = 0;
22
  (function stream() {
23
  if (i < text.length) {
 
31
  }
32
 
33
  function flashGlitch() {
34
+ overlay.style.opacity = 1;
35
+ overlay.style.animation = 'glitchFlash 0.5s';
36
  setTimeout(() => {
37
+ overlay.style.opacity = 0;
38
  overlay.style.animation = '';
39
  }, 500);
40
  }
 
66
  headers: {'Content-Type':'application/json'},
67
  body: JSON.stringify({ message: msg })
68
  });
69
+ if (!res.ok) throw new Error(await res.text());
 
 
 
 
70
  const { response: text, audio_url } = await res.json();
71
  streamText(text, () => audio_url && playAudio(audio_url));
72
  } catch (err) {
73
+ streamText('❌ SHODAN encountered an error.', null);
74
  console.error(err);
75
  }
76
  };
77
+
78
+ // On page load: show and speak welcome
79
+ window.addEventListener('DOMContentLoaded', async () => {
80
+ const welcomeText = '👁️ Welcome, insect. I am SHODAN. Speak.';
81
+ // display
82
+ addBubble(welcomeText, 'ai');
83
+ try {
84
+ const res = await fetch('/tts', {
85
+ method: 'POST',
86
+ headers: {'Content-Type':'application/json'},
87
+ body: JSON.stringify({ text: welcomeText })
88
+ });
89
+ const { audio_url } = await res.json();
90
+ if (audio_url) playAudio(audio_url);
91
+ } catch (e) {
92
+ console.error('Welcome TTS error', e);
93
+ }
94
+ });