forked from 9channel/dd-danmaku
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ede.js
1577 lines (1462 loc) · 80.4 KB
/
ede.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 Jellyfin danmaku extension
// @description Jellyfin弹幕插件
// @namespace https://github.com/RyoLee
// @author RyoLee
// @version 1.51
// @copyright 2022, RyoLee (https://github.com/RyoLee)
// @license MIT; https://raw.githubusercontent.com/Izumiko/jellyfin-danmaku/jellyfin/LICENSE
// @icon https://github.githubassets.com/pinned-octocat.svg
// @updateURL https://cdn.jsdelivr.net/gh/Izumiko/jellyfin-danmaku@gh-pages/ede.user.js
// @downloadURL https://cdn.jsdelivr.net/gh/Izumiko/jellyfin-danmaku@gh-pages/ede.user.js
// @grant GM_xmlhttpRequest
// @connect *
// @match *://*/*/web/index.html
// @match *://*/web/index.html
// @match *://*/*/web/
// @match *://*/web/
// @match https://jellyfin-web.pages.dev/
// ==/UserScript==
(async function () {
'use strict';
if (document.querySelector('meta[name="application-name"]').content !== 'Jellyfin') {
return;
}
// ------ configs start------
const isInTampermonkey = !(typeof GM_xmlhttpRequest === 'undefined');
const isLocalCors = (!isInTampermonkey && document.currentScript?.src) ? new URL(document.currentScript?.src).searchParams.has("noCors") : false;
const corsProxy = 'https://ddplay-api.930524.xyz/cors/';
const apiPrefix = isInTampermonkey
? 'https://api.dandanplay.net'
: isLocalCors
? `${window.location.origin}/ddplay-api`
: corsProxy + 'https://api.dandanplay.net';
// const apiPrefix = 'https://api.930524.xyz';
const authPrefix = isLocalCors ? apiPrefix : corsProxy + 'https://api.dandanplay.net'; // 在Worker上计算Hash
let ddplayStatus = JSON.parse(localStorage.getItem('ddplayStatus')) || { isLogin: false, token: '', tokenExpire: 0 };
const check_interval = 200;
// 0:当前状态关闭 1:当前状态打开
let danmaku_icons = ['comments_disabled', 'comment'];
const search_icon = 'find_replace';
const source_icon = 'library_add';
let log_icons = ['code_off', 'code'];
const settings_icon = 'tune';
const send_icon = 'send';
const spanClass = 'xlargePaperIconButton material-icons ';
const buttonOptions = {
class: 'paper-icon-button-light',
is: 'paper-icon-button-light',
};
const uiAnchorStr = 'pause';
const uiQueryStr = '.btnPause';
const mediaContainerQueryStr = "div[data-type='video-osd']";
const mediaQueryStr = 'video';
let isNewJellyfin = true;
let itemId = '';
// Intercept XMLHttpRequest
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (_, url) {
this.addEventListener('load', function () {
if (url.endsWith('PlaybackInfo')) {
const res = JSON.parse(this.responseText);
itemId = res.MediaSources[0].Id;
}
});
originalOpen.apply(this, arguments);
};
const displayButtonOpts = {
title: '弹幕开关',
id: 'displayDanmaku',
onclick: () => {
if (window.ede.loading) {
showDebugInfo('正在加载,请稍后再试');
return;
}
showDebugInfo('切换弹幕开关');
window.ede.danmakuSwitch = (window.ede.danmakuSwitch + 1) % 2;
window.localStorage.setItem('danmakuSwitch', window.ede.danmakuSwitch);
document.querySelector('#displayDanmaku').children[0].className = spanClass + danmaku_icons[window.ede.danmakuSwitch];
if (window.ede.danmaku) {
window.ede.danmakuSwitch == 1 ? window.ede.danmaku.show() : window.ede.danmaku.hide();
}
},
};
const searchButtonOpts = {
title: '搜索弹幕',
id: 'searchDanmaku',
class: search_icon,
onclick: () => {
if (window.ede.loading) {
showDebugInfo('正在加载,请稍后再试');
return;
}
showDebugInfo('手动匹配弹幕');
reloadDanmaku('search');
},
};
const sourceButtonOpts = {
title: '增加弹幕源',
id: 'addDanmakuSource',
class: source_icon,
onclick: () => {
showDebugInfo('手动增加弹幕源');
let source = prompt('请输入弹幕源地址:');
if (source) {
getCommentsByUrl(source)
.then(comments => {
if (comments !== null) {
createDanmaku(comments)
.then(() => {
showDebugInfo('弹幕就位');
// 如果已经登录,把弹幕源提交给弹弹Play
if (ddplayStatus.isLogin) {
postRelatedSource(source);
}
})
.catch(error => {
console.error('创建弹幕失败:', error);
});
}
}
)
} else {
showDebugInfo('未获取弹幕源地址');
}
},
};
const logButtonOpts = {
title: '日志开关',
id: 'displayLog',
onclick: () => {
if (window.ede.loading) {
showDebugInfo('正在加载,请稍后再试');
return;
}
window.ede.logSwitch = (window.ede.logSwitch + 1) % 2;
window.localStorage.setItem('logSwitch', window.ede.logSwitch);
document.querySelector('#displayLog').children[0].className = spanClass + log_icons[window.ede.logSwitch];
let logSpan = document.querySelector('#debugInfo');
if (logSpan) {
window.ede.logSwitch == 1 ? (logSpan.style.display = 'block') && showDebugInfo('开启日志显示') : (logSpan.style.display = 'none');
}
}
};
const settingButtonOpts = {
title: '弹幕设置',
id: 'danmakuSettings',
class: settings_icon,
onclick: () => {
if (document.getElementById('danmakuModal')) {
return;
}
const modal = document.createElement('div');
modal.id = 'danmakuModal';
modal.className = 'dialogContainer';
modal.innerHTML = `
<div class="dialog" style="padding: 20px; border-radius: .3em; position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);">
<div style="display: flex; flex-direction: column; gap: 5px;">
<div style="display: flex;">
<span id="lbopacity" style="flex: auto;">透明度:</span>
<input style="width: 50%;" type="range" id="opacity" min="0" max="1" step="0.1" value="${window.ede.opacity || 0.7}" />
</div>
<div style="display: flex;">
<span id="lbspeed" style="flex: auto;">弹幕速度:</span>
<input style="width: 50%;" type="range" id="speed" min="20" max="600" step="10" value="${window.ede.speed || 200}" />
</div>
<div style="display: flex;">
<label style="flex: auto;">字体:</label>
<div><input style="flex-grow: 1;" id="danmakuFontFamily" placeholder="sans-serif" value="${window.ede.fontFamily ?? "sans-serif"}" /></div>
</div>
<div style="display: flex;">
<span id="lbfontSize" style="flex: auto;">字体大小:</span>
<input style="width: 50%;" type="range" id="fontSize" min="8" max="80" step="1" value="${window.ede.fontSize || 18}" />
</div>
<div style="display: flex;">
<label style="flex: auto;">其他字体选项:</label>
<div><input style="flex-grow: 1;" id="danmakuFontOptions" placeholder="" value="${window.ede.fontOptions ?? ""}" /></div>
</div>
<div style="display: flex;">
<span id="lbheightRatio" style="flex: auto;">高度比例:</span>
<input style="width: 50%;" type="range" id="heightRatio" min="0" max="1" step="0.05" value="${window.ede.heightRatio || 0.9}" />
</div>
<div style="display: flex;">
<span id="lbdanmakuDensityLimit" style="flex: auto;">密度限制等级:</span>
<input style="width: 50%;" type="range" id="danmakuDensityLimit" min="0" max="3" step="1" value="${window.ede.danmakuDensityLimit}" />
</div>
<div style="display: flex;">
<label style="flex: auto;">弹幕过滤:</label>
<div><input type="checkbox" id="filterBilibili" name="danmakuFilter" value="1" ${((window.ede.danmakuFilter & 1) === 1) ? 'checked' : ''} />
<label for="filterBilibili">B站</label></div>
<div><input type="checkbox" id="filterGamer" name="danmakuFilter" value="2" ${((window.ede.danmakuFilter & 2) === 2) ? 'checked' : ''} />
<label for="filterGamer">巴哈</label></div>
<div><input type="checkbox" id="filterDanDanPlay" name="danmakuFilter" value="4" ${((window.ede.danmakuFilter & 4) === 4) ? 'checked' : ''} />
<label for="filterDanDanPlay">弹弹</label></div>
<div><input type="checkbox" id="filterOthers" name="danmakuFilter" value="8" ${((window.ede.danmakuFilter & 8) === 8) ? 'checked' : ''} />
<label for="filterOthers">其他</label></div>
</div>
<div style="display: flex;">
<label style="flex: auto;">弹幕类型过滤:</label>
<div><input type="checkbox" id="filterBottom" name="danmakuModeFilter" value="1" ${((window.ede.danmakuModeFilter & 1) === 1) ? 'checked' : ''} />
<label for="filterBottom">底部</label></div>
<div><input type="checkbox" id="filterTop" name="danmakuModeFilter" value="2" ${((window.ede.danmakuModeFilter & 2) === 2) ? 'checked' : ''} />
<label for="filterTop">顶部</label></div>
<div><input type="checkbox" id="filterRoll" name="danmakuModeFilter" value="4" ${((window.ede.danmakuModeFilter & 4) === 4) ? 'checked' : ''} />
<label for="filterRoll">滚动</label></div>
</div>
<div style="display: flex;">
<label style="flex: auto;">简繁转换:</label>
<div><input type="radio" id="chConvert0" name="chConvert" value="0" ${(window.ede.chConvert === 0) ? 'checked' : ''}>
<label for="chConvert0">不转换</label></div>
<div><input type="radio" id="chConvert1" name="chConvert" value="1" ${(window.ede.chConvert === 1) ? 'checked' : ''}>
<label for="chConvert1">简体</label></div>
<div><input type="radio" id="chConvert2" name="chConvert" value="2" ${(window.ede.chConvert === 2) ? 'checked' : ''}>
<label for="chConvert2">繁体</label></div>
</div>
<div style="display: flex;">
<label style="flex: auto;">使用本地xml弹幕:</label>
<div><input type="radio" id="enableXmlDanmaku" name="useXmlDanmaku" value="1" ${(window.ede.useXmlDanmaku === 1) ? 'checked' : ''}>
<label for="chConvert0">是</label></div>
<div><input type="radio" id="disableXmlDanmaku" name="useXmlDanmaku" value="0" ${(window.ede.useXmlDanmaku === 0) ? 'checked' : ''}>
<label for="chConvert1">否</label></div>
</div>
<div style="display: flex;">
<label style="flex: auto;">当前弹幕偏移时间:</label>
<div><input style="flex-grow: 1;" id="danmakuOffsetTime" placeholder="秒" value="${window.ede.curEpOffset || 0}" /></div>
</div>
</div>
<div style="display: flex; justify-content: space-between; margin-top: 10px;">
<button id="saveSettings" class="raised button-submit block btnSave formDialogFooterItem emby-button">保存设置</button>
<button id="cancelSettings" class="raised button-cancel block btnCancel formDialogFooterItem emby-button">取消</button>
</div>
</div>`;
document.body.appendChild(modal);
function showCurrentVal(id, ticks) {
const val = document.getElementById(id).value;
const span = document.getElementById('lb' + id);
const prefix = span.innerText.split(':')[0];
if (ticks) {
span.innerText = prefix + ': ' + ticks[val];
} else {
span.innerText = prefix + ': ' + val;
}
}
showCurrentVal('opacity');
showCurrentVal('speed');
showCurrentVal('fontSize');
showCurrentVal('heightRatio');
showCurrentVal('danmakuDensityLimit', ['无', '低', '中', '高']);
const closeModal = () => {
document.body.removeChild(modal);
};
document.getElementById('saveSettings').onclick = () => {
try {
window.ede.opacity = parseFloatOfRange(document.getElementById('opacity').value, 0, 1);
window.localStorage.setItem('danmakuopacity', window.ede.opacity.toString());
showDebugInfo(`设置弹幕透明度:${window.ede.opacity}`);
window.ede.speed = parseFloatOfRange(document.getElementById('speed').value, 20, 600);
window.localStorage.setItem('danmakuspeed', window.ede.speed.toString());
showDebugInfo(`设置弹幕速度:${window.ede.speed}`);
window.ede.fontSize = parseFloatOfRange(document.getElementById('fontSize').value, 8, 40);
window.localStorage.setItem('danmakusize', window.ede.fontSize.toString());
showDebugInfo(`设置弹幕大小:${window.ede.fontSize}`);
window.ede.heightRatio = parseFloatOfRange(document.getElementById('heightRatio').value, 0, 1);
window.localStorage.setItem('danmakuheight', window.ede.heightRatio.toString());
showDebugInfo(`设置弹幕高度:${window.ede.heightRatio}`);
window.ede.danmakuFilter = 0;
document.querySelectorAll('input[name="danmakuFilter"]:checked').forEach(element => {
window.ede.danmakuFilter += parseInt(element.value, 10);
});
window.localStorage.setItem('danmakuFilter', window.ede.danmakuFilter);
showDebugInfo(`设置弹幕过滤:${window.ede.danmakuFilter}`);
window.ede.danmakuModeFilter = 0;
document.querySelectorAll('input[name="danmakuModeFilter"]:checked').forEach(element => {
window.ede.danmakuModeFilter += parseInt(element.value, 10);
});
window.localStorage.setItem('danmakuModeFilter', window.ede.danmakuModeFilter);
showDebugInfo(`设置弹幕模式过滤:${window.ede.danmakuModeFilter}`);
window.ede.danmakuDensityLimit = parseInt(document.getElementById('danmakuDensityLimit').value);
window.localStorage.setItem('danmakuDensityLimit', window.ede.danmakuDensityLimit);
showDebugInfo(`设置弹幕密度限制等级:${window.ede.danmakuDensityLimit}`);
window.ede.chConvert = parseInt(document.querySelector('input[name="chConvert"]:checked').value);
window.localStorage.setItem('chConvert', window.ede.chConvert);
showDebugInfo(`设置简繁转换:${window.ede.chConvert}`);
window.ede.useXmlDanmaku = parseInt(document.querySelector('input[name="useXmlDanmaku"]:checked').value);
window.localStorage.setItem('useXmlDanmaku', window.ede.useXmlDanmaku);
showDebugInfo(`是否使用本地xml弹幕:${window.ede.useXmlDanmaku}`);
const epOffset = parseFloat(document.getElementById('danmakuOffsetTime').value);
window.ede.curEpOffsetModified = epOffset !== window.ede.curEpOffset;
if (window.ede.curEpOffsetModified) {
window.ede.curEpOffset = epOffset;
showDebugInfo(`设置弹幕偏移时间:${window.ede.curEpOffset}`);
}
window.ede.fontFamily = document.getElementById("danmakuFontFamily").value || "sans-serif";
window.localStorage.setItem('danmakuFontFamily', window.ede.fontFamily);
showDebugInfo(`字体:${window.ede.fontFamily}`);
window.ede.fontOptions = document.getElementById("danmakuFontOptions").value;
window.localStorage.setItem('danmakuFontOptions', window.ede.fontOptions);
showDebugInfo(`字体选项:${window.ede.fontOptions}`);
reloadDanmaku('reload');
closeModal();
} catch (e) {
alert(`Invalid input: ${e.message}`);
}
};
document.getElementById('cancelSettings').onclick = closeModal;
document.getElementById('opacity').oninput = () => showCurrentVal('opacity');
document.getElementById('speed').oninput = () => showCurrentVal('speed');
document.getElementById('fontSize').oninput = () => showCurrentVal('fontSize');
document.getElementById('heightRatio').oninput = () => showCurrentVal('heightRatio');
document.getElementById('danmakuDensityLimit').oninput = () => showCurrentVal('danmakuDensityLimit', ['无', '低', '中', '高']);
}
};
const sendDanmakuOpts = {
title: '发送弹幕',
id: 'sendDanmaku',
class: send_icon,
onclick: () => {
// 登录窗口
if (!document.getElementById('loginDialog')) {
const modal = document.createElement('div');
modal.id = 'loginDialog';
modal.className = 'dialogContainer';
modal.style.display = 'none';
modal.innerHTML = `
<div class="dialog" style="padding: 20px; border-radius: .3em; position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);">
<form id="loginForm">
<div style="display: flex; flex-direction: column; gap: 5px;">
<div style="display: flex;">
<span style="flex: auto;">请输入弹弹Play账号密码</span>
</div>
<div style="display: flex;">
<span style="flex: auto;">账号:</span>
<input id="ddPlayAccount" placeholder="账号" value="" style="width: 70%;" />
</div>
<div style="display: flex;">
<span style="flex: auto;">密码:</span>
<input id="ddPlayPassword" placeholder="密码" value="" style="width: 70%;" type="password" />
</div>
</div>
<div style="display: flex; justify-content: space-between; margin-top: 10px;">
<button id="loginBtn" class="raised button-submit block formDialogFooterItem emby-button" type="submit">登录</button>
<button id="cancelBtn" class="raised button-cancel block formDialogFooterItem emby-button" type="button">取消</button>
</div>
</form>
</div>
`;
document.body.appendChild(modal);
document.getElementById('loginForm').onsubmit = (e) => {
e.preventDefault();
const account = document.getElementById('ddPlayAccount').value;
const password = document.getElementById('ddPlayPassword').value;
if (account && password) {
loginDanDanPlay(account, password).then(status => {
if (status) {
document.getElementById('loginBtn').innerText = '登录✔️';
let sleep = new Promise(resolve => setTimeout(resolve, 1500));
sleep.then(() => {
document.getElementById('loginDialog').style.display = 'none';
});
modal.removeEventListener('keydown', event => event.stopPropagation(), true);
}
});
}
};
document.getElementById('cancelBtn').onclick = () => {
document.getElementById('loginDialog').style.display = 'none';
modal.removeEventListener('keydown', event => event.stopPropagation(), true);
};
}
// 发送窗口
if (!document.getElementById('sendDanmakuDialog')) {
const modal = document.createElement('div');
modal.id = 'sendDanmakuDialog';
modal.className = 'dialogContainer';
modal.style.display = 'none';
modal.innerHTML = `
<div class="dialog" style="padding: 20px; border-radius: .3em; position: fixed; left: 50%; bottom: 0; transform: translate(-50%, -50%); width: 40%;">
<form id="sendDanmakuForm" autocomplete="off">
<div style="display: flex; flex-direction: column; gap: 5px;">
<div style="display: flex;">
<span id="lbAnimeTitle" style="flex: auto;"></span>
</div>
<div style="display: flex;">
<span id="lbEpisodeTitle" style="flex: auto;"></span>
</div>
<div style="display: flex;">
<div><input type="radio" id="danmakuMode1" name="danmakuMode" value="1" checked>
<label for="danmakuMode1">滚动</label></div>
<div><input type="radio" id="danmakuMode4" name="danmakuMode" value="4">
<label for="danmakuMode4">底部</label></div>
<div><input type="radio" id="danmakuMode5" name="danmakuMode" value="5">
<label for="danmakuMode5">顶部</label></div>
</div>
<div style="display: flex;">
<input style="flex-grow: 1;" id="danmakuText" placeholder="请输入弹幕内容" value="" />
<button id="sendDanmakuBtn" class="raised button-submit emby-button" style="padding: .2em .5em;" type="submit">发送</button>
<button id="cancelSendDanmakuBtn" class="raised button-cancel emby-button" style="padding: .2em .5em;" type="button">取消</button>
</div>
</div>
</form>
</div>
`;
document.body.appendChild(modal);
document.getElementById('sendDanmakuForm').onsubmit = (e) => {
e.preventDefault();
const danmakuText = document.getElementById('danmakuText').value;
if (danmakuText === '') {
const txt = document.getElementById('danmakuText');
txt.placeholder = '弹幕内容不能为空!';
txt.focus();
return;
}
const _media = document.querySelector(mediaQueryStr);
const currentTime = _media.currentTime;
const mode = parseInt(document.querySelector('input[name="danmakuMode"]:checked').value);
sendDanmaku(danmakuText, currentTime, mode);
// 清空输入框的值
document.getElementById('danmakuText').value = '';
modal.style.display = 'none';
modal.removeEventListener('keydown', event => event.stopPropagation(), true);
};
document.getElementById('cancelSendDanmakuBtn').onclick = () => {
modal.style.display = 'none';
modal.removeEventListener('keydown', event => event.stopPropagation(), true);
};
}
if (ddplayStatus.isLogin) {
const txt = document.getElementById('danmakuText');
txt.placeholder = '请输入弹幕内容';
txt.value = '';
txt.focus();
document.getElementById('sendDanmakuDialog').style.display = 'block';
document.getElementById('sendDanmakuDialog').addEventListener('keydown', event => event.stopPropagation(), true);
const animeTitle = window.ede.episode_info ? window.ede.episode_info.animeTitle : '';
const episodeTitle = window.ede.episode_info ? window.ede.episode_info.episodeTitle : '';
document.getElementById('lbAnimeTitle').innerText = `当前番剧: ${animeTitle || ''}`;
document.getElementById('lbEpisodeTitle').innerText = `当前集数: ${episodeTitle || ''}`;
} else {
document.getElementById('loginDialog').style.display = 'block';
document.getElementById('loginDialog').addEventListener('keydown', event => event.stopPropagation(), true);
}
}
};
// ------ configs end------
/* eslint-disable */
/* https://cdn.jsdelivr.net/npm/danmaku/dist/danmaku.min.js */
// prettier-ignore
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Danmaku=e()}(this,(function(){"use strict";var t=function(){if("undefined"==typeof document)return"transform";for(var t=["oTransform","msTransform","mozTransform","webkitTransform","transform"],e=document.createElement("div").style,i=0;i<t.length;i++)if(t[i]in e)return t[i];return"transform"}();function e(t){var e=document.createElement("div");if(e.style.cssText="position:absolute;","function"==typeof t.render){var i=t.render();if(i instanceof HTMLElement)return e.appendChild(i),e}if(e.textContent=t.text,t.style)for(var n in t.style)e.style[n]=t.style[n];return e}var i={name:"dom",init:function(){var t=document.createElement("div");return t.style.cssText="overflow:hidden;white-space:nowrap;transform:translateZ(0);",t},clear:function(t){for(var e=t.lastChild;e;)t.removeChild(e),e=t.lastChild},resize:function(t,e,i){t.style.width=e+"px",t.style.height=i+"px"},framing:function(){},setup:function(t,i){var n=document.createDocumentFragment(),s=0,r=null;for(s=0;s<i.length;s++)(r=i[s]).node=r.node||e(r),n.appendChild(r.node);for(i.length&&t.appendChild(n),s=0;s<i.length;s++)(r=i[s]).width=r.width||r.node.offsetWidth,r.height=r.height||r.node.offsetHeight},render:function(e,i){i.node.style[t]="translate("+i.x+"px,"+i.y+"px)"},remove:function(t,e){t.removeChild(e.node),this.media||(e.node=null)}},n="undefined"!=typeof window&&window.devicePixelRatio||1,s=Object.create(null);function r(t,e){if("function"==typeof t.render){var i=t.render();if(i instanceof HTMLCanvasElement)return t.width=i.width,t.height=i.height,i}var r=document.createElement("canvas"),h=r.getContext("2d"),o=t.style||{};o.font=o.font||"10px sans-serif",o.textBaseline=o.textBaseline||"bottom";var a=1*o.lineWidth;for(var d in a=a>0&&a!==1/0?Math.ceil(a):1*!!o.strokeStyle,h.font=o.font,t.width=t.width||Math.max(1,Math.ceil(h.measureText(t.text).width)+2*a),t.height=t.height||Math.ceil(function(t,e){if(s[t])return s[t];var i=12,n=t.match(/(\d+(?:\.\d+)?)(px|%|em|rem)(?:\s*\/\s*(\d+(?:\.\d+)?)(px|%|em|rem)?)?/);if(n){var r=1*n[1]||10,h=n[2],o=1*n[3]||1.2,a=n[4];"%"===h&&(r*=e.container/100),"em"===h&&(r*=e.container),"rem"===h&&(r*=e.root),"px"===a&&(i=o),"%"===a&&(i=r*o/100),"em"===a&&(i=r*o),"rem"===a&&(i=e.root*o),void 0===a&&(i=r*o)}return s[t]=i,i}(o.font,e))+2*a,r.width=t.width*n,r.height=t.height*n,h.scale(n,n),o)h[d]=o[d];var u=0;switch(o.textBaseline){case"top":case"hanging":u=a;break;case"middle":u=t.height>>1;break;default:u=t.height-a}return o.strokeStyle&&h.strokeText(t.text,a,u),h.fillText(t.text,a,u),r}function h(t){return 1*window.getComputedStyle(t,null).getPropertyValue("font-size").match(/(.+)px/)[1]}var o={name:"canvas",init:function(t){var e=document.createElement("canvas");return e.context=e.getContext("2d"),e._fontSize={root:h(document.getElementsByTagName("html")[0]),container:h(t)},e},clear:function(t,e){t.context.clearRect(0,0,t.width,t.height);for(var i=0;i<e.length;i++)e[i].canvas=null},resize:function(t,e,i){t.width=e*n,t.height=i*n,t.style.width=e+"px",t.style.height=i+"px"},framing:function(t){t.context.clearRect(0,0,t.width,t.height)},setup:function(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.canvas=r(n,t._fontSize)}},render:function(t,e){t.context.drawImage(e.canvas,e.x*n,e.y*n)},remove:function(t,e){e.canvas=null}},a="undefined"!=typeof window&&(window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame)||function(t){return setTimeout(t,50/3)},d="undefined"!=typeof window&&(window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame)||clearTimeout;function u(t,e,i){for(var n=0,s=0,r=t.length;s<r-1;)i>=t[n=s+r>>1][e]?s=n:r=n;return t[s]&&i<t[s][e]?s:r}function m(t){return/^(ltr|top|bottom)$/i.test(t)?t.toLowerCase():"rtl"}function c(){var t=9007199254740991;return[{range:0,time:-t,width:t,height:0},{range:t,time:t,width:0,height:0}]}function l(t){t.ltr=c(),t.rtl=c(),t.top=c(),t.bottom=c()}function f(){return void 0!==window.performance&&window.performance.now?window.performance.now():Date.now()}function p(t){var e=this,i=this.media?this.media.currentTime:f()/1e3,n=this.media?this.media.playbackRate:1;function s(t,s){if("top"===s.mode||"bottom"===s.mode)return i-t.time<e._.duration;var r=(e._.width+t.width)*(i-t.time)*n/e._.duration;if(t.width>r)return!0;var h=e._.duration+t.time-i,o=e._.width+s.width,a=e.media?s.time:s._utc,d=o*(i-a)*n/e._.duration,u=e._.width-d;return h>e._.duration*u/(e._.width+s.width)}for(var r=this._.space[t.mode],h=0,o=0,a=1;a<r.length;a++){var d=r[a],u=t.height;if("top"!==t.mode&&"bottom"!==t.mode||(u+=d.height),d.range-d.height-r[h].range>=u){o=a;break}s(d,t)&&(h=a)}var m=r[h].range,c={range:m+t.height,time:this.media?t.time:t._utc,width:t.width,height:t.height};return r.splice(h+1,o-h-1,c),"bottom"===t.mode?this._.height-t.height-m%this._.height:m%(this._.height-t.height)}function g(){if(!this._.visible||!this._.paused)return this;if(this._.paused=!1,this.media)for(var t=0;t<this._.runningList.length;t++){var e=this._.runningList[t];e._utc=f()/1e3-(this.media.currentTime-e.time)}var i=this,n=function(t,e,i,n){return function(s){t(this._.stage);var r=(s||f())/1e3,h=this.media?this.media.currentTime:r,o=this.media?this.media.playbackRate:1,a=null,d=0,u=0;for(u=this._.runningList.length-1;u>=0;u--)a=this._.runningList[u],h-(d=this.media?a.time:a._utc)>this._.duration&&(n(this._.stage,a),this._.runningList.splice(u,1));for(var m=[];this._.position<this.comments.length&&(a=this.comments[this._.position],!((d=this.media?a.time:a._utc)>=h));)h-d>this._.duration||(this.media&&(a._utc=r-(this.media.currentTime-a.time)),m.push(a)),++this._.position;for(e(this._.stage,m),u=0;u<m.length;u++)(a=m[u]).y=p.call(this,a),this._.runningList.push(a);for(u=0;u<this._.runningList.length;u++){a=this._.runningList[u];var c=(this._.width+a.width)*(r-a._utc)*o/this._.duration;"ltr"===a.mode&&(a.x=c-a.width),"rtl"===a.mode&&(a.x=this._.width-c),"top"!==a.mode&&"bottom"!==a.mode||(a.x=this._.width-a.width>>1),i(this._.stage,a)}}}(this._.engine.framing.bind(this),this._.engine.setup.bind(this),this._.engine.render.bind(this),this._.engine.remove.bind(this));return this._.requestID=a((function t(e){n.call(i,e),i._.requestID=a(t)})),this}function _(){return!this._.visible||this._.paused||(this._.paused=!0,d(this._.requestID),this._.requestID=0),this}function v(){if(!this.media)return this;this.clear(),l(this._.space);var t=u(this.comments,"time",this.media.currentTime);return this._.position=Math.max(0,t-1),this}function w(t){t.play=g.bind(this),t.pause=_.bind(this),t.seeking=v.bind(this),this.media.addEventListener("play",t.play),this.media.addEventListener("pause",t.pause),this.media.addEventListener("playing",t.play),this.media.addEventListener("waiting",t.pause),this.media.addEventListener("seeking",t.seeking)}function y(t){this.media.removeEventListener("play",t.play),this.media.removeEventListener("pause",t.pause),this.media.removeEventListener("playing",t.play),this.media.removeEventListener("waiting",t.pause),this.media.removeEventListener("seeking",t.seeking),t.play=null,t.pause=null,t.seeking=null}function x(t){this._={},this.container=t.container||document.createElement("div"),this.media=t.media,this._.visible=!0,this.engine=(t.engine||"DOM").toLowerCase(),this._.engine="canvas"===this.engine?o:i,this._.requestID=0,this._.speed=Math.max(0,t.speed)||144,this._.duration=4,this.comments=t.comments||[],this.comments.sort((function(t,e){return t.time-e.time}));for(var e=0;e<this.comments.length;e++)this.comments[e].mode=m(this.comments[e].mode);return this._.runningList=[],this._.position=0,this._.paused=!0,this.media&&(this._.listener={},w.call(this,this._.listener)),this._.stage=this._.engine.init(this.container),this._.stage.style.cssText+="position:relative;pointer-events:none;",this.resize(),this.container.appendChild(this._.stage),this._.space={},l(this._.space),this.media&&this.media.paused||(v.call(this),g.call(this)),this}function b(){if(!this.container)return this;for(var t in _.call(this),this.clear(),this.container.removeChild(this._.stage),this.media&&y.call(this,this._.listener),this)Object.prototype.hasOwnProperty.call(this,t)&&(this[t]=null);return this}var L=["mode","time","text","render","style"];function T(t){if(!t||"[object Object]"!==Object.prototype.toString.call(t))return this;for(var e={},i=0;i<L.length;i++)void 0!==t[L[i]]&&(e[L[i]]=t[L[i]]);if(e.text=(e.text||"").toString(),e.mode=m(e.mode),e._utc=f()/1e3,this.media){var n=0;void 0===e.time?(e.time=this.media.currentTime,n=this._.position):(n=u(this.comments,"time",e.time))<this._.position&&(this._.position+=1),this.comments.splice(n,0,e)}else this.comments.push(e);return this}function E(){return this._.visible?this:(this._.visible=!0,this.media&&this.media.paused||(v.call(this),g.call(this)),this)}function k(){return this._.visible?(_.call(this),this.clear(),this._.visible=!1,this):this}function C(){return this._.engine.clear(this._.stage,this._.runningList),this._.runningList=[],this}function z(){return this._.width=this.container.offsetWidth,this._.height=this.container.offsetHeight,this._.engine.resize(this._.stage,this._.width,this._.height),this._.duration=this._.width/this._.speed,this}var D={get:function(){return this._.speed},set:function(t){return"number"!=typeof t||isNaN(t)||!isFinite(t)||t<=0?this._.speed:(this._.speed=t,this._.width&&(this._.duration=this._.width/t),t)}};function M(t){t&&x.call(this,t)}return M.prototype.destroy=function(){return b.call(this)},M.prototype.emit=function(t){return T.call(this,t)},M.prototype.show=function(){return E.call(this)},M.prototype.hide=function(){return k.call(this)},M.prototype.clear=function(){return C.call(this)},M.prototype.resize=function(){return z.call(this)},Object.defineProperty(M.prototype,"speed",D),M}));
/* eslint-enable */
class EDE {
constructor() {
// 简繁转换 0:不转换 1:简体 2:繁体
const chConvert = window.localStorage.getItem('chConvert');
this.chConvert = chConvert ? parseInt(chConvert) : 0;
// 开关弹幕 0:关闭 1:打开
const danmakuSwitch = window.localStorage.getItem('danmakuSwitch');
this.danmakuSwitch = danmakuSwitch ? parseInt(danmakuSwitch) : 1;
// 开关日志 0:关闭 1:打开
const logSwitch = window.localStorage.getItem('logSwitch');
this.logSwitch = logSwitch ? parseInt(logSwitch) : 0;
// 弹幕透明度
const opacityRecord = window.localStorage.getItem('danmakuopacity');
this.opacity = opacityRecord ? parseFloatOfRange(opacityRecord, 0.0, 1.0) : 0.7
// 弹幕速度
const speedRecord = window.localStorage.getItem('danmakuspeed');
this.speed = speedRecord ? parseFloatOfRange(speedRecord, 0.0, 1000.0) : 200
// 弹幕字体大小
const sizeRecord = window.localStorage.getItem('danmakusize');
this.fontSize = sizeRecord ? parseFloatOfRange(sizeRecord, 0.0, 50.0) : 18
// 弹幕高度
const heightRecord = window.localStorage.getItem('danmakuheight');
this.heightRatio = heightRecord ? parseFloatOfRange(heightRecord, 0.0, 1.0) : 0.9
// 弹幕过滤
const danmakuFilter = window.localStorage.getItem('danmakuFilter');
this.danmakuFilter = danmakuFilter ? parseInt(danmakuFilter) : 0;
this.danmakuFilter = this.danmakuFilter >= 0 && this.danmakuFilter < 16 ? this.danmakuFilter : 0;
// 按弹幕模式过滤
const danmakuModeFilter = window.localStorage.getItem('danmakuModeFilter');
this.danmakuModeFilter = danmakuModeFilter ? parseInt(danmakuModeFilter) : 0;
this.danmakuModeFilter = this.danmakuModeFilter >= 0 && this.danmakuModeFilter < 8 ? this.danmakuModeFilter : 0;
// 弹幕密度限制等级 0:不限制 1:低 2:中 3:高
const danmakuDensityLimit = window.localStorage.getItem('danmakuDensityLimit');
this.danmakuDensityLimit = danmakuDensityLimit ? parseInt(danmakuDensityLimit) : 0;
// 使用Jellyfin弹幕插件提供的xml弹幕替代本脚本在线搜索的弹幕
const useXmlDanmaku = window.localStorage.getItem('useXmlDanmaku');
this.useXmlDanmaku = useXmlDanmaku ? parseInt(useXmlDanmaku) : 0;
// 当前剧集弹幕偏移时间
this.curEpOffset = 0;
this.curEpOffsetModified = false;
// 字体
const fontFamily = window.localStorage.getItem('danmakuFontFamily');
this.fontFamily = fontFamily ?? "sans-serif";
// 字体选项
const fontOptions = window.localStorage.getItem('danmakuFontOptions');
this.fontOptions = fontOptions ?? "";
this.danmaku = null;
this.episode_info = null;
this.obResize = null;
this.obMutation = null;
this.loading = false;
}
}
const parseFloatOfRange = (str, lb, hb) => {
let parsedValue = parseFloat(str);
if (isNaN(parsedValue)) {
throw new Error('输入无效!');
}
return Math.min(Math.max(parsedValue, lb), hb);
};
function createButton(opt) {
let button = document.createElement('button');
button.className = buttonOptions.class;
button.setAttribute('is', buttonOptions.is);
button.setAttribute('title', opt.title);
button.setAttribute('id', opt.id);
let icon = document.createElement('span');
icon.className = spanClass + opt.class;
button.appendChild(icon);
button.onclick = opt.onclick;
return button;
}
function initListener() {
let container = document.querySelector(mediaQueryStr);
// 页面未加载
if (!container) {
if (window.ede.episode_info) {
window.ede.episode_info = null;
}
return;
}
}
function initUI() {
// 页面未加载
let uiAnchor = document.getElementsByClassName(uiAnchorStr);
if (!uiAnchor || !uiAnchor[0]) {
return;
}
// 已初始化
if (document.getElementById('danmakuCtr')) {
return;
}
showDebugInfo('正在初始化UI');
// 弹幕按钮容器div
let uiEle = null;
document.querySelectorAll(uiQueryStr).forEach(function (element) {
if (element.offsetParent != null) {
uiEle = element.parentNode;
}
});
if (uiEle == null) {
return;
}
let parent = uiEle.parentNode;
let menubar = document.createElement('div');
menubar.id = 'danmakuCtr';
if (!window.ede.episode_info) {
menubar.style.opacity = 0.5;
}
parent.insertBefore(menubar, uiEle.nextSibling);
// 弹幕开关
displayButtonOpts.class = danmaku_icons[window.ede.danmakuSwitch];
menubar.appendChild(createButton(displayButtonOpts));
// 手动匹配
menubar.appendChild(createButton(searchButtonOpts));
// 手动增加弹幕源
menubar.appendChild(createButton(sourceButtonOpts));
// 弹幕设置
menubar.appendChild(createButton(settingButtonOpts));
// 日志开关
logButtonOpts.class = log_icons[window.ede.logSwitch];
menubar.appendChild(createButton(logButtonOpts));
// 发送弹幕
menubar.appendChild(createButton(sendDanmakuOpts));
let _container = null;
document.querySelectorAll(mediaContainerQueryStr).forEach(function (element) {
if (!element.classList.contains('hide')) {
_container = element;
}
});
let span = document.createElement('span');
span.id = 'debugInfo';
span.style.position = 'absolute';
span.style.overflow = 'auto';
span.style.zIndex = '99';
span.style.right = '50px';
span.style.top = '50px';
span.style.background = 'rgba(28, 28, 28, .8)';
span.style.color = '#fff';
span.style.padding = '20px';
span.style.borderRadius = '.3em';
span.style.maxHeight = '50%'
window.ede.logSwitch == 1 ? (span.style.display = 'block') : (span.style.display = 'none');
_container.appendChild(span);
showDebugInfo('UI初始化完成');
reloadDanmaku('init');
refreshDanDanPlayToken();
}
async function loginDanDanPlay(account, passwd) {
const loginUrl = authPrefix + '/api/v2/login';
const params = {
'userName': account,
'password': passwd
};
try {
const resp = await fetch(loginUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': navigator.userAgent
},
body: JSON.stringify(params)
});
if (resp.status !== 200) {
showDebugInfo('登录失败 http error:' + resp.status);
alert('登录失败 http error:' + resp.status);
return false;
}
const json = await resp.json();
if (json.errorCode !== 0) {
showDebugInfo('登录失败 ' + json.errorMessage);
alert('登录失败 ' + json.errorMessage);
return false;
}
ddplayStatus.isLogin = true;
ddplayStatus.token = json.token;
ddplayStatus.tokenExpire = json.tokenExpireTime;
window.localStorage.setItem('ddplayStatus', JSON.stringify(ddplayStatus));
showDebugInfo('登录成功');
return true;
} catch (error) {
console.error('登录失败', error);
alert('登录失败');
return false;
}
}
async function refreshDanDanPlayToken() {
if (ddplayStatus.isLogin) {
const now = Math.floor(Date.now() / 1000);
const expire = new Date(ddplayStatus.tokenExpire).getTime() / 1000;
if (expire < now) {
ddplayStatus.isLogin = false;
return;
} else if (expire - now > 259200) { // Token expires in more than 3 days, no need to refresh
return;
} else { // Refresh token before 3 days
const refreshUrl = apiPrefix + '/api/v2/login/renew';
try {
const resp = await fetch(refreshUrl, {
method: 'GET',
headers: {
'Accept': 'application/json',
'User-Agent': navigator.userAgent,
'Authorization': 'Bearer ' + ddplayStatus.token
}
});
if (resp.status !== 200) {
showDebugInfo('刷新弹弹Play Token失败 http error:' + resp.status);
return;
}
const json = await resp.json();
if (json.errorCode === 0) {
ddplayStatus.isLogin = true;
ddplayStatus.token = json.token;
ddplayStatus.tokenExpire = json.tokenExpireTime;
} else {
showDebugInfo('刷新弹弹Play Token失败');
showDebugInfo(json.errorMessage);
}
} catch (error) {
console.error('刷新弹弹Play Token失败', error);
}
}
}
}
async function sendDanmaku(danmakuText, time, mode = 1, color = 0xffffff) {
if (ddplayStatus.isLogin) {
if (!window.ede.episode_info || !window.ede.episode_info.episodeId) {
showDebugInfo('发送弹幕失败 未获取到弹幕信息');
alert('请先获取弹幕信息');
return;
}
const danmakuUrl = apiPrefix + '/api/v2/comment/' + window.ede.episode_info.episodeId;
const params = {
'time': time,
'mode': mode,
'color': color,
'comment': danmakuText
};
try {
const resp = await fetch(danmakuUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': navigator.userAgent,
'Authorization': 'Bearer ' + ddplayStatus.token
},
body: JSON.stringify(params)
});
if (resp.status !== 200) {
showDebugInfo('发送弹幕失败 http error:' + resp.status);
return;
}
const json = await resp.json();
if (json.errorCode === 0) {
const colorStr = `000000${color.toString(16)}`.slice(-6);
const modemap = { 6: 'ltr', 1: 'rtl', 5: 'top', 4: 'bottom' }[mode];
const comment = {
text: danmakuText,
mode: modemap,
time: time,
style: {
font: `${window.ede.fontOptions} ${window.ede.fontSize}px ${window.ede.fontFamily}`,
fillStyle: `#${colorStr}`,
strokeStyle: colorStr === '000000' ? '#fff' : '#000',
lineWidth: 2.0,
},
};
window.ede.danmaku.emit(comment);
showDebugInfo('发送弹幕成功');
} else {
showDebugInfo('发送弹幕失败');
showDebugInfo(json.errorMessage);
alert('发送失败:' + json.errorMessage);
}
} catch (error) {
console.error('发送弹幕失败', error);
showDebugInfo('发送弹幕失败');
}
}
}
async function postRelatedSource(relatedUrl) {
if (!ddplayStatus.isLogin) {
showDebugInfo('发送相关链接失败 未登录');
alert('请先登录');
return;
}
if (!window.ede.episode_info || !window.ede.episode_info.episodeId) {
showDebugInfo('发送弹幕失败 未获取到弹幕信息');
alert('请先获取弹幕信息');
return;
}
const url = apiPrefix + '/api/v2/related/' + window.ede.episode_info.episodeId;
const params = {
'episodeId': window.ede.episode_info.episodeId,
'url': relatedUrl,
'shift': 0
};
try {
const resp = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': navigator.userAgent,
'Authorization': 'Bearer ' + ddplayStatus.token
},
body: JSON.stringify(params)
});
if (resp.status !== 200) {
showDebugInfo('发送相关链接失败 http error:' + resp.code);
return;
}
const json = await resp.json();
if (json.errorCode === 0) {
showDebugInfo('发送相关链接成功');
} else {
showDebugInfo('发送相关链接失败');
showDebugInfo(json.errorMessage);
alert('弹幕源提交弹弹Play失败:' + json.errorMessage);
}
} catch (error) {
console.error('发送相关链接失败', error);
showDebugInfo('发送相关链接失败');
}
}
async function showDebugInfo(msg) {
let span = document.getElementById('debugInfo');
while (!span) {
await new Promise((resolve) => setTimeout(resolve, 200));
span = document.getElementById('debugInfo');
}
let msgStr = msg;
if (typeof msg !== 'string') {
msgStr = JSON.stringify(msg);
}
let lastLine = span.innerHTML.slice(span.innerHTML.lastIndexOf('<br>') + 4);
let baseLine = lastLine.replace(/ X\d+$/, '');
if (baseLine === msgStr) {
let count = 2;
if (lastLine.match(/ X(\d+)$/)) {
count = parseInt(lastLine.match(/ X(\d+)$/)[1]) + 1;
}
msgStr = `${msgStr} X${count}`;
span.innerHTML = span.innerHTML.slice(0, span.innerHTML.lastIndexOf('<br>') + 4) + msgStr;
} else {
span.innerHTML += span.innerHTML === '' ? msgStr : '<br>' + msgStr;
}
console.log(msg);
}
async function getEmbyItemInfo() {
let playingInfo = null;
while (!playingInfo) {
await new Promise((resolve) => setTimeout(resolve, 200));
if (isNewJellyfin) {
// params: userId, itemId
playingInfo = await ApiClient.getItem(ApiClient.getCurrentUserId(), itemId);
} else {
let sessionInfo = await ApiClient.getSessions({
userId: ApiClient.getCurrentUserId(),
deviceId: ApiClient.deviceId(),
});
if (!sessionInfo[0].NowPlayingItem) {
await new Promise(resolve => setTimeout(resolve, 150));
continue;
}
playingInfo = sessionInfo[0].NowPlayingItem;
}
}
showDebugInfo('获取Item信息成功: ' + (playingInfo.SeriesName || playingInfo.Name));
return playingInfo;
}
function makeGetRequest(url) {
if (isInTampermonkey) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "GET",
url: url,
headers: {
"Accept-Encoding": "gzip,br",
"Accept": "application/json"
},
onload: function (response) {
response.json = () => Promise.resolve(JSON.parse(response.responseText));
response.text = () => Promise.resolve(response.responseText);
response.ok = response.status >= 200 && response.status < 300;
resolve(response);
},
onerror: function (error) {
reject(error);
}
});
});
} else {
return fetch(url, {
method: 'GET',
headers: {
"Accept-Encoding": "gzip,br",
"Accept": "application/json",
"User-Agent": navigator.userAgent
}
});
}
}
async function getEpisodeInfo(is_auto = true) {
let item = await getEmbyItemInfo();
if (!item) {
return null;
}
let _id;
let animeName;
let anime_id = -1;
let episode;
_id = item.SeasonId || item.Id;
animeName = item.SeriesName || item.Name;
episode = item.IndexNumber || 1;
let session = item.ParentIndexNumber;
if (session > 1) {
animeName += session;
}
let _id_key = '_anime_id_rel_' + _id;
let _name_key = '_anime_name_rel_' + _id;
let _episode_key = '_episode_id_rel_' + _id + '_' + episode;
if (is_auto) {
//优先使用记忆设置
if (window.localStorage.getItem(_episode_key)) {
const episodeInfo = JSON.parse(window.localStorage.getItem(_episode_key));
return episodeInfo;
}
}
if (window.localStorage.getItem(_id_key)) {
anime_id = window.localStorage.getItem(_id_key);
}
if (window.localStorage.getItem(_name_key)) {
animeName = window.localStorage.getItem(_name_key);
}
if (!is_auto) {
animeName = prompt('确认动画名:', animeName);
if (animeName == null || animeName == '') {
return null;
}
}
const _episode_key_offset = _episode_key + '_offset';
if (window.ede.curEpOffsetModified) {
window.localStorage.setItem(_episode_key_offset, window.ede.curEpOffset);
}
window.ede.curEpOffset = window.localStorage.getItem(_episode_key_offset) || 0;
let searchUrl = apiPrefix + '/api/v2/search/episodes?anime=' + animeName + '&withRelated=true';
let animaInfo = await makeGetRequest(searchUrl)
.then((response) => response.json())
.catch((error) => {
showDebugInfo('查询失败:', error);
return null;
});
if (animaInfo.animes.length == 0) {
const seriesInfo = await ApiClient.getItem(ApiClient.getCurrentUserId(), item.SeriesId || item.Id);
animeName = seriesInfo.OriginalTitle;
if (animeName?.length > 0) {
searchUrl = apiPrefix + '/api/v2/search/episodes?anime=' + animeName + '&withRelated=true';
animaInfo = await makeGetRequest(searchUrl)
.then((response) => response.json())
.catch((error) => {
showDebugInfo('查询失败:', error);
return null;
});
}
}
if (animaInfo.animes.length == 0) {
showDebugInfo('弹幕查询无结果');
return null;
}
showDebugInfo('节目查询成功');
let selecAnime_id = 1;
if (anime_id != -1) {
for (let index = 0; index < animaInfo.animes.length; index++) {
if (animaInfo.animes[index].animeId == anime_id) {
selecAnime_id = index + 1;
}
}
}
if (!is_auto) {
let anime_lists_str = list2string(animaInfo);
showDebugInfo(anime_lists_str);
selecAnime_id = prompt('选择节目:\n' + anime_lists_str, selecAnime_id);
selecAnime_id = parseInt(selecAnime_id) - 1;
window.localStorage.setItem(_id_key, animaInfo.animes[selecAnime_id].animeId);
window.localStorage.setItem(_name_key, animaInfo.animes[selecAnime_id].animeTitle);
let episode_lists_str = ep2string(animaInfo.animes[selecAnime_id].episodes);
episode = prompt('选择剧集:\n' + episode_lists_str, parseInt(episode) || 1);
if (episode == null || episode == '') {
return null;
}
episode = parseInt(episode) - 1;
} else {
selecAnime_id = parseInt(selecAnime_id) - 1;
let initialTitle = animaInfo.animes[selecAnime_id].episodes[0].episodeTitle;
const match = initialTitle.match(/第(\d+)话/);