-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.js
1197 lines (1135 loc) · 47.1 KB
/
main.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
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
var scrollDistance = 0, // How many pixels are we currently still hiding?
scrollReference = 0, // Distance when we started scrolling
imageExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.webp', '.av1'],
bitLevels = [ 10000, 1000, 500, 100, 1 ],
frames = 0,
fpsInterval,
lastFrameReset,
lastMoveTimeoutId = null,
messageQueue = [],
delayQueue = [],
lastMessageTimestamp = 0,
unreadMessages = 0;
/** Store settings with a local cache. Storing these variables directly in localStorage would remove the variable's type information **/
var Settings = function() {
var transparentBackgroundKeys = ['notice-color', 'highlight-color', 'channel-color'];
// Clone default settings so they can be used to reset
var settings = Object.assign({}, defaultSettings);
// Restore previous settings
var previousSettings = localStorage.getItem('config') !== null ? JSON.parse(localStorage.getItem('config')) : {};
Object.assign(settings, previousSettings);
// Store all settings as CSS variables as well
Object.keys(settings).forEach((key) => document.body.style.setProperty('--' + key, settings[key]));
transparentBackgroundKeys.forEach((key) => document.body.style.setProperty('--' + key.replace('-color', '-background-color'), settings[key] + '50'));
var update = (key, value) => {
settings[key] = value;
localStorage.setItem('config', JSON.stringify(settings));
document.body.style.setProperty('--' + key, value);
// Generate transparent background color values
if (transparentBackgroundKeys.indexOf(key) != -1) {
document.body.style.setProperty(key.replace('-color', '-background-color'), value + '50');
}
};
return {
'get': (key) => settings[key],
'set': update,
'toggle': (key) => update(key, !settings[key]),
'reset': () => {
Object.assign(settings, defaultSettings);
localStorage.setItem('config', JSON.stringify(defaultSettings));
}
}
}();
var HexCompressor = function() {
const characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=#';
return {
color2string: (color) => {
var code = '';
var firstHalf = parseInt(color.substr(1, 3), 16);
code += characters.charAt(Math.floor(firstHalf / 64));
code += characters.charAt(firstHalf % 64);
var secondHalf = parseInt(color.substr(4, 3), 16);
code += characters.charAt(Math.floor(secondHalf / 64));
code += characters.charAt(secondHalf % 64);
return code;
},
string2color: (string) => '#' +
("00" + (characters.indexOf(string.charAt(0)) * 64 + characters.indexOf(string.charAt(1))).toString(16)).slice(-3) +
("00" + (characters.indexOf(string.charAt(2)) * 64 + characters.indexOf(string.charAt(3))).toString(16)).slice(-3)
};
}();
var highlightUsers = Settings.get('highlight-users').toLowerCase().split(',').filter((user) => user != ''),
highlightKeyphrases = Settings.get('highlight-keyphrases').toLowerCase().split(',').filter((phrase) => phrase != '');
// Object containing references to all relevant UI blocks
var ui = {
main: {
curtain: document.getElementById('curtain'),
fps: document.getElementById('fps')
},
chat: {
body: document.getElementById('chat'),
container: document.getElementById('chat-container')
},
commands: {
body: document.getElementById('commands'),
settings: document.getElementById('settings-toggle'),
fullscreen: document.getElementById('fullscreen')
},
messageEntry: {
body: document.getElementById('message-entry'),
username: document.getElementById('message-username'),
field: document.querySelector('#message-entry .message-field')
},
settings: {
body: document.getElementById('settings'),
twitch: {
channel: document.getElementById('settings-channel'),
channelOverride: document.getElementById('settings-channel-override'),
identity: {
toggle: document.getElementById('settings-twitch-messagefield'),
body: document.getElementById('settings-twitch-messaging'),
username: document.getElementById('settings-twitch-username'),
token: document.getElementById('settings-twitch-token')
}
},
style: {
custom: {
container: document.getElementById('styles'),
selector: document.getElementById('settings-custom-style'),
exchange: document.getElementById('settings-custom-style-exchange'),
field: document.getElementById('settings-custom-style-exchange-field'),
preview: document.getElementById('style-template')
},
fontSize: document.getElementById('settings-font-size'),
hideCursor: document.getElementById('settings-hide-cursor'),
adjustTitle: document.getElementById('settings-adjust-page-title'),
showUnreadInTitle: document.getElementById('settings-unread-counter-in-page-title'),
animateEmoji : document.getElementById('settings-animate-emoji')
},
behaviour: {
limitRate: {
toggle: document.getElementById('settings-limit-message-rate'),
body: document.getElementById('settings-limit-message-rate').parentNode.nextElementSibling,
field: document.getElementById('settings-message-rate')
},
reverseOrder: document.getElementById('settings-new-messages-on-top'),
smoothScroll: {
body: document.getElementById('settings-smooth-scroll').parentNode.nextElementSibling,
duration: document.getElementById('settings-smooth-scroll-duration')
},
chatDelay: {
toggle: document.getElementById('settings-enable-chat-delay'),
body: document.getElementById('settings-chat-delay').parentNode.parentNode,
delay: document.getElementById('settings-chat-delay')
}
},
messageHandling: {
inlineImages: {
body: document.getElementById('settings-inline-images').parentNode.nextElementSibling.nextElementSibling,
height: document.getElementById('settings-inline-images-height')
},
timestamps: document.getElementById('settings-timestamps'),
highlightUsers: document.getElementById('settings-highlight-users'),
keyPhrases: document.getElementById('settings-highlight-keyphrases')
}
},
notifications: {
chatOverload: {
body: document.getElementById('chat-overload'),
count: document.getElementById('chat-overload-count')
},
networkStatus: document.getElementById('network-status'),
keyPhrases: document.getElementById('settings-highlight-keyphrases')
}
};
/** Set up chat client **/
var configuration = {
options: {
skipMembership: true,
skipUpdatingEmotesets: true // the API no longer exists on Kraken
}
};
if (Settings.get('identity')) {
configuration.identity = Settings.get('identity');
}
var client = new tmi.client(configuration);
client.on('message', handleChat);
client.on('roomstate', handleRoomstate);
client.on('subscription', (channel, username, method, message, userstate) => handleSubscription(username, message, userstate));
client.on('resub', (channel, username, months, message, userstate, methods) => handleSubscription(username, message, userstate));
client.on('submysterygift', (channel, username, numbOfSubs, methods, userstate) => handleSubscription(username, null, userstate));
client.on('cheer', handleCheer);
client.on('raided', (channel, username, viewers) => addNotice(`${username} raided the channel with ${viewers} viewers!`));
client.on('usernotice', (msgid, channel, userstate, message) => handleChat(channel, userstate, message));
client.on('slowmode', (channel, enabled, length) => addNotice(`Slowmode chat has been ${enabled ? 'activated' : 'deactivated'}.`));
client.on('followersonly', (channel, enabled, length) => addNotice(`Followers-only chat has been ${enabled ? 'activated' : 'deactivated'}.`));
client.on('emoteonly', (channel, enabled) => addNotice(`Emote-only chat has been ${enabled ? 'activated' : 'deactivated'}.`));
client.on('hosting', (channel, target) => addNotice(`The channel is now hosting ${target}.`));
client.on('unhost', (channel) => addNotice(`The channel has stopped hosting another channel.`));
client.on('messagedeleted', handleMessageDeletion);
client.on('ban', (channel, username, reason, userstate) => handleModAction('ban', username, null, userstate));
client.on('timeout', (channel, username, reason, duration, userstate) => handleModAction('timeout', username, duration, userstate));
client.on('clearchat', (channel) => {
ui.chat.body.textContent = '';
addNotice('Chat has been cleared by a moderator');
});
// Network connection monitoring
client.on('disconnected', () => ui.notifications.networkStatus.classList.remove('hidden'));
client.on('connected', () => {
if (!ui.notifications.networkStatus.classList.contains('hidden')) {
addNotice('Connection reestablished, resuming chat monitoring.');
}
ui.notifications.networkStatus.classList.add('hidden');
});
client.connect().then(() => {
let channelFromPath = (document.location.href.match(/channel=([A-Za-z0-9_]+)/) || [null])[1];
if (channelFromPath) {
joinChannel(channelFromPath);
ui.settings.twitch.channelOverride.classList.remove('hidden');
} else {
joinChannel(Settings.get('channel'));
}
});
/** Interface interactions **/
// Message sending
ui.messageEntry.body.addEventListener('submit', (e) => {
let field = ui.messageEntry.field;
if (field.value.trim().length > 0) {
field.disabled = true;
client.say(client.channels[0], field.value.trim()).then(() => {
field.value = '';
field.disabled = false;
field.focus();
}).catch(() => {
field.disabled = false;
field.focus();
});
}
e.preventDefault();
});
if (document.body.classList.contains('show-message-entry')) {
ui.messageEntry.field.focus();
}
// Settings
ui.commands.settings.addEventListener('click', () => {
ui.settings.body.classList.toggle('hidden');
ui.settings.body.scrollTop = 0;
ui.commands.settings.classList.toggle('open');
});
document.querySelectorAll('.help').forEach((help) => help.addEventListener('click', () => help.classList.toggle('visible')));
// Twitch
ui.settings.twitch.channel.value = Settings.get('channel');
ui.settings.twitch.channel.addEventListener('input', (e) => e.target.value = e.target.value.replaceAll('https://www.twitch.tv/', '').replaceAll('twitch.tv/', ''));
ui.settings.twitch.channel.form.addEventListener('submit', (e) => {
var channel = ui.settings.twitch.channel.value;
if (channel != '' && client.channels.indexOf(ensureHash(channel)) == -1) {
if (client.channels.length > 0) {
client.leave(ensureHash(client.channels[0]));
}
// Fade out all previous channel messages before joining new one
ui.chat.body.querySelectorAll('div').forEach((msg) => msg.style.opacity = 0.5);
Settings.set('channel', channel);
joinChannel(channel);
ui.settings.twitch.channelOverride.classList.add('hidden');
}
e.preventDefault();
});
if (Settings.get('identity')) {
let identity = ui.settings.twitch.identity;
identity.username.value = Settings.get('identity').username;
identity.token.value = Settings.get('identity').token;
identity.body.classList.remove('disabled');
identity.toggle.classList.remove('disabled');
identity.toggle.disabled = false;
ui.messageEntry.username.textContent = Settings.get('identity').username;
document.body.classList.toggle('show-message-entry', Settings.get('twitch-messagefield'));
}
configureToggler('twitch-messagefield', () => {
document.body.classList.toggle('show-message-entry', Settings.get('twitch-messagefield'));
if (Settings.get('twitch-messagefield') && client.username.toLowerCase() != Settings.get('identity').username.toLowerCase()) {
ui.messageEntry.username.textContent = Settings.get('identity').username;
client.disconnect();
client.opts.identity = Settings.get('identity');
client.opts.username = Settings.get('identity').username;
client.connect();
}
});
ui.settings.twitch.identity.username.addEventListener('input', (e) => {
let identity = ui.settings.twitch.identity;
if (e.target.value.length > 0 && identity.token.value.length > 0 && identity.token.validity.valid) {
Settings.set('identity', {
'username': identity.username.value,
'password': identity.token.value
});
identity.body.classList.remove('disabled');
identity.toggle.disabled = false;
} else {
identity.body.classList.add('disabled');
identity.toggle.disabled = false;
identity.toggle.checked = false;
document.body.classList.remove('show-message-entry');
Settings.set('twitch-messagefield', false);
Settings.set('identity', null);
if (!client.username.startsWith('justinfan')) { // already logged out
identity.username.textContent = '';
client.disconnect();
client.opts.identity = {};
delete client.opts.username;
client.connect();
}
}
});
ui.settings.twitch.identity.token.addEventListener('input', (e) => {
let identity = ui.settings.twitch.identity;
if (!/^oauth:[0-9a-z]{30}$/.test(e.target.value)) {
e.target.setCustomValidity('Invalid token');
e.target.reportValidity();
identity.body.classList.add('disabled');
identity.toggle.disabled = true;
identity.toggle.checked = false;
document.body.classList.remove('show-message-entry');
Settings.set('twitch-messagefield', false);
Settings.set('identity', null);
if (!client.username.startsWith('justinfan')) { // already logged out
identity.username.textContent = '';
client.disconnect();
client.opts.identity = {};
delete client.opts.username;
client.connect();
}
return;
}
e.target.setCustomValidity('');
if (identity.username.value.length > 0) {
Settings.set('identity', {
'username': identity.username.value,
'password': identity.token.value
});
identity.body.classList.remove('disabled');
identity.toggle.disabled = false;
}
});
// Style
if (Settings.get('show-command-buttons')) {
ui.commands.body.classList.remove('hidden');
}
if (document.fullscreenEnabled && Settings.get('support-fullscreen')) {
ui.commands.fullscreen.addEventListener('click', () => {
if (document.fullscreenElement) {
document.exitFullscreen()
} else {
document.documentElement.requestFullscreen();
}
});
} else {
ui.commands.fullscreen.classList.add('hidden');
}
[
{
'name': "default",
'background': "#000000",
'odd-background': "#111111",
'separator': "#444444",
'text': "#eeeeee",
'user': "#008000",
'moderator': "#8383f9",
'channel': "#0d86ff",
'notice': "#eeeeee",
'highlight': "#731180",
'vip': '#4686f8',
'admin': '#a970ff',
'staff': '#a970ff'
}, {
'name': "bright",
'background': "#eeeeee",
'odd-background': "#dddddd",
'separator': "#bbbbbb",
'text': "#111111",
'user': "#00b000",
'moderator': "#8383f9",
'channel': "#0d86ff",
'notice': "#111111",
'highlight': "#731180",
'vip': '#4686f8',
'admin': '#a970ff',
'staff': '#a970ff'
}, {
'name': 'LRR',
'background': "#202020",
'odd-background': "#111111",
'separator': "#464646",
'text': "#d2d2d2",
'user': "#5282ff",
'moderator': "#f15a24",
'channel': "#f15a24",
'notice': "#d2d2d2",
'highlight': "#e1480f",
'vip': '#aaf2a6',
'admin': '#a970ff',
'staff': '#a970ff'
}
].forEach(createStylePreview);
var colorFields = ['background', 'odd-background', 'separator', 'text', 'user', 'moderator', 'channel', 'notice', 'highlight', 'vip', 'admin', 'staff'];
var customStyleValues = {
'name': "custom"
};
colorFields.forEach(key => customStyleValues[key] = Settings.get(`${key}-color`));
var customStylePreview = createStylePreview(customStyleValues, true);
colorFields.forEach(key => {
document.getElementById(`settings-${key}-color`).value = Settings.get(`${key}-color`);
document.getElementById(`settings-${key}-color`).addEventListener('change', (e) => {
Settings.set(`${key}-color`, e.target.value);
customStylePreview.style.setProperty(`--style-${key}`, e.target.value);
if (['channel', 'notice', 'highlight'].indexOf(key) != -1) {
customStylePreview.style.setProperty(`--style-${key}-background`, e.target.value + '50');
}
updateImportExport();
});
});
updateImportExport();
ui.settings.style.custom.field.addEventListener('input', (e) => {
let code = e.target.value;
if (code.length == 36) { // Allow old exports to work
code += ['vip', 'admin', 'staff'].map((name) => HexCompressor.color2string(Settings.get(`${name}-color`))).join('');
}
if (!/^[0-9a-zA-Z+#]{48}$/.test(code)) {
e.target.setCustomValidity('Invalid code');
e.target.reportValidity();
return;
}
e.target.setCustomValidity('');
colorFields.forEach((key, index) => {
var newColor = HexCompressor.string2color(code.substring(index * 4, (index * 4) + 4));
Settings.set(key + '-color', newColor);
document.getElementById('settings-' + key + '-color').value = newColor;
});
});
ui.settings.style.custom.selector.classList.toggle('hidden', Settings.get('style-preset') != 'custom');
ui.settings.style.custom.exchange.classList.toggle('hidden', Settings.get('style-preset') != 'custom');
ui.settings.style.fontSize.value = Settings.get('font-size').slice(0, -2); // remove pixel unit
ui.settings.style.fontSize.addEventListener('change', (e) => Settings.set('font-size', e.target.value + 'px'));
ui.settings.style.animateEmoji.value = Settings.get('animate-emoji');
ui.settings.style.animateEmoji.addEventListener('change', (e) => {
Settings.set('animate-emoji', e.target.value);
if (e.target.value != 'auto') { // Adjust existing emoji for existing emoji if forced enabled or disabled
Array.from(ui.chat.body.querySelectorAll('div.emoticon')).forEach(updateEmoji);
}
});
document.body.classList.toggle('hide-cursor', Settings.get('hide-cursor'));
ui.settings.style.hideCursor.checked = Settings.get('hide-cursor');
configureToggler('hide-cursor', () => document.body.classList.toggle('hide-cursor', Settings.get('hide-cursor')));
document.addEventListener('mousemove', () => {
if (Settings.get('hide-cursor')) {
document.body.classList.remove('hide-cursor');
clearTimeout(lastMoveTimeoutId);
lastMoveTimeoutId = setTimeout(() => Settings.get('hide-cursor') && document.body.classList.add('hide-cursor'), 4000);
}
});
ui.settings.style.adjustTitle.checked = Settings.get('adjust-page-title');
configureToggler('adjust-page-title', updateTitle);
ui.settings.style.showUnreadInTitle.checked = Settings.get('unread-counter-in-page-title');
configureToggler('unread-counter-in-page-title');
// Monitor unread messages
document.addEventListener('visibilitychange', updateUnreadCounter);
// Chat Behavior
document.body.classList.toggle('limit-message-rate', !Settings.get('limit-message-rate'));
ui.settings.behaviour.limitRate.toggle.checked = Settings.get('limit-message-rate');
configureToggler('limit-message-rate', () => {
document.body.classList.toggle('limit-message-rate', !Settings.get('limit-message-rate'));
ui.settings.behaviour.limitRate.body.classList.toggle('hidden', !Settings.get('limit-message-rate'));
if (!Settings.get('limit-message-rate')) {
flushMessageQueue();
ui.notifications.chatOverload.body.classList.add('hidden');
}
});
if (Settings.get('limit-message-rate')) {
ui.settings.behaviour.limitRate.body.classList.remove('hidden');
}
ui.settings.behaviour.limitRate.field.value = Settings.get('message-rate');
ui.settings.behaviour.limitRate.field.addEventListener('input', (e) => {
var rate = parseInt(e.target.value);
if (!isNaN(rate) && e.target.validity.valid) {
Settings.set('message-rate', rate);
}
});
document.body.classList.toggle('reverse-order', !Settings.get('new-messages-on-top'));
configureToggler('new-messages-on-top', () => {
document.body.classList.toggle('reverse-order', !Settings.get('new-messages-on-top'));
scrollDistance = scrollReference = 0;
ui.chat.container.scrollTop = Settings.get('new-messages-on-top') ? 0 : ui.chat.container.scrollHeight - window.innerHeight;
});
configureToggler('smooth-scroll', () => {
scrollDistance = scrollReference = 0;
ui.chat.container.scrollTop = Settings.get('new-messages-on-top') ? 0 : ui.chat.container.scrollHeight - window.innerHeight;
ui.settings.behaviour.smoothScroll.body.classList.toggle('hidden', !Settings.get('smooth-scroll'));
});
if (Settings.get('smooth-scroll')) {
ui.settings.behaviour.smoothScroll.body.classList.remove('hidden');
}
ui.settings.behaviour.smoothScroll.duration.value = Settings.get('smooth-scroll-duration');
ui.settings.behaviour.smoothScroll.duration.addEventListener('input', (e) => {
var duration = parseInt(e.target.value);
if (!isNaN(duration) && e.target.validity.valid) {
Settings.set('smooth-scroll-duration', duration);
}
});
configureToggler('enable-chat-delay', () => {
toggleChatDelay();
ui.settings.behaviour.chatDelay.body.classList.toggle('hidden', !Settings.get('enable-chat-delay'));
});
if (Settings.get('enable-chat-delay')) {
ui.settings.behaviour.chatDelay.body.classList.remove('hidden');
}
ui.settings.behaviour.chatDelay.delay.value = Settings.get('chat-delay');
ui.settings.behaviour.chatDelay.delay.addEventListener('change', (e) => setChatDelay(e.target.value));
// Message Handling
ui.chat.body.classList.toggle('align-messages', Settings.get('align-messages'));
configureToggler('align-messages', () => ui.chat.body.classList.toggle('align-messages', Settings.get('align-messages')));
['combine-messages', 'format-urls', 'shorten-urls', 'unfurl-youtube', 'show-subscriptions', 'show-bits', 'show-mod-actions'].forEach(configureToggler);
configureToggler('inline-images', () => ui.settings.messageHandling.inlineImages.body.classList.toggle('hidden', !Settings.get('inline-images')));
if (Settings.get('inline-images')) {
ui.settings.messageHandling.inlineImages.body.classList.remove('hidden');
}
ui.settings.messageHandling.inlineImages.height.value = Settings.get('inline-images-height').slice(0, -2); // remove vh unit
ui.settings.messageHandling.inlineImages.height.addEventListener('input', (e) => {
var height = parseInt(e.target.value);
if (!isNaN(height) && e.target.validity.valid) {
Settings.set('inline-images-height', height + 'vh');
}
});
configureToggler('unfurl-twitter', () => {
if (typeof twttr == 'undefined') {
var twitterScript = document.createElement('script');
twitterScript.src = 'https://platform.twitter.com/widgets.js';
document.body.appendChild(twitterScript);
}
});
if (Settings.get('unfurl-twitter')) {
var twitterScript = document.createElement('script');
twitterScript.src = 'https://platform.twitter.com/widgets.js';
document.body.appendChild(twitterScript);
}
ui.settings.messageHandling.timestamps.value = Settings.get('timestamps');
ui.chat.body.classList.toggle('hide-timestamps', Settings.get('timestamps') == '');
ui.settings.messageHandling.timestamps.addEventListener('change', (e) => {
Settings.set('timestamps', e.target.value);
ui.chat.body.classList.toggle('hide-timestamps', e.target.value == '');
Array.from(ui.chat.body.querySelectorAll('.timestamp')).forEach(updateTimestamp);
});
ui.settings.messageHandling.highlightUsers.value = Settings.get('highlight-users');
ui.settings.messageHandling.highlightUsers.addEventListener('input', (e) => {
Settings.set('highlight-users', e.target.value.toLowerCase());
highlightUsers = e.target.value.toLowerCase().split(',').filter((user) => user != '');
});
ui.settings.messageHandling.keyPhrases.value = Settings.get('highlight-keyphrases');
ui.settings.messageHandling.keyPhrases.addEventListener('input', (e) => {
Settings.set('highlight-keyphrases', e.target.value.toLowerCase());
highlightKeyphrases = e.target.value.toLowerCase().split(',').filter((phrase) => phrase != '');
});
configureToggler('show-fps', (e) => handleFPS(e.target.checked));
if (Settings.get('show-fps')) {
handleFPS(true);
}
// Hotkeys
document.body.addEventListener('keydown', (e) => {
if (!Settings.get('support-hotkeys')) {
return;
}
if ((e.key == 'H' || e.key == 'h') && e.shiftKey && e.ctrlKey) {
ui.main.curtain.classList.toggle('hidden');
e.preventDefault();
} else if ((e.key == 'D' || e.key == 'd') && e.shiftKey && e.ctrlKey) {
Settings.toggle('enable-chat-delay');
ui.settings.behaviour.chatDelay.toggle.checked = !ui.settings.behaviour.chatDelay.toggle.checked;
ui.settings.behaviour.chatDelay.body.classList.toggle('hidden');
toggleChatDelay();
e.preventDefault();
} else if ((e.key == 'S' || e.key == 's') && e.shiftKey && e.ctrlKey) {
ui.settings.body.classList.toggle('hidden');
ui.settings.body.scrollTop = 0;
ui.commands.settings.classList.toggle('open');
e.preventDefault();
} else if ((e.key == 'Escape')) {
ui.settings.body.classList.add('hidden');
ui.commands.settings.classList.remove('open');
e.preventDefault();
}
});
function joinChannel(channel) {
client.join(ensureHash(channel)).then(() => {
console.log('Joined channel ' + channel);
updateTitle()
}, (error) => {
addNotice(`Failed to join requested channel. Reason: ${decodeMessageId(error)}`);
console.error('Failed to join requested channel', error);
updateTitle();
});
}
// Decode a Message ID returned by the Twitch API to a human-readable message
function decodeMessageId(messageId) {
let knownMessages = {
'msg_banned': 'You are permanently banned from talking in this channel.',
'msg_channel_blocked': 'Your account is not in good standing in this channel.',
'msg_channel_suspended': 'This channel does not exist or has been suspended.',
'msg_requires_verified_phone_number': 'A verified phone number is required to chat in this channel. Please visit https://www.twitch.tv/settings/security to verify your phone number.',
'msg_suspended': 'You don\'t have permission to perform that action.',
'msg_verified_email': 'This room requires a verified account to chat. Please verify your account at https://www.twitch.tv/settings/security.'
};
return knownMessages[messageId] || messageId;
}
// Process the next frame, this is the main driver of the application
var lastFrame = +new Date();
function step(now) {
if (Settings.get('show-fps')) {
frames++;
}
if (Settings.get('enable-chat-delay') && Settings.get('chat-delay') != 0) {
while (delayQueue.length > 0 && parseInt(delayQueue[0].dataset.timestamp) + (Settings.get('chat-delay') * 1000) < Date.now()) {
addMessage(delayQueue.shift(), true);
}
}
if (Settings.get('limit-message-rate')) {
if (messageQueue.length > 40) {
ui.notifications.chatOverload.body.classList.remove('hidden');
// Cull the queue to a reasonable length and update the counter
ui.notifications.chatOverload.count.textContent = parseInt(ui.notifications.chatOverload.count.textContent) + messageQueue.splice(-40).length;
}
if (messageQueue.length < 10 && !ui.notifications.chatOverload.body.classList.contains('hidden')) {
ui.notifications.chatOverload.body.classList.add('hidden');
ui.notifications.chatOverload.count.textContent = "0";
}
if (messageQueue.length > 0 && now - lastMessageTimestamp > 1000 / Settings.get('message-rate')) {
processChat.apply(this, messageQueue.shift());
lastMessageTimestamp = now;
}
}
if (Settings.get('smooth-scroll') && scrollDistance > 0) {
// Estimate how far along we are in scrolling in the current scroll reference
var currentStep = Settings.get('smooth-scroll-duration') / (now - lastFrame);
scrollDistance -= scrollReference / currentStep;
scrollDistance = Math.max(scrollDistance, 0);
ui.chat.container.scrollTop = Math.round(Settings.get('new-messages-on-top') ? scrollDistance : ui.chat.container.scrollHeight - window.innerHeight - scrollDistance);
}
lastFrame = now;
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
/** Chat event handling **/
function handleChat(channel, userstate, message) {
increaseUnreadCounter();
if (Settings.get('limit-message-rate')) {
messageQueue.push([ channel, userstate, message ]);
} else {
processChat(channel, userstate, message);
}
}
function processChat(channel, userstate, message) {
try {
// If enabled, combine messages instead of adding a new message
var id = 'message-' + message.toLowerCase().replace(/[^\p{Letter}]/gu, '');
if (Settings.get('combine-messages') && document.getElementById(id)) {
var matchedMessage = document.getElementById(id);
if (!matchedMessage.counter) {
var counterContainer = document.createElement('span'),
counter = document.createElement('span');
counterContainer.className = 'counter';
counterContainer.innerHTML = '× ';
counterContainer.appendChild(counter);
counter.textContent = '1';
matchedMessage.appendChild(counterContainer);
matchedMessage.counter = counter;
}
ui.chat.body.appendChild(matchedMessage);
matchedMessage.querySelector('.counter').classList.add('bump');
matchedMessage.counter.textContent++;
setTimeout(() => matchedMessage.querySelector('.counter').classList.remove('bump'), 150);
return;
}
var chatLine = createChatLine(userstate, message);
if (Settings.get('combine-messages')) {
chatLine.id = id;
}
// Deal with loading user-provided inline images
var userImages = Array.from(chatLine.querySelectorAll('img.user-image'));
if (userImages.length > 0) {
userImages.forEach((userImage) => {
if (userImage.complete) { // most likely it was already cached
userImage.classList.add('loaded');
return;
}
userImage.addEventListener('load', () => {
if (userImage.dataset.mq && userImage.naturalWidth == 120) { // Failed to load, placeholder received
if (userImage.dataset.hq) {
userImage.src = userImage.dataset.hq;
userImage.dataset.hq = '';
return;
} else if (userImage.dataset.mq) {
userImage.src = userImage.dataset.mq;
userImage.dataset.mq = '';
return;
}
}
var oldChatLineHeight = chatLine.scrollHeight;
userImage.classList.add('loaded');
var loadingText = chatLine.querySelector('.image-loading');
if (chatLine.querySelector('.user-image:not(.loaded)') == null && loadingText != null) {
loadingText.remove();
}
scrollReference = scrollDistance += Math.max(0, chatLine.scrollHeight - oldChatLineHeight);
});
userImage.addEventListener('error', () => {
var loadingText = chatLine.querySelector('.image-loading');
if (loadingText) {
loadingText.textContent = '[image loading failed]';
}
});
});
if (userImages.some(image => !image.complete)) {
var loadingText = document.createElement('span');
loadingText.className = 'image-loading';
loadingText.textContent = '[Loading image...]';
var firstBreakLine = chatLine.querySelector('br');
firstBreakLine.insertAdjacentText('beforebegin', ' ');
firstBreakLine.insertAdjacentElement('beforebegin', loadingText);
}
}
// Load Twitter/X messages, if any
var tweets = Array.from(chatLine.querySelectorAll('div.tweet-embed'));
if (tweets.length > 0 && typeof twttr != 'undefined' && twttr.init) {
tweets.forEach((tweet) => {
twttr.widgets
.createTweet(tweet.dataset.tweet, tweet, {
theme: 'dark',
conversation: 'none',
cards: 'hidden',
dnt: 'true'
})
.then(el => {
scrollReference = scrollDistance += el.scrollHeight;
})
.catch(e => console.log(e));
});
}
addMessage(chatLine);
// Check whether the message we just added was a message that was already deleted
if (userstate.deleted) {
deleteMessage(userstate.id);
}
} catch (error) {
console.error('Error parsing chat message: ' + message, error);
}
}
function handleRoomstate(channel, state) {
flushDelayQueue();
flushMessageQueue();
addNotice(`Joined ${channel}.`);
if (state.slow) {
addNotice(`Channel is in slow mode.`);
}
if (state['followers-only'] != -1) {
addNotice(`Channel is in followers-only mode.`);
}
if (state['emote-only']) {
addNotice(`Channel is in emote-only mode.`);
}
if (Settings.get('enable-chat-delay') && Settings.get('chat-delay') != 0) {
addNotice(`Chat is set to an artificial delay of ${Settings.get('chat-delay')} second${Settings.get('chat-delay') == 1 ? '' : 's'}.`);
}
}
function handleSubscription(username, message, userstate) {
if (!Settings.get('show-subscriptions')) {
return;
}
var chatLine = document.createElement('div');
chatLine.className = 'highlight subscription';
var subscriptionNotice = document.createElement('div');
subscriptionNotice.textContent = userstate['system-msg'].replaceAll('\\s', ' ');
chatLine.append(subscriptionNotice);
if (message && message.length > 0) {
chatLine.append(createChatLine(userstate, message));
}
addMessage(chatLine);
}
function handleCheer(channel, userstate, message) {
// We could consider to transform the cheer emotes in the message instead of removing them (https://dev.twitch.tv/docs/irc/tags/#privmsg-twitch-tags)
var chatMessage = message;
bitLevels.forEach((level) => chatMessage = chatMessage.replaceAll(new RegExp(`\\b[a-zA-Z]+${level}\\b`, 'g'), ''));
var chatLine = createChatLine(userstate, chatMessage),
cheer = document.createElement('span'),
bitLevel = bitLevels.find((level) => parseInt(userstate.bits) >= level),
cheerIcon = document.createElement('img');
if (Settings.get('show-bits')) {
if (bitLevel == undefined) {
console.warn(`Could not parse bits received from ${userstate.username}`, userstate.bits);
return;
}
let imageStyle = Settings.get('animate-emoji') == 'yes' ? 'animated' : 'static'; // TODO: support 'auto'
cheerIcon.src = `https://static-cdn.jtvnw.net/bits/dark/${imageStyle}/${bitLevel}/1.5.gif`;
cheerIcon.alt = 'Bits';
cheer.appendChild(cheerIcon);
cheer.className = `cheer cheer-${bitLevel}`;
cheer.appendChild(document.createTextNode(userstate.bits));
chatLine.insertBefore(cheer, chatLine.lastChild);
}
addMessage(chatLine);
}
function handleMessageDeletion(channel, username, deletedMessage, userstate) {
deleteMessage(userstate['target-msg-id']);
}
function handleModAction(action, username, duration, userstate) {
if (Settings.get('show-mod-actions')) {
if (action == 'timeout') {
addNotice(`${username} was given a time-out of ${duration} second${duration == 1 ? '' : 's'}.`);
} else if (action == 'ban') {
addNotice(`${username} has been banned.`);
}
}
Array.from(ui.chat.body.querySelectorAll(`span[data-user="${userstate["target-user-id"]}"]`)).map(message => message.id).forEach(deleteMessage);
}
function handleFPS(enable) {
ui.main.fps.innerHTML = ' ';
ui.main.fps.classList.toggle('hidden', !enable);
lastFrameReset = Date.now();
frames = 0;
if (enable) {
fpsInterval = setInterval(updateFPS, 1000);
} else {
clearInterval(fpsInterval);
}
}
function updateFPS() {
var currentFrameTime = Date.now();
ui.main.fps.textContent = (frames / (currentFrameTime - lastFrameReset) * 1000).toFixed(1);
lastFrameReset = currentFrameTime;
frames = 0;
}
/** Helper functions **/
function configureToggler(key, callback) {
document.getElementById(`settings-${key}`).checked = Settings.get(key);
document.getElementById(`settings-${key}`).addEventListener('click', (e) => {
Settings.toggle(key);
if (typeof callback == 'function') {
callback(e);
}
});
}
function createChatLine(userstate, message) {
// <div><span class="timestamp">$timestamp</span><span class="chat-user moderator">$username</span><span id="$msg-id">$message</span></div>
var chatLine = document.createElement('div'),
chatTimestamp = document.createElement('span'),
chatName = document.createElement('span'),
chatMessage = document.createElement('span');
chatTimestamp.className = 'timestamp';
chatTimestamp.dataset.timestamp = userstate['tmi-sent-ts'] || Date.now();
updateTimestamp(chatTimestamp);
chatLine.appendChild(chatTimestamp);
chatName.className = 'chat-user';
if (userstate.badges?.vip) {
chatName.classList.add('vip');
}
if (userstate.badges?.admin) {
chatName.classList.add('admin');
}
if (userstate.badges?.staff) {
chatName.classList.add('staff');
}
if (userstate.mod) {
chatName.classList.add('moderator');
}
if (userstate['message-type'] == 'action') {
chatName.classList.add('action');
}
chatName.textContent = userstate['display-name'] || userstate.username;
if (userstate['message-type'] == 'announcement') {
chatName.textContent = '📢 ' + chatName.textContent;
}
if (chatName.textContent.toLowerCase() == removeHash(client.channels[0]).toLowerCase()) {
chatLine.className = 'highlight channel';
}
chatMessage.innerHTML = formatMessage(message, userstate.emotes);
chatMessage.id = userstate.id;
if (userstate['user-id']) {
chatMessage.dataset.user = userstate['user-id'];
}
if (highlightUsers.indexOf(chatName.textContent.toLowerCase()) != -1) {
chatLine.className = 'highlight';
}
if (highlightKeyphrases.find((phrase) => message.toLowerCase().indexOf(phrase) != -1)) {
chatLine.className = 'highlight';
}
chatLine.appendChild(chatName);
chatLine.appendChild(chatMessage);
return chatLine;
}
function addNotice(message) {
var chatLine = document.createElement('div');
chatLine.textContent = message;
chatLine.className = 'notice';
addMessage(chatLine);
}
function addMessage(chatLine, bypass) {
if (chatLine.className != 'notice' && !bypass && Settings.get('enable-chat-delay') && Settings.get('chat-delay') != 0) {
chatLine.dataset.timestamp = Date.now();
delayQueue.push(chatLine);
return;
}
ui.chat.body.appendChild(chatLine);
// Calculate height for smooth scrolling
scrollReference = scrollDistance += chatLine.scrollHeight;
if (!Settings.get('new-messages-on-top') && !Settings.get('smooth-scroll')) {
ui.chat.container.scrollTop = ui.chat.container.scrollHeight - window.innerHeight;
}
// Check whether we can remove some of the oldest messages
while (chat.childNodes.length > 2 && ui.chat.body.scrollHeight - (window.innerHeight + (Settings.get('smooth-scroll') ? scrollDistance : 0)) > ui.chat.body.firstChild.scrollHeight + ui.chat.body.childNodes[1].scrollHeight) {
// Always remove two elements at the same time to prevent switching the odd and even rows
ui.chat.body.firstChild.remove();
ui.chat.body.firstChild.remove();
}
}
function deleteMessage(messageId) {
var message = document.getElementById(messageId);
if (message == null) {
var messageToDelete = messageQueue.find(entry => entry[1].id == messageId);
if (messageToDelete) {
messageToDelete[2] = '<Message deleted by moderator>'; // Text will be replaced, but just intended to put it back on one line
messageToDelete[1].deleted = true;
}
return;
}
if (message.classList.contains('deleted')) { // Weird, but ok
return;
}
message.parentNode.style.height = (message.parentNode.scrollHeight - 7) + 'px'; // 2 x 3px padding + 1px border = 7
message.textContent = '<Message deleted by moderator>';
message.classList.add('deleted');
}
/*
To deal with message formatting, the message gets turned into an array of characters first.
Twitch provides the IDs of the emotes and from where to where they are located in the message.
We replace those emote-related characters with empty strings and place an <img> tag as a string at the 'from' location.
Other changes take place in a similar way, by calculating the 'from' and 'to' values ourselves.
As a last step, all entries in the array with 1 character are transformed into HTML entities if they are potentially dangerous.
At the end, we join() the character array again, forming a message safe to assign to the innerHTML property.
*/
function formatMessage(text, emotes) {
if (Settings.get('new-messages-on-top')) {
text = text.replaceAll('^', '⌄');
}
let message = text.split('');
message = formatEmotes(message, emotes);
message = formatLinks(message, text);
return htmlEntities(message).join('');
}
function formatEmotes(text, emotes) {
if (!emotes) {
return text;
}
for (var id in emotes) {
emotes[id].forEach((range) => {
if (typeof range == 'string') {
range = range.split('-').map(index => parseInt(index));
let emote = text.slice(range[0], range[1] + 1).join('');
let imageStyle = Settings.get('animate-emoji') == 'yes' ? 'default' : 'static'; // TODO: support 'auto'
let baseUrl = `https://static-cdn.jtvnw.net/emoticons/v2/${id}/${imageStyle}/dark`;
replaceText(text, `<div class="emoticon" style="background-image: image-set(url('${baseUrl}/1.0') 1x,url('${baseUrl}/2.0') 2x,url('${baseUrl}/3.0') 4x" alt="${emote}" title="${emote}"></div>`, range[0], range[1]);
}
});
};
return text;
}
function formatLinks(text, originalText) {
var urlRegex = /(https?:\/\/)?(www\.)?([0-9a-zA-Z-_\.]+\.[0-9a-zA-Z]+\/)([0-9a-zA-Z-_+:;,|`%^\(\)\[\]#=&\/\.\?\|\~@]*[0-9a-zA-Z-_+:;|`%^\(\)\[\]#=&\/\.\?\|\~])?/g;
var match;
while ((match = urlRegex.exec(originalText)) !== null) {
var urlText = url = match[0];
if (!match[1]) {
url = 'https://' + url;
}
var path = match[4] || '';
if (Settings.get('inline-images')) {
var giphy = /^https?:\/\/giphy\.com\/gifs\/(.*-)?([a-zA-Z0-9]+)$/gm.exec(urlText);
if (giphy) {
url = `https://media1.giphy.com/media/${giphy[2].split("-").pop()}/giphy.gif`;
path = `media/${giphy[2].split("-").pop()}/giphy.gif`;