fix sponsorblock plugin

This commit is contained in:
Araxeus
2021-10-30 12:43:51 +03:00
parent 62e8e673eb
commit 286bc0113e
4 changed files with 29 additions and 38 deletions

View File

@ -1,5 +1,5 @@
module.exports = () => { module.exports = () => {
document.addEventListener('apiLoaded', e => { document.addEventListener('apiLoaded', () => {
document.querySelector('video').addEventListener('loadeddata', e => { document.querySelector('video').addEventListener('loadeddata', e => {
e.target.pause(); e.target.pause();
}) })

View File

@ -1,4 +1,12 @@
const fetch = require("node-fetch"); const fetch = require("node-fetch");
const is = require("electron-is");
// see https://github.com/node-fetch/node-fetch/issues/568#issuecomment-932200523
// will not be needed if project's electron version >= v15.1.0 (https://github.com/node-fetch/node-fetch/issues/568#issuecomment-932435180)
const https = require("https");
const agent = new https.Agent({
rejectUnauthorized: false,
});
const defaultConfig = require("../../config/defaults"); const defaultConfig = require("../../config/defaults");
const registerCallback = require("../../providers/song-info"); const registerCallback = require("../../providers/song-info");
@ -24,6 +32,7 @@ module.exports = (win, options) => {
}); });
}; };
const fetchSegments = async (apiURL, categories) => { const fetchSegments = async (apiURL, categories) => {
const sponsorBlockURL = `${apiURL}/api/skipSegments?videoID=${videoID}&categories=${JSON.stringify( const sponsorBlockURL = `${apiURL}/api/skipSegments?videoID=${videoID}&categories=${JSON.stringify(
categories categories
@ -35,6 +44,7 @@ const fetchSegments = async (apiURL, categories) => {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
redirect: "follow", redirect: "follow",
agent // fixes error: 'CERT_HAS_EXPIRED'
}); });
if (resp.status !== 200) { if (resp.status !== 200) {
return []; return [];
@ -45,7 +55,10 @@ const fetchSegments = async (apiURL, categories) => {
); );
return sortedSegments; return sortedSegments;
} catch { } catch (e) {
if (is.dev()) {
console.log('error on sponsorblock request:', e);
}
return []; return [];
} }
}; };

View File

@ -2,8 +2,6 @@ const { ipcRenderer } = require("electron");
const is = require("electron-is"); const is = require("electron-is");
const { ontimeupdate } = require("../../providers/video-element");
let currentSegments = []; let currentSegments = [];
module.exports = () => { module.exports = () => {
@ -11,17 +9,19 @@ module.exports = () => {
currentSegments = segments; currentSegments = segments;
}); });
ontimeupdate((videoElement) => { document.addEventListener('apiLoaded', () => {
currentSegments.forEach((segment) => { document.querySelector('video').addEventListener('timeupdate', e => {
if ( currentSegments.forEach((segment) => {
videoElement.currentTime >= segment[0] && if (
videoElement.currentTime <= segment[1] e.target.currentTime >= segment[0] &&
) { e.target.currentTime <= segment[1]
videoElement.currentTime = segment[1]; ) {
if (is.dev()) { e.target.currentTime = segment[1];
console.log("SponsorBlock: skipping segment", segment); if (is.dev()) {
console.log("SponsorBlock: skipping segment", segment);
}
} }
} });
}); })
}); }, { once: true, passive: true})
}; };

View File

@ -1,22 +0,0 @@
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);
}
};