-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNicoTube.user.js
1083 lines (971 loc) · 49.4 KB
/
NicoTube.user.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
// ==UserScript==
// @name NicoTube
// @namespace NicoTube
// @version 0.1.0
// @description Youtubeのライブチャットをniconicoの様に描画します
// @author @nagataaaas
// @name:en NicoTube
// @description:en Render Youtube live chat on the video just like niconico
// @match https://www.youtube.com/*
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_getResourceText
// @grant GM_addStyle
// @grant GM_setClipboard
// @resource IMPORTED_CSS https://raw.githubusercontent.com/nagataaaas/NicoTube/master/style.css
// @noframes
/* globals jQuery, $ */
// ==/UserScript==
// comment icon by Freepik(http://www.freepik.com/) from www.flaticon.com
// block icon by Those Icons(https://www.flaticon.com/free-icon/) from www.flaticon.com
(function () {
'use strict';
const my_css = GM_getResourceText("IMPORTED_CSS");
GM_addStyle(my_css);
const getVars = () => {
return {
resolutionRatio: 1,
commentSpeed: GM_getValue('commentSpeed', 5), // from appear to disappear
fontFamily: 'Arial', // font of chat
userCommentStrokeColor: 'yellow', // color of box of user comment
isBold: true, // is comment font bold
emojiBorder: true, // whether show border to emoji
fontSize: 0.05, // Relative size to height of video
commentMargin: 0.7, // Relative size to fontSize
fontColor: '#FFFFFF', // color of comment (default to white
memberFontColor: '#2cb879', // color of member comment (default to green
moderatorFontColor: '#7581b8', // color of moderator comment (default to purple
canvasAlpha: 0.5, // alpha of comment
borderWidth: 0.15, // Relative size to fontSize
borderColor: '#000000', // color of border (default to black
blockedUsers: [], // list of list of blocked userid [[userID, comment], [userID, comment], [userID, comment]...]
blockedComments: [], // list of list of hash of blocked AllCommentLanes [[hash, actual comment], [hash, actual comment], [hash, actual comment]...],
blockedKeyWords: [], // list of blocked keyword [keyword, keyword, keyword...] (word can be regex)
showSuperChatAmount: true, // whether show the amount of super chat
showSuperChatBackground: true, // whether show the background of super chat
showSuperChatStroke: true, // whether show the stroke of super chat
changeColorOfMember: true, // whether change the color of member
changeColorOfModerator: true, // whether change the color of moderator
welcomeNewMember: true // pop welcome animation to new member
};
};
let AllCommentLanes;
let globalMutations = {
comment: null,
contextMenu: null,
hover: null
};
// vars
let config;
let player;
let chatField;
let nicoTubeOn;
let needToRedraw;
let currentPlayState;
let toolBarRight;
let nicoTubeSwitch;
let commentCanvas;
let commentContext;
let video;
let contextMenuCanvas;
let settingButton;
let isMouseOn;
let nicoTubeContextMenu;
let commentAddMutation;
// intervals
let expireInterval;
let waitReadyInterval;
let chatFieldWait;
// id of each elements
let nicoTubeContextMenuWrapperID = 'nicoTubeContextMenuWrapper';
let nicoChatCanvasID = 'nicoChatCanvas';
let nicoTubeSwitchID = 'nicoTubeSwitch';
let videoSelector = '#movie_player > div.html5-video-container > video';
let rightToolBarSelector = '#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls';
let toolNamePopSelector = '#movie_player > div.ytp-tooltip.ytp-bottom';
let contextMenuCanvasID = 'contextMenuCanvas';
let messageFieldSelector = '#message';
// html elements
let availableIcon = `<path d="m0 0v342h120.148438v112.054688l181.253906-112.054688h210.597656v-342zm360.996094 214.335938h-209.992188v-30h209.988282v30zm0-65.175782h-209.992188v-30h209.988282v30zm0 0" fill="white" id="nicoTubepath1"/>`;
let unavailableIcon = `<path d="m0 0v342h120.148438v112.054688l181.253906-112.054688h210.597656v-342zm482 312h-189.121094l-142.730468 88.238281v-88.238281h-120.148438v-282h452zm0 0" fill="white" id="nicoTubepath2" style="visibility:hidden;"/><path d="m151.003906 121.160156h209.988282v30h-209.988282zm0 0" fill="white" id="nicoTubepath3" style="visibility:hidden;"/><path d="m151.003906 183h209.988282v30h-209.988282zm0 0" fill="white" id="nicoTubepath4" style="visibility:hidden;"/>`;
let nicoTubeToggleSwitch = `<button class="ytp-button" show="true" id="${nicoTubeSwitchID}"><svg height="100%" viewBox="-128 -157 768 768" width="100%">${availableIcon}${unavailableIcon}</svg></button>`;
let canvasHtml = `<canvas id='${nicoChatCanvasID}' style='pointer-events: none; z-index:2000;' position='absolute'></canvas>`;
let nicoTubeContextMenuHTML = `
<div class="ytp-popup ytp-contextmenu" style="width: 300px; height: 240px; display: none;" id="nicoTubeContextMenu">
<div class="ytp-panel" style="min-width: 250px; width: 300px; height: 240px;">
<canvas id="contextMenuCanvas"></canvas>
<div class="ytp-panel-menu" role="menu" style="height: 180px;">
<div class="ytp-menuitem" aria-haspopup="false" aria-checked="false" tabindex="0" id="userBlock">
<div class="ytp-menuitem-icon">
<svg fill="none" height="24" viewBox="0 0 512 512" width="24">
<path d="M426.667,85.333c-7.76,0-15.052,2.083-21.333,5.729V64c0-23.531-19.135-42.667-42.667-42.667 c-8.781,0-16.938,2.667-23.729,7.219C333.094,11.948,317.25,0,298.667,0c-18.583,0-34.427,11.948-40.271,28.552 c-6.792-4.552-14.948-7.219-23.729-7.219C211.135,21.333,192,40.469,192,64v264.146c0,5.375-3.542,8.135-5.063,9.073 s-5.615,2.865-10.375,0.469l-68.5-34.25c-6.24-3.125-13.229-4.771-20.208-4.771c-24.917,0-45.187,20.271-45.187,45.188V352 c0,2.958,1.229,5.781,3.385,7.802l123.115,114.906C194.937,498.75,228.542,512,263.781,512h66.885 c76.458,0,138.667-62.208,138.667-138.667V128C469.333,104.469,450.198,85.333,426.667,85.333z M448,373.333 c0,64.698-52.635,117.333-117.333,117.333h-66.885c-29.813,0-58.25-11.208-80.052-31.563L64,347.365v-3.51 C64,330.698,74.698,320,87.854,320c3.677,0,7.375,0.875,10.667,2.521l68.5,34.25c9.969,5.01,21.635,4.458,31.135-1.396 c9.5-5.875,15.177-16.052,15.177-27.229V64c0-11.76,9.573-21.333,21.333-21.333C246.427,42.667,256,52.24,256,64v138.667 c0,5.896,4.771,10.667,10.667,10.667c5.896,0,10.667-4.771,10.667-10.667v-160c0-11.76,9.573-21.333,21.333-21.333 c11.76,0,21.333,9.573,21.333,21.333v160c0,5.896,4.771,10.667,10.667,10.667c5.896,0,10.667-4.771,10.667-10.667V64 c0-11.76,9.573-21.333,21.333-21.333S384,52.24,384,64v170.667c0,4.042,2.323,7.75,5.938,9.563c0.01,0,0.021,0.01,0.042,0.021 s0.042,0.021,0.063,0.031c0.052,0.021,0.104,0.042,0.146,0.073c0.021,0,0.021,0.021,0.042,0.01l0.01,0.01 c0.01,0,0.021,0.01,0.021,0.01c0.021,0.01,0.021,0.01,0.042,0.01c0.01,0.021,0.021,0.021,0.031,0.021l0.031,0.01 c0.01,0,0.01,0.021,0.042,0.021c0.021,0.042,0.042,0.01,0.042,0.01c5.135,2.25,11.24,0.094,13.802-5 c0.917-1.844,1.26-3.823,1.083-5.729V128c0-11.76,9.573-21.333,21.333-21.333S448,116.24,448,128V373.333z" fill="white"/>
</svg>
</div>
<div class="ytp-menuitem-label">投稿ユーザーブロック</div>
<div class="ytp-menuitem-content"></div>
</div>
<div class="ytp-menuitem" aria-haspopup="false" role="menuitem" tabindex="0" id="commentBlock">
<div class="ytp-menuitem-icon">
<svg fill="none" height="24" viewBox="0 0 512 512" width="24">
<path d="M426.667,85.333c-7.76,0-15.052,2.083-21.333,5.729V64c0-23.531-19.135-42.667-42.667-42.667 c-8.781,0-16.938,2.667-23.729,7.219C333.094,11.948,317.25,0,298.667,0c-18.583,0-34.427,11.948-40.271,28.552 c-6.792-4.552-14.948-7.219-23.729-7.219C211.135,21.333,192,40.469,192,64v264.146c0,5.375-3.542,8.135-5.063,9.073 s-5.615,2.865-10.375,0.469l-68.5-34.25c-6.24-3.125-13.229-4.771-20.208-4.771c-24.917,0-45.187,20.271-45.187,45.188V352 c0,2.958,1.229,5.781,3.385,7.802l123.115,114.906C194.937,498.75,228.542,512,263.781,512h66.885 c76.458,0,138.667-62.208,138.667-138.667V128C469.333,104.469,450.198,85.333,426.667,85.333z M448,373.333 c0,64.698-52.635,117.333-117.333,117.333h-66.885c-29.813,0-58.25-11.208-80.052-31.563L64,347.365v-3.51 C64,330.698,74.698,320,87.854,320c3.677,0,7.375,0.875,10.667,2.521l68.5,34.25c9.969,5.01,21.635,4.458,31.135-1.396 c9.5-5.875,15.177-16.052,15.177-27.229V64c0-11.76,9.573-21.333,21.333-21.333C246.427,42.667,256,52.24,256,64v138.667 c0,5.896,4.771,10.667,10.667,10.667c5.896,0,10.667-4.771,10.667-10.667v-160c0-11.76,9.573-21.333,21.333-21.333 c11.76,0,21.333,9.573,21.333,21.333v160c0,5.896,4.771,10.667,10.667,10.667c5.896,0,10.667-4.771,10.667-10.667V64 c0-11.76,9.573-21.333,21.333-21.333S384,52.24,384,64v170.667c0,4.042,2.323,7.75,5.938,9.563c0.01,0,0.021,0.01,0.042,0.021 s0.042,0.021,0.063,0.031c0.052,0.021,0.104,0.042,0.146,0.073c0.021,0,0.021,0.021,0.042,0.01l0.01,0.01 c0.01,0,0.021,0.01,0.021,0.01c0.021,0.01,0.021,0.01,0.042,0.01c0.01,0.021,0.021,0.021,0.031,0.021l0.031,0.01 c0.01,0,0.01,0.021,0.042,0.021c0.021,0.042,0.042,0.01,0.042,0.01c5.135,2.25,11.24,0.094,13.802-5 c0.917-1.844,1.26-3.823,1.083-5.729V128c0-11.76,9.573-21.333,21.333-21.333S448,116.24,448,128V373.333z" fill="white"/>
</svg>
</div>
<div class="ytp-menuitem-label">コメントブロック</div>
<div class="ytp-menuitem-content"></div>
</div>
<div class="ytp-menuitem" aria-haspopup="false" role="menuitem" tabindex="0" id="commentCopy">
<div class="ytp-menuitem-icon">
<svg height="100%" viewBox="0 0 488.3 488.3" width="100%" fill="#FFFFFF">
<g><g>
<path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/>
<path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/>
</g></g>
</svg>
</div>
<div class="ytp-menuitem-label">コメントコピー</div>
<div class="ytp-menuitem-content"></div>
</div>
<a href="#" target="_blank" class="ytp-menuitem" aria-haspopup="false" role="menuitem" tabindex="0" id="openURL">
<div class="ytp-menuitem-icon">
<svg height="100%" viewBox="0 0 36 36" width="100%">
<path d="M5.85 18.0c0.0-2.56 2.08-4.65 4.65-4.65h6.0V10.5H10.5c-4.14 .0-7.5 3.36-7.5 7.5s3.36 7.5 7.5 7.5h6.0v-2.85H10.5c-2.56 .0-4.65-2.08-4.65-4.65zM12.0 19.5h12.0v-3.0H12.0v3.0zm13.5-9.0h-6.0v2.85h6.0c2.56 .0 4.65 2.08 4.65 4.65s-2.08 4.65-4.65 4.65h-6.0V25.5h6.0c4.14 .0 7.5-3.36 7.5-7.5s-3.36-7.5-7.5-7.5z" fill="#fff"></path>
</svg>
</div>
<div class="ytp-menuitem-label">URLを開く</div>
<div class="ytp-menuitem-content"></div>
</a>
</div>
</div>}`;
// decode escaped html string
const htmlDecode = (input) => {
let doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
};
const saveConfig = () => {
// --------------------------------------------------------------------------------------------------------------------------------------
};
// add method to String to calculate it's hash
Object.defineProperty(String.prototype, 'hashCode', {
value: function () {
let hash = 0, i, chr;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
});
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
const createElementFromHTML = (htmlString) => {
let div = document.createElement('div');
div.id = nicoTubeContextMenuWrapperID;
document.body.append(div);
div.innerHTML = htmlString.trim();
return div.firstChild;
};
let fontSizeCache;
const calcFontSize = () => {
if (!fontSizeCache) {
fontSizeCache = commentCanvas.height * config.fontSize
}
return fontSizeCache
};
// functions to get elements
const getChatField = () => {
return document.getElementById('chatframe') && document.getElementById('chatframe').contentDocument.querySelector('#items.style-scope.yt-live-chat-item-list-renderer')
};
const getInputField = () => {
return document.getElementById('chatframe') && document.getElementById('chatframe').contentDocument.querySelector('#input-panel')
};
const getPlayer = () => {
return document.getElementById('movie_player')
};
const getNicoTubeSwitch = () => {
return document.getElementById(nicoTubeSwitchID)
};
const getChatCanvas = () => {
return document.getElementById(nicoChatCanvasID)
};
const getContextMenuWrapper = () => {
return document.getElementById(nicoTubeContextMenuWrapperID)
};
const getRightToolBar = () => {
return document.querySelector(rightToolBarSelector);
};
const getVideo = () => {
return document.querySelector(videoSelector);
};
const getToolNamePop = () => {
return document.querySelector(toolNamePopSelector);
};
const getContextMenuCanvas = () => {
return document.getElementById(contextMenuCanvasID);
};
// observe url change.
let hrefHist = location.href;
const URLChangeObserver = new MutationObserver(function (mutations) {
if (hrefHist !== location.href) {
hrefHist = location.href;
nicoTubeFinalize();
waitReady();
}
});
URLChangeObserver.disconnect();
// wait live chat box pops up and be ready
const waitReady = () => {
let findCount = 1;
clearInterval(waitReadyInterval);
waitReadyInterval = setInterval(() => {
findCount++;
if (findCount > 30) {
clearInterval(waitReadyInterval);
}
if (getPlayer() && getChatField()) {
clearInterval(waitReadyInterval);
nicoTubeInit();
}
}, 1000);
};
// and run when browser is ready
$(document).ready(() => {
URLChangeObserver.observe(document, {childList: true, subtree: true});
waitReady();
});
const nicoTubeInit = () => {
// setting
setTimeout(() => {
config = getVars();
player = getPlayer();
chatField = getChatField();
nicoTubeOn = true;
needToRedraw = false;
currentPlayState = 1;
});
setTimeout(() => {
// add nicoTube switch
toolBarRight = getRightToolBar();
toolBarRight.insertAdjacentHTML('afterbegin', nicoTubeToggleSwitch);
nicoTubeSwitch = getNicoTubeSwitch();
});
setTimeout(() => {
// comment Canvas
player.insertAdjacentHTML('afterbegin', canvasHtml);
commentCanvas = getChatCanvas();
commentContext = commentCanvas.getContext('2d');
});
setTimeout(() => {
// player settings
video = getVideo();
// positions
player.style.position = 'relative';
commentCanvas.style.position = 'absolute';
video.style.position = 'absolute';
});
setTimeout(() => {
setNicoTubeSwitch();
setCommentCanvas();
mainCanvasResize();
setResizeObserve();
});
setTimeout(() => {
AllCommentLanes = commentInit();
setCommentObserveInterval();
setExpireInterval();
setNicoTubeContextMenu();
setVideoOn();
});
setTimeout(() => {
update();
})
};
const nicoTubeFinalize = () => {
// stop finding chat box and disconnect all mutations
clearInterval(waitReadyInterval);
globalMutations.comment && globalMutations.comment.disconnect();
globalMutations.hover && globalMutations.hover.disconnect();
globalMutations.contextMenu && globalMutations.contextMenu.disconnect();
// remove all element created by nicoTube
getNicoTubeSwitch() && getNicoTubeSwitch().remove();
getChatCanvas() && getChatCanvas().remove();
getContextMenuWrapper() && getContextMenuWrapper().remove();
// remove interval
expireInterval && clearInterval(expireInterval);
waitReadyInterval && clearInterval(waitReadyInterval);
chatFieldWait && clearInterval(chatFieldWait);
// clear all chat element
AllCommentLanes && AllCommentLanes.forEach((commentLane) => {
commentLane.forEach(() => {
commentLane.shift();
});
});
};
// on off switch
const setNicoTubeSwitch = () => {
nicoTubeSwitch.onclick = () => {
let currentSwitch = $(nicoTubeSwitch).attr('show') === 'true';
$(nicoTubeSwitch).attr('show', !currentSwitch);
$('#nicoTubepath1').css('visibility', (currentSwitch ? 'hidden' : 'visible'));
$('#nicoTubepath2').css('visibility', (!currentSwitch ? 'hidden' : 'visible'));
$('#nicoTubepath3').css('visibility', (!currentSwitch ? 'hidden' : 'visible'));
$('#nicoTubepath4').css('visibility', (!currentSwitch ? 'hidden' : 'visible'));
nicoTubeOn = !currentSwitch;
$(commentCanvas).css('visibility', (nicoTubeOn ? 'visible' : 'hidden'));
};
// switch mouseover mouseout
isMouseOn = false;
const hoverObserver = globalMutations.hover || new MutationObserver((mutations) => {
$('#movie_player > div.ytp-tooltip.ytp-bottom').css('display', '');
$('#movie_player > div.ytp-tooltip.ytp-bottom').css('opacity', '1.0')
});
globalMutations.hover = hoverObserver;
nicoTubeSwitch.onmouseover = () => {
settingButton = document.querySelector('#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls > button.ytp-button.ytp-settings-button');
isMouseOn = true;
hoverObserver.disconnect();
if (!getToolNamePop()) {
let e = new Event('mouseover');
settingButton.dispatchEvent(e);
settingButton.classList.remove('ytp-preview');
nicoTubeSwitch.onmouseover();
return
}
hoverObserver.observe(getToolNamePop(), {
attributes: true,
childList: false,
characterData: false
});
$(getToolNamePop()).css({'display': '', 'left': $(nicoTubeSwitch).offset().left + 'px'});
$(getToolNamePop().querySelector('div.ytp-tooltip-text-wrapper > span')).text('NicoTube')
};
nicoTubeSwitch.onmouseout = () => {
isMouseOn = false;
hoverObserver.disconnect();
$(getToolNamePop()).css({'display': 'none'})
}
};
const setNicoTubeContextMenu = () => {
// youtube default contextmenu
nicoTubeContextMenu = createElementFromHTML(nicoTubeContextMenuHTML);
const contextMenuCanvasInit = (menuCanvas) => {
menuCanvas.width = 300;
menuCanvas.height = 50;
};
const contextMenuCanvasDraw = (menuCanvas, fromCanvas) => {
let contextMenuCanvasFontSize = 30;
let menuCtx = menuCanvas.getContext('2d');
let ratio = contextMenuCanvasFontSize / (calcFontSize() * 2);
menuCanvas.width += 0;
menuCtx.drawImage(fromCanvas, contextMenuCanvasFontSize * 2 / 3, contextMenuCanvasFontSize / 2, fromCanvas.width * ratio, fromCanvas.height * ratio)
};
const contextMenuPopObserver = globalMutations.contextMenu || new MutationObserver((mutations) => {
mutations[0].target.style.display = 'none';
});
contextMenuCanvas = getContextMenuCanvas();
contextMenuCanvasInit(contextMenuCanvas);
globalMutations.contextMenu = contextMenuPopObserver;
let rightClickTargetComment;
let rightClickTargetCommentIndex;
// comment right click
$(document).on('contextmenu', (e) => {
let x = e.clientX + window.pageXOffset;
let y = e.clientY + window.pageYOffset;
let canvasX = $(commentCanvas).offset().left;
let canvasY = $(commentCanvas).offset().top;
if (canvasX <= x && x <= canvasX + commentCanvas.width && canvasY <= y && y <= canvasY + commentCanvas.height) {
y -= canvasY;
let lane = Math.floor(y / (calcFontSize() * (config.commentMargin + 1)));
let matchComment;
if (!AllCommentLanes || !AllCommentLanes[lane]) return;
let index = AllCommentLanes[lane].length - 1;
AllCommentLanes[lane].reverse().some((comment) => {
let comX = commentCanvas.width - (commentCanvas.width + comment.width) * ((currentTick() - comment.timestamp) / (config.commentSpeed)) + commentCanvas.getBoundingClientRect().left;
if (comX <= x && x <= comX + comment.width) {
matchComment = comment;
rightClickTargetCommentIndex = {lane: lane, index: index};
return true;
}
index--;
});
if (!matchComment) {
return
}
rightClickTargetComment = matchComment;
let youtubeContextMenu = document.querySelector('body > div.ytp-popup.ytp-contextmenu') || document.querySelector('#movie_player > div.ytp-popup.ytp-contextmenu.ytp-big-mode');
youtubeContextMenu.style.display = 'none';
contextMenuPopObserver.disconnect();
contextMenuPopObserver.observe(youtubeContextMenu, {
attributes: true,
childList: false,
characterData: false,
attributeFilter: ['display']
});
console.log(matchComment)
if (matchComment.url === null) {
document.getElementById('openURL').style.display = 'none';
} else {
document.getElementById('openURL').style.display = '';
document.getElementById('openURL').href = matchComment.url;
}
e.preventDefault();
setTimeout(() => {
$(nicoTubeContextMenu).css({
'top': y, 'left': x, 'display': ''
});
}, 50);
contextMenuCanvasDraw(contextMenuCanvas, matchComment.canvas);
$(commentCanvas).css('pointer-events', '');
}
});
// click on nicoTube context menu
$('#userBlock').on('click', () => {
$(nicoTubeContextMenu).css('display', 'none');
$(commentCanvas).css('pointer-events', 'none');
config.blockedUsers.push([rightClickTargetComment.from, commentToText(rightClickTargetComment)]);
for (let i = 0; i < AllCommentLanes.length; i++) {
AllCommentLanes[i] = AllCommentLanes[i].filter(comment => comment.from !== rightClickTargetComment.from)
}
needToRedraw = true;
});
$('#commentBlock').on('click', () => {
config.blockedComments.push([rightClickTargetComment.hash, commentToText(rightClickTargetComment)]);
$(nicoTubeContextMenu).css('display', 'none');
$(commentCanvas).css('pointer-events', 'none');
for (let i = 0; i < AllCommentLanes.length; i++) {
AllCommentLanes[i] = AllCommentLanes[i].filter(comment => comment.hash !== rightClickTargetComment.hash)
}
needToRedraw = true;
});
$('#commentCopy').on('click', () => {
$(nicoTubeContextMenu).css('display', 'none');
$(commentCanvas).css('pointer-events', 'none');
GM_setClipboard(commentToText(rightClickTargetComment));
});
$(commentCanvas).on('click', () => {
$(commentCanvas).css('pointer-events', 'none');
});
};
// comment to text to copy
const commentToText = (comment) => {
return comment.parsed.map((com) => {
if (com.type === 1) return ':' + com.text + ':'
return com.text
}).join('');
};
const setCommentCanvas = () => {
// click canvas after right click comment
commentCanvas.onclick = () => {
$(nicoTubeContextMenu).css('display', 'none');
globalMutations.contextMenu.disconnect();
}
};
const currentTick = () => {
return video.currentTime
};
const createCanvas = (width, height, comment) => {
let cvs = new OffscreenCanvas(width * 1.2, height * 2);
let context = cvs.getContext('2d');
context.font = `${config.isBold ? 'bold' : ''} ${calcFontSize()}pt '${config.fontFamily}'`;
context.textBaseline = 'top';
if (comment.type === 0 || comment.type === 2 || (comment.type === 1 && !config.changeColorOfMember) || (comment.type === 5 && !config.changeColorOfModerator)) {
context.fillStyle = config.fontColor;
} else if (comment.type === 1) {
context.fillStyle = config.memberFontColor;
} else if (comment.type === 5) {
context.fillStyle = config.moderatorFontColor;
}
context.lineWidth = calcFontSize() * config.borderWidth;
context.strokeStyle = config.borderColor;
return {canvas: cvs, context: context}
};
// resize canvas handler
const mainCanvasResize = () => {
commentCanvas.width = player.getBoundingClientRect().width * config.resolutionRatio;
commentCanvas.height = video.getBoundingClientRect().height * config.resolutionRatio;
commentCanvas.style.width = player.getBoundingClientRect().width + 'px';
commentCanvas.style.height = video.getBoundingClientRect().height + 'px';
commentCanvas.style.top = video.style.top;
commentContext.font = `${config.isBold ? 'bold' : ''} ${calcFontSize()}pt '${config.fontFamily}'`;
commentContext.shadowColor = config.fontColor;
commentContext.globalAlpha = config.canvasAlpha;
commentContext.textBaseline = 'top';
commentContext.fillStyle = config.fontColor;
commentContext.lineWidth = calcFontSize() * config.borderWidth;
commentContext.strokeStyle = config.borderColor;
};
const setResizeObserve = () => {
// resize observe
let whenResized;
const resizeObserver = new ResizeObserver(entry => {
clearTimeout(whenResized);
whenResized = setTimeout(resizeHandler, 300);
});
resizeObserver.disconnect();
resizeObserver.observe(video);
resizeObserver.observe(player);
};
const resizeHandler = () => {
fontSizeCache = 0;
nicoTubeOn = false;
globalMutations.comment && globalMutations.comment.disconnect();
needToRedraw = true;
mainCanvasResize();
AllCommentLanes.forEach((commentLane) => {
commentLane.forEach((comment) => {
setTimeout(() => {
comment.redraw();
})
})
});
chatFieldWait = setInterval(() => {
chatField = getChatField();
if (chatField) {
clearInterval(chatFieldWait);
nicoTubeOn = true;
setTimeout(() => {
globalMutations.comment && globalMutations.comment.observe(chatField, {
attributes: false,
childList: true,
characterData: false
});
})
}
}, 50)
};
// comment observe
const commentInit = () => {
return [...Array(Math.floor(1 / config.fontSize / (1 + config.commentMargin)))].map(x => [])
};
commentAddMutation = false;
const setCommentObserveInterval = () => {
setInterval(() => {
globalMutations.comment && globalMutations.comment.disconnect();
globalMutations.comment = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
// wait for moment for image appear
setTimeout(() => {
if (mutation.addedNodes.length > 0) {
Array.from(mutation.addedNodes).forEach((node) => {
let minLine, minMove = 10e30;
let parsedComment = parseComment(node);
node.remove();
if (parsedComment.postTime < currentTick() - config.commentSpeed) {
return; // too old to add
}
if (config.blockedUsers.some((blockedUser) => { // blocked user
return blockedUser[0] == parsedComment.from
}) || config.blockedComments.some((blockedComment) => { // blocked comment
return blockedComment[0] == parsedComment.hash
}) || config.blockedKeyWords.some((blockedKeyWord) => {
return RegExp(blockedKeyWord).test(commentToText(parsedComment))
})) {
return
}
if (parsedComment.type === 4) {
return // youtube engage
} else if (parsedComment.type === 3) {
config.welcomeNewMember && welcomeNewMember(parsedComment);
return // new member banner
}
let commentSize = calcCommentSize(parsedComment);
let commentWidth = commentSize.width;
let fullMoveWidth = $(commentCanvas).width() + commentWidth;
let commentCanvasRatio = $(commentCanvas).width() / fullMoveWidth;
let startGetOut = currentTick() + commentCanvasRatio * config.commentSpeed;
// this will solve the problem that new many comments will flow into first lane
let addCommentInterval = setInterval(() => {
if (!commentAddMutation) {
clearInterval(addCommentInterval);
commentAddMutation = true;
AllCommentLanes.some((commentLane) => {
let lastCommentShowComplete = commentLane.length === 0 || config.commentSpeed / (commentCanvas.width + commentLane[commentLane.length - 1].width) * commentLane[commentLane.length - 1].width + commentLane[commentLane.length - 1].timestamp;
if (commentLane.length === 0 || (commentLane[commentLane.length - 1].expired < startGetOut && lastCommentShowComplete < currentTick())) {
parsedComment.laneOffset = 0;
commentLane.push(commentObj(parsedComment, currentTick(), currentTick() + config.commentSpeed, commentSize));
return true;
}
if (minMove > commentLane[commentLane.length - 1].expired) {
minMove = commentLane[commentLane.length - 1].expired;
parsedComment.laneOffset = 0.5;
minLine = commentLane;
}
}) || (() => {
minLine.push(commentObj(parsedComment, currentTick(), currentTick() + config.commentSpeed, commentSize));
return true
})();
commentAddMutation = false;
}
}, getRandomInt(1, 5))
})
}
}, 200)
});
});
getChatField() && globalMutations.comment.observe(getChatField(), {
attributes: false,
childList: true,
characterData: false
});
getChatField().style.display = 'none'
}, 1000)
};
// render to offscreen canvas
const drawCommentOffScreen = (canvas, context, comment) => {
let currentX = calcFontSize() * config.borderWidth * 2;
let imageSrc, newImage;
let loadedCount = 0;
let toBeLoaded = 0;
let newImages = [];
if (comment.type === 2) {
let wholeX = comment.parsed.map((seps) => {
switch (seps.type) {
case 0:
return seps.width;
case 1:
return seps.width
}
}).reduce((p, x) => p + x, 0.0);
let beforeFillStyle = context.fillStyle;
let beforeStrokeStyle = context.strokeStyle;
context.fillStyle = comment.BackGroundColor;
config.showSuperChatBackground && context.fillRect(0, 0, wholeX + comment.height * 0.2, comment.height * 1.5);
context.strokeStyle = comment.strokeColor;
config.showSuperChatStroke && context.strokeRect(0, 0, wholeX + comment.height * 0.2, comment.height * 1.5);
context.fillStyle = beforeFillStyle;
context.strokeStyle = beforeStrokeStyle;
}
comment.parsed.forEach((seps) => {
switch (seps.type) {
case 0:
context.strokeText(seps.text, Math.floor(currentX), comment.height * 0.3);
context.fillText(seps.text, Math.floor(currentX), comment.height * 0.3);
currentX += seps.width;
break;
case 1:
newImage = new Image();
imageSrc = seps.src;
toBeLoaded += 1;
newImage.onload = () => {
loadedCount += 1;
if (loadedCount === toBeLoaded) {
newImages.forEach((image) => {
if (config.emojiBorder) {
context.save();
context.filter = `drop-shadow(rgb(0,0, 0) 0px 0px 1px) drop-shadow(rgb(0,0, 0) 0px 0px ${calcFontSize() / 200}px)`.repeat(3);
}
context.drawImage(image, Math.floor(image.cX), comment.height * 0.25 / 1.2, seps.width / 1.2, seps.width / 1.2);
if (config.emojiBorder) {
context.restore();
}
})
}
};
newImage.src = imageSrc;
newImage.cX = currentX + seps.width * 0.1 / 1.2;
currentX += seps.width;
newImages.push(newImage);
break
}
});
if (comment.from === 'self') {
context.strokeStyle = config.userCommentStrokeColor;
context.lineWidth = 4;
context.strokeRect(context.lineWidth / 2, context.lineWidth / 2, currentX + calcFontSize() * 0.2, comment.height * 1.5);
}
};
// each comment object
const commentObj = (parsedComment, timestamp, expired, size) => {
let obj = {
url: parsedComment.url,
laneOffset: parsedComment.laneOffset,
parsed: parsedComment.parsed,
id: parsedComment.id,
type: parsedComment.type,
author: parsedComment.author,
from: parsedComment.from,
BackGroundColor: parsedComment.BackGroundColor,
strokeColor: parsedComment.strokeColor,
postTime: parsedComment.postTime,
purchaseAmount: parsedComment.purchaseAmount,
hash: parsedComment.hash,
timestamp: timestamp,
expired: expired,
width: size.width,
height: size.height
};
let canvasCtx = createCanvas(obj.width, obj.height, parsedComment);
obj.canvas = canvasCtx.canvas;
obj.ctx = canvasCtx.context;
obj.redraw = () => {
let size = calcCommentSize(obj);
obj.width = size.width;
obj.height = size.height;
obj.canvas.width = obj.width * 1.2;
obj.canvas.height = obj.height * 2;
obj.ctx.font = `${config.isBold ? 'bold' : ''} ${calcFontSize()}pt '${config.fontFamily}'`;
obj.ctx.textBaseline = 'top';
if (obj.type === 0 || obj.type === 2 || (obj.type === 1 && !config.changeColorOfMember) || (obj.type === 5 && !config.changeColorOfModerator)) {
obj.ctx.fillStyle = config.fontColor;
} else if (obj.type === 1) {
obj.ctx.fillStyle = config.memberFontColor;
} else if (obj.type === 5) {
obj.ctx.fillStyle = config.moderatorFontColor;
}
obj.ctx.lineWidth = calcFontSize() * config.borderWidth;
obj.ctx.strokeStyle = config.borderColor;
drawCommentOffScreen(obj.canvas, obj.ctx, obj);
};
drawCommentOffScreen(obj.canvas, obj.ctx, obj);
return obj
};
// each statement object
const stateObj = (type, data) => {
let obj = {type: type};
let elem;
switch (type) {
case 0: // str
obj.text = data;
break;
case 1: // img
elem = $(data).get(0);
obj.src = elem.src;
obj.text = elem.alt;
obj.isPureEmoji = obj.text.length === 1;
break;
}
return obj
};
// comment parse
const parseComment = (comment) => {
let userIdIndexOfImageSrc = 6;
// type:
// 0: normal
// 1: member
// 2: super chat
// 3: member subscribe
// 4: youtube engage
// 5: moderator
let obj = {};
let author = $(comment.querySelector('#author-name'));
let card = $(comment.querySelector('#card'));
let timestamp = $(comment.querySelector('#timestamp'));
obj.parsed = [];
obj.id = comment.id;
if (card.hasClass('yt-live-chat-membership-item-renderer')) {
obj.type = 3;
obj.parsed = [stateObj(0, obj.author)];
return obj;
} else if (card.hasClass('yt-live-chat-viewer-engagement-message-renderer')) {
obj.type = 4;
return obj;
} else if (author.hasClass('member')) {
obj.type = 1;
} else if (author.hasClass('moderator')) {
obj.type = 5;
} else if (card.hasClass('yt-live-chat-paid-message-renderer')) {
obj.type = 2;
obj.BackGroundColor = $(comment).css('--yt-live-chat-paid-message-primary-color');
obj.strokeColor = $(comment).css('--yt-live-chat-paid-message-secondary-color')
} else {
obj.type = 0;
}
obj.author = author.text().trim();
let src = $(comment.querySelector('#img')).attr('src');
let from = src.split('/')[src.split('/').length - 1].split('=')[0];
if ('yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' === from) { // place holder. by YouTube
obj.from = 'self';
} else {
obj.from = from;
}
if (obj.type === 0 || obj.type === 1 || obj.type === 5) {
timestamp = timestamp.text().split(':');
obj.postTime = (timestamp.length === 2) ? timestamp[0] * 60 + timestamp[1] * 1 : timestamp[0] * 3600 + timestamp[1] * 60 + timestamp[2] * 1;
comment = comment.querySelector(messageFieldSelector).innerHTML;
} else if (obj.type === 2) {
timestamp = timestamp.text().split(':');
obj.postTime = (timestamp.length === 2) ? timestamp[0] * 60 + timestamp[1] * 1 : timestamp[0] * 3600 + timestamp[1] * 60 + timestamp[2] * 1;
obj.purchaseAmount = $(comment.querySelector('#purchase-amount')).text();
config.showSuperChatAmount && obj.parsed.push(stateObj(0, ' ' + obj.purchaseAmount + ' '));
comment = comment.querySelector(messageFieldSelector).innerHTML;
}
let imageTagSeparator = /(<img.+?>)/g;
let separated = comment.split(imageTagSeparator);
separated = separated.filter(n => n);
separated.forEach((sep) => {
// type:
// 0: str
// 1: image
if (sep.startsWith('<img')) {
obj.parsed.push(stateObj(1, sep))
} else {
obj.parsed.push(stateObj(0, htmlDecode((sep))))
}
});
let commentText = obj.parsed.map((com) => {
if (com.type === 1) return ':' + com.text + ':'
return com.text
}).join('');
const regex = /https?:\/\/[^ ]+/g;
let urls = commentText.match(regex);
if (urls && urls.length) {
obj.url = urls[0]
} else {
obj.url = null
}
obj.hash = commentToText(obj).hashCode();
return obj
};
const calcCommentSize = (comment) => {
let width = 0.0;
let height = 0.0;
let fullwidth = commentContext.measureText('あ').width;
let currentWidth;
let metrics;
let texts = [];
comment.parsed.forEach((state) => {
switch (state.type) {
case 0: // str
metrics = commentContext.measureText(state.text);
currentWidth = metrics.width;
texts.push(state.text);
width += currentWidth;
state.width = currentWidth;
break;
case 1: // emoji
width += fullwidth * 1.2;
height = Math.floor(fullwidth);
state.width = fullwidth * 1.2;
break
}
});
metrics = commentContext.measureText(texts.join(''));
comment.height = Math.max(metrics.actualBoundingBoxDescent - metrics.actualBoundingBoxAscent, height);
return {width: width, height: comment.height}
};
// comment expire
const setExpireInterval = () => {
expireInterval = setInterval(() => {
AllCommentLanes.forEach((commentLane) => {
commentLane.forEach((comment) => {
if (comment.expired < currentTick() || comment.timestamp > currentTick()) {
commentLane.shift();
}
});
});
}, 100);
};
// stop-play observe
const setVideoOn = () => {
const checkVideoIsPaused = () => {
return !video.paused
};
video.onpause = () => {
currentPlayState = 0;
};
video.onplay = () => {
currentPlayState = 1;
};
// seek handler
video.onseeking = () => {
currentPlayState = 0;
};
video.onseeked = () => {
currentPlayState = checkVideoIsPaused();
}
};
// draw comment
const drawComment = (comment, x, y) => {
try {
commentContext.drawImage(comment.canvas, x, parseInt(y + calcFontSize() * (config.commentMargin + 1) * comment.laneOffset));
} catch (e) {
}
};
let newComers = [];
let hands = ['👋', '👋🏻', '👋🏼', '👋🏽', '👋🏾', '👋🏿'];
const welcomeNewMember = (comment) => {
let canvas = createCanvas(calcFontSize() * 3, calcFontSize() * 3, comment);
let rotationCanvas = createCanvas(calcFontSize() * 3, calcFontSize() * 3, comment);
let timestamp = performance.now();
let hand = hands[Math.floor(timestamp % hands.length)];
canvas.context.textBaseline = 'bottom';
rotationCanvas.context.textAlign = 'bottom';
canvas.context.textBaseline = 'right';
rotationCanvas.context.textAlign = 'right';