-
Notifications
You must be signed in to change notification settings - Fork 2
/
youtube_sponsorblock.js
51 lines (48 loc) · 1.85 KB
/
youtube_sponsorblock.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
// ==UserScript==
// @name Sponsorblock
// @version 1.1.0
// @description Skip sponsor segments automatically
// @author afreakk
// @author vongaisberg
// @match *://*.youtube.com/*
// @exclude *://*.youtube.com/subscribe_embed?*
// ==/UserScript==
const delay = 1000;
const tryFetchSkipSegments = (videoID) =>
fetch(`https://sponsor.ajay.app/api/skipSegments?videoID=${videoID}`)
.then((r) => r.json())
.then((rJson) =>
rJson.filter((a) => a.actionType === 'skip').map((a) => a.segment)
)
.catch(
(e) =>
console.log(
`Sponsorblock: failed fetching skipSegments for ${videoID}, reason: ${e}`
) || []
);
const skipSegments = async () => {
const videoID = new URL(document.location).searchParams.get('v');
if (!videoID) {
return;
}
const key = `segmentsToSkip-${videoID}`;
window[key] = window[key] || (await tryFetchSkipSegments(videoID));
for (const v of document.querySelectorAll('video')) {
if (Number.isNaN(v.duration)) continue;
for (const [start, end] of window[key]) {
if (v.currentTime < end && v.currentTime >= start) {
console.log(`Sponsorblock: skipped video @${v.currentTime} from ${start} to ${end}`);
v.currentTime = end;
return
}
const timeToSponsor = (start - v.currentTime) / v.playbackRate;
if (v.currentTime < start && timeToSponsor < (delay / 1000)) {
console.log(`Sponsorblock: Almost at sponsor segment, sleep for ${timeToSponsor * 1000}ms`);
setTimeout(skipSegments, timeToSponsor * 1000);
}
}
}
};
if (!window.skipSegmentsIntervalID) {
window.skipSegmentsIntervalID = setInterval(skipSegments, delay);
}