mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-09 17:51:46 +00:00
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
const { ipcMain } = require('electron');
|
|
const fetch = require('node-fetch');
|
|
const is = require('electron-is');
|
|
|
|
const { sortSegments } = require('./segments');
|
|
|
|
const defaultConfig = require('../../config/defaults');
|
|
|
|
let videoID;
|
|
|
|
module.exports = (win, options) => {
|
|
const { apiURL, categories } = {
|
|
...defaultConfig.plugins.sponsorblock,
|
|
...options,
|
|
};
|
|
|
|
ipcMain.on('video-src-changed', async (_, data) => {
|
|
videoID = JSON.parse(data)?.videoDetails?.videoId;
|
|
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 (error) {
|
|
if (is.dev()) {
|
|
console.log('error on sponsorblock request:', error);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
};
|