Merge branch 'master' into lastfm

This commit is contained in:
semvis123
2021-04-03 17:21:56 +02:00
committed by GitHub
24 changed files with 577 additions and 175 deletions

View File

@ -1,22 +1,32 @@
const { ipcRenderer } = require("electron");
const { getImage } = require("./song-info");
global.songInfo = {};
ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
global.songInfo = JSON.parse(extractedSongInfo);
global.songInfo.image = await getImage(global.songInfo.imageSrc);
});
const injectListener = () => {
var oldXHR = window.XMLHttpRequest;
function newXHR() {
var realXHR = new oldXHR();
realXHR.addEventListener("readystatechange", () => {
if(realXHR.readyState==4 && realXHR.status==200){
if (realXHR.responseURL.includes('/player')){
// if the request is the contains the song info send the response to ipcMain
ipcRenderer.send(
"song-info-request",
realXHR.responseText
);
realXHR.addEventListener(
"readystatechange",
() => {
if (realXHR.readyState == 4 && realXHR.status == 200) {
if (realXHR.responseURL.includes("/player")) {
// if the request contains the song info, send the response to ipcMain
ipcRenderer.send("song-info-request", realXHR.responseText);
}
}
}
}, false);
},
false
);
return realXHR;
}
window.XMLHttpRequest = newXHR;
}
};
module.exports = injectListener;

View File

@ -1,5 +1,4 @@
const { ipcMain } = require("electron");
const { nativeImage } = require("electron");
const { ipcMain, nativeImage } = require("electron");
const fetch = require("node-fetch");
@ -38,21 +37,25 @@ const songInfo = {
uploadDate: "",
imageSrc: "",
image: null,
isPaused: true,
isPaused: undefined,
songDuration: 0,
elapsedSeconds: 0,
url: "",
};
const handleData = async (_event, responseText) => {
data = JSON.parse(responseText);
const handleData = async (responseText, win) => {
let data = JSON.parse(responseText);
songInfo.title = data?.videoDetails?.title;
songInfo.artist = data?.videoDetails?.author;
songInfo.views = data?.videoDetails?.viewCount;
songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.[0]?.url;
songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url;
songInfo.songDuration = data?.videoDetails?.lengthSeconds;
songInfo.image = await getImage(songInfo.imageSrc);
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
}
songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical;
win.webContents.send("update-song-info", JSON.stringify(songInfo));
};
const registerProvider = (win) => {
// This variable will be filled with the callbacks once they register
@ -77,9 +80,15 @@ const registerProvider = (win) => {
});
// This will be called when the song-info-front finds a new request with song data
ipcMain.on('song-info-request', handleData);
ipcMain.on("song-info-request", async (_, responseText) => {
await handleData(responseText, win);
callbacks.forEach((c) => {
c(songInfo);
});
});
return registerCallback;
};
module.exports = registerProvider;
module.exports.getImage = getImage;