forked from izzy/stream-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.html
1641 lines (1435 loc) · 64.2 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
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@500;800&display=swap" rel="stylesheet">
<style>
body,
html,
#chat {
background: transparent;
padding: 0;
margin: 0;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
overflow: hidden;
}
@keyframes append-animate {
from {
height: 0;
opacity: 0;
}
to {
height: auto;
opacity: 1;
}
}
@keyframes chat {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
.chat-message {
animation: append-animate .3s linear;
transition: max-height 0.3s ease-out;
height: auto;
word-break: normal;
overflow-wrap: break-word;
hyphens: auto;
hyphenate-character: "»";
}
.msg-badges {
vertical-align: middle;
}
.msg-user::after {
content: ": ";
}
.msg-pronoun {
padding: 0rem 0.3rem;
}
.msg-badges>img {
width: 1rem;
box-shadow: 0 0 2px black;
}
.msg-text>img {
vertical-align: middle;
width: 1.4rem;
}
.msg-text>.announcement {
font-weight: bold;
}
.msg-timestamp {
padding: 0rem 0.2em;
font-size: 0.8em;
}
.msg-user {
font-weight: bold;
}
#version-notice {
width: 100%;
position: absolute;
top: 0;
left: 0;
background: #ffc39a;
padding: 1rem;
font-size: 2rem;
font-weight: bold;
color: #950000;
box-sizing: border-box;
}
#connection-status {
width: 100%;
position: fixed;
bottom: 0;
left: 0;
padding: 1rem;
font-size: 1.5rem;
font-weight: bold;
color: #410000;
background-color: #ff827c;
box-sizing: border-box;
}
</style>
<style type="text" id="enable-bubbles">
#chat {
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.chat-message {
margin: 0 5px 5px 5px;
background-color: rgba(0, 0, 0, 0.6);
border: 2px solid #ffe0f0;
border-radius: 6px;
color: white;
}
.msg-text,
.msg-user {
padding: 0.4rem;
display: block;
}
.msg-user {
background: #ffe0f0;
font-weight: bold;
color: black;
}
.msg-pronoun {
right: 1em;
position: absolute;
}
.msg-timestamp {
border: 1px solid transparent;
border-radius: 1rem;
background-color: rgba(0, 0, 0, 0.6);
margin-right: 0.25rem;
vertical-align: middle;
padding: 0.1rem 0.3rem;
color: white;
font-size: .6rem;
}
.msg-user {
font-weight: normal;
}
.msg-user::after {
content: "";
}
.msg-text>.announcement {
display: block;
}
</style>
<style id="enable-horizontal" type="text">
#chat {
display: flex;
flex-direction: row;
justify-content: flex-end;
white-space: nowrap;
}
.chat-message {
margin-left: 1rem;
float: left;
}
.msg-user, .msg-text {
padding: 0.4rem;
}
</style>
<style id="horizontal-bubbles" type="text">
.msg-pronoun {
position: inherit;
}
.msg-user {
text-align: right;
}
.msg-text>.announcement {
display: inline;
}
</style>
<style id="user-styles" type="text/css"></style>
</head>
<body>
<div id="chat"></div>
<div id="version-notice" style="display: none;"></div>
<div id="connection-status" style="display: none;"></div>
<script>
const STREAMCHAT_VERSION = '0.3.3';
const STREAMCHAT_GH_USER = 'izzy';
const STREAMCHAT_GH_REPO = 'stream-chat';
class ConnectionStatus {
/**
* @type {number}
* @value 0
*/
static DISCONNECTED = 0;
/**
* @type {number}
* @value 1
*/
static CONNECTING = 1;
/**
* @type {number}
* @value 2
*/
static CONNECTED = 2;
/**
* @type {number}
* @value 3
*/
static ERROR = 3;
/**
* @type {Object.<string, number>}
*/
status = {
'StreamerBot': ConnectionStatus.DISCONNECTED,
'BeanBot': ConnectionStatus.DISCONNECTED
}
update() {
let status = ConnectionStatus.CONNECTED;
for (let key in this.status) {
if (!config['plugins'].hasOwnProperty(key.toLowerCase()) || config['plugins'][key.toLowerCase()]['enabled'] == false) {
continue;
}
if (this.status[key] == ConnectionStatus.DISCONNECTED) {
status = ConnectionStatus.DISCONNECTED;
break;
} else if (this.status[key] == ConnectionStatus.CONNECTING) {
status = ConnectionStatus.CONNECTING;
}
}
let status_text = "",
status_div = document.getElementById("connection-status");
switch (status) {
case ConnectionStatus.DISCONNECTED:
status_text = "disconnected. Is your bot running?";
status_div.style.display = "block";
break;
case ConnectionStatus.CONNECTING:
status_text = "connecting";
status_div.style.display = "block";
break;
case ConnectionStatus.CONNECTED:
status_div.style.display = "none";
break;
case ConnectionStatus.ERROR:
status_text = "experiencing an error";
status_div.style.display = "block";
break;
}
status_div.innerText = "You are currently " + status_text;
}
}
const PLUGIN_LIST = Object.keys(new ConnectionStatus().status);
window.CONNECTION_STATUS = new Proxy(new ConnectionStatus(), {
get(target, name, receiver) {
if (PLUGIN_LIST.includes(name)) {
return Reflect.get(target['status'], name, receiver);
} else {
return Reflect.get(target, name, receiver);
}
},
set(target, name, value, receiver) {
if (PLUGIN_LIST.includes(name)) {
target['status'][name] = value;
target.update();
} else {
return Reflect.set(target, name, value, receiver);
}
}
});
/**
* Checks the current version of streamchat against the latest release on GitHub.
* @returns {string} A message indicating whether the current version is up to date or not.
*/
async function version_check() {
const res = await fetch(`https://api.github.com/repos/${STREAMCHAT_GH_USER}/${STREAMCHAT_GH_REPO}/releases/latest`)
.then(response => response.json())
.then(data => {
const version = data.tag_name.replace(/^v/i, "");
const version_parts = version.split('.');
const current_version_parts = STREAMCHAT_VERSION.split('.');
let upToDate = true;
if (version_parts[0] > current_version_parts[0]) {
console.debug(`${STREAMCHAT_GH_REPO} version ${STREAMCHAT_VERSION} is outdated. There is a major update to version ${data.tag_name} available.`);
upToDate = false;
} else if ((version_parts[0] === current_version_parts[0] && version_parts[1] > current_version_parts[1]) ||
(version_parts[0] === current_version_parts[0] && version_parts[1] === current_version_parts[1] && version_parts[2] > current_version_parts[2])) {
console.debug(`${STREAMCHAT_GH_REPO} version ${STREAMCHAT_VERSION} is outdated. Please update to version ${data.tag_name}`);
upToDate = false;
} else {
console.debug(`${STREAMCHAT_GH_REPO} version ${STREAMCHAT_VERSION} is up to date or newer than the latest release ${data.tag_name}`);
}
return { version: version, upToDate: upToDate, error: null };
}).catch(error => {
console.error(error);
return { version: 'unknown', upToDate: true, error: error };
});
return res;
}
function getRnd(max, min = 0) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* @param {string} p The search parameter name
* @param {boolean} d The default value
* @returns {boolean} The value of the search parameter, or the default value if the parameter is not set.
*/
function searchParamIsTrue(p, d = false) {
let v = new URLSearchParams(window.location.search).get(p);
if (v === undefined || v === null) {
return d;
}
return String(v).toLowerCase() === 'true' || String(v) === '1';
}
function searchParamOrDefault(p, d = null) {
let v = new URLSearchParams(window.location.search).get(p);
if (v === null) {
return d;
}
return v;
}
function htmlentities(str) {
return str.replace(/[\u00A0-\u9999<>\&]/gim, (i) => {
return '&#' + i.charCodeAt(0) + ';';
});
}
/**
* @param {string} tag The HTML tag name
* @param {object} attributes Attributes for the HTML tag
* @param {string} text The innerText of the HTML tag
* @returns {string}
*/
function createElement(tag, attributes, text = false) {
let element = document.createElement(tag);
if (attributes !== undefined) {
for (let key in attributes) {
element.setAttribute(key, attributes[key]);
}
}
if (text !== undefined && text !== false) {
element.innerText = text;
}
return element;
}
function parseURL() {
let url = new URL(document.URL);
direction = 'vertical';
if (url.searchParams.get("direction") !== null) {
direction = url.searchParams.get("direction").toLowerCase() === 'horizontal' ? 'horizontal' :
'vertical';
}
const get_color = (p, d = null) => {
let u = url.searchParams.get(p);
if (u === null) {
return d;
}
let c = {
r: u.slice(0, 2),
g: u.slice(2, 4),
b: u.slice(4, 6)
};
return c;
}
let colors = {
'page_background': get_color('background'),
'text_background': get_color('bubble_color'),
'text': get_color('text_color'),
'message': get_color('msg_color'),
'announcement': {
'text': get_color('announcement_color'),
'background': get_color('announcement_bg_color'),
},
'highlight': {
'text': get_color('highlight_color'),
'background': get_color('highlight_bg_color'),
},
'default': get_color('default_color', {
r: 'ff',
g: 'e0',
b: 'f0'
}),
'pastel': searchParamIsTrue("pastel"),
'bubble_border': get_color("bubble_border_color"),
}
let cmdprefix = null;
if (url.searchParams.get("cmdprefix") !== null) {
cmdprefix = url.searchParams.get("cmdprefix");
}
let bot_list = [];
if (url.searchParams.get("bots") !== null) {
bot_list = url.searchParams.get("bots").toLowerCase().split(',');
}
// TODO: Implement different timestamp formats
let timestamp = false;
let timestamp_locale = 'en-US';
let timestamp_options = {
hour: '2-digit',
minute: '2-digit'
};
if (url.searchParams.get("timestamp") !== null) {
timestamp = searchParamIsTrue("timestamp");
}
if (url.searchParams.get("timestamp_locale") !== null) {
timestamp_locale = url.searchParams.get("timestamp_locale");
}
// Streamer.Bot specific configuration
let streamerbotEnabled = true;
let streamerbotConfig = {
'enabled': false,
'twitch': false,
'youtube': false,
'websocket': '',
};
if (streamerbotEnabled === searchParamIsTrue("sb_enabled", true)) {
let sb_ws_uri = 'ws://127.0.0.1:8080/';
if (url.searchParams.get("sb_ws_uri") !== null) {
sb_ws_uri = decodeURI(url.searchParams.get("sb_ws_uri"));
}
streamerbotConfig = {
'enabled': streamerbotEnabled,
'twitch': searchParamIsTrue("sb_twitch", true),
'youtube': searchParamIsTrue("sb_youtube", true),
'websocket': sb_ws_uri
}
}
// Bean.Bot specific configuration
let beanbotEnabled = true;
let beanbotConfig = {
'enabled': false,
'twitch': false,
'websocket': 'ws://localhost:6969/',
};
return {
'plugins': {
'streamerbot': streamerbotConfig,
'beanbot': {
'enabled': searchParamIsTrue("bb_enabled"),
}
},
'ui': {
'direction': direction,
'bubbles': {
'enabled': searchParamIsTrue("bubbles"),
'border': {
'radius': searchParamOrDefault("bubble_border_radius", null),
'size': searchParamOrDefault("bubble_border_size", null),
}
},
'colors': colors,
'timestamp': {
'enabled': searchParamIsTrue("timestamp"),
'locale': timestamp_locale,
'options': timestamp_options,
},
'fade_duration': searchParamOrDefault("fade_duration", false),
'max_messages': searchParamOrDefault("max_messages", false),
'pronouns': searchParamIsTrue("pronouns", true),
'highlights': searchParamIsTrue("highlights", true),
'announcements': searchParamIsTrue("announcements", true),
'badges': {
'enabled': searchParamIsTrue("badges", true),
'left': searchParamIsTrue("badges_left"),
},
'emote_size': searchParamOrDefault('emote_size', '1.4rem'),
'font': {
'family': searchParamOrDefault("fontfamily", "Open Sans"),
'size': searchParamOrDefault("fontsize", "large")
},
},
'exclusion': {
'cmdprefix': cmdprefix,
'bots': bot_list,
},
'debug': searchParamIsTrue("debug"),
'version': {
'current': STREAMCHAT_VERSION,
'check': searchParamIsTrue("version_check", true),
'alert': searchParamIsTrue("version_alert", false),
}
}
}
const config = parseURL();
console.debug(`Starting stream chat version ${STREAMCHAT_VERSION}`);
console.debug(['Loaded config', config]);
let socket, pronouns_users = {},
pronouns;
// Fill the pronoun cache
// TODO: Handle errors on this
fetch('https://pronouns.alejo.io/api/pronouns').then(response => response.json()).then(data => {
pronouns = data;
});
/**
* Get the pronoun for a user and cache it for future use
*
* @param {string} user The user to get the pronoun for
* @returns {string} The pronoun for the user
*/
async function fetch_pronoun(user) {
user = user.toLowerCase();
if (user in pronouns_users) {
return pronouns_users[user];
} else {
return fetch(`https://pronouns.alejo.io/api/users/${user}`).then(response => response.json()).then(
data => {
pronouns_users[user] = data[0];
return data;
}).catch(() => {
return '';
});
}
}
/**
* Get the pronoun for a user.
* This is a wrapper for fetch_pronoun() that returns either the pronoun or false when pronouns are disabled,
* when pronouns for the user aren't set or the API is unavailable.
*
* @param {string} user The user to get the pronoun for
* @returns {string|bool} The pronoun for the user
*/
function get_pronoun(user) {
user = user.toLowerCase();
if (config['ui']['pronouns'] === true && (user in pronouns_users && pronouns_users[user] !== undefined)) {
if (pronouns_users[user]['pronoun_id'] === "any") {
return "Any";
} else {
return pronouns.filter(p => p.name === pronouns_users[user]['pronoun_id'])[0].display;
}
} else {
return false;
}
}
/**
* Get the text colour for a user.
* TODO: change color to RGB object
*
* @param {Color} color The user's text color
* @returns {string} The calculated color according to settings
*/
function get_text_color(color, noDefault = false) {
// TODO pastel mode for text color
if (config['ui']['colors']['text'] && noDefault === false) {
return config['ui']['colors']['text'];
}
let color_r = parseInt((color.r), 16);
let color_g = parseInt((color.g), 16);
let color_b = parseInt((color.b), 16);
let brightness = Math.round(((parseInt(color_r) * 299) +
(parseInt(color_g) * 587) +
(parseInt(color_b) * 114)) / 1000);
if (brightness < 125) {
return {
r: 'FF',
g: 'FF',
b: 'FF'
};
} else {
return {
r: '00',
g: '00',
b: '00'
};
}
}
/**
* Get the background color for a user.
* @param {Color} color An RGB tuple of the user's text color
* @param {string} override_source Config attribute to take for overrides
* @returns {Color} The background color for the user
*/
function get_user_color(color, override_source = "text_background") {
// TODO pastel mode for background color
if (config['ui']['colors'][override_source]) {
return config['ui']['colors'][override_source];
}
if (color === null || color === undefined) {
return config['ui']['colors']['default'];
} else {
return color;
}
}
/**
* Returns a normalized version of the given color as RGB struct.
* @param {string} color A string containing a color in hexadecimal RGB notation
* @returns {Color} The normalized color tuple
*/
function get_color(color) {
if (color === null || color === undefined) {
return config['ui']['colors']['default'];
} else {
color = color.replace('#', '');
return {
r: color.slice(0, 2),
g: color.slice(2, 4),
b: color.slice(4, 6)
};
}
}
/**
* Returns a hex code from an RGB color struct.
* @param {Color} color An RGB color struct
* @returns {string} A hex color code
*/
function get_color_hex(color) {
if (color === 'transparent') {
return 'transparent';
} else if (color === null || color === undefined) {
return get_color_hex(config['ui']['colors']['default']);
} else {
return `#${color.r}${color.g}${color.b}`;
}
}
/**
* Enum to differenciate between different highlight styles
* Currently only used for Twitch messages
*/
const Highlights = {
None: Symbol('None'),
Highlight: Symbol('Highlight'),
//Mention: Symbol('Mention'), // TODO: Might be worth implementing this?
Announcement: Symbol('Announcement'),
};
let add_message = (id, message, author, color, timestamp, badges = [], highlight = Highlights.None) => {
let background_color = get_user_color(color);
let text_color = get_text_color(background_color);
let el_badges = createElement('span', { 'class': 'msg-badges' });
let el_pronoun = createElement('span', { 'class': 'msg-pronoun' }, author['pronoun']);
let el_message = createElement('span', { 'class': 'msg-text' });
let el_user = createElement('span', { 'class': 'msg-user' }, author.name);
let div_message = createElement('div', {
'id': id,
'data-user-id': author['id'],
'class': 'chat-message',
});
let message_class = [];
if (highlight !== Highlights.None) {
if (highlight === Highlights.Highlight && config['ui']['highlights'] === true) {
div_message.classList.add('highlight');
} else if (highlight === Highlights.Announcement) {
div_message.classList.add('announcement');
}
}
// TODO: omg this code is a mess and needs to get some a e s t h e t i c s
// For one, if we have user configs for some colours, we don't need to overwrite them
// Also, we should probably use CSS variables for some things
// Alternatively maybe just a data attribute with the user's color like
// data-user-color="#123456" and then use that in the CSS with color: attr(data-user-color)
// which would be a lot cleaner and would get the style issues out of the JS code
if (config['ui']['bubbles']['enabled'] === true) {
div_message.classList.add('bubble');
}
// For announcements we want the background to be filled
if (config['ui']['announcements'] === true &&
(highlight === Highlights.Announcement || highlight === Highlights.Highlight)) {
let config_key = highlight === Highlights.Announcement ? 'announcement' : 'highlight';
if (config['ui']['colors'][config_key]['background']) {
background_color = config['ui']['colors'][config_key]['background'];
} else {
background_color = get_user_color(color);
}
if (config['ui']['colors'][config_key]['text']) {
text_color = config['ui']['colors'][config_key]['text'];
} else {
// We calculate the text color based on the new background color
// If we don't do this, the text might be completely unreadable
// Second parameter is noDefault to prevent getting the default
// text color which might or might not work with the new background
text_color = get_text_color(background_color, true);
}
if (config['ui']['bubbles']['enabled'] === true) {
// Announcement/Highlight styling for message bubbles
div_message.style.color = get_color_hex(text_color);
if (config['ui']['colors']['bubble_border']) {
div_message.style.borderColor = get_color_hex(config['ui']['colors']['bubble_border']);
} else {
div_message.style.borderColor = get_color_hex(background_color);
}
el_user.style.color = get_color_hex(text_color);
el_user.style.backgroundColor = get_color_hex(background_color);
} else {
// Announcement/Highlight styling for regular text messages
div_message.style.color = get_color_hex(text_color);
el_message.style.color = get_color_hex(text_color);
}
div_message.style.backgroundColor = get_color_hex(background_color);
// TODO: For announcement, get an enum for the colour of the announcement
// and add a CSS class with nice colours and gradients
} else {
// Everything specific for anything that's not announcement or highlight
if (config['ui']['bubbles']['enabled'] === true) {
if (!config['ui']['colors']['bubble_border']) {
div_message.style.borderColor = get_color_hex(background_color);
}
el_user.style.backgroundColor = get_color_hex(background_color);
} else {
// If we don't use bubbles, we don't use the user colour as background
if (!config['ui']['colors']['text_color']) {
text_color = get_user_color(color, "text_color");
}
}
el_user.style.color = get_color_hex(text_color);
}
if (config['ui']['badges']['enabled'] === true && badges.length > 0) {
for (let badge of badges) {
let el_badge = createElement('img', {
'src': badge["url"],
});
el_badges.appendChild(el_badge);
}
// Add badges to the user line
if (config['ui']['badges']['left'] === false) {
el_user.appendChild(el_badges);
} else {
el_user.prepend(el_badges);
}
}
el_message.innerHTML = message;
// Add the timestamp
if (config['ui']['timestamp']['enabled'] === true) {
let el_timestamp = createElement('span', { 'class': 'msg-timestamp' },
new Date().toLocaleTimeString(config['ui']['timestamp']['locale'],
config['ui']['timestamp']['options'])
);
if (config['ui']['bubbles']['enabled'] === false) {
div_message.appendChild(el_timestamp);
} else {
el_user.prepend(el_timestamp);
}
}
// Add the pronoun to the user line
if (author['pronoun']) {
el_user.appendChild(el_pronoun);
}
// Adds the user line and message to the message div
div_message.appendChild(el_user);
div_message.appendChild(el_message);
document.getElementById('chat').appendChild(div_message);
const element = document.getElementById('chat');
element.scrollTop = element.scrollHeight;
}
let StreamerBot = {
'message': {
'twitch': async (msg_id, user_id, author, author_color, message, emotes = [], role = 0,
badges = [], highlight = Highlights.None, pronoun = false) => {
// TODO handle cheermotes
if (skip_message(message, author)) {
return;
}
if (pronoun === false) {
await fetch_pronoun(author);
pronoun = get_pronoun(author);
}
author = {
name: author,
id: user_id,
pronoun: pronoun === false ? false : `(${pronoun})`,
}
if (emotes.length === 0) {
// XSS protection for the message
message = htmlentities(message);
} else {
let message_index = 0;
let message_new = '';
for (const emote in emotes.sort((a, b) => { if (a.startIndex > b.startIndex) { return 1; } else { return -1; } })) {
const el_emote = document.createElement('img');
// The replace call is a workaround for FFZ emotes, see #35
// https://github.com/izzy/stream-chat/issues/35#issuecomment-1484156496
el_emote.src = emotes[emote].imageUrl.replace('https:https', 'https');;
if (parseInt(config['ui']['emote_size']) > 0) {
el_emote.style.height = `${config['ui']['emote_size']}`;
el_emote.style.width = 'auto';
}
// Add the text before the emote to the message, html escaped
// then add the emote as img element
message_new +=
htmlentities(message.substring(message_index, emotes[emote].startIndex))
+ el_emote.outerHTML;
// This is the index of the next character after the emote
// so we can continue the loop from there
message_index = emotes[emote].endIndex + 1;
}
message_new += htmlentities(message.substring(message_index));
message = message_new;
}
if (badges.length > 0) {
badges = badges.map(badge => {
return {
url: badge["imageUrl"]
};
});
}
if (Highlights.Announcement === highlight) {
message = '<span class="announcement">📢 Announcement: </span>' + message;
}
let color = get_color(author_color);
add_message(msg_id, message, author, color, 0, badges, highlight);
},
'youtube': (message_id, user_id, user_name, message, timestamp, owner, moderator, sponsor,
verified) => {
// TODO: Add message
// TODO: Add youtube logo as badge
// TODO: Add badges for owner, moderator, sponsor, verified
let author = {
name: user_name,
id: user_id,
pronoun: false,
}
let color = get_user_color({
r: 'FF',
g: '00',
b: '00'
});
let badges = [{
'url': 'https://yt3.ggpht.com/m6yqTzfmHlsoKKEZRSZCkqf6cGSeHtStY4rIeeXLAk4N9GY_yw3dizdZoxTrjLhlY4r_rkz3GA=w24-h24-c-k-nd'
}];
message = htmlentities(message);
const yt_emote_width = '28';
const yt_emote_height = '28';
if (parseInt(config['ui']['emote_size']) > 0) {
const yt_emote_width = config['ui']['emote_size'];
const yt_emote_height = config['ui']['emote_size'];
}
const yt_emotes = {
':yt:': `https://yt3.ggpht.com/m6yqTzfmHlsoKKEZRSZCkqf6cGSeHtStY4rIeeXLAk4N9GY_yw3dizdZoxTrjLhlY4r_rkz3GA=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':oops:': `https://yt3.ggpht.com/qByNS7xmuQXsb_5hxW2ggxwQZRN8-biWVnnKuL5FK1zudxIeim48zRVPk6DRq_HgaeKltHhm=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':buffering:': `https://yt3.ggpht.com/foWgzjN0ggMAA0CzDPfPZGyuGwv_7D7Nf6FGLAiomW5RRXj0Fs2lDqs2U6L52Z4J2Zb-D5tCUAA=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':stayhome:': `https://yt3.ggpht.com/u3QDxda8o4jrk_b01YtJYKb57l8Zw8ks8mCwGkiZ5hC5cQP_iszbsggxIWquZhuLRBzl5IEM2w=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':dothefive:': `https://yt3.ggpht.com/ktU04FFgK_a6yaXCS1US-ReFkLjD22XllcIMOyBRHuYKLsrxpVxsauV1gSC2RPraMJWXpWcY=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':elbowbump:': `https://yt3.ggpht.com/gt39CIfizoIAce9a8IzjfrADV5CjTbSyFKUlLMXzYILxJRjwAgYQQJ9PXXxnRvrnTec7ZpfHN4k=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':goodvibes:': `https://yt3.ggpht.com/6LPOiCw9bYr3ZXe8AhUoIMpDe_0BglC4mBmi-uC4kLDqDIuPu4J3ErgV0lEhgzXiBluq-I8j=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':thanksdoc:': `https://yt3.ggpht.com/Av7Vf8FxIp0_dQg4cJrPcGmmL7v9RXraOXMp0ZBDN693ewoMTHbbS7D7V3GXpbtZPSNcRLHTQw=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':videocall:': `https://yt3.ggpht.com/bP-4yir3xZBWh-NKO4eGJJglr8m4dRnHrAKAXikaOJ0E5YFNkJ6IyAz3YhHMyukQ1kJNgQAo=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':virtualhug:': `https://yt3.ggpht.com/-o0Di2mE5oaqf_lb_RI3igd0fptmldMWF9kyQpqKWkdAd7M4cT5ZKzDwlmSSXdcBp3zVLJ41yg=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':yougotthis:': `https://yt3.ggpht.com/WxLUGtJzyLd4dcGaWnmcQnw9lTu9BW3_pEuCp6kcM2pxF5p5J28PvcYIXWh6uCm78LxGJVGn9g=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':sanitizer:': `https://yt3.ggpht.com/4PaPj_5jR1lkidYakZ4EkxVqNr0Eqp4g0xvlYt_gZqjTtVeyHBszqf57nB9s6uLh7d2QtEhEWEc=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':takeout:': `https://yt3.ggpht.com/ehUiXdRyvel0hba-BopQoDWTvM9ogZcMPaaAeR6IA9wkocdG21aFVN_IylxRGHtl2mE6L9jg1Do=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':hydrate:': `https://yt3.ggpht.com/Plqt3RM7NBy-R_eA90cIjzMEzo8guwE0KqJ9QBeCkPEWO7FvUqKU_Vq03Lmv9XxMrG6A3Ouwpg=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':chillwcat:': `https://yt3.ggpht.com/ZN5h05TnuFQmbzgGvIfk3bgrV-_Wp8bAbecOqw92s2isI6GLHbYjTyZjcqf0rKQ5t4jBtlumzw=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':chillwdog:': `https://yt3.ggpht.com/jiaOCnfLX0rqed1sISxULaO7T-ktq2GEPizX9snaxvMLxQOMmWXMmAVGyIbYeFS2IvrMpxvFcQ=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':elbowcough:': `https://yt3.ggpht.com/kWObU3wBMdHS43q6-ib2KJ-iC5tWqe7QcEITaNApbXEZfrik9E57_ve_BEPHO86z4Xrv8ikMdW0=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':learning:': `https://yt3.ggpht.com/LiS1vw8KUXmczimKGfA-toRYXOcV1o-9aGSNRF0dGLk15Da2KTAsU-DXkIao-S7-kCkSnJwt=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':washhands:': `https://yt3.ggpht.com/66Fn-0wiOmLDkoKk4FSa9vD0yymtWEulbbQK2x-kTBswQ2auer_2ftvmrJGyMMoqEGNjJtipBA=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':socialdist': `https://yt3.ggpht.com/0WD780vTqUcS0pFq423D8WRuA_T8NKdTbRztChITI9jgOqOxD2r6dthbu86P6fIggDR6omAPfnQ=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
':shelterin:': `https://yt3.ggpht.com/KgaktgJ3tmEFB-gMtjUcuHd6UKq50b-S3PbHEOSUbJG7UddPoJSmrIzysXA77jJp5oRNLWG84Q=w${yt_emote_width}-h${yt_emote_height}-c-k-nd`,
};
for (const e in yt_emotes) {
let el_emote = document.createElement('img');
el_emote.src = yt_emotes[e];
el_emote.style = `height: ${yt_emote_height}px; width: ${yt_emote_width}px;`;
message = message.replaceAll(e, el_emote.outerHTML);
}
add_message(message_id, message, author, color, timestamp, badges, Highlights.None);
}
},
}
let BeanBot = {
'message': {
'twitch': (message) => {
console.log(message);
}
}
}
/**
* Checks if a message should be skipped.
* @param {string} message The message to check
* @param {string} user The user name to check
* @returns {boolean} True if the message should be skipped, false otherwise
*/
function skip_message(message, user) {
if ((config['exclusion']['cmdprefix'] !== false && message.startsWith(config['exclusion']['cmdprefix'])) ||