forked from DishpitDev/Slopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
653 lines (553 loc) · 21 KB
/
chat.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>chat</title>
<script src="static/js/tunnel_lib.js" defer></script>
<style>
:root {
--bg-primary: #1a1a1a;
--bg-secondary: #252525;
--border-color: #333;
--text-primary: #e0e0e0;
--text-secondary: #b0b0b0;
}
body {
margin: 0;
padding: 0;
height: 100vh;
overflow: hidden;
background-color: var(--bg-primary);
color: var(--text-primary);
font-family: monospace;
}
#chatContainer {
height: 100vh;
display: flex;
flex-direction: column;
}
#mainContent {
flex: 1;
overflow: hidden;
position: relative;
}
#roomInfo {
padding: 15px;
font-size: large;
}
#roomInfo>p {
display: inline-block;
transform: translateX(100px)scale(0.5, 1);
background-color: var(--bg-secondary);
border-bottom: 1px solid var(--border-color);
font-size: large;
}
#messages {
height: calc(100% - 100px);
/* Subtract room info height */
overflow-y: auto;
padding: 20px;
background-color: var(--bg-secondary);
color: white;
font-weight: 300;
}
#bottomPanel {
background-color: var(--bg-secondary);
border-top: 1px solid var(--border-color);
padding: 15px;
}
.bot {
font-weight: 600;
font-size: smaller;
background-color: #555;
padding: 2px 3px 2px 4px;
margin-right: 5px;
border-radius: 3px;
}
.message {
margin-bottom: 6px;
display: flex;
align-items: flex-end;
}
.username {
font-weight: bold;
position: relative;
}
.username:after {
content: ": ";
margin-right: 6px;
/* forgot that the space wouldn't render lol */
}
.username:after {
content: ": ";
color: var(--text-primary);
}
#inputContainer {
display: flex;
gap: 10px;
margin-bottom: 10px;
}
.ceo {
position: absolute;
bottom: 0;
right: 0;
font-size: 5pt;
}
.ceo img {
max-width: 115px;
max-height: 115px;
}
#messageInput {
flex: 1;
padding: 8px;
border: 2px inset var(--border-color);
border-right: 2px var(--border-color);
border-bottom: 2px var(--border-color);
background-color: var(--bg-primary);
color: var(--text-primary);
font-family: monospace;
appearance: none;
-moz-appearance: none;
}
#messageInput:focus {
outline: 1px solid var(--border-color);
}
button {
color: var(--text-primary);
background-color: var(--border-color);
border: 2px outset var(--border-color);
font-family: monospace;
appearance: none;
-moz-appearance: none;
}
button:hover {
border: 2px outset var(--text-secondary);
}
#usernameError {
color: red;
margin-top: 5px;
display: none;
}
.hi {
position: absolute;
top: 35px;
left: 700px;
transform: rotate3d(0, 1, 1, 0deg);
animation-name: gay;
animation-duration: 4s;
animation-iteration-count: infinite;
}
@keyframes gay {
0% {
transform: rotate3d(0, 1, 1, 0deg);
}
100% {
transform: rotate3d(0, 1, 1, 360deg);
}
}
body.hide-images .chat-image {
display: none;
}
body.blur-images .chat-image {
filter: blur(1rem);
}
body.blur-images .chat-image:hover {
filter: none;
transition: 0.25s filter linear;
-webkit-transition: 0.25s -webkit-filter linear;
-moz-transition: 0.25s -moz-filter linear;
-ms-transition: 0.25s -ms-filter linear;
-o-transition: 0.25s -o-filter linear;
}
.chat-image {
max-width: 50vw;
max-height: 50vh;
}
.message-text {
display: flex;
align-items: flex-end;
}
.message-text span {
white-space: pre-wrap;
padding-right: 5px;
}
.message-text img {
max-width: 50vw;
max-height: 50vh;
margin-right: 5px;
}
.message-text img.emoji {
max-width: 2em;
max-height: 2em;
margin-right: 5px;
}
</style>
</head>
<body>
<div id="chatContainer">
<div id="mainContent">
<div class="ceo">
<img src="static/images/CEO.jpeg" alt="CEO of the chat">
<p>The CEO is watching you</p>
</div>
<div id="roomInfo">
<p>Connected to room: <strong>global</strong></p>
</div>
<div id="messages"></div>
</div>
<div class="hi">
<a href="/index.html">
<img src="/static/images/4db62b6a70d285b0efb299ee6816504e.jpg" width="100" height="100">
<p style="color:magenta;">Go BACK</p>
</a>
</div>
<div id="bottomPanel">
<div id="inputContainer">
<input type="text" id="messageInput" placeholder="Type a message..." maxlength="200">
<button id="sendButton" style="font-weight: bold;" onclick="sendMessage()">Send</button>
</div>
<div id="controls">
<button id="createButton" onclick="createOrJoinRoom()">Create or Join a Room</button>
<button id="setUsernameButton" onclick="setUserName()">Set Username</button>
<button id="toggleImagesButton" onclick="toggleImages()">Hide Images</button>
</div>
<div id="usernameError"></div>
</div>
</div>
<script>
// Track usernames in the current room
window.usernames = new Set();
window.mutedUsernames = new Set();
window.usernameConnection = null;
window.chatState = 0; // 0 show 1 hide 2 blur
let notificationSound = new Audio("/static/audio/youvegotmail.mp3")
let emojis = {};
function getRandomSoftColor() {
const hue = Math.floor(Math.random() * 360);
return `hsl(${hue}, 65%, 75%)`; // fairly soft
}
async function initUsernameTracking(room) {
// Create a separate subchannel for username management
window.usernameConnection = new TunnelConnection(
'https://slopify_chat.infernity.dev/',
room,
"usernames",
handleUsernameMessage,
{ encryption: false, compression: false }
);
await window.usernameConnection.init();
await window.usernameConnection.create();
await window.usernameConnection.connect();
}
function handleUsernameMessage(message) {
try {
let data = JSON.parse(message);
if (data.type === 'username_taken') {
window.usernames.add(data.username);
} else if (data.type === 'username_released') {
window.usernames.delete(data.username);
} else if (data.type === 'username_request') {
// If we have this username, respond that it's taken
if (data.username === window.userName) {
window.usernameConnection.send(JSON.stringify({
type: 'username_taken',
username: data.username
}));
}
}
} catch (e) {
console.error('Error handling username message:', e);
}
}
async function createOrJoinRoom() {
let room = prompt("Enter the name of the room you want to join or create, leave it blank to randomize:");
window.chatConnection.close();
if (window.usernameConnection) {
// Release old username from previous room
window.usernameConnection.send(JSON.stringify({
type: 'username_released',
username: window.userName
}));
window.usernameConnection.close();
}
if (room) {
window.chatConnection = getTunnelConnection(room);
} else {
window.chatConnection = getTunnelConnection('');
room = window.chatConnection.tunnelId;
}
await window.chatConnection.init();
await window.chatConnection.create();
await window.chatConnection.connect();
// Initialize username tracking for new room
window.usernames.clear();
await initUsernameTracking(room);
document.getElementById('roomInfo').textContent = `Connected to room: ${room}`;
localMessage('System:', `${window.userName} connected to room ${room}. ---`, true);
// Announce username in new room
window.usernameConnection.send(JSON.stringify({
type: 'username_taken',
username: window.userName
}));
localMessage(localStorage['userName'], 'i connected', false, false)
}
async function isUsernameAvailable(username) {
return new Promise((resolve) => {
if (window.usernames.has(username)) {
resolve(false);
return;
}
// Request if username is taken
window.usernameConnection.send(JSON.stringify({
type: 'username_request',
username: username
}));
// Wait a short time for responses
setTimeout(() => {
resolve(!window.usernames.has(username));
}, 500);
});
}
async function setUserName() {
let username = prompt('Enter your username:');
if (!username) {
setUserName();
return;
}
// Username validation
if (username.length < 3 || username.length > 15) {
alert('Username must be between 3 and 15 characters');
setUserName();
return;
}
if (!/^[a-zA-Z0-9_-]+$/.test(username)) {
alert('Username can only contain letters, numbers, underscores, and hyphens');
setUserName();
return;
}
const available = await isUsernameAvailable(username);
if (!available) {
alert('Username is already taken in this room. Please choose another.');
setUserName();
return;
}
// Release old username if exists
if (window.userName) {
window.usernameConnection.send(JSON.stringify({
type: 'username_released',
username: window.userName
}));
}
// Set new username
window.userName = username;
localStorage['userName'] = username;
// Announce new username
window.usernameConnection.send(JSON.stringify({
type: 'username_taken',
username: username
}));
localMessage(localStorage['userName'], 'i connected', false, false)
}
function getTunnelConnection(id) {
return new TunnelConnection(
'https://slopify_chat.infernity.dev/',
id,
"main",
processMessage,
{ encryption: false, compression: false }
)
}
function textContainsEmoji(text) {
return text.match(/:[a-z_]+:/g);
}
function splitImageFromText(messageText) {
let messageTextElement = document.createElement('div');
messageTextElement.classList.add('message-text');
messageText.split(' ').map((word) => {
if (word.endsWith('.pic') && window.chatState !== 1 /* Hide images */) {
let imageElement = document.createElement('img');
imageElement.classList.add('chat-image');
imageElement.src = word.slice(0, -4);
messageTextElement.appendChild(imageElement);
} else if (textContainsEmoji(word) && emojis[word.slice(1, -1)]) {
let emojiElement = document.createElement('img');
emojiElement.classList.add('emoji');
emojiElement.src = emojis[word.slice(1, -1)];
messageTextElement.appendChild(emojiElement);
} else {
let textElement = document.createElement('span');
textElement.textContent = word;
messageTextElement.appendChild(textElement);
}
});
return messageTextElement;
}
function toggleImages(state) {
if (typeof (state) !== 'undefined') {
window.chatState = state;
} else {
window.chatState++;
if (window.chatState > 2) {
window.chatState = 0;
}
}
console.log(window.chatState);
let btn = document.getElementById('toggleImagesButton');
document.body.classList.remove('hide-images');
document.body.classList.remove('blur-images');
switch (window.chatState) {
case 0:
btn.innerText = 'Hide Images';
break;
case 1:
btn.innerText = 'Blur Images';
document.body.classList.add('hide-images');
break;
case 2:
btn.innerText = 'Show Images';
document.body.classList.add('blur-images');
break;
}
localStorage.setItem('imageState', String(window.chatState));
}
function localMessage(username, message, isBot, isSystem) {
if (!isSystem) isSystem = false;
if (window.mutedUsernames.has(username)) { return };
let messagesContainer = document.getElementById('messages');
let messageContainer = document.createElement('div');
messageContainer.classList.add('message');
let usernameElement = document.createElement('div');
usernameElement.textContent = username;
usernameElement.classList.add('username');
let messageTextElement = splitImageFromText(message);
if (isSystem) {
usernameElement.style.color = '#ff6b6b';
messageTextElement.style.color = '#ff6b6b';
} else {
if (!window.userColors) window.userColors = {};
if (!window.userColors[username]) {
window.userColors[username] = getRandomSoftColor();
}
usernameElement.style.color = window.userColors[username];
}
if (isBot) {
let botElement = document.createElement('div')
botElement.textContent = "bot";
botElement.classList.add('bot');
messageContainer.appendChild(botElement);
}
if (message.includes(localStorage['userName'])) {
notificationSound.play();
messageTextElement.style.backgroundColor = "rgba(233, 211, 150, 0.5)";
usernameElement.style.backgroundColor = "rrgba(233, 211, 150, 0.5)";
}
messageContainer.appendChild(usernameElement);
messageContainer.appendChild(messageTextElement);
messagesContainer.appendChild(messageContainer);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function processMessage(message) {
try {
let messageData = JSON.parse(message);
if (!messageData.bot) { messageData.bot = false } else { messageData.bot = true; }
if (!messageData.username || !messageData.message) throw new Error('Invalid message data: ' + message);
localMessage(messageData.username, messageData.message, messageData.bot);
if (messageData.flash) {
flashGreen();
}
} catch (e) {
console.error(e);
}
}
function sendMessage() {
let userMessage = document.getElementById('messageInput').value;
handleLocalCommands(userMessage)
if (!userMessage.trim()) return;
if (userMessage.includes("blink")) {
flashGreen();
}
let messageData = {
username: window.userName,
message: userMessage
};
window.chatConnection.send(JSON.stringify(messageData));
document.getElementById('messageInput').value = '';
}
function handleLocalCommands(message) {
if (message[0] == '~') {
let s = message.split(' ')
let command = s[0];
switch (command) {
case '~mute':
let userToMute = s[1]
window.mutedUsernames.add(userToMute);
alert("User " + userToMute + " muted succesfully.")
break;
case '~unmute':
let userToUnmute = s[1]
window.mutedUsernames.delete(userToUnmute);
alert("User " + userToMute + " unmuted succesfully.")
break;
}
return true
}
return false
}
document.addEventListener('DOMContentLoaded', async () => {
window.userName = localStorage['userName'] || null;
// Initialize connections
window.chatConnection = getTunnelConnection('global');
await window.chatConnection.init();
await window.chatConnection.create();
await window.chatConnection.connect();
emojis = await fetch('https://api.github.com/emojis').then(response => response.json()).catch(() => ({}));
await initUsernameTracking('global');
if (!window.userName) {
setUserName();
} else {
// Announce existing username
window.usernameConnection.send(JSON.stringify({
type: 'username_taken',
username: window.userName
}));
}
document.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
sendMessage();
}
});
window.chatState = localStorage.getItem('imageState');
if (null === window.chatState) {
window.chatState = 1;
}
if (localStorage.getItem('hideImages') === "true") {
window.chatState = 1;
localStorage.removeItem('hideImages');
}
toggleImages(window.chatState);
});
function flashGreen() {
document.body.style.backgroundColor = 'green';
setTimeout(() => {
document.body.style.backgroundColor = '';
}, 100);
}
</script>
<script>
const ceo = document.querySelector('.ceo');
const ceoRect = ceo.getBoundingClientRect();
document.addEventListener('mousemove', (event) => {
const mouseX = event.clientX;
const mouseY = event.clientY;
const boxCenterX = ceoRect.left + ceoRect.width / 2;
const boxCenterY = ceoRect.top + ceoRect.height / 2;
const deltaX = mouseX - boxCenterX;
const deltaY = mouseY - boxCenterY;
const angleZ = -(deltaY / ceoRect.height) * 1.2;
const angleY = (deltaX / ceoRect.width) * 2;
ceo.style.transform = `rotateZ(${angleZ}deg) rotateY(${angleY}deg)`;
});
</script>
</body>
</html>