-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
131 lines (112 loc) · 4.36 KB
/
scripts.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Setup before functions
let typingTimeout;
const typingDelay = 2000;
let userEditableElement;
const followUpMessages = ["and I am from", "and I work in", "and no one gives a flying..."];
const instructionsElement = document.getElementById('instructions');
function updateInstructions(text) {
instructionsElement.textContent = text;
}
function hideInstructions() {
instructionsElement.style.opacity = '0';
}
function showInstructions() {
instructionsElement.style.opacity = '1';
}
function focusLatestEditableSpan() {
const editableSpans = document.querySelectorAll('[contenteditable="true"]');
const latestSpan = editableSpans[editableSpans.length - 1];
if (latestSpan) {
latestSpan.focus();
}
}
function setupEventListeners() {
userEditableElement = document.getElementById('edit');
userEditableElement.addEventListener('keyup', () => {
clearTimeout(typingTimeout);
if (userEditableElement.innerText) {
updateInstructions("Loading response from haha ai");
typingTimeout = setTimeout(() => addNextMessage(0), typingDelay);
}
});
}
document.addEventListener('DOMContentLoaded', () => {
setupEventListeners();
focusLatestEditableSpan();
showInstructions();
});
function addNextMessage(index) {
const contentElement = document.getElementById("content");
contentElement.innerHTML += followUpMessages[index] + " ";
if (index < followUpMessages.length - 1) {
// Create editable span for all but the last message
const newEditableSpan = document.createElement('span');
newEditableSpan.contentEditable = "true";
newEditableSpan.setAttribute("contenteditable", "true");
newEditableSpan.setAttribute("id", `edit${index + 1}`);
contentElement.appendChild(newEditableSpan);
newEditableSpan.addEventListener('keyup', () => {
clearTimeout(typingTimeout);
if (newEditableSpan.innerText) {
updateInstructions("Loading response from haha ai");
typingTimeout = setTimeout(() => addNextMessage(index + 1), typingDelay);
}
});
focusLatestEditableSpan();
updateInstructions("Start typing");
} else {
// This is the last message, add the final text after a delay
const finalDelay = 1000;
setTimeout(() => {
hideInstructions();
setTimeout(addFinalText, 300); // Hide instructions before showing final text
}, finalDelay);
}
}
// Final text displayed
function addFinalText() {
const finalText = document.createElement('h1');
finalText.textContent = "Jk remember you are valuable and be kind to yourself :-)";
finalText.className = 'final-text';
document.body.appendChild(finalText);
createTextRain();
}
function createTextRain() {
const numberOfDrops = 100;
let dropsFinished = 0;
const haVariations = ["ha", "haha"];
for (let i = 0; i < numberOfDrops; i++) {
setTimeout(() => {
const drop = document.createElement('div');
drop.className = 'text-drop';
drop.style.left = `${Math.random() * 100}vw`;
drop.style.animationDuration = `${Math.random() * 2 + 3}s`;
drop.textContent = haVariations[Math.floor(Math.random() * haVariations.length)];
document.body.appendChild(drop);
drop.addEventListener('animationend', () => {
document.body.removeChild(drop);
dropsFinished++;
if (dropsFinished === numberOfDrops) {
setTimeout(resetHaha, 1000);
}
});
}, i * 50);
}
}
function resetHaha() {
const contentElement = document.getElementById("content");
contentElement.innerHTML = 'Hello my name is <span id="edit" contenteditable="true" class="single-line"></span>';
const additionalSpans = document.querySelectorAll('[id^="edit"]:not(#edit)');
additionalSpans.forEach(span => span.remove());
const finalText = document.querySelector('.final-text');
if (finalText) {
finalText.remove();
}
const textDrops = document.querySelectorAll('.text-drop');
textDrops.forEach(drop => drop.remove());
clearTimeout(typingTimeout);
showInstructions();
updateInstructions("Start typing");
setupEventListeners();
focusLatestEditableSpan();
}