Skip to content

Commit 6726e26

Browse files
committed
rework songInfo pause listener
1 parent cfe719b commit 6726e26

File tree

3 files changed

+54
-58
lines changed

3 files changed

+54
-58
lines changed

plugins/sponsorblock/back.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (win, options) => {
1313
...options,
1414
};
1515

16-
ipcMain.on("song-info-request", async (_, data) => {
16+
ipcMain.on("video-src-changed", async (_, data) => {
1717
videoID = JSON.parse(data)?.videoDetails?.videoId;
1818
const segments = await fetchSegments(apiURL, categories);
1919
win.webContents.send("sponsorblock-skip", segments);

providers/song-info-front.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ module.exports = () => {
1616
document.addEventListener('apiLoaded', apiEvent => {
1717
const video = document.querySelector('video');
1818
// name = "dataloaded" and abit later "dataupdated"
19-
apiEvent.detail.addEventListener('videodatachange', (name, dataEvent) => {
19+
apiEvent.detail.addEventListener('videodatachange', (name, _dataEvent) => {
2020
if (name !== 'dataloaded') return;
2121
video.dispatchEvent(srcChangedEvent);
22-
ipcRenderer.send("song-info-request", JSON.stringify(dataEvent.playerResponse));
22+
ipcRenderer.send("video-src-changed", JSON.stringify(apiEvent.detail.getPlayerResponse()));
2323
})
24-
24+
for (const status of ['playing', 'pause']) {
25+
video.addEventListener(status, sendSongInfo);
26+
}
27+
function sendSongInfo() {
28+
const data = apiEvent.detail.getPlayerResponse();
29+
data.videoDetails.elapsedSeconds = Math.floor(video.currentTime);
30+
data.videoDetails.isPaused = video.paused;
31+
ipcRenderer.send("song-info-request", JSON.stringify(apiEvent.detail.getPlayerResponse()));
32+
}
2533
}, { once: true, passive: true });
2634
};

providers/song-info.js

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,6 @@ const fetch = require("node-fetch");
44

55
const config = require("../config");
66

7-
// Grab the progress using the selector
8-
const getProgress = async (win) => {
9-
// Get current value of the progressbar element
10-
return win.webContents.executeJavaScript(
11-
'document.querySelector("#progress-bar").value'
12-
);
13-
};
14-
15-
// Grab the native image using the src
16-
const getImage = async (src) => {
17-
const result = await fetch(src);
18-
const buffer = await result.buffer();
19-
const output = nativeImage.createFromBuffer(buffer);
20-
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
21-
return getImage(src.slice(0, src.lastIndexOf(".jpg")+4));
22-
} else {
23-
return output;
24-
}
25-
};
26-
27-
// To find the paused status, we check if the title contains `-`
28-
const getPausedStatus = async (win) => {
29-
const title = await win.webContents.executeJavaScript("document.title");
30-
return !title.includes("-");
31-
};
32-
337
// Fill songInfo with empty values
348
/**
359
* @typedef {songInfo} SongInfo
@@ -47,21 +21,48 @@ const songInfo = {
4721
url: "",
4822
};
4923

24+
// Grab the native image using the src
25+
const getImage = async (src) => {
26+
const result = await fetch(src);
27+
const buffer = await result.buffer();
28+
const output = nativeImage.createFromBuffer(buffer);
29+
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
30+
return getImage(src.slice(0, src.lastIndexOf(".jpg") + 4));
31+
} else {
32+
return output;
33+
}
34+
};
35+
5036
const handleData = async (responseText, win) => {
51-
let data = JSON.parse(responseText);
52-
songInfo.title = cleanupName(data?.videoDetails?.title);
53-
songInfo.artist =cleanupName(data?.videoDetails?.author);
54-
songInfo.views = data?.videoDetails?.viewCount;
55-
songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url.split("?")[0];
56-
songInfo.songDuration = data?.videoDetails?.lengthSeconds;
57-
songInfo.image = await getImage(songInfo.imageSrc);
58-
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
59-
songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical?.split("&")[0];
60-
61-
// used for options.resumeOnStart
62-
config.set("url", data?.microformat?.microformatDataRenderer?.urlCanonical);
63-
64-
win.webContents.send("update-song-info", JSON.stringify(songInfo));
37+
const data = JSON.parse(responseText);
38+
if (!data) return;
39+
40+
const microformat = data.microformat?.microformatDataRenderer;
41+
if (microformat) {
42+
songInfo.uploadDate = microformat.uploadDate;
43+
songInfo.url = microformat.urlCanonical?.split("&")[0];
44+
45+
// used for options.resumeOnStart
46+
config.set("url", microformat.urlCanonical);
47+
}
48+
49+
const videoDetails = data.videoDetails;
50+
if (videoDetails) {
51+
songInfo.title = cleanupName(videoDetails.title);
52+
songInfo.artist = cleanupName(videoDetails.author);
53+
songInfo.views = videoDetails.viewCount;
54+
songInfo.songDuration = videoDetails.lengthSeconds;
55+
songInfo.elapsedSeconds = videoDetails.elapsedSeconds;
56+
songInfo.isPaused = videoDetails.isPaused;
57+
58+
const oldUrl = songInfo.imageSrc;
59+
songInfo.imageSrc = videoDetails.thumbnail?.thumbnails?.pop()?.url.split("?")[0];
60+
if (oldUrl !== songInfo.imageSrc) {
61+
songInfo.image = await getImage(songInfo.imageSrc);
62+
}
63+
64+
win.webContents.send("update-song-info", JSON.stringify(songInfo));
65+
}
6566
};
6667

6768
// This variable will be filled with the callbacks once they register
@@ -81,19 +82,6 @@ const registerCallback = (callback) => {
8182
};
8283

8384
const registerProvider = (win) => {
84-
win.on("page-title-updated", async () => {
85-
// Get and set the new data
86-
songInfo.isPaused = await getPausedStatus(win);
87-
88-
const elapsedSeconds = await getProgress(win);
89-
songInfo.elapsedSeconds = elapsedSeconds;
90-
91-
// Trigger the callbacks
92-
callbacks.forEach((c) => {
93-
c(songInfo);
94-
});
95-
});
96-
9785
// This will be called when the song-info-front finds a new request with song data
9886
ipcMain.on("song-info-request", async (_, responseText) => {
9987
await handleData(responseText, win);
@@ -114,7 +102,7 @@ const suffixesToRemove = [
114102

115103
function cleanupName(name) {
116104
if (!name) return name;
117-
const lowCaseName = name.toLowerCase();
105+
const lowCaseName = name.toLowerCase();
118106
for (const suffix of suffixesToRemove) {
119107
if (lowCaseName.endsWith(suffix)) {
120108
return name.slice(0, -suffix.length);

0 commit comments

Comments
 (0)