-
-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathsampleoverlay.html
More file actions
813 lines (742 loc) · 31 KB
/
Copy pathsampleoverlay.html
File metadata and controls
813 lines (742 loc) · 31 KB
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
<!DOCTYPE html>
<!-- AI GUIDANCE FOR EDITING THIS OVERLAY -->
<!--
This is a Social Stream Ninja (SSN) chat overlay that displays messages from various platforms.
If editing this file, please note:
1. CORE FUNCTIONALITY:
- Messages arrive via WebRTC (iframe) or WebSocket (if &server parameter is used)
- Session ID is crucial - users must provide &session in URL parameters
- Message structure includes: {chatname, chatmessage, chatimg, type, contentimg, etc.}
- "type" field indicates source platform (youtube, twitch, x, kick, etc.)
2. SCROLLING MECHANISM:
- New messages are added at the bottom
- Page scrolls upward using CSS transforms on #message-list-wrapper
- The spacer element manages scroll position as older messages are removed
- DO NOT modify the scroll logic without thorough testing
3. STYLING GUIDELINES:
- Edit .message, .text, .name styles to change message appearance
- Images/badges have size constraints to maintain layout
- Background is transparent (#0000) for OBS overlay compatibility
- Message animations use CSS transitions for fade-in/out effects
4. BEST PRACTICES:
- Test any changes with different message types
- Preserve the iframe implementation for WebRTC functionality
- Maintain opacity/transform animations for smooth message display
- Keep the maximum message count logic intact
This overlay works by:
1. Receiving messages through WebRTC data channels (iframe) or WebSocket
2. Creating message DOM elements with proper styling based on message type
3. Animating messages in/out with CSS transitions
4. Managing scroll position to keep newest messages visible
5. Removing oldest messages when count exceeds MAX_MESSAGES
-->
<html lang="en">
<head>
<script type="text/javascript" src="./js/local-server-url.js"></script>
<script type="text/javascript" src="./js/transport-dedupe.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat Overlay</title>
<script src="./libs/colours.js" type="text/javascript"></script>
<!-- Links to Social Stream Ninja resources -->
<link rel="canonical" href="https://socialstream.ninja/">
<meta name="generator" content="Social Stream Ninja - https://github.com/steveseguin/social_stream">
<style>
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: #0000; /* Transparent background for overlay */
overflow: hidden; /* Prevent body scrollbars */
box-sizing: border-box;
}
* {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-rendering: optimizeLegibility;
}
#chat-container {
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%); /* Center horizontally */
width: 100%;
max-width: 350px;
height: 100vh; /* Or your desired fixed height for the chat box */
overflow: hidden; /* Crucial: content exceeding this will be hidden */
padding: 0 25px; /* Horizontal padding only on the main container */
box-sizing: border-box;
}
#message-list-wrapper {
width: 100%;
transition: transform 0.5s ease-out;
/* This element now starts at the top of chat-container by default.
It grows downwards as messages are added.
The transform will pull it upwards to keep newest messages visible. */
}
.message {
background: linear-gradient(145deg, #1a2a48, #162238);
padding: 15px;
margin-bottom: 15px;
border-radius: 15px;
border: 1px solid #2e3b55;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
color: #e4e6eb;
opacity: 0;
transform: translateY(30px);
transition: opacity 0.6s ease-out, transform 0.6s ease-out,
max-height 0.5s ease-in-out, margin-bottom 0.5s ease-in-out, padding-top 0.5s ease-in-out, padding-bottom 0.5s ease-in-out, border-width 0.5s ease-in-out, filter 0.3s ease-out;
flex-shrink: 0;
overflow: hidden;
max-height: 500px;
box-sizing: border-box;
position: relative;
--scroll-opacity: 1;
filter: opacity(var(--scroll-opacity));
will-change: transform, opacity, filter;
}
.message.proximity-fading {
--scroll-opacity: 0;
}
.message-with-edge {
border-left: 4px solid; /* Width will be consistent but color will be set dynamically */
padding-left: 11px; /* Reduce left padding to account for the border */
}
.message.visible {
opacity: 1;
transform: translateY(0);
}
.message.fading {
opacity: 0;
transition: opacity 1.5s ease-out;
}
.message.hidden {
opacity: 0;
transform: translateY(-30px);
max-height: 0px !important;
padding-top: 0px !important;
padding-bottom: 0px !important;
margin-bottom: 0px !important; /* This is important for accurate height calculation if we were animating out */
border-width: 0px !important;
}
/* Reverse mode: messages slide down instead of up */
.reverse-mode .message {
transform: translateY(-30px);
}
.reverse-mode .message.visible {
transform: translateY(0);
}
.reverse-mode .message.hidden {
transform: translateY(30px);
}
.avatar-wrapper {
width: 50px;
height: 50px;
border-radius: 50%;
display: inline-block;
margin-right: 10px;
position: relative;
overflow: hidden;
background: linear-gradient(45deg, #4ab8f9, #4cd137);
padding: 2px;
vertical-align: middle;
flex-shrink: 0;
}
#spacer {
width: 100%;
/* height is controlled by inline style & JS */
padding: 0;
margin: 0;
border: none;
box-sizing: border-box;
/* The spacer should not be visible or interactable */
background: transparent;
opacity: 1; /* ensure it contributes to layout if needed, but it's transparent */
transform: none;
transition: none; /* Height changes should be immediate, not animated */
}
.name { color: white; font-size: 1.1em; font-weight: 700; max-width: 200px; overflow: hidden;}
.text { font-size: 0.9em; line-height: 1.4; margin: 8px 0; word-wrap: break-word; }
.text img, .text svg { max-width:100px; max-height: 24px; vertical-align: bottom; }
.donation { color: #4cd137; font-weight: 600; }
.name-bg { background-color: #4a6fa5; padding: 4px 8px; border-radius: 5px; display: inline-block; vertical-align: middle; margin-right: 8px; }
.membership-status { color: #ffd700; font-weight: 700; font-size: 0.85em; margin-bottom: 5px; }
.large-image { max-width: 100%; height: auto; margin-top: 10px; border-radius: 10px; display: block; }
.avatar { width: 100%; height: 100%; border-radius: 50%; background-size: cover; background-position: center; }
.source-icon { width: 18px; height: 18px; vertical-align: middle; margin-left: 5px; }
.badge { display: inline-block; height: 0.9em; margin-left: 4px; vertical-align: middle; }
.message-content-wrapper {
display: flex;
align-items: flex-start;
border-radius: 8px;
}
.message-text-meta {
flex-grow: 1;
min-width: 0;
}
.badge.svg { overflow: hidden; }
.badge.svg svg { display: block; width: auto !important; height: 100% !important; max-width: 100px !important; max-height: 100% !important; }
</style>
</head>
<body>
<div id="chat-container">
<div id="message-list-wrapper">
<div id="spacer" style="height: 2200px; width: 100%;"></div>
</div>
</div>
<script>
var urlParams = new URLSearchParams(window.location.search);
var SERVER_EXCLUSIVE_TRANSPORT_VERSION = "3.52.0";
// Transport migration guard: only pages with full server parity may skip the legacy bridge.
var TRANSPORT_CAPABILITIES = {
serverFeed: true,
serverTargeted: false,
upstreamCommands: false,
customChannel: false,
legacyBridgeRequired: false,
exclusiveServerParams: { server: false, server2: true, server3: false }
};
function versionAtLeast(value, minimum) {
var a = String(value || "").split(".");
var b = String(minimum || "").split(".");
var len = Math.max(a.length, b.length);
for (var i = 0; i < len; i++) {
var av = parseInt(a[i], 10) || 0;
var bv = parseInt(b[i], 10) || 0;
if (av > bv) return true;
if (av < bv) return false;
}
return true;
}
// Only feed-carrying server params listed by this page may disable the bridge; server3 alone is command-only.
function hasExclusiveServerTransport(caps) {
var params = caps && typeof caps.exclusiveServerParams === "object" ? caps.exclusiveServerParams : { server2: true };
return (params.server === true && urlParams.has("server")) ||
(params.server2 !== false && urlParams.has("server2")) ||
(params.server3 === true && urlParams.has("server2") && urlParams.has("server3"));
}
function useServerOnlyTransport() {
var caps = (typeof TRANSPORT_CAPABILITIES === "object" && TRANSPORT_CAPABILITIES) ? TRANSPORT_CAPABILITIES : {};
return hasExclusiveServerTransport(caps) &&
versionAtLeast(urlParams.get("v"), SERVER_EXCLUSIVE_TRANSPORT_VERSION) &&
caps.serverFeed === true &&
caps.legacyBridgeRequired !== true &&
caps.upstreamCommands !== true &&
caps.customChannel !== true;
}
var roomID = "iWWnKL28tQ";
if (urlParams.has("session")) {
roomID = urlParams.get("session");
}
var password = urlParams.get("password") || urlParams.get("pass") || urlParams.get("pw") || "false";
var featuredMode = false;
var reversed = urlParams.has("reverse");
var deleteOnlyLast = urlParams.has("deleteonlylast");
const chatContainer = document.getElementById('chat-container');
const messageListWrapper = document.getElementById('message-list-wrapper');
const MAX_MESSAGES = urlParams.has("limit") ? parseInt(urlParams.get("limit")) : 20; // Adjust as needed
const showtime = urlParams.has("showtime") ? parseInt(urlParams.get("showtime")) : 30000;
const topSpacer = document.getElementById('spacer');
const initialSpacerHeight = topSpacer.style.height || '2200px';
var overlayMessageCounter = 0;
var localBlockUserList = {};
var timedOutUsers = {};
// In reverse mode, hide the spacer and position container at top
if (reversed) {
topSpacer.style.display = 'none';
chatContainer.style.bottom = 'auto';
chatContainer.style.top = '0';
chatContainer.classList.add('reverse-mode');
}
const messageTimestamps = new Map();
const MAX_SPACER_HEIGHT_BEFORE_RESET = 18000000;
const PROXIMITY_FADE_DISTANCE = (() => {
if (urlParams.has("fadezone")) {
const parsed = parseInt(urlParams.get("fadezone"), 10);
if (!Number.isNaN(parsed) && parsed >= 0) {
return parsed;
}
}
return 160; // Pixels before reaching top where fade begins
})();
// Gradually fade out each message as it approaches the edge of the chat container.
// In normal mode, fades at top edge. In reverse mode, fades at bottom edge.
function updateProximityFade() {
const messages = Array.from(messageListWrapper.children).filter(
child => child.id !== 'spacer' && child.classList.contains('message')
);
if (messages.length === 0) {
requestAnimationFrame(updateProximityFade);
return;
}
const containerRect = chatContainer.getBoundingClientRect();
const fadeDistance = PROXIMITY_FADE_DISTANCE;
messages.forEach(message => {
if (!message.isConnected) {
return;
}
if (fadeDistance <= 0) {
if (message.style.getPropertyValue('--scroll-opacity') !== '1') {
message.style.removeProperty('--scroll-opacity');
}
message.classList.remove('proximity-fading');
delete message.dataset.proximityFadeStarted;
return;
}
const rect = message.getBoundingClientRect();
// In reverse mode, measure distance from bottom edge; otherwise from top
const distanceFromEdge = reversed
? (containerRect.bottom - rect.bottom)
: (rect.top - containerRect.top);
const fadeStarted = message.dataset.proximityFadeStarted === 'true';
if (distanceFromEdge <= fadeDistance) {
if (!fadeStarted) {
message.dataset.proximityFadeStarted = 'true';
message.classList.add('proximity-fading');
message.style.setProperty('--scroll-opacity', '0');
}
return;
}
if (!fadeStarted) {
if (message.classList.contains('proximity-fading')) {
message.classList.remove('proximity-fading');
}
message.style.removeProperty('--scroll-opacity');
}
});
requestAnimationFrame(updateProximityFade);
}
requestAnimationFrame(updateProximityFade);
function adjustMessageWrapperScroll() {
if (reversed) return; // No transform needed in reverse mode
requestAnimationFrame(() => {
const wrapperScrollHeight = messageListWrapper.scrollHeight;
const containerClientHeight = chatContainer.clientHeight;
let translateY = 0;
if (wrapperScrollHeight > containerClientHeight) {
translateY = -(wrapperScrollHeight - containerClientHeight);
}
translateY = Math.min(0, translateY);
messageListWrapper.style.transform = `translateY(${translateY}px)`;
});
}
function getOverlayMessages() {
return Array.from(messageListWrapper.children).filter(function(child) {
return child.id !== 'spacer' && child.classList.contains('message');
});
}
function getPayloadMessageId(data) {
if (data.mid !== undefined && data.mid !== null && data.mid !== "") {
return String(data.mid);
}
if (data.id !== undefined && data.id !== null && data.id !== "") {
return String(data.id);
}
if (data.messageId !== undefined && data.messageId !== null && data.messageId !== "") {
return String(data.messageId);
}
if (data.message_id !== undefined && data.message_id !== null && data.message_id !== "") {
return String(data.message_id);
}
if (data.meta && data.meta.messageId !== undefined && data.meta.messageId !== null && data.meta.messageId !== "") {
return String(data.meta.messageId);
}
if (data.meta && data.meta.message_id !== undefined && data.meta.message_id !== null && data.meta.message_id !== "") {
return String(data.meta.message_id);
}
overlayMessageCounter += 1;
return 'msg-' + Date.now() + '-' + overlayMessageCounter;
}
function removeOverlayMessage(message) {
if (!message) {
return;
}
messageTimestamps.delete(message.id);
if (message.parentNode) {
message.parentNode.removeChild(message);
}
}
function getNewestMessage(messages) {
return messages.reduce(function(newest, message) {
const newestNumber = Number(newest.dataset.mid);
const messageNumber = Number(message.dataset.mid);
if (!Number.isNaN(newestNumber) && !Number.isNaN(messageNumber)) {
return newestNumber > messageNumber ? newest : message;
}
return (messageTimestamps.get(newest.id) || 0) > (messageTimestamps.get(message.id) || 0) ? newest : message;
});
}
function removeMatchingMessages(matches, onlyLast) {
const messages = getOverlayMessages().filter(matches);
if (!messages.length) {
return false;
}
if (onlyLast) {
removeOverlayMessage(getNewestMessage(messages));
} else {
messages.forEach(removeOverlayMessage);
}
adjustMessageWrapperScroll();
return true;
}
function deleteMessageById(id) {
if (id === undefined || id === null || id === "") {
return false;
}
const idString = String(id);
return removeMatchingMessages(function(message) {
return message.dataset.mid === idString || message.id === idString;
}, true);
}
function deleteMessages(data, onlyLast) {
if (!data.delete || typeof data.delete !== "object") {
return false;
}
const deleteData = data.delete;
return removeMatchingMessages(function(message) {
if (deleteData.id !== undefined && deleteData.id !== null && deleteData.id !== "") {
const idString = String(deleteData.id);
return message.dataset.mid === idString || message.id === idString;
}
if (deleteData.chatname && message.dataset.chatname !== String(deleteData.chatname)) {
return false;
}
if (deleteData.type && message.dataset.sourceType !== String(deleteData.type)) {
return false;
}
return !!(deleteData.chatname || deleteData.type);
}, onlyLast || deleteData.onlyLast);
}
function removeUserMessages(username, type) {
if (!username) {
return false;
}
username = String(username);
type = type ? String(type) : "";
return removeMatchingMessages(function(message) {
if (message.dataset.chatname !== username) {
return false;
}
if (!type) {
return true;
}
return message.dataset.sourceType === type || (type === "youtube" && message.dataset.sourceType === "youtubeshorts");
}, false);
}
function addLocalBlock(username, type) {
if (!username) {
return;
}
username = String(username);
const types = type ? [String(type)] : ["*"];
if (type === "youtube") {
types.push("youtubeshorts");
}
if (!localBlockUserList[username]) {
localBlockUserList[username] = [];
}
types.forEach(function(blockType) {
if (!localBlockUserList[username].includes(blockType)) {
localBlockUserList[username].push(blockType);
}
});
setTimeout(function() {
if (!localBlockUserList[username]) {
return;
}
types.forEach(function(blockType) {
const index = localBlockUserList[username].indexOf(blockType);
if (index !== -1) {
localBlockUserList[username].splice(index, 1);
}
});
if (!localBlockUserList[username].length) {
delete localBlockUserList[username];
}
}, 10000);
}
function isMessageBlocked(data) {
if (!data.chatname) {
return false;
}
const chatname = String(data.chatname);
const type = data.type ? String(data.type) : "";
if (localBlockUserList[chatname] && (localBlockUserList[chatname].includes(type) || localBlockUserList[chatname].includes("*"))) {
return true;
}
if (type && timedOutUsers[chatname + ":" + type]) {
if (timedOutUsers[chatname + ":" + type] < Date.now()) {
delete timedOutUsers[chatname + ":" + type];
} else {
return true;
}
}
return false;
}
function clearAllMessages() {
getOverlayMessages().forEach(removeOverlayMessage);
topSpacer.style.height = initialSpacerHeight;
messageListWrapper.style.transform = '';
adjustMessageWrapperScroll();
}
function handleControlPayload(data) {
if (!data || typeof data !== "object") {
return false;
}
if ("deleteMessage" in data) {
deleteMessageById(data.deleteMessage);
return true;
}
if ("clearAll" in data || data.action === "clear" || data.action === "clearAll") {
clearAllMessages();
return true;
}
if ("delete" in data) {
deleteMessages(data, deleteOnlyLast);
return true;
}
if ("timeoutUser" in data && data.timeoutUser) {
const timeoutName = data.timeoutUser.username || data.timeoutUser.chatname;
if (timeoutName && data.timeoutUser.type && data.timeoutUser.duration) {
timedOutUsers[String(timeoutName) + ":" + String(data.timeoutUser.type)] = Date.now() + (Number(data.timeoutUser.duration) * 1000);
}
removeUserMessages(timeoutName, data.timeoutUser.type);
return true;
}
if ("blockUser" in data && data.blockUser) {
const blockName = data.blockUser.chatname || data.blockUser.username;
addLocalBlock(blockName, data.blockUser.type);
removeUserMessages(blockName, data.blockUser.type);
return true;
}
return false;
}
setInterval(() => {
const now = Date.now();
const messages = getOverlayMessages();
messages.forEach(message => {
const timestamp = messageTimestamps.get(message.id);
if (timestamp) {
const age = now - timestamp;
if (showtime > 0 && age > showtime && !message.classList.contains('fading')) {
message.classList.add('fading');
}
}
});
}, 1000);
// Cross-transport dedupe: in server mode the same feed message can arrive over both
// the websocket relay and the VDO.Ninja bridge; render only the first copy.
var CROSS_TRANSPORT_DEDUPE_TTL_MS = 12000;
var recentTransportDeliveries = new Map();
var transportDeliveryDedupe = typeof SocialStreamTransportDedupe !== "undefined" ? SocialStreamTransportDedupe.create() : null;
function isDuplicateTransportDelivery(data, source) {
if (!(urlParams.has("server") || urlParams.has("server2") || urlParams.has("server3"))) {
return false;
}
if (transportDeliveryDedupe) return transportDeliveryDedupe(data, source);
if (!data || typeof data !== "object") {
return false;
}
if (data.id === undefined || data.id === null) {
return false;
}
if ("action" in data || "get" in data || "callback" in data || "mid" in data) {
return false;
}
if (!(data.chatname || data.chatmessage || data.chatimg || data.hasDonation || data.membership || data.event || data.contentimg)) {
return false;
}
var key =
String(data.id) +
"|" + (data.type || "") +
"|" + (data.chatname || "") +
"|" + String(data.chatmessage || "").slice(0, 100) +
"|" + (data.hasDonation || "") +
"|" + (data.event || "");
var now = Date.now();
recentTransportDeliveries.forEach(function (seenAt, seenKey) {
if (now - seenAt > CROSS_TRANSPORT_DEDUPE_TTL_MS) {
recentTransportDeliveries.delete(seenKey);
}
});
if (recentTransportDeliveries.has(key)) {
return true;
}
recentTransportDeliveries.set(key, now);
return false;
}
function addMessageToOverlay(data, source) {
if (isDuplicateTransportDelivery(data, source)) {
return;
}
if (handleControlPayload(data)) {
return;
}
if (!data || typeof data !== "object") {
return;
}
if (isMessageBlocked(data)) {
return;
}
if (!data.chatname && !data.chatmessage && !data.hasDonation && !data.donation && !data.contentimg){
return;
}
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
// Generate unique ID and track timestamp for aging
const messageId = getPayloadMessageId(data);
messageDiv.id = messageId;
messageDiv.dataset.mid = messageId;
if (data.chatname) {
messageDiv.dataset.chatname = data.chatname;
}
if (data.type) {
messageDiv.dataset.sourceType = data.type;
}
messageTimestamps.set(messageId, Date.now());
var chatbadgesHtml = "";
if (data.chatbadges) {
data.chatbadges.forEach(badge => {
if (typeof badge === "object") {
if (badge.type === "img" && badge.src) {
chatbadgesHtml += `<img class='badge' src='${badge.src}' alt='badge'>`;
} else if (badge.type === "svg" && badge.html) {
chatbadgesHtml += `<span class='badge svg'>${badge.html}</span>`;
}
} else {
chatbadgesHtml += `<img class='badge' src='${badge}' alt='badge'>`;
}
});
}
let avatarBorderColor = "linear-gradient(45deg, #4ab8f9, #4cd137)"; // Default gradient
if (data.nameColor) {
avatarBorderColor = `linear-gradient(45deg, ${data.nameColor}, ${data.nameColor}88)`;
}
let leftEdgeColor = null;
if (typeof getColorFromType === 'function' && data.type) {
try {
leftEdgeColor = getColorFromType(data.type);
} catch (e) {
console.error("Error getting color from type:", e);
}
}
if (leftEdgeColor) {
messageDiv.style.borderLeft = `4px solid ${leftEdgeColor}`;
messageDiv.style.paddingLeft = '12px';
}
const sourceIconHtml = data.type ? `<img src="https://socialstream.ninja/sources/images/${data.type}.png" alt="Source" class="source-icon">` : '';
const avatarHtml = data.chatimg ?
`<div class="avatar-wrapper" style="background: ${avatarBorderColor}"><div class="avatar" style="background-image: url('${data.chatimg}');"></div></div>` : '';
const nameHtml = data.chatname ? `<div class="name-bg"><div class="name" ${data.nameColor ? `style="text-shadow: 0 0 3px ${data.nameColor}"` : ''}>${data.chatname}</div></div>` : '';
const membershipHtml = data.membership ? `<div class="membership-status">${data.membership}</div>` : '';
const messageTextHtml = `<div class="text" ${data.event ? 'style="font-style: italic;"' : ''}>${data.chatmessage ? data.chatmessage : ''}</div>`;
const donationHtml = data.hasDonation ? `<div class="donation">${data.hasDonation}</div>` : '';
const contentImgHtml = data.contentimg ?
`<div style="padding: 3px; border-radius: 12px; background: ${avatarBorderColor}; display: inline-block; margin-top: 10px;">
<img src="${data.contentimg}" alt="Content Image" class="large-image" style="margin-top: 0; border-radius: 8px;" onerror="this.parentNode.style.display='none';">
</div>` : '';
messageDiv.innerHTML = `
<div class="message-content-wrapper">
${avatarHtml}
<div class="message-text-meta">
<div>
${nameHtml}
${sourceIconHtml}
${chatbadgesHtml}
</div>
${membershipHtml}
${messageTextHtml}
${donationHtml}
</div>
</div>
${contentImgHtml}
`;
if (reversed) {
// In reverse mode, insert new messages at the top (after spacer if present)
const firstMessage = messageListWrapper.querySelector('.message');
if (firstMessage) {
messageListWrapper.insertBefore(messageDiv, firstMessage);
} else {
messageListWrapper.appendChild(messageDiv);
}
} else {
messageListWrapper.appendChild(messageDiv);
}
void messageDiv.offsetWidth;
messageDiv.classList.add('visible');
const messages = getOverlayMessages();
if (messages.length > MAX_MESSAGES) {
// In reverse mode, oldest messages are at the bottom (end of array)
const oldestMessage = reversed ? messages[messages.length - 1] : messages[0];
if (oldestMessage) {
if (!reversed) {
const messageStyle = getComputedStyle(oldestMessage);
const marginBottom = parseFloat(messageStyle.marginBottom) || 0;
const spaceOccupiedByOldestMessage = oldestMessage.offsetHeight + marginBottom;
const currentSpacerHeight = parseFloat(topSpacer.style.height) || 0;
let newPotentialSpacerHeight = currentSpacerHeight + spaceOccupiedByOldestMessage;
if (newPotentialSpacerHeight > MAX_SPACER_HEIGHT_BEFORE_RESET) {
console.log(`Spacer height (${newPotentialSpacerHeight}px) exceeded threshold (${MAX_SPACER_HEIGHT_BEFORE_RESET}px). Resetting to 0px.`);
newPotentialSpacerHeight = 0; // Reset the height
}
topSpacer.style.height = newPotentialSpacerHeight + 'px';
}
removeOverlayMessage(oldestMessage);
}
}
adjustMessageWrapperScroll();
}
if (!useServerOnlyTransport()) {
var iframe = document.createElement("iframe");
if (featuredMode){
iframe.src = `https://vdo.socialstream.ninja/?ln&password=${encodeURIComponent(password)}&salt=vdo.ninja&label=overlay&exclude=${roomID}&scene&novideo&noaudio&cleanoutput&room=${roomID}`;
} else {
iframe.src = "https://vdo.socialstream.ninja/?ln&salt=vdo.ninja&password="+encodeURIComponent(password)+"&push&label=dock&vd=0&ad=0&novideo&noaudio&autostart&cleanoutput&room="+roomID;
}
iframe.style.cssText = "width: 0px; height: 0px; position: fixed; left: -100px; top: -100px;";
document.body.appendChild(iframe);
window.addEventListener("message", function (e) {
if (e.source != iframe.contentWindow) return;
if (e.data && typeof e.data === "object" && e.data.dataReceived && typeof e.data.dataReceived === "object" && e.data.dataReceived.overlayNinja) {
addMessageToOverlay(e.data.dataReceived.overlayNinja, "p2p");
}
});
}
var conCon = 1;
var socketserver = false;
var serverURL = urlParams.has("localserver") ? SocialStreamLocalServer.getWebSocketUrl() : "wss://io.socialstream.ninja";
function setupSocket(){
socketserver.onclose = function (){
setTimeout(function(){
conCon+=1;
socketserver = new WebSocket(serverURL);
setupSocket();
},100*conCon);
};
socketserver.onopen = function (){
conCon = 1;
socketserver.send(JSON.stringify({"join":roomID, "out":3, "in":4}));
};
socketserver.addEventListener('message', function (event) {
if (event.data){
try {
var data = JSON.parse(event.data);
if (data) {
addMessageToOverlay(data, "server");
}
} catch (error) {
console.error("Error processing WebSocket message:", error, "Data:", event.data);
}
}
});
}
if (urlParams.has("server") || urlParams.has("server2")){
serverURL = urlParams.get("server") || urlParams.get("server2") || serverURL;
socketserver = new WebSocket(serverURL);
setupSocket();
}
</script>
</body>
</html>