From 51364b63e7b7cb9cc2b33266bba9b2852b769dfe Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sat, 23 Oct 2021 16:13:06 +0300 Subject: [PATCH] get songInfo from youtube API --- providers/song-info-front.js | 42 ++++++++++++++++++++---------------- providers/song-info.js | 39 ++++++++++++--------------------- 2 files changed, 37 insertions(+), 44 deletions(-) diff --git a/providers/song-info-front.js b/providers/song-info-front.js index fccc54a8..25cea261 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -9,23 +9,27 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => { global.songInfo.image = await getImage(global.songInfo.imageSrc); }); -const injectListener = () => { - const oldXHR = window.XMLHttpRequest; - function newXHR() { - const realXHR = new oldXHR(); - realXHR.addEventListener( - "readystatechange", - () => { - if (realXHR.readyState === 4 && realXHR.status === 200 - && realXHR.responseURL.includes("/player")) { - // if the request contains the song info, send the response to ipcMain - ipcRenderer.send("song-info-request", realXHR.responseText); - } - }, - false - ); - return realXHR; - } - window.XMLHttpRequest = newXHR; +function setup() { + if (document.querySelector('#movie_player')) { + injectListener(); + return; + } + + const observer = new MutationObserver(() => { + if (document.querySelector('#movie_player')) { + observer.disconnect(); + injectListener(); + } + }) + + observer.observe(document.documentElement, { childList: true, subtree: true }); +} + +function injectListener() { + document.querySelector('video').addEventListener('loadedmetadata', () => { + const data = document.querySelector('#movie_player').getPlayerResponse(); + ipcRenderer.send("song-info-request", JSON.stringify(data)); + }); }; -module.exports = injectListener; + +module.exports = setup; diff --git a/providers/song-info.js b/providers/song-info.js index 6e5a119c..d386e23a 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -2,15 +2,11 @@ const { ipcMain, nativeImage } = require("electron"); const fetch = require("node-fetch"); -// This selects the progress bar, used for current progress -const progressSelector = "#progress-bar"; - - // Grab the progress using the selector const getProgress = async (win) => { // Get current value of the progressbar element return win.webContents.executeJavaScript( - 'document.querySelector("' + progressSelector + '").value' + 'document.querySelector("#progress-bar").value' ); }; @@ -29,16 +25,10 @@ const getImage = async (src) => { // To find the paused status, we check if the title contains `-` const getPausedStatus = async (win) => { const title = await win.webContents.executeJavaScript("document.title"); + console.log('doc title = ',title) return !title.includes("-"); }; -const getArtist = async (win) => { - return win.webContents.executeJavaScript(` - document.querySelector(".subtitle.ytmusic-player-bar .yt-formatted-string") - ?.textContent - `); -} - // Fill songInfo with empty values /** * @typedef {songInfo} SongInfo @@ -59,14 +49,13 @@ const songInfo = { const handleData = async (responseText, win) => { let data = JSON.parse(responseText); songInfo.title = cleanupName(data?.videoDetails?.title); - songInfo.artist = - (await getArtist(win)) || cleanupName(data?.videoDetails?.author); + songInfo.artist =cleanupName(data?.videoDetails?.author); songInfo.views = data?.videoDetails?.viewCount; 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; + songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical?.split("&")[0]; win.webContents.send("update-song-info", JSON.stringify(songInfo)); }; @@ -111,19 +100,19 @@ const registerProvider = (win) => { }; const suffixesToRemove = [ - " - Topic", - "VEVO", - " (Performance Video)", - " (Official Music Video)", - " (Official Video)", - " (Clip officiel)", + " - topic", + "vevo", + " (performance video)", + " (official music video)", + " (official video)", + " (clip officiel)", ]; + function cleanupName(artist) { - if (!artist) { - return artist; - } + if (!artist) return artist; + const lowerCaseArtist = artist.toLowerCase(); for (const suffix of suffixesToRemove) { - if (artist.endsWith(suffix)) { + if (lowerCaseArtist.endsWith(suffix)) { return artist.slice(0, -suffix.length); } }