Repository navigation
Expand file tree
/
Copy pathX-Vision.js
More file actions
2497 lines (2101 loc) · 111 KB
/
Copy pathX-Vision.js
File metadata and controls
2497 lines (2101 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
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 改 - X-Vision:沉浸式影院播放器
// @namespace https://github.com/ZiPenOk
// @version 2.0.6
// @description 改 - X-Vision:沉浸式影院播放器 - Apple风格设计,支持长按2倍速、智能预加载、手势快捷操作、PiP画中画、智能续播(记住进度)修复背景播放问题
// @author ZiPenOK (原作者Luke Liou)
// @license MIT
// @match https://twitter-ero-video-ranking.com/*
// @match https://x-ero-anime.com/*
// @match https://truvaze.com/*
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @grant GM_openInTab
// @connect twitter-ero-video-ranking.com
// @connect video.twimg.com
// @connect pbs.twimg.com
// @connect *
// @homepageURL https://github.com/ZiPenOk/scripts
// @supportURL https://github.com/ZiPenOk/scripts/issues
// @updateURL https://github.com/ZiPenOk/scripts/raw/refs/heads/main/X-Vision.js
// @downloadURL https://github.com/ZiPenOk/scripts/raw/refs/heads/main/X-Vision.js
// @run-at document-start
// ==/UserScript==
(function () {
'use strict';
// ========================================
// 0. 核心修正: 立即注入 Referer 禁用策略
// ========================================
const meta = document.createElement('meta');
meta.name = "referrer";
meta.content = "no-referrer";
if (document.head) {
document.head.appendChild(meta);
} else {
const observer = new MutationObserver((mutations, obs) => {
if (document.head) {
document.head.appendChild(meta);
obs.disconnect();
}
});
observer.observe(document.documentElement, { childList: true });
}
// ========================================
// 样式定义 - TikTok风格模态框
// ========================================
const styles = `
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;700&display=swap');
body>div.container.mx-auto.mt-3.mb-3.px-2.relative{background-color:#fff !important}
:root{--primary-red:#FE2C55;--primary-cyan:#25F4EE;--glass-bg:rgba(20,20,20,0.3);--glass-bg-hover:rgba(40,40,40,0.5);--glass-border:rgba(255,255,255,0.08);--glass-blur:blur(20px);--shadow-sm:0 4px 12px rgba(0,0,0,0.2);--shadow-lg:0 8px 32px rgba(0,0,0,0.4);--ease-elastic:cubic-bezier(0.68,-0.6,0.32,1.6);--ease-smooth:cubic-bezier(0.25,0.8,0.25,1)}
html.tiktok-modal-open,body.tiktok-modal-open{overflow:hidden !important;width:100% !important;height:100% !important;overscroll-behavior:none !important;touch-action:none !important;background-color:#000 !important}
body.tiktok-modal-open{position:fixed !important;left:0 !important;right:0 !important}
.tiktok-modal-overlay{position:fixed;top:0;left:0;width:100vw !important;height:100vh !important;height:100dvh !important;background:#000;z-index:2147483647;display:none;flex-direction:column !important;margin:0 !important;box-sizing:border-box !important;max-width:none !important;max-height:none !important;font-family:'Outfit',-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;padding-top:env(safe-area-inset-top);padding-bottom:env(safe-area-inset-bottom);padding-left:env(safe-area-inset-left);padding-right:env(safe-area-inset-right);opacity:0;transition:opacity 0.3s var(--ease-smooth);-webkit-touch-callout:none !important;-webkit-user-select:none !important;-khtml-user-select:none !important;-moz-user-select:none !important;-ms-user-select:none !important;user-select:none !important;-webkit-tap-highlight-color:transparent !important}
.tiktok-modal-overlay.active{display:flex;opacity:1;animation:modalIn 0.4s var(--ease-smooth) forwards}
@keyframes modalIn{from{transform:scale(0.98);opacity:0}
to{transform:scale(1);opacity:1}
}
@keyframes tiktokFadeIn{from{opacity:0}
to{opacity:1}
}
@keyframes slideOutUp{from{transform:translateY(0);opacity:1}
to{transform:translateY(-100%);opacity:0}
}
@keyframes slideInUp{from{transform:translateY(100%);opacity:0}
to{transform:translateY(0);opacity:1}
}
@keyframes slideOutDown{from{transform:translateY(0);opacity:1}
to{transform:translateY(100%);opacity:0}
}
@keyframes slideInDown{from{transform:translateY(-100%);opacity:0}
to{transform:translateY(0);opacity:1}
}
.tiktok-video-player.slide-out-up{animation:slideOutUp 0.3s ease-out forwards}
.tiktok-video-player.slide-in-up{animation:slideInUp 0.3s ease-out forwards}
.tiktok-video-player.slide-out-down{animation:slideOutDown 0.3s ease-out forwards}
.tiktok-video-player.slide-in-down{animation:slideInDown 0.3s ease-out forwards}
.tiktok-video-container{flex:1;position:relative;width:100% !important;height:100% !important;max-width:none !important;margin:0 !important;padding:0 !important;box-sizing:border-box !important;background:#000;overflow:hidden;display:flex;align-items:center;justify-content:center;background-size:cover;background-position:center;background-repeat:no-repeat}
.tiktok-video-container::before{content:'';position:absolute;top:0;left:0;right:0;bottom:0;background:inherit;filter:blur(20px) brightness(0.4);transform:scale(1.1);z-index:0}
.tiktok-video-container::after{content:'';position:absolute;top:0;left:0;right:0;height:100px;background:linear-gradient(to bottom,rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.2) 40%,transparent 100%);pointer-events:none;z-index:2}
.tiktok-progress-container::before{content:'';position:absolute;left:-10px;right:-10px;height:120px;background:linear-gradient(to top,rgba(0,0,0,0.7) 0%,rgba(0,0,0,0.3) 50%,transparent 100%);z-index:-1;pointer-events:none}
.tiktok-video-player{position:absolute;top:0;left:0;width:100% !important;height:100% !important;max-width:none !important;max-height:none !important;object-fit:contain;cursor:pointer;z-index:1;background:#000;opacity:0;transition:opacity 0.15s ease-out}
.tiktok-video-player.visible{opacity:1}
.tiktok-thumbnail-layer{position:absolute;top:0;left:0;width:100% !important;height:100% !important;max-width:none !important;max-height:none !important;object-fit:contain;z-index:2;pointer-events:none;opacity:1;transition:opacity 0.15s ease-out;background:#000}
.tiktok-thumbnail-layer.hidden{opacity:0}
.tiktok-video-player::-webkit-media-controls{display:none !important}
.tiktok-video-player::-webkit-media-controls-enclosure{display:none !important}
.tiktok-video-player.slide-out-up,.tiktok-thumbnail-layer.slide-out-up{animation:slideOutUp 0.3s ease-out forwards}
.tiktok-video-player.slide-in-up,.tiktok-thumbnail-layer.slide-in-up{animation:slideInUp 0.3s ease-out forwards}
.tiktok-video-player.slide-out-down,.tiktok-thumbnail-layer.slide-out-down{animation:slideOutDown 0.3s ease-out forwards}
.tiktok-video-player.slide-in-down,.tiktok-thumbnail-layer.slide-in-down{animation:slideInDown 0.3s ease-out forwards}
.glass-panel{background:var(--glass-bg);backdrop-filter:var(--glass-blur);-webkit-backdrop-filter:var(--glass-blur);border:1px solid var(--glass-border);box-shadow:var(--shadow-sm);color:rgba(255,255,255,0.95)}
.tiktok-close-btn{position:absolute;top:20px;right:20px;width:44px;height:44px;border-radius:50%;display:flex;align-items:center;justify-content:center;cursor:pointer;z-index:2147483648;transition:all 0.3s var(--ease-smooth);background:var(--glass-bg);backdrop-filter:var(--glass-blur);-webkit-backdrop-filter:var(--glass-blur);border:1px solid var(--glass-border);border:none}
.tiktok-close-btn:hover{background:var(--glass-bg-hover);transform:rotate(90deg) scale(1.1)}
.tiktok-close-btn svg{width:24px;height:24px;fill:white}
.tiktok-unread-toggle{position:absolute;top:20px;right:80px;height:44px;padding:0 16px;border-radius:22px;display:flex;align-items:center;gap:10px;cursor:pointer;z-index:2147483648;font-size:0.85rem;font-weight:500;letter-spacing:0.5px;transition:all 0.3s var(--ease-smooth);background:var(--glass-bg);backdrop-filter:var(--glass-blur);-webkit-backdrop-filter:var(--glass-blur);border:1px solid var(--glass-border);color:white}
.tiktok-unread-toggle:hover{background:var(--glass-bg-hover)}
.toggle-switch{width:36px;height:20px;background:rgba(255,255,255,0.2);border-radius:10px;position:relative;transition:background 0.3s var(--ease-smooth)}
.toggle-switch::after{content:'';position:absolute;top:2px;left:2px;width:16px;height:16px;background:#fff;border-radius:50%;box-shadow:0 2px 4px rgba(0,0,0,0.2);transition:transform 0.3s var(--ease-elastic)}
.tiktok-unread-toggle.active .toggle-switch{background:var(--primary-red);box-shadow:0 0 10px rgba(254,44,85,0.4)}
.tiktok-unread-toggle.active .toggle-switch::after{transform:translateX(16px)}
.tiktok-video-count{position:absolute;top:20px;left:20px;padding:8px 16px;border-radius:20px;font-size:0.85rem;font-weight:600;letter-spacing:1px;z-index:2147483648;background:var(--glass-bg);backdrop-filter:var(--glass-blur);color:white}
.tiktok-actions{position:absolute;right:16px;bottom:140px;display:flex;flex-direction:column;gap:24px;z-index:2147483648}
.tiktok-action-item{display:flex;flex-direction:column;align-items:center;gap:6px;cursor:pointer}
.tiktok-action-icon{width:50px;height:50px;border-radius:50%;display:flex;align-items:center;justify-content:center;transition:all 0.3s var(--ease-elastic);background:var(--glass-bg);backdrop-filter:blur(10px);border:1px solid var(--glass-border)}
.tiktok-action-item:hover .tiktok-action-icon{transform:scale(1.15);background:var(--glass-bg-hover);border-color:rgba(255,255,255,0.3)}
.tiktok-action-icon svg{width:26px;height:26px;fill:#fff;filter:drop-shadow(0 2px 4px rgba(0,0,0,0.3));transition:all 0.3s ease}
.tiktok-action-icon.liked{border-color:rgba(254,44,85,0.5);background:rgba(254,44,85,0.15)}
.tiktok-action-icon.liked svg{fill:var(--primary-red);filter:drop-shadow(0 0 8px rgba(254,44,85,0.6));animation:heartBeat 0.4s var(--ease-elastic)}
@keyframes heartBeat{0%{transform:scale(1)}
50%{transform:scale(1.4)}
100%{transform:scale(1)}
}
.tiktok-action-text{font-size:0.75rem;font-weight:600;color:#fff;text-shadow:0 2px 4px rgba(0,0,0,0.5);opacity:0.9}
.tiktok-video-info{position:absolute;bottom:60px;left:16px;right:80px;z-index:2147483647;perspective:1000px}
.tiktok-video-info h3{font-size:1.1rem;font-weight:700;line-height:1.4;margin:0;color:#fff;text-shadow:0 2px 10px rgba(0,0,0,0.5);transform-origin:left bottom;animation:slideUpFade 0.5s var(--ease-smooth)}
@keyframes slideUpFade{from{transform:translateY(20px);opacity:0}
to{transform:translateY(0);opacity:1}
}
.tiktok-progress-container{position:absolute;bottom:20px;left:10px;right:10px;height:48px;display:flex;align-items:center;padding:0 10px;z-index:2147483648;cursor:pointer;touch-action:none}
.tiktok-progress-container:active .tiktok-progress-bar,.tiktok-progress-container:hover .tiktok-progress-bar,.tiktok-progress-container.dragging .tiktok-progress-bar{height:8px;background:rgba(255,255,255,0.5);border-radius:4px}
.tiktok-progress-container:active .tiktok-progress-filled::after,.tiktok-progress-container:hover .tiktok-progress-filled::after,.tiktok-progress-container.dragging .tiktok-progress-filled::after{transform:translateY(-50%) scale(1)}
.tiktok-progress-container.dragging .tiktok-progress-filled::after{transform:translateY(-50%) scale(1.3);box-shadow:0 0 20px rgba(255,255,255,1),0 0 30px rgba(254,44,85,0.5)}
.tiktok-progress-container.dragging .tiktok-progress-filled{box-shadow:0 0 15px rgba(255,255,255,0.8),0 0 25px rgba(254,44,85,0.4)}
.tiktok-progress-bar{flex:1;height:4px;background:rgba(255,255,255,0.3);border-radius:2px;position:relative;transition:all 0.15s cubic-bezier(0.25,0.8,0.25,1)}
.tiktok-progress-filled{height:100%;background:#fff;border-radius:2px;width:0%;position:relative;box-shadow:0 0 10px rgba(255,255,255,0.5);transition:box-shadow 0.15s ease}
.tiktok-progress-filled::after{content:'';position:absolute;right:-10px;top:50%;width:20px;height:20px;background:#fff;border-radius:50%;transform:translateY(-50%) scale(0);transition:transform 0.2s cubic-bezier(0.68,-0.6,0.32,1.6),box-shadow 0.2s ease;box-shadow:0 0 15px rgba(255,255,255,0.8);pointer-events:none}
.tiktok-time-display{margin-left:12px;font-size:0.75rem;font-weight:500;font-variant-numeric:tabular-nums;color:rgba(255,255,255,0.9);text-shadow:0 1px 2px rgba(0,0,0,0.5);opacity:0.8}
.tiktok-loading,.tiktok-error{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);z-index:2147483648;display:flex;flex-direction:column;align-items:center;color:white;text-align:center}
.tiktok-error{display:none;padding:20px;max-width:80%}
.tiktok-loading-spinner{width:40px;height:40px;border:3px solid rgba(255,255,255,0.1);border-top-color:var(--primary-red);border-radius:50%;animation:spin 0.8s linear infinite}
@keyframes spin{0%{transform:rotate(0deg)}
100%{transform:rotate(360deg)}
}
.tiktok-loading-text{margin-top:15px;color:rgba(255,255,255,0.7);font-size:0.9rem}
.tiktok-error svg{width:64px;height:64px;fill:var(--primary-red);margin-bottom:20px}
.tiktok-error h3{font-size:1.2rem;margin-bottom:10px}
.tiktok-error p{color:rgba(255,255,255,0.7);margin-bottom:20px}
.tiktok-error-btn{background:var(--primary-red);color:white;border:none;padding:12px 24px;border-radius:24px;font-size:1rem;cursor:pointer;transition:background 0.3s}
.tiktok-error-btn:hover{background:#e8254d}
.tiktok-original-link{position:absolute;bottom:30px;right:20px;z-index:2147483648}
.tiktok-original-link a{display:flex;align-items:center;gap:6px;color:rgba(255,255,255,0.8);text-decoration:none;font-size:0.8rem;padding:8px 16px;background:rgba(255,255,255,0.1);border-radius:20px;transition:all 0.3s;backdrop-filter:blur(10px)}
.tiktok-original-link a:hover{background:rgba(255,255,255,0.2);color:white}
.tiktok-volume-control{position:absolute;left:16px;bottom:140px;display:flex;align-items:center;gap:12px;z-index:2147483648;transition:all 0.3s var(--ease-smooth);padding:8px;border-radius:30px;background:rgba(0,0,0,0.2);backdrop-filter:blur(4px)}
.tiktok-volume-control:hover,.tiktok-volume-control.active{background:var(--glass-bg);backdrop-filter:var(--glass-blur);border:1px solid var(--glass-border)}
.tiktok-volume-btn{width:36px;height:36px;border-radius:50%;display:flex;align-items:center;justify-content:center;cursor:pointer;transition:all 0.3s var(--ease-smooth);flex-shrink:0}
.tiktok-volume-control:hover .tiktok-volume-btn{background:rgba(255,255,255,0.1)}
.tiktok-volume-btn svg{width:20px;height:20px;fill:#fff;filter:drop-shadow(0 2px 4px rgba(0,0,0,0.3))}
.tiktok-volume-btn.muted svg{fill:rgba(255,255,255,0.5)}
.tiktok-volume-slider-container{width:0;height:36px;display:flex;align-items:center;overflow:hidden;transition:width 0.3s var(--ease-smooth),opacity 0.3s var(--ease-smooth);opacity:0}
.tiktok-volume-control:hover .tiktok-volume-slider-container,.tiktok-volume-control.active .tiktok-volume-slider-container{width:80px;opacity:1;margin-right:8px}
.tiktok-volume-slider{-webkit-appearance:none;appearance:none;width:100%;height:4px;background:rgba(255,255,255,0.3);border-radius:2px;outline:none;cursor:pointer}
.tiktok-volume-slider::-webkit-slider-thumb{-webkit-appearance:none;appearance:none;width:14px;height:14px;background:#fff;border-radius:50%;cursor:pointer;box-shadow:0 1px 4px rgba(0,0,0,0.3);transition:transform 0.2s ease}
.tiktok-volume-slider::-webkit-slider-thumb:hover{transform:scale(1.2)}
.tiktok-header{position:absolute;top:0;left:0;right:0;padding:16px 20px;padding-top:calc(16px + env(safe-area-inset-top));display:flex;justify-content:space-between;align-items:center;z-index:2147483648;pointer-events:none}
.tiktok-header>*{pointer-events:auto}
.tiktok-header-right{display:flex;align-items:center;gap:12px}
.tiktok-settings-btn{width:40px;height:40px;border-radius:50%;display:flex;align-items:center;justify-content:center;cursor:pointer;background:var(--glass-bg);backdrop-filter:var(--glass-blur);-webkit-backdrop-filter:var(--glass-blur);border:1px solid var(--glass-border);transition:all 0.3s var(--ease-smooth)}
.tiktok-settings-btn:hover,.tiktok-settings-btn.active{background:var(--glass-bg-hover);transform:rotate(45deg)}
.tiktok-settings-btn svg{width:22px;height:22px;fill:white}
.tiktok-close-btn{position:static;border:none;width:40px;height:40px}
.tiktok-settings-panel{position:absolute;top:70px;right:20px;top:calc(70px + env(safe-area-inset-top));width:240px;background:rgba(20,20,20,0.95);backdrop-filter:blur(20px);-webkit-backdrop-filter:blur(20px);border:1px solid rgba(255,255,255,0.1);border-radius:16px;padding:8px;display:flex;flex-direction:column;gap:4px;opacity:0;transform:translateY(-10px) scale(0.95);pointer-events:none;transition:all 0.25s var(--ease-smooth);z-index:2147483649;box-shadow:0 10px 40px rgba(0,0,0,0.5)}
.tiktok-settings-panel.active{opacity:1;transform:translateY(0) scale(1);pointer-events:auto}
.tiktok-setting-item{display:flex;justify-content:space-between;align-items:center;padding:12px 16px;border-radius:12px;cursor:pointer;transition:background 0.2s;color:rgba(255,255,255,0.9);font-size:14px;font-weight:500}
.tiktok-setting-item:hover{background:rgba(255,255,255,0.1)}
.tiktok-setting-label{display:flex;align-items:center;gap:8px}
.tiktok-setting-label svg{width:18px;height:18px;fill:rgba(255,255,255,0.7)}
.setting-switch{width:40px;height:24px;background:rgba(255,255,255,0.2);border-radius:12px;position:relative;transition:background 0.3s}
.setting-switch::after{content:'';position:absolute;top:2px;left:2px;width:20px;height:20px;background:#fff;border-radius:50%;transition:transform 0.3s var(--ease-elastic);box-shadow:0 2px 4px rgba(0,0,0,0.2)}
.tiktok-setting-item.active .setting-switch{background:var(--primary-cyan)}
.tiktok-setting-item[data-type="unread"].active .setting-switch{background:var(--primary-red)}
.tiktok-setting-item.active .setting-switch::after{transform:translateX(16px)}
.tiktok-overlay-icon{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%) scale(0.5);width:80px;height:80px;background:rgba(0,0,0,0.4);backdrop-filter:blur(4px);border-radius:50%;display:flex;align-items:center;justify-content:center;pointer-events:none;opacity:0;transition:all 0.2s var(--ease-elastic);z-index:2147483648}
.tiktok-overlay-icon.visible{opacity:1;transform:translate(-50%,-50%) scale(1)}
.tiktok-overlay-icon svg{width:40px;height:40px;fill:rgba(255,255,255,0.9)}
.tiktok-speed-overlay{position:absolute;top:100px;left:50%;transform:translateX(-50%);padding:8px 16px;background:rgba(0,0,0,0.6);backdrop-filter:blur(10px);border-radius:20px;color:white;font-size:14px;font-weight:600;display:flex;align-items:center;gap:6px;opacity:0;transition:opacity 0.2s;z-index:2147483648;pointer-events:none}
.tiktok-speed-overlay.visible{opacity:1}
.tiktok-speed-overlay svg{width:16px;height:16px;fill:white}
.tiktok-debug-btn{padding:6px 12px;border-radius:20px;background:rgba(255,255,255,0.05);backdrop-filter:blur(4px);border:1px solid rgba(255,255,255,0.05);color:rgba(255,255,255,0.4);font-size:11px;font-weight:500;cursor:pointer;transition:all 0.2s ease;white-space:nowrap;display:flex;align-items:center;height:32px;margin-right:4px}
.tiktok-debug-btn:hover{background:rgba(255,255,255,0.15);color:rgba(255,255,255,0.9);border-color:rgba(255,255,255,0.2)}
@media (max-width:768px){.tiktok-header{padding:12px 16px}
.tiktok-settings-btn,.tiktok-close-btn{width:36px;height:36px}
.tiktok-video-count{position:static;background:rgba(0,0,0,0.3);padding:4px 10px;font-size:12px}
.tiktok-debug-btn{padding:0 8px;font-size:10px;background:transparent;border:none}
.tiktok-volume-control{bottom:110px;left:10px}
.tiktok-actions{bottom:110px;right:10px}
.tiktok-volume-control.active .tiktok-volume-slider-container{width:100px}
}
`;
// ========================================
// 主要功能类 - TikTokModalPlayer
// ========================================
class TikTokModalPlayer {
constructor() {
this.currentVideoIndex = 0;
this.videoList = [];
this.filteredVideoList = []; // 过滤后的视频列表
this.isLoading = false;
this.isDragging = false;
this.isSpeeding = false;
this.startY = 0;
this.currentY = 0;
this.videoElement = null;
this.isLiked = false;
this.videoUrlCache = new Map();
this.isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
this.isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent) ||
(/AppleWebKit/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent));
this.isIOSSafari = this.isIOS && this.isSafari;
// 动画状态
this.isTransitioning = false;
// 进度条拖拽状态
this.isProgressDragging = false;
// 重试机制状态
this.retryCount = 0;
this.MAX_RETRIES = 3;
this.transitionTimers = [];
this.retryTimeoutId = null; // 用于取消旧的重试定时器
this.loadVersion = 0; // 加载版本号,用于丢弃过期的回调
// 已观看视频记录 (localStorage)
this.WATCHED_STORAGE_KEY = 'tiktok_modal_watched_videos';
this.watchedVideos = this.loadWatchedVideos();
this.PROGRESS_STORAGE_KEY = 'tiktok_modal_video_progress';
this.videoProgress = this.loadVideoProgress();
this.progressSaveThrottle = 0;
// 只看未读模式
this.unreadOnlyMode = false;
// 循环播放模式
this.isLooping = false;
// 音量控制
this.VOLUME_STORAGE_KEY = 'tiktok_modal_volume';
this.currentVolume = this.loadSavedVolume();
this.isMuted = false;
this.savedScrollY = 0;
this.savedBodyTop = '';
this.savedBodyOverflow = '';
this.PERF_MODE_KEY = 'tiktok_modal_perf_mode';
this.perfModeEnabled = this.loadPerfMode();
// Debug模式开关
this.debugMode = false;
// 统计数据
this.stats = {
mp4: 0,
failed: 0,
total: 0,
parsedHistory: []
};
this.init();
}
init() {
// 强制全局禁用 Referer (解决403的关键)
const meta = document.createElement('meta');
meta.name = "referrer";
meta.content = "no-referrer";
document.head.appendChild(meta);
// 确保 viewport-fit=cover 以扩展到安全区域 (解决底部白条)
const viewport = document.querySelector('meta[name="viewport"]');
if (viewport) {
if (!viewport.content.includes('viewport-fit=cover')) {
viewport.content = `${viewport.content}, viewport-fit=cover`;
}
} else {
const v = document.createElement('meta');
v.name = 'viewport';
v.content = 'width=device-width, initial-scale=1, viewport-fit=cover';
document.head.appendChild(v);
}
// 注入样式
this.injectStyles();
// 创建模态框DOM (立即可用)
this.createModalDOM();
// 创建 Debug 按钮
this.createDebugButton();
// 绑定事件
this.bindEvents();
// 异步收集视频列表 - 不阻塞模态框初始化
this.scheduleVideoCollection();
// 启动 DOM 监听,处理动态加载的内容 (修复切换排序后失效的问题)
this.setupMutationObserver();
console.log('🎬 TikTok Modal Player 初始化完成 - 模态框已就绪');
}
createDebugButton() {
const btn = document.createElement('div');
btn.className = 'tiktok-debug-btn';
btn.textContent = 'Log'; // 简化文字
btn.title = '点击复制调试日志';
btn.addEventListener('click', (e) => {
e.stopPropagation();
this.copyDebugLogs();
});
const modal = document.getElementById('tiktok-modal');
if (modal) {
// 尝试插入到 header 右侧容器
const headerRight = modal.querySelector('.tiktok-header-right');
if (headerRight) {
// 插入到第一个位置(在设置按钮左边)
headerRight.insertBefore(btn, headerRight.firstChild);
} else {
modal.appendChild(btn);
}
}
}
copyDebugLogs() {
const report = [
`=== Twitter Video Player Debug Log ===`,
`Time: ${new Date().toLocaleString()}`,
`UA: ${navigator.userAgent}`,
`Platform: ${navigator.platform}`,
`Version: ${GM_info?.script?.version || 'Unknown'}`,
``,
`=== Statistics ===`,
`Total Parsed: ${this.stats.total}`,
`MP4: ${this.stats.mp4}`,
`Failed: ${this.stats.failed}`,
`Platform: ${this.isIOS ? 'iOS' : 'Other'} / ${this.isSafari ? 'Safari' : 'Non-Safari'}`,
``,
`=== Recent Parsed Videos (Last 20) ===`,
...this.stats.parsedHistory.slice(-20).map(item =>
`[${item.time}] ${item.type.toUpperCase()}: ${item.url.slice(-50)}`
)
].join('\n');
navigator.clipboard.writeText(report).then(() => {
const originalText = document.querySelector('.tiktok-debug-btn').textContent;
document.querySelector('.tiktok-debug-btn').textContent = '✅ Copied!';
setTimeout(() => {
if(document.querySelector('.tiktok-debug-btn'))
document.querySelector('.tiktok-debug-btn').textContent = originalText;
}, 2000);
}).catch(err => {
console.error('Copy failed', err);
alert('复制失败,请查看控制台');
});
}
setupMutationObserver() {
// 防抖定时器
let timeout = null;
const observer = new MutationObserver((mutations) => {
// 如果模态框是打开的,暂停DOM扫描以节省资源
if (this.isModalOpen()) return;
// 检查是否有相关节点变动 (简单的性能优化)
let shouldUpdate = false;
for (const mutation of mutations) {
if (mutation.addedNodes.length > 0) {
shouldUpdate = true;
break;
}
}
if (shouldUpdate) {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
console.log('DOM 变动检测 - 重新扫描视频...');
this.collectVideoLinks();
}, 500); // 缩短防抖时间到 500ms,提高响应速度
}
});
// 监听 body 的子树变化
observer.observe(document.body, {
childList: true,
subtree: true
});
console.log('👀 DOM 监听器已启动');
}
// 初始化可视区域观察者
initViewportObserver() {
// 使用 IntersectionObserver 监听视频元素是否进入可视区域
this.viewportObserver = new IntersectionObserver((entries) => {
// 如果模态框已打开,暂停预加载以节省带宽
if (this.isModalOpen()) return;
entries.forEach(entry => {
// 当元素进入可视区域超过 10% 时触发
if (entry.isIntersecting && entry.intersectionRatio > 0.1) {
const element = entry.target;
// 找到对应的视频对象
const index = this.videoList.findIndex(v => v.element === element);
if (index !== -1) {
// 触发预加载 (带有防抖,避免滚动过快时触发太多请求)
this.scheduleSingleVideoPreload(index);
}
}
});
}, {
root: null, // 视口作为根
rootMargin: '100px', // 提前 100px 触发
threshold: 0.1
});
}
scheduleSingleVideoPreload(index) {
// 简单的防抖机制
if (this._preloadTimeout) clearTimeout(this._preloadTimeout);
this._preloadTimeout = setTimeout(() => {
this.preloadSingleVideo(index);
}, 500); // 停留 500ms 后才开始预加载
}
async preloadSingleVideo(index) {
const video = this.videoList[index];
if (!video) return;
// 1. 如果没有真实URL,先解析
if (!this.videoUrlCache.has(video.url)) {
// console.log(`👁️ 视频进入可视区域/悬停,触发预解析: ${index}`);
try {
const realUrl = await this.fetchRealVideoUrl(video.url);
if (realUrl) {
this.videoUrlCache.set(video.url, realUrl);
this.warmupVideoConnection(realUrl);
}
} catch (e) {
console.error('预解析失败:', e);
}
} else {
// 2. 如果已有真实URL,直接预热连接
// console.log(`🔥 视频已解析,触发连接预热: ${index}`);
const realUrl = this.videoUrlCache.get(video.url);
this.warmupVideoConnection(realUrl);
}
}
// 使用空闲时间收集视频,不阻塞其他操作
scheduleVideoCollection() {
const collect = () => {
this.collectVideoLinks();
// 首次收集后,如果列表为空,可能页面还没渲染完,设置一个短定时器再次检查
if (this.videoList.length === 0) {
setTimeout(() => this.collectVideoLinks(), 1000);
}
};
// 立即尝试一次
collect();
// 确保在 DOM 加载完成后再次执行
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', collect);
window.addEventListener('load', collect);
}
}
// 启动列表预加载(核心优化:提前解析视频地址)
async startListPreloading() {
if (this.isListPreloading) return;
this.isListPreloading = true;
// console.log('🚀 启动列表预解析 (Top 5)...');
// 优化:优先预加载前3个视频,确保首屏秒开
const videosToPreload = this.videoList.slice(0, 3);
let hasNewPreload = false;
for (const video of videosToPreload) {
// 如果已经缓存了真实URL,跳过
if (this.videoUrlCache.has(video.url)) continue;
hasNewPreload = true;
try {
const realUrl = await this.fetchRealVideoUrl(video.url);
if (realUrl) {
this.videoUrlCache.set(video.url, realUrl);
}
await new Promise(r => setTimeout(r, 100));
} catch (e) {
console.error('列表预解析失败:', video.url, e);
}
}
this.isListPreloading = false;
if (hasNewPreload) {
console.log('✨ 列表预解析完成 (新缓存已更新)');
}
}
injectStyles() {
const styleSheet = document.createElement('style');
styleSheet.textContent = styles;
document.head.appendChild(styleSheet);
}
svgIcon(path, attrs = '') {
return `<svg ${attrs} viewBox="0 0 24 24"><path d="${path}"/></svg>`;
}
settingItem(type, iconPath, label, id = `tiktok-${type}-toggle`) {
return `
<div class="tiktok-setting-item" id="${id}" data-type="${type}">
<div class="tiktok-setting-label">
${this.svgIcon(iconPath)}
<span>${label}</span>
</div>
<div class="setting-switch"></div>
</div>`;
}
actionItem(action, iconPath, label, title = label, iconId = '') {
const idAttr = iconId ? ` id="${iconId}"` : '';
return `
<div class="tiktok-action-item" data-action="${action}" title="${title}">
<div class="tiktok-action-icon"${idAttr}>
${this.svgIcon(iconPath)}
</div>
<span class="tiktok-action-text">${label}</span>
</div>`;
}
createModalDOM() {
const modal = document.createElement('div');
modal.className = 'tiktok-modal-overlay';
modal.id = 'tiktok-modal';
// Disable context menu to prevent conflicts with long press
modal.oncontextmenu = (e) => {
e.preventDefault();
e.stopPropagation();
return false;
};
modal.innerHTML = `
<!-- 顶部控制栏 -->
<div class="tiktok-header">
<div class="tiktok-header-left">
<div class="tiktok-video-count" id="tiktok-count">1 / 1</div>
</div>
<div class="tiktok-header-right">
<button class="tiktok-settings-btn" id="tiktok-settings-btn" title="设置">
<svg viewBox="0 0 24 24"><path d="M19.14,12.94c0.04-0.3,0.06-0.61,0.06-0.94c0-0.32-0.02-0.64-0.06-0.94l2.03-1.58c0.18-0.14,0.23-0.41,0.12-0.61 l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39,0.96c-0.5-0.38-1.03-0.7-1.62-0.94L14.4,2.81c-0.04-0.24-0.24-0.41-0.48-0.41 h-3.84c-0.24,0-0.43,0.17-0.47,0.41L9.25,5.35C8.66,5.59,8.12,5.92,7.63,6.29L5.24,5.33c-0.22-0.08-0.47,0-0.59,0.22L2.74,8.87 C2.62,9.08,2.66,9.34,2.86,9.48l2.03,1.58C4.84,11.36,4.8,11.69,4.8,12s0.02,0.64,0.06,0.94l-2.03,1.58 c-0.18,0.14-0.23,0.41-0.12,0.61l1.92,3.32c0.12,0.22,0.37,0.29,0.59,0.22l2.39-0.96c0.5,0.38,1.03,0.7,1.62,0.94l0.36,2.54 c0.05,0.24,0.24,0.41,0.48,0.41h3.84c0.24,0,0.44-0.17,0.47-0.41l0.36-2.54c0.59-0.24,1.13-0.56,1.62-0.94l2.39,0.96 c0.22,0.08,0.47,0,0.59-0.22l1.92-3.32c0.12-0.22,0.07-0.47-0.12-0.61L19.14,12.94z M12,15.6c-1.98,0-3.6-1.62-3.6-3.6 s1.62-3.6,3.6-3.6s3.6,1.62,3.6,3.6S13.98,15.6,12,15.6z"/></svg>
</button>
<button class="tiktok-close-btn" id="tiktok-close" aria-label="关闭">
<svg viewBox="0 0 24 24">
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
</svg>
</button>
</div>
</div>
<!-- 设置面板 -->
<div class="tiktok-settings-panel" id="tiktok-settings-panel">
${this.settingItem('perf', 'M15.67 4H14V2h-4v2H8.33C7.6 4 7 4.6 7 5.33v15.33C7 21.4 7.6 22 8.33 22h7.33c.74 0 1.34-.6 1.34-1.33V5.33C17 4.6 16.4 4 15.67 4zM11 20v-5.5H9L13 7v5.5h2L11 20z', '低功耗模式')}
${this.settingItem('unread', 'M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z', '只看未读')}
${this.settingItem('loop', 'M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z', '循环播放')}
</div>
<div class="tiktok-video-container" id="tiktok-container">
<!-- 交互反馈覆盖层 -->
<div class="tiktok-overlay-icon" id="tiktok-pause-icon">
<svg viewBox="0 0 24 24"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>
</div>
<div class="tiktok-speed-overlay" id="tiktok-speed-overlay">
<svg viewBox="0 0 24 24"><path d="M10 8v8l6-4-6-4zm9-5H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 9.5l-2.5 1.5V11H12v2.5L9.5 12 12 10.5V13h3.5v-2.5L18 12.5z"/></svg>
<span>2x 倍速中</span>
</div>
<div class="tiktok-loading" id="tiktok-loading">
<div class="tiktok-loading-spinner"></div>
<div class="tiktok-loading-text">加载中...</div>
</div>
<div class="tiktok-error" id="tiktok-error">
<svg viewBox="0 0 24 24">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/>
</svg>
<h3>视频加载失败</h3>
<p>无法加载视频,请尝试在原页面观看</p>
<button class="tiktok-error-btn" id="tiktok-retry">重试</button>
</div>
<!-- 双层渲染:缩略图层(立即显示) -->
<img class="tiktok-thumbnail-layer" id="tiktok-thumbnail" alt="" />
<!-- 双层渲染:视频层(背后加载) -->
<video class="tiktok-video-player" id="tiktok-player" playsinline webkit-playsinline x5-playsinline preload="metadata" muted referrerpolicy="no-referrer"></video>
<div class="tiktok-progress-container" id="tiktok-progress-container">
<div class="tiktok-progress-bar" id="tiktok-progress-bar">
<div class="tiktok-progress-filled" id="tiktok-progress-filled"></div>
</div>
<div class="tiktok-time-display" id="tiktok-time-display">0:00 / 0:00</div>
</div>
<div class="tiktok-video-info" id="tiktok-info">
<h3 id="tiktok-title">视频标题</h3>
</div>
<div class="tiktok-volume-control" id="tiktok-volume-control">
<div class="tiktok-volume-btn" id="tiktok-volume-btn" title="静音/取消静音">
<svg id="tiktok-volume-icon" viewBox="0 0 24 24">
<path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/>
</svg>
</div>
<div class="tiktok-volume-slider-container">
<input type="range" class="tiktok-volume-slider" id="tiktok-volume-slider" min="0" max="1" step="0.05" value="1">
</div>
</div>
<div class="tiktok-actions" id="tiktok-actions">
${this.actionItem('like', 'M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z', '喜欢', '点赞', 'tiktok-like-icon')}
${this.actionItem('download', 'M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z', '下载', '下载', 'tiktok-download-icon')}
${this.actionItem('share', 'M18 16.08c-.76 0-1.44.3-1.96.77L8.91 12.7c.05-.23.09-.46.09-.7s-.04-.47-.09-.7l7.05-4.11c.54.5 1.25.81 2.04.81 1.66 0 3-1.34 3-3s-1.34-3-3-3-3 1.34-3 3c0 .24.04.47.09.7L8.04 9.81C7.5 9.31 6.79 9 6 9c-1.66 0-3 1.34-3 3s1.34 3 3 3c.79 0 1.5-.31 2.04-.81l7.12 4.16c-.05.21-.08.43-.08.65 0 1.61 1.31 2.92 2.92 2.92s2.92-1.31 2.92-2.92-1.31-2.92-2.92-2.92z', '分享', '分享')}
</div>
</div>
`;
document.body.appendChild(modal);
}
bindEvents() {
// 关闭按钮
const closeBtn = document.getElementById('tiktok-close');
this.bindTap(closeBtn, () => this.closeModal(), { stopPropagation: false });
// 点击背景关闭 & 设置面板关闭逻辑
const modal = document.getElementById('tiktok-modal');
modal.addEventListener('click', (e) => {
if (e.target.id === 'tiktok-modal') {
this.closeModal();
}
// 点击非设置区域关闭设置面板
const settingsPanel = document.getElementById('tiktok-settings-panel');
const settingsBtn = document.getElementById('tiktok-settings-btn');
if (settingsPanel && settingsPanel.classList.contains('active')) {
if (!settingsPanel.contains(e.target) && !settingsBtn.contains(e.target)) {
settingsPanel.classList.remove('active');
settingsBtn.classList.remove('active');
}
}
});
// 重试按钮
document.getElementById('tiktok-retry').addEventListener('click', () => {
this.loadVideo(this.currentVideoIndex);
});
// 设置按钮
const settingsBtn = document.getElementById('tiktok-settings-btn');
const settingsPanel = document.getElementById('tiktok-settings-panel');
const toggleSettings = (e) => {
e.preventDefault();
e.stopPropagation();
const isActive = settingsPanel.classList.toggle('active');
settingsBtn.classList.toggle('active', isActive);
};
this.bindTap(settingsBtn, toggleSettings);
// 防止点击面板内部触发关闭
settingsPanel.addEventListener('click', (e) => e.stopPropagation());
settingsPanel.addEventListener('touchend', (e) => e.stopPropagation());
// 设置项点击处理 (事件委托)
const handleSettingClick = (type) => {
switch(type) {
case 'perf':
this.togglePerfMode();
break;
case 'unread':
this.toggleUnreadMode();
break;
case 'loop':
this.toggleLoopMode();
break;
}
this.updateSettingsUI();
};
document.querySelectorAll('.tiktok-setting-item').forEach(item => {
this.bindTap(item, () => handleSettingClick(item.dataset.type));
});
// 初始化设置UI状态
this.updateSettingsUI();
// 键盘导航
document.addEventListener('keydown', (e) => {
if (!this.isModalOpen()) return;
switch (e.key) {
case 'Escape':
this.closeModal();
break;
case 'ArrowUp':
e.preventDefault();
this.previousVideo();
break;
case 'ArrowDown':
e.preventDefault();
this.nextVideo();
break;
case 'ArrowLeft':
e.preventDefault();
this.seekBy(-5);
break;
case 'ArrowRight':
e.preventDefault();
this.seekBy(5);
break;
case ' ':
e.preventDefault();
this.togglePlay();
break;
case 'Enter':
this.togglePlay();
break;
}
});
// 触摸滑动 - 移动端
const container = document.getElementById('tiktok-container');
let touchStartY = 0;
let touchStartX = 0;
container.addEventListener('touchstart', (e) => {
touchStartY = e.touches[0].clientY;
touchStartX = e.touches[0].clientX;
this.isDragging = true;
}, { passive: true });
container.addEventListener('touchmove', (e) => {
if (!this.isDragging) return;
if (this.isProgressDragging) return;
const touchY = e.touches[0].clientY;
const touchX = e.touches[0].clientX;
const diffY = touchStartY - touchY;
const diffX = touchStartX - touchX;
// 判断是垂直滑动还是水平滑动
if (Math.abs(diffY) > Math.abs(diffX) && Math.abs(diffY) > 30) {
e.preventDefault(); // 阻止页面滚动
}
}, { passive: false });
container.addEventListener('touchend', (e) => {
if (!this.isDragging) return;
this.isDragging = false;
const touchEndY = e.changedTouches[0].clientY;
const touchEndX = e.changedTouches[0].clientX;
const diffY = touchStartY - touchEndY;
const diffX = touchStartX - touchEndX;
const threshold = 50;
// 确保是垂直滑动
if (Math.abs(diffY) > Math.abs(diffX) && Math.abs(diffY) > threshold) {
if (diffY > 0) {
// 向上滑动 - 下一个视频
this.nextVideo();
} else {
// 向下滑动 - 上一个视频
this.previousVideo();
}
}
}, { passive: true });
container.addEventListener('touchcancel', () => {
this.isDragging = false;
}, { passive: true });
// 鼠标滚轮导航 - PC端
let wheelTimeout = null;
container.addEventListener('wheel', (e) => {
if (!this.isModalOpen()) return;
if (wheelTimeout) return; // 防止快速滚动
wheelTimeout = setTimeout(() => {
wheelTimeout = null;
}, 500);
e.preventDefault();
if (e.deltaY > 0) {
this.nextVideo();
} else {
this.previousVideo();
}
}, { passive: false });
// 鼠标拖拽 - PC端
let mouseStartY = 0;
let isMouseDown = false;
container.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // 只响应左键
mouseStartY = e.clientY;
isMouseDown = true;
container.style.cursor = 'grabbing';
});
container.addEventListener('mousemove', (e) => {
if (!isMouseDown) return;
const diff = mouseStartY - e.clientY;
if (Math.abs(diff) > 100) {
isMouseDown = false;
container.style.cursor = 'pointer';
if (diff > 0) {
this.nextVideo();
} else {
this.previousVideo();
}
}
});
container.addEventListener('mouseup', () => resetMouseState());
container.addEventListener('mouseleave', () => resetMouseState());
function resetMouseState() {
isMouseDown = false;
container.style.cursor = 'pointer';
}
// 操作按钮
document.querySelectorAll('.tiktok-action-item').forEach(item => {
this.bindTap(item, () => this.handleAction(item.dataset.action), { preventDefault: false });
});
// 初始化视频事件绑定
this.videoElement = document.getElementById('tiktok-player');
this.bindVideoPlayerEvents(this.videoElement);
// 进度条交互
this.setupProgressBarInteraction();
// 音量控制交互
this.setupVolumeControl();
}
updateSettingsUI() {
// 更新设置面板中各个开关的状态
const perfItem = document.querySelector('.tiktok-setting-item[data-type="perf"]');
const unreadItem = document.querySelector('.tiktok-setting-item[data-type="unread"]');
const loopItem = document.querySelector('.tiktok-setting-item[data-type="loop"]');
if (perfItem) perfItem.classList.toggle('active', this.perfModeEnabled);
if (unreadItem) unreadItem.classList.toggle('active', this.unreadOnlyMode);
if (loopItem) loopItem.classList.toggle('active', this.isLooping);
}
toggleLoopMode() {
this.isLooping = !this.isLooping;
console.log(`🔁 循环播放: ${this.isLooping ? '开启' : '关闭'}`);
if (this.videoElement) {
this.videoElement.loop = this.isLooping;
}
}
seekBy(seconds) {
if (this.videoElement && this.videoElement.duration) {
const newTime = Math.max(0, Math.min(this.videoElement.duration, this.videoElement.currentTime + seconds));
this.videoElement.currentTime = newTime;
this.updateProgressBar();
this.saveCurrentVideoProgress(true);
}
}
// 绑定视频播放器相关的所有事件
bindVideoPlayerEvents(videoEl) {
// 视频加载完成
videoEl.addEventListener('loadedmetadata', () => {
if (this.loadStartTime) {
console.log(`[Timer] 视频元数据加载完成 (loadedmetadata),总耗时: ${Date.now() - this.loadStartTime}ms`);
}
this.hideLoading();
this.updateTimeDisplay();
// 恢复之前的倍速设置
if (this.isSpeeding) {
videoEl.playbackRate = 2.0;
}
// 应用循环设置
videoEl.loop = this.isLooping;
});
videoEl.addEventListener('loadeddata', () => {
if (this.loadStartTime) {
console.log(`[Timer] 视频首帧加载完成 (loadeddata),总耗时: ${Date.now() - this.loadStartTime}ms`);
}
});
videoEl.addEventListener('canplay', () => {
}, { once: false });
videoEl.addEventListener('timeupdate', () => {
if (!this.isProgressDragging) {
this.updateProgressBar();
}
this.saveCurrentVideoProgressThrottled();
});
// 视频加载错误处理
videoEl.addEventListener('error', (e) => {
if (!this.isModalOpen()) return;
if (e.target !== this.videoElement) {
console.log('⏭️ 忽略旧视频的 error 事件 (已切换到其他视频)');
return;
}
const currentVideo = this.getCurrentVideo();
if (this.retryCount < this.MAX_RETRIES) {
this.retryCount++;
const delay = Math.pow(2, this.retryCount) * 1000;
console.log(`🔄 视频加载失败,${delay / 1000}秒后尝试第 ${this.retryCount}/${this.MAX_RETRIES} 次重试...`);
const versionAtError = this.loadVersion;
if (currentVideo) {
if (this.videoUrlCache.has(currentVideo.url)) {
console.log('🧹 清除可能的过期URL缓存');
this.videoUrlCache.delete(currentVideo.url);
}
if (this.retryTimeoutId) {
clearTimeout(this.retryTimeoutId);
}
this.retryTimeoutId = setTimeout(() => {
if (this.loadVersion !== versionAtError || !this.isModalOpen()) {
console.log('⏭️ 放弃重试 (用户已切换视频)');
return;
}
console.log('🔁 发起重试...');
this.loadVideo(this.currentVideoIndex); // 重新加载完整流程
}, delay);
return;
}
}
this.showError();
});
videoEl.addEventListener('ended', () => {
if (!this.isModalOpen()) return;
this.clearCurrentVideoProgress();
if (this.isLooping) {
// 循环播放已由 video.loop 属性处理,这里作为备用
videoEl.play();
} else {
// 自动播放下一个视频
this.nextVideo();
}
});
// 点击视频播放/暂停 (与长按逻辑区分)
let pressTimer = null;
let isLongPress = false;
const container = document.getElementById('tiktok-container');
const startPress = (e) => {
// 忽略非主按键
if (e.type === 'mousedown' && e.button !== 0) return;
// 忽略控件上的点击
if (e.target.closest('.tiktok-volume-control, .tiktok-actions, .tiktok-progress-container, .tiktok-header')) return;