-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into chromecast
- Loading branch information
Showing
12 changed files
with
229 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,10 @@ | ||
let videoElement = null; | ||
|
||
const observer = new MutationObserver((mutations, observer) => { | ||
if (!videoElement) { | ||
videoElement = document.querySelector("video"); | ||
} | ||
|
||
if (videoElement) { | ||
videoElement.ontimeupdate = () => { | ||
if (videoElement.currentTime === 0 && videoElement.duration !== NaN) { | ||
// auto-confirm-when-paused plugin can interfere here if not disabled! | ||
videoElement.pause(); | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
function observeVideoElement() { | ||
observer.observe(document, { | ||
childList: true, | ||
subtree: true, | ||
const { ontimeupdate } = require("../../providers/video-element"); | ||
|
||
module.exports = () => { | ||
ontimeupdate((videoElement) => { | ||
if (videoElement.currentTime === 0 && videoElement.duration !== NaN) { | ||
// auto-confirm-when-paused plugin can interfere here if not disabled! | ||
videoElement.pause(); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = observeVideoElement; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const fetch = require("node-fetch"); | ||
|
||
const defaultConfig = require("../../config/defaults"); | ||
const registerCallback = require("../../providers/song-info"); | ||
const { sortSegments } = require("./segments"); | ||
|
||
let videoID; | ||
|
||
module.exports = (win, options) => { | ||
const { apiURL, categories } = { | ||
...defaultConfig.plugins.sponsorblock, | ||
...options, | ||
}; | ||
|
||
registerCallback(async (info) => { | ||
const newURL = info.url || win.webContents.getURL(); | ||
const newVideoID = new URL(newURL).searchParams.get("v"); | ||
|
||
if (videoID !== newVideoID) { | ||
videoID = newVideoID; | ||
const segments = await fetchSegments(apiURL, categories); | ||
win.webContents.send("sponsorblock-skip", segments); | ||
} | ||
}); | ||
}; | ||
|
||
const fetchSegments = async (apiURL, categories) => { | ||
const sponsorBlockURL = `${apiURL}/api/skipSegments?videoID=${videoID}&categories=${JSON.stringify( | ||
categories | ||
)}`; | ||
try { | ||
const resp = await fetch(sponsorBlockURL, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
redirect: "follow", | ||
}); | ||
if (resp.status !== 200) { | ||
return []; | ||
} | ||
const segments = await resp.json(); | ||
const sortedSegments = sortSegments( | ||
segments.map((submission) => submission.segment) | ||
); | ||
|
||
return sortedSegments; | ||
} catch { | ||
return []; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { ipcRenderer } = require("electron"); | ||
|
||
const is = require("electron-is"); | ||
|
||
const { ontimeupdate } = require("../../providers/video-element"); | ||
|
||
let currentSegments = []; | ||
|
||
module.exports = () => { | ||
ipcRenderer.on("sponsorblock-skip", (_, segments) => { | ||
currentSegments = segments; | ||
}); | ||
|
||
ontimeupdate((videoElement) => { | ||
currentSegments.forEach((segment) => { | ||
if ( | ||
videoElement.currentTime >= segment[0] && | ||
videoElement.currentTime <= segment[1] | ||
) { | ||
videoElement.currentTime = segment[1]; | ||
if (is.dev()) { | ||
console.log("SponsorBlock: skipping segment", segment); | ||
} | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Segments are an array [ [start, end], … ] | ||
module.exports.sortSegments = (segments) => { | ||
segments.sort((segment1, segment2) => | ||
segment1[0] === segment2[0] | ||
? segment1[1] - segment2[1] | ||
: segment1[0] - segment2[0] | ||
); | ||
|
||
const compiledSegments = []; | ||
let currentSegment; | ||
|
||
segments.forEach((segment) => { | ||
if (!currentSegment) { | ||
currentSegment = segment; | ||
return; | ||
} | ||
|
||
if (currentSegment[1] < segment[0]) { | ||
compiledSegments.push(currentSegment); | ||
currentSegment = segment; | ||
return; | ||
} | ||
|
||
currentSegment[1] = Math.max(currentSegment[1], segment[1]); | ||
}); | ||
compiledSegments.push(currentSegment); | ||
|
||
return compiledSegments; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { sortSegments } = require("../segments"); | ||
|
||
test("Segment sorting", () => { | ||
expect( | ||
sortSegments([ | ||
[0, 3], | ||
[7, 8], | ||
[5, 6], | ||
]) | ||
).toEqual([ | ||
[0, 3], | ||
[5, 6], | ||
[7, 8], | ||
]); | ||
|
||
expect( | ||
sortSegments([ | ||
[0, 5], | ||
[6, 8], | ||
[4, 6], | ||
]) | ||
).toEqual([[0, 8]]); | ||
|
||
expect( | ||
sortSegments([ | ||
[0, 6], | ||
[7, 8], | ||
[4, 6], | ||
]) | ||
).toEqual([ | ||
[0, 6], | ||
[7, 8], | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
let videoElement = null; | ||
|
||
module.exports.ontimeupdate = (cb) => { | ||
const observer = new MutationObserver((mutations, observer) => { | ||
if (!videoElement) { | ||
videoElement = document.querySelector("video"); | ||
if (videoElement) { | ||
observer.disconnect(); | ||
videoElement.ontimeupdate = () => cb(videoElement); | ||
} | ||
} | ||
}); | ||
|
||
if (!videoElement) { | ||
observer.observe(document, { | ||
childList: true, | ||
subtree: true, | ||
}); | ||
} else { | ||
videoElement.ontimeupdate = () => cb(videoElement); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters