-
Notifications
You must be signed in to change notification settings - Fork 11
/
yt_sponsor_block.js
1092 lines (944 loc) · 29.1 KB
/
yt_sponsor_block.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 YouTube ads skip with SponsorBlock
// @version 0.0.1
// @description Skip ads on YouTube with SponsorBlock.
// @author Hyperweb
// @match m.youtube.com
// @match www.youtube.com
// @match www.youtube-nocookie.com
// @match music.youtube.com
// @grant GM.notification
// @license LGPL-3.0-or-later
// @noframes
// ==/UserScript==
const API_URL = "https://sponsor.ajay.app/api/skipSegments/";
const USER_AGENT_QUERY = "userAgent";
const CATEGORY_QUERY = "category";
const CATEGORIES = ["sponsor", "intro"];
const ActionType = {
Skip: "skip",
Mute: "mute",
Full: "full",
Poi: "poi",
};
let sponsorDataFound = false;
//the actual sponsorTimes if loaded and UUIDs associated with them
let sponsorTimes = null;
//what video id are these sponsors for
let sponsorVideoID = null;
/** Has the sponsor been skipped */
let sponsorSkipped = [];
let sponsorTimesSubmitting = [];
// It resumes with a slightly later time on chromium
let lastTimeFromWaitingEvent = null;
// Is the video currently being switched
let switchingVideos = null;
// Made true every videoID change
let firstEvent = false;
// Used by the play and playing listeners to make sure two aren't
// called at the same time
let lastCheckTime = 0;
let lastCheckVideoTime = -1;
let lastKnownVideoTime;
let waitingMutationObserver;
let waitingElements;
// List of videos that have had event listeners added to them
const videosWithEventListeners = [];
// Skips are scheduled to ensure precision.
// Skips are rescheduled every seeking event.
// Skips are canceled every seeking event
let currentSkipSchedule = null;
let currentSkipInterval = null;
let isAdPlaying = false;
//the video
let video;
let videoMuted = false; // Has it been attempted to be muted
let videoMutationObserver = null;
let isSafari = true;
const waitForElement = async (selector) => {
return await new Promise((resolve) => {
waitingElements.push({
selector,
callback: resolve,
});
if (!waitingMutationObserver) {
waitingMutationObserver = new MutationObserver(() => {
const foundSelectors = [];
for (const { selector, callback } of waitingElements) {
const element = document.querySelector(selector);
if (element) {
callback(element);
foundSelectors.push(selector);
}
}
waitingElements = waitingElements.filter(
(element) => !foundSelectors.includes(element.selector)
);
if (waitingElements.length === 0) {
waitingMutationObserver.disconnect();
waitingMutationObserver = null;
}
});
waitingMutationObserver.observe(document.body, {
childList: true,
subtree: true,
});
}
});
};
const isVisible = (element) => {
return element && element.offsetWidth > 0 && element.offsetHeight > 0;
};
const findValidElement = (elements) => {
for (const el of elements) {
if (el && isVisible(el)) {
return el;
}
}
return null;
};
const refreshVideoAttachments = () => {
const newVideo = findValidElement(document.querySelectorAll("video"));
if (newVideo && newVideo !== video) {
video = newVideo;
if (!videosWithEventListeners.includes(video)) {
videosWithEventListeners.push(video);
setupVideoListeners();
}
}
};
const addPageListeners = () => {
const refreshListners = () => {
if (!isVisible(video)) {
refreshVideoAttachments();
}
};
document.addEventListener("yt-navigate-finish", refreshListners);
};
const skipToTime = ({ v, skipTime, skippingSegments }) => {
const autoSkip = true;
if (autoSkip && v.currentTime !== skipTime[1]) {
switch (skippingSegments[0].actionType) {
case ActionType.Poi:
case ActionType.Skip: {
// Fix for looped videos not working when skipping to the end #426
// for some reason you also can't skip to 1 second before the end
if (v.loop && v.duration > 1 && skipTime[1] >= v.duration - 1) {
v.currentTime = 0;
} else if (
navigator.vendor === "Apple Computer, Inc." &&
v.duration > 1 &&
skipTime[1] >= v.duration
) {
// MacOS will loop otherwise #1027
v.currentTime = v.duration - 0.001;
} else {
try {
const delta = parseInt(skipTime[1] - skipTime[0]);
GM.notification({
text: `${ delta } second(s) skipped with SponsorBlock via Hyperweb`,
position: 'bottom',
style: 'bar',
timeout: 5000,
});
} catch {}
v.currentTime = skipTime[1];
}
break;
}
case ActionType.Mute: {
if (!v.muted) {
v.muted = true;
videoMuted = true;
}
break;
}
}
}
};
const updateVirtualTime = () => {
lastKnownVideoTime = {
videoTime: video.currentTime,
preciseTime: performance.now(),
};
};
/**
* Update the isAdPlaying flag and hide preview bar/controls if ad is playing
*/
const updateAdFlag = () => {
isAdPlaying = document.getElementsByClassName("ad-showing").length > 0;
};
const cancelSponsorSchedule = () => {
if (currentSkipSchedule !== null) {
clearTimeout(currentSkipSchedule);
currentSkipSchedule = null;
}
if (currentSkipInterval !== null) {
clearInterval(currentSkipInterval);
currentSkipInterval = null;
}
};
const inMuteSegment = (currentTime) => {
const checkFunction = (segment) =>
segment.actionType === ActionType.Mute &&
segment.segment[0] <= currentTime &&
segment.segment[1] > currentTime;
return sponsorTimes?.some(checkFunction);
};
/**
* This makes sure the videoID is still correct and if the sponsorTime is included
*/
const incorrectVideoCheck = (videoID = null, sponsorTime = null) => {
const currentVideoID = getYouTubeVideoID(document);
if (
currentVideoID !== (videoID || sponsorVideoID) ||
(sponsorTime &&
(!sponsorTimes ||
!sponsorTimes?.some((time) => time.segment === sponsorTime.segment)) &&
!sponsorTimesSubmitting.some(
(time) => time.segment === sponsorTime.segment
))
) {
// Something has really gone wrong
console.error(
"[SponsorBlock] The videoID recorded when trying to skip is different than what it should be."
);
console.error(
"[SponsorBlock] VideoID recorded: " +
sponsorVideoID +
". Actual VideoID: " +
currentVideoID
);
// Video ID change occured
videoIDChange(currentVideoID);
return true;
} else {
return false;
}
};
/**
* This returns index if the skip option is not AutoSkip
*
* Finds the last endTime that occurs in a segment that the given
* segment skips into that is part of an AutoSkip category.
*
* Used to find where a segment should truely skip to if there are intersecting submissions due to
* them having different categories.
*
* @param sponsorTimes
* @param index Index of the given sponsor
* @param hideHiddenSponsors
*/
const getLatestEndTimeIndex = (
sponsorTimes,
index,
hideHiddenSponsors = true
) => {
// Only combine segments for AutoSkip
if (
index == -1 || // ||
// !shouldAutoSkip(sponsorTimes[index])
sponsorTimes[index].actionType !== ActionType.Skip
) {
return index;
}
// Default to the normal endTime
let latestEndTimeIndex = index;
for (let i = 0; i < sponsorTimes?.length; i++) {
const currentSegment = sponsorTimes[i].segment;
const latestEndTime = sponsorTimes[latestEndTimeIndex].segment[1];
if (
currentSegment[0] <= latestEndTime &&
currentSegment[1] > latestEndTime &&
// && (!hideHiddenSponsors || sponsorTimes[i].hidden === SponsorHideType.Visible)
// && shouldAutoSkip(sponsorTimes[i])
sponsorTimes[i].actionType === ActionType.Skip
) {
// Overlapping segment
latestEndTimeIndex = i;
}
}
// Keep going if required
if (latestEndTimeIndex !== index) {
latestEndTimeIndex = getLatestEndTimeIndex(
sponsorTimes,
latestEndTimeIndex,
hideHiddenSponsors
);
}
return latestEndTimeIndex;
};
/**
* Gets just the start times from a sponsor times array.
* Optionally specify a minimum
*
* @param sponsorTimes
* @param minimum
* @param hideHiddenSponsors
* @param includeIntersectingSegments If true, it will include segments that start before
* the current time, but end after
*/
const getStartTimes = (
sponsorTimes,
includeIntersectingSegments,
includeNonIntersectingSegments,
minimum = undefined,
onlySkippableSponsors = false,
hideHiddenSponsors = false
) => {
if (!sponsorTimes) return { includedTimes: [], scheduledTimes: [] };
const includedTimes = [];
const scheduledTimes = [];
const possibleTimes = sponsorTimes.map((sponsorTime) => ({
...sponsorTime,
scheduledTime: sponsorTime.segment[0],
}));
// Schedule at the end time to know when to unmute
sponsorTimes
.filter((sponsorTime) => sponsorTime.actionType === ActionType.Mute)
.forEach((sponsorTime) => {
if (
!possibleTimes.some(
(time) => sponsorTime.segment[1] === time.scheduledTime
)
) {
possibleTimes.push({
...sponsorTime,
scheduledTime: sponsorTime.segment[1],
});
}
});
for (let i = 0; i < possibleTimes.length; i++) {
if (
(minimum === undefined ||
(includeNonIntersectingSegments &&
possibleTimes[i].scheduledTime >= minimum) ||
(includeIntersectingSegments &&
possibleTimes[i].scheduledTime < minimum &&
possibleTimes[i].segment[1] > minimum)) &&
// (!onlySkippableSponsors || shouldSkip(possibleTimes[i])) &&
!hideHiddenSponsors && // ||
// possibleTimes[i].hidden === SponsorHideType.Visible) &&
possibleTimes[i].actionType !== ActionType.Poi
) {
scheduledTimes.push(possibleTimes[i].scheduledTime);
includedTimes.push(possibleTimes[i]);
}
}
return { includedTimes, scheduledTimes };
};
/**
* Returns info about the next upcoming sponsor skip
*/
const getNextSkipIndex = (
currentTime,
includeIntersectingSegments,
includeNonIntersectingSegments
) => {
const { includedTimes: submittedArray, scheduledTimes: sponsorStartTimes } =
getStartTimes(
sponsorTimes,
includeIntersectingSegments,
includeNonIntersectingSegments
);
const { scheduledTimes: sponsorStartTimesAfterCurrentTime } = getStartTimes(
sponsorTimes,
includeIntersectingSegments,
includeNonIntersectingSegments,
currentTime,
true,
false //true
);
const minSponsorTimeIndex = sponsorStartTimes.indexOf(
Math.min(...sponsorStartTimesAfterCurrentTime)
);
const endTimeIndex = getLatestEndTimeIndex(
submittedArray,
minSponsorTimeIndex
);
const {
includedTimes: unsubmittedArray,
scheduledTimes: unsubmittedSponsorStartTimes,
} = getStartTimes(
sponsorTimesSubmitting,
includeIntersectingSegments,
includeNonIntersectingSegments
);
const { scheduledTimes: unsubmittedSponsorStartTimesAfterCurrentTime } =
getStartTimes(
sponsorTimesSubmitting,
includeIntersectingSegments,
includeNonIntersectingSegments,
currentTime,
false,
false
);
const minUnsubmittedSponsorTimeIndex = unsubmittedSponsorStartTimes.indexOf(
Math.min(...unsubmittedSponsorStartTimesAfterCurrentTime)
);
const previewEndTimeIndex = getLatestEndTimeIndex(
unsubmittedArray,
minUnsubmittedSponsorTimeIndex
);
if (
(minUnsubmittedSponsorTimeIndex === -1 && minSponsorTimeIndex !== -1) ||
sponsorStartTimes[minSponsorTimeIndex] <
unsubmittedSponsorStartTimes[minUnsubmittedSponsorTimeIndex]
) {
return {
array: submittedArray,
index: minSponsorTimeIndex,
endIndex: endTimeIndex,
openNotice: true,
};
} else {
return {
array: unsubmittedArray,
index: minUnsubmittedSponsorTimeIndex,
endIndex: previewEndTimeIndex,
openNotice: false,
};
}
};
const startSponsorSchedule = (
includeIntersectingSegments = false,
currentTime = null,
includeNonIntersectingSegments = true
) => {
cancelSponsorSchedule();
// Don't skip if advert playing and reset last checked time
if (isAdPlaying) {
// Reset lastCheckVideoTime
lastCheckVideoTime = -1;
lastCheckTime = 0;
return;
}
if (!video || video.paused) return;
if (currentTime === undefined || currentTime === null) {
const virtualTime =
lastTimeFromWaitingEvent ??
(lastKnownVideoTime?.videoTime
? (performance.now() - lastKnownVideoTime.preciseTime) / 1000 +
lastKnownVideoTime.videoTime
: null);
if (
// (lastTimeFromWaitingEvent || !utils.isFirefox()) &&
!isSafari &&
virtualTime &&
Math.abs(virtualTime - video.currentTime) < 0.6
) {
currentTime = virtualTime;
} else {
currentTime = video.currentTime;
}
}
lastTimeFromWaitingEvent = null;
if (videoMuted && !inMuteSegment(currentTime)) {
video.muted = false;
videoMuted = false;
}
if (incorrectVideoCheck()) return;
const skipInfo = getNextSkipIndex(
currentTime,
includeIntersectingSegments,
includeNonIntersectingSegments
);
if (skipInfo.index === -1) return;
const currentSkip = skipInfo.array[skipInfo.index];
const skipTime = [
currentSkip.scheduledTime,
skipInfo.array[skipInfo.endIndex].segment[1],
];
const timeUntilSponsor = skipTime[0] - currentTime;
const videoID = sponsorVideoID;
// Find all indexes in between the start and end
let skippingSegments = [skipInfo.array[skipInfo.index]];
if (skipInfo.index !== skipInfo.endIndex) {
skippingSegments = [];
for (const segment of skipInfo.array) {
if (
// shouldAutoSkip(segment) &&
segment.segment[0] >= skipTime[0] &&
segment.segment[1] <= skipTime[1]
) {
skippingSegments.push(segment);
}
}
}
const skippingFunction = (forceVideoTime) => {
let forcedSkipTime = null;
let forcedIncludeIntersectingSegments = false;
let forcedIncludeNonIntersectingSegments = true;
if (incorrectVideoCheck(videoID, currentSkip)) return;
forceVideoTime ||= video.currentTime;
if (forceVideoTime >= skipTime[0] && forceVideoTime < skipTime[1]) {
skipToTime({
v: video,
skipTime,
skippingSegments,
// openNotice: skipInfo.openNotice,
});
if (
// utils.getCategorySelection(currentSkip.category)?.option ===
// CategorySkipOption.ManualSkip ||
currentSkip.actionType === ActionType.Mute
) {
forcedSkipTime = skipTime[0] + 0.001;
} else {
forcedSkipTime = skipTime[1];
forcedIncludeIntersectingSegments = true;
forcedIncludeNonIntersectingSegments = false;
}
}
startSponsorSchedule(
forcedIncludeIntersectingSegments,
forcedSkipTime,
forcedIncludeNonIntersectingSegments
);
};
if (timeUntilSponsor < 0.003) {
skippingFunction(currentTime);
} else {
const delayTime = timeUntilSponsor * 1000 * (1 / video.playbackRate);
if (delayTime < 300) {
// For Firefox, use interval instead of timeout near the end to combat imprecise video time
const startIntervalTime = performance.now();
const startVideoTime = Math.max(currentTime, video.currentTime);
currentSkipInterval = setInterval(() => {
const intervalDuration = performance.now() - startIntervalTime;
if (intervalDuration >= delayTime || video.currentTime >= skipTime[0]) {
clearInterval(currentSkipInterval);
if (!video.muted) {
// Workaround for more accurate skipping on Chromium
video.muted = true;
video.muted = false;
}
skippingFunction(
Math.max(
video.currentTime,
startVideoTime + (video.playbackRate * intervalDuration) / 1000
)
);
}
}, 1);
} else {
// Schedule for right before to be more precise than normal timeout
currentSkipSchedule = setTimeout(
skippingFunction,
Math.max(0, delayTime - 100)
);
}
}
};
/**
* Triggered every time the video duration changes.
* This happens when the resolution changes or at random time to clear memory.
*/
const durationChangeListener = () => {
updateAdFlag();
};
const setupVideoListeners = () => {
video.addEventListener("durationchange", durationChangeListener);
switchingVideos = false;
video.addEventListener("play", () => {
// If it is not the first event, then the only way to get to 0 is if there is a seek event
// This check makes sure that changing the video resolution doesn't cause the extension to think it
// gone back to the begining
if (!firstEvent && video.currentTime === 0) return;
firstEvent = false;
updateVirtualTime();
if (switchingVideos) {
switchingVideos = false;
// If already segments loaded before video, retry to skip starting segments
if (sponsorTimes) startSkipScheduleCheckingForStartSponsors();
}
// Check if an ad is playing
updateAdFlag();
// Make sure it doesn't get double called with the playing event
if (
Math.abs(lastCheckVideoTime - video.currentTime) > 0.3 ||
(lastCheckVideoTime !== video.currentTime &&
Date.now() - lastCheckTime > 2000)
) {
lastCheckTime = Date.now();
lastCheckVideoTime = video.currentTime;
startSponsorSchedule();
}
});
video.addEventListener("playing", () => {
updateVirtualTime();
// Make sure it doesn't get double called with the play event
if (
Math.abs(lastCheckVideoTime - video.currentTime) > 0.3 ||
(lastCheckVideoTime !== video.currentTime &&
Date.now() - lastCheckTime > 2000)
) {
lastCheckTime = Date.now();
lastCheckVideoTime = video.currentTime;
startSponsorSchedule();
}
});
video.addEventListener("seeking", () => {
if (!video.paused) {
// Reset lastCheckVideoTime
lastCheckTime = Date.now();
lastCheckVideoTime = video.currentTime;
updateVirtualTime();
lastTimeFromWaitingEvent = null;
startSponsorSchedule();
}
});
video.addEventListener("ratechange", () => startSponsorSchedule());
// Used by videospeed extension (https://github.com/igrigorik/videospeed/pull/740)
video.addEventListener("videoSpeed_ratechange", () => startSponsorSchedule());
const paused = () => {
// Reset lastCheckVideoTime
lastCheckVideoTime = -1;
lastCheckTime = 0;
lastKnownVideoTime = {
videoTime: null,
preciseTime: null,
};
lastTimeFromWaitingEvent = video.currentTime;
cancelSponsorSchedule();
};
video.addEventListener("pause", paused);
video.addEventListener("waiting", paused);
startSponsorSchedule();
};
const getYouTubeVideoID = (document) => {
const url = document.URL;
// clips should never skip, going from clip to full video has no indications.
if (url.includes("youtube.com/clip/")) return false;
// skip to document and don't hide if on /embed/
if (url.includes("/embed/") && url.includes("youtube.com"))
return getYouTubeVideoIDFromDocument(document, false);
// skip to URL if matches youtube watch or invidious or matches youtube pattern
if (
!url.includes("youtube.com") ||
url.includes("/watch") ||
url.includes("/shorts/") ||
url.includes("playlist")
)
return getYouTubeVideoIDFromURL(url);
// skip to document if matches pattern
if (
url.includes("/channel/") ||
url.includes("/user/") ||
url.includes("/c/")
)
return getYouTubeVideoIDFromDocument(document);
// not sure, try URL then document
return (
getYouTubeVideoIDFromURL(url) ||
getYouTubeVideoIDFromDocument(document, false)
);
};
const getYouTubeVideoIDFromDocument = (document, hideIcon = true) => {
// get ID from document (channel trailer / embedded playlist)
const videoURL = document
.querySelector('[data-sessionlink="feature=player-title"]')
?.getAttribute("href");
if (videoURL) {
return getYouTubeVideoIDFromURL(videoURL);
} else {
return false;
}
};
const getYouTubeVideoIDFromURL = (url) => {
if (url.startsWith("https://www.youtube.com/tv#/"))
url = url.replace("#", "");
//Attempt to parse url
let urlObject = null;
try {
urlObject = new URL(url);
} catch (e) {
console.error("[SB] Unable to parse URL: " + url);
return false;
}
// Check if valid hostname
if (
![
"m.youtube.com",
"www.youtube.com",
"www.youtube-nocookie.com",
"music.youtube.com",
].includes(urlObject.host)
) {
return false;
}
//Get ID from searchParam
if (
(urlObject.searchParams.has("v") &&
["/watch", "/watch/"].includes(urlObject.pathname)) ||
urlObject.pathname.startsWith("/tv/watch")
) {
const id = urlObject.searchParams.get("v");
return id.length == 11 ? id : false;
} else if (
urlObject.pathname.startsWith("/embed/") ||
urlObject.pathname.startsWith("/shorts/")
) {
try {
const id = urlObject.pathname.split("/")[2];
if (id?.length >= 11) return id.slice(0, 11);
} catch (e) {
console.error("[SB] Video ID not valid for " + url);
return false;
}
}
return false;
};
const resetValues = () => {
lastCheckTime = 0;
lastCheckVideoTime = -1;
//reset sponsor times
sponsorTimes = null;
sponsorSkipped = [];
if (switchingVideos === null) {
// When first loading a video, it is not switching videos
switchingVideos = false;
} else {
switchingVideos = true;
}
firstEvent = true;
// Reset advert playing flag
isAdPlaying = false;
};
const setupVideoMutationListener = () => {
const videoContainer = document.querySelector(".html5-video-container");
if (!videoContainer || videoMutationObserver !== null) return;
videoMutationObserver = new MutationObserver(refreshVideoAttachments);
videoMutationObserver.observe(videoContainer, {
attributes: true,
childList: true,
subtree: true,
});
};
const getHashParams = () => {
const windowHash = window.location.hash.slice(1);
if (windowHash) {
const params = windowHash.split("&").reduce((acc, param) => {
const [key, value] = param.split("=");
const decoded = decodeURIComponent(value);
try {
acc[key] = decoded?.match(/{|\[/) ? JSON.parse(decoded) : value;
} catch (e) {
console.error(`Failed to parse hash parameter ${key}: ${value}`);
}
return acc;
}, {});
return params;
}
return {};
};
const getHash = async (value, times = 5000) => {
if (times <= 0) return "";
let hashHex = value;
for (let i = 0; i < times; i++) {
const hashBuffer = await crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(hashHex).buffer
);
const hashArray = Array.from(new Uint8Array(hashBuffer));
hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}
return hashHex;
};
const urlTimeToSeconds = (time) => {
if (!time) {
return 0;
}
const re = /(?:(\d{1,3})h)?(?:(\d{1,2})m)?(\d+)s?/;
const match = re.exec(time);
if (match) {
const hours = parseInt(match[1] ?? "0", 10);
const minutes = parseInt(match[2] ?? "0", 10);
const seconds = parseInt(match[3] ?? "0", 10);
return hours * 3600 + minutes * 60 + seconds;
} else if (/\d+/.test(time)) {
return parseInt(time, 10);
}
};
const getStartTimeFromUrl = (url) => {
const urlParams = new URLSearchParams(url);
const time = urlParams?.get("t") || urlParams?.get("time_continue");
return urlTimeToSeconds(time);
};
/**
* Only should be used when it is okay to skip a sponsor when in the middle of it
*
* Ex. When segments are first loaded
*/
const startSkipScheduleCheckingForStartSponsors = () => {
// switchingVideos is ignored in Safari due to event fire order. See #1142
if ((!switchingVideos || isSafari) && sponsorTimes) {
// See if there are any starting sponsors
let startingSegmentTime = getStartTimeFromUrl(document.URL) || -1;
let found = false;
let startingSegment = null;
for (const time of sponsorTimes) {
if (
time.segment[0] <= video.currentTime &&
time.segment[0] > startingSegmentTime &&
time.segment[1] > video.currentTime &&
time.actionType !== ActionType.Poi
) {
startingSegmentTime = time.segment[0];
startingSegment = time;
found = true;
break;
}
}
if (!found) {
for (const time of sponsorTimesSubmitting) {
if (
time.segment[0] <= video.currentTime &&
time.segment[0] > startingSegmentTime &&
time.segment[1] > video.currentTime &&
time.actionType !== ActionType.Poi
) {
startingSegmentTime = time.segment[0];
startingSegment = time;
found = true;
break;
}
}
}
// For highlight category
const poiSegments = sponsorTimes
.filter(
(time) =>
time.segment[1] > video.currentTime &&
time.actionType === ActionType.Poi
)
.sort((a, b) => b.segment[0] - a.segment[0]);
for (const time of poiSegments) {
// const skipOption = utils.getCategorySelection(time.category)?.option;
// if (skipOption !== CategorySkipOption.ShowOverlay) {
skipToTime({
v: video,
skipTime: time.segment,
skippingSegments: [time],
// openNotice: true,
// unskipTime: video.currentTime,
});
// if (skipOption === CategorySkipOption.AutoSkip) break;
//}
}
if (false && startingSegmentTime !== -1) {
startSponsorSchedule(undefined, startingSegmentTime);
} else {
startSponsorSchedule();
}
}
};
const retryFetch = () => {
sponsorDataFound = false;
setTimeout(() => {
if (sponsorVideoID && sponsorTimes?.length === 0) {
sponsorsLookup();
}
}, 10000 + Math.random() * 30000);
};
const sponsorsLookup = async (keepOldSubmissions = true) => {
if (!video || !isVisible(video)) {
refreshVideoAttachments();
}
//there is still no video here
if (!video) {
setTimeout(() => sponsorsLookup(), 100);
return;
}
setupVideoMutationListener();
const response = await getVideoSegments();
if (response?.ok) {
const recievedSegments = (await response.json())
?.filter((video) => video.videoID === sponsorVideoID)
?.map((video) => video.segments)[0];
if (!recievedSegments || !recievedSegments.length) {
// return if no video found
retryFetch();
return;
}
sponsorDataFound = true;
// Check if any old submissions should be kept
if (sponsorTimes !== null && keepOldSubmissions) {
for (let i = 0; i < sponsorTimes.length; i++) {
if (sponsorTimes[i].source === SponsorSourceType.Local) {
// This is a user submission, keep it
recievedSegments.push(sponsorTimes[i]);