From 65ce62adc1077a0fd36d2a7e49df567e43ca2595 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Tue, 9 Nov 2021 13:29:41 +0200 Subject: [PATCH 1/9] use $('video') srcChanged event instead of loadeddata/metadata --- plugins/disable-autoplay/front.js | 2 +- plugins/playback-speed/front.js | 2 +- plugins/video-toggle/front.js | 6 +++--- providers/song-info-front.js | 24 ++++++++++++++++++------ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/plugins/disable-autoplay/front.js b/plugins/disable-autoplay/front.js index 532405de..615b978a 100644 --- a/plugins/disable-autoplay/front.js +++ b/plugins/disable-autoplay/front.js @@ -1,6 +1,6 @@ module.exports = () => { document.addEventListener('apiLoaded', () => { - document.querySelector('video').addEventListener('loadeddata', e => { + document.querySelector('video').addEventListener('srcChanged', e => { e.target.pause(); }) }, { once: true, passive: true }) diff --git a/plugins/playback-speed/front.js b/plugins/playback-speed/front.js index f1ea78e8..c5e43064 100644 --- a/plugins/playback-speed/front.js +++ b/plugins/playback-speed/front.js @@ -49,7 +49,7 @@ const observePopupContainer = () => { const observeVideo = () => { $('video').addEventListener('ratechange', forcePlaybackRate) - $('video').addEventListener('loadeddata', forcePlaybackRate) + $('video').addEventListener('srcChanged', forcePlaybackRate) } const setupWheelListener = () => { diff --git a/plugins/video-toggle/front.js b/plugins/video-toggle/front.js index 588642a0..2fff595f 100644 --- a/plugins/video-toggle/front.js +++ b/plugins/video-toggle/front.js @@ -35,13 +35,13 @@ function setup() { setOptions("video-toggle", options); }) - $('video').addEventListener('loadedmetadata', videoStarted); + $('video').addEventListener('srcChanged', videoStarted); } function changeDisplay(showVideo) { - if (!showVideo && $('ytmusic-player').getAttribute('playback-mode') !== "ATV_PREFERRED") { + if (!showVideo) { $('video').style.top = "0"; - $('ytmusic-player').style.margin = "auto 21.5px"; + $('ytmusic-player').style.margin = "auto 0px"; $('ytmusic-player').setAttribute('playback-mode', "ATV_PREFERRED"); } diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 4ff6041d..bbbd981e 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -10,10 +10,22 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => { }); module.exports = () => { - document.addEventListener('apiLoaded', e => { - document.querySelector('video').addEventListener('loadedmetadata', () => { - const data = e.detail.getPlayerResponse(); - ipcRenderer.send("song-info-request", JSON.stringify(data)); - }); - }, { once: true, passive: true }) + document.addEventListener('apiLoaded', e => observeSrcChange(e.detail), { once: true, passive: true }); }; + +// used because 'loadeddata' or 'loadedmetadata' weren't firing on song start for some users (https://github.com/th-ch/youtube-music/issues/473) +function observeSrcChange(api) { + const srcChangedEvent = new CustomEvent('srcChanged'); + + const video = document.querySelector('video'); + + const playbackModeObserver = new MutationObserver((mutations) => { + mutations.forEach(mutation => { + if (mutation.target.src) { // in first mutation src is usually an empty string (loading) + video.dispatchEvent(srcChangedEvent); + ipcRenderer.send("song-info-request", JSON.stringify(api.getPlayerResponse())); + } + }) + }); + playbackModeObserver.observe(video, { attributeFilter: ["src"] }) +} From 895136af0ad1f4423c7a9bfab11c4d8ac523fcf7 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Tue, 9 Nov 2021 16:08:40 +0200 Subject: [PATCH 2/9] used youtube's videodatachange event --- plugins/disable-autoplay/front.js | 6 ++--- plugins/video-toggle/front.js | 38 +++++++++++++++++++++++-------- providers/song-info-front.js | 29 ++++++++++------------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/plugins/disable-autoplay/front.js b/plugins/disable-autoplay/front.js index 615b978a..11e78ebc 100644 --- a/plugins/disable-autoplay/front.js +++ b/plugins/disable-autoplay/front.js @@ -1,7 +1,7 @@ module.exports = () => { - document.addEventListener('apiLoaded', () => { - document.querySelector('video').addEventListener('srcChanged', e => { - e.target.pause(); + document.addEventListener('apiLoaded', e => { + document.querySelector('video').addEventListener('srcChanged', () => { + e.detail.pauseVideo(); }) }, { once: true, passive: true }) }; diff --git a/plugins/video-toggle/front.js b/plugins/video-toggle/front.js index 2fff595f..5adb7ab0 100644 --- a/plugins/video-toggle/front.js +++ b/plugins/video-toggle/front.js @@ -6,6 +6,8 @@ function $(selector) { return document.querySelector(selector); } let options; +let api; + const switchButtonDiv = ElementFromFile( templatePath(__dirname, "button_template.html") ); @@ -17,7 +19,9 @@ module.exports = (_options) => { document.addEventListener('apiLoaded', setup, { once: true, passive: true }); } -function setup() { +function setup(e) { + api = e.detail; + $('ytmusic-player-page').prepend(switchButtonDiv); $('#song-image.ytmusic-player').style.display = "block" @@ -36,6 +40,8 @@ function setup() { }) $('video').addEventListener('srcChanged', videoStarted); + + observeThumbnail(); } function changeDisplay(showVideo) { @@ -51,11 +57,8 @@ function changeDisplay(showVideo) { } function videoStarted() { - if (videoExist()) { - const thumbnails = $('#movie_player').getPlayerResponse()?.videoDetails?.thumbnail?.thumbnails; - if (thumbnails && thumbnails.length > 0) { - $('#song-image img').src = thumbnails[thumbnails.length-1].url; - } + if (api.getPlayerResponse().videoDetails.musicVideoType === 'MUSIC_VIDEO_TYPE_OMV') { // or `$('#player').videoMode_` + forceThumbnail($('#song-image img')); switchButtonDiv.style.display = "initial"; if (!options.hideVideo && $('#song-video.ytmusic-player').style.display === "none") { changeDisplay(true); @@ -66,10 +69,6 @@ function videoStarted() { } } -function videoExist() { - return $('#player').videoMode_; -} - // on load, after a delay, the page overrides the playback-mode to 'OMV_PREFERRED' which causes weird aspect ratio in the image container // this function fix the problem by overriding that override :) function forcePlaybackMode() { @@ -83,3 +82,22 @@ function forcePlaybackMode() { }); playbackModeObserver.observe($('ytmusic-player'), { attributeFilter: ["playback-mode"] }) } + +function observeThumbnail() { + const playbackModeObserver = new MutationObserver(mutations => { + if (!$('#player').videoMode_) return; + + mutations.forEach(mutation => { + if (!mutation.target.src.startsWith('data:')) return; + forceThumbnail(mutation.target) + }); + }); + playbackModeObserver.observe($('#song-image img'), { attributeFilter: ["src"] }) +} + +function forceThumbnail(img) { + const thumbnails = $('#movie_player').getPlayerResponse()?.videoDetails?.thumbnail?.thumbnails; + if (thumbnails && thumbnails.length > 0) { + img.src = thumbnails[thumbnails.length - 1].url; + } +} diff --git a/providers/song-info-front.js b/providers/song-info-front.js index bbbd981e..072d7a53 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -9,23 +9,18 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => { global.songInfo.image = await getImage(global.songInfo.imageSrc); }); -module.exports = () => { - document.addEventListener('apiLoaded', e => observeSrcChange(e.detail), { once: true, passive: true }); -}; - // used because 'loadeddata' or 'loadedmetadata' weren't firing on song start for some users (https://github.com/th-ch/youtube-music/issues/473) -function observeSrcChange(api) { - const srcChangedEvent = new CustomEvent('srcChanged'); +const srcChangedEvent = new CustomEvent('srcChanged'); - const video = document.querySelector('video'); - - const playbackModeObserver = new MutationObserver((mutations) => { - mutations.forEach(mutation => { - if (mutation.target.src) { // in first mutation src is usually an empty string (loading) - video.dispatchEvent(srcChangedEvent); - ipcRenderer.send("song-info-request", JSON.stringify(api.getPlayerResponse())); - } +module.exports = () => { + document.addEventListener('apiLoaded', apiEvent => { + const video = document.querySelector('video'); + // name = "dataloaded" and abit later "dataupdated" + apiEvent.detail.addEventListener('videodatachange', (name, dataEvent) => { + if (name !== 'dataloaded') return; + ipcRenderer.send("song-info-request", JSON.stringify(dataEvent.playerResponse)); + video.dispatchEvent(srcChangedEvent); }) - }); - playbackModeObserver.observe(video, { attributeFilter: ["src"] }) -} + + }, { once: true, passive: true }); +}; From 6dbed73e6be8e10029faad3e702d9bc1a62d6b0a Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Tue, 9 Nov 2021 18:15:26 +0200 Subject: [PATCH 3/9] fix disable autoplay --- plugins/disable-autoplay/front.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/plugins/disable-autoplay/front.js b/plugins/disable-autoplay/front.js index 11e78ebc..c34a453a 100644 --- a/plugins/disable-autoplay/front.js +++ b/plugins/disable-autoplay/front.js @@ -1,7 +1,14 @@ module.exports = () => { - document.addEventListener('apiLoaded', e => { - document.querySelector('video').addEventListener('srcChanged', () => { - e.detail.pauseVideo(); + document.addEventListener('apiLoaded', apiEvent => { + apiEvent.detail.addEventListener('videodatachange', name => { + if (name === 'dataloaded') { + apiEvent.detail.pauseVideo(); + document.querySelector('video').ontimeupdate = e => { + e.target.pause(); + } + } else { + document.querySelector('video').ontimeupdate = null; + } }) }, { once: true, passive: true }) }; From 08fdd07969d2090d20ab8c5358e649fe86faec2d Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Wed, 10 Nov 2021 20:44:13 +0200 Subject: [PATCH 4/9] speed up sponsorblock --- plugins/sponsorblock/back.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/plugins/sponsorblock/back.js b/plugins/sponsorblock/back.js index 0f16f102..592d486d 100644 --- a/plugins/sponsorblock/back.js +++ b/plugins/sponsorblock/back.js @@ -1,8 +1,8 @@ const fetch = require("node-fetch"); const is = require("electron-is"); +const { ipcMain } = require("electron"); const defaultConfig = require("../../config/defaults"); -const registerCallback = require("../../providers/song-info"); const { sortSegments } = require("./segments"); let videoID; @@ -13,15 +13,10 @@ module.exports = (win, options) => { ...options, }; - registerCallback(async (info) => { - const newURL = info.url || win.webContents.getURL(); - const newVideoID = new URL(newURL).searchParams.get("v"); - - if (videoID !== newVideoID) { - videoID = newVideoID; - const segments = await fetchSegments(apiURL, categories); - win.webContents.send("sponsorblock-skip", segments); - } + ipcMain.on("song-info-request", async (_, data) => { + videoID = JSON.parse(data)?.videoDetails?.videoId; + const segments = await fetchSegments(apiURL, categories); + win.webContents.send("sponsorblock-skip", segments); }); }; From cfe719b6bd2db3f15e668930df43a678cbc7c2f6 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Fri, 12 Nov 2021 17:46:40 +0200 Subject: [PATCH 5/9] use native thumbnail without modifiers --- plugins/video-toggle/front.js | 2 +- providers/song-info-front.js | 2 +- providers/song-info.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/video-toggle/front.js b/plugins/video-toggle/front.js index 5adb7ab0..d550a375 100644 --- a/plugins/video-toggle/front.js +++ b/plugins/video-toggle/front.js @@ -98,6 +98,6 @@ function observeThumbnail() { function forceThumbnail(img) { const thumbnails = $('#movie_player').getPlayerResponse()?.videoDetails?.thumbnail?.thumbnails; if (thumbnails && thumbnails.length > 0) { - img.src = thumbnails[thumbnails.length - 1].url; + img.src = thumbnails[thumbnails.length - 1].url.split("?")[0]; } } diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 072d7a53..03a8c6b1 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -18,8 +18,8 @@ module.exports = () => { // name = "dataloaded" and abit later "dataupdated" apiEvent.detail.addEventListener('videodatachange', (name, dataEvent) => { if (name !== 'dataloaded') return; - ipcRenderer.send("song-info-request", JSON.stringify(dataEvent.playerResponse)); video.dispatchEvent(srcChangedEvent); + ipcRenderer.send("song-info-request", JSON.stringify(dataEvent.playerResponse)); }) }, { once: true, passive: true }); diff --git a/providers/song-info.js b/providers/song-info.js index 37cd55fa..1c2c96c7 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -52,7 +52,7 @@ const handleData = async (responseText, win) => { songInfo.title = cleanupName(data?.videoDetails?.title); songInfo.artist =cleanupName(data?.videoDetails?.author); songInfo.views = data?.videoDetails?.viewCount; - songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url; + songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url.split("?")[0]; songInfo.songDuration = data?.videoDetails?.lengthSeconds; songInfo.image = await getImage(songInfo.imageSrc); songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate; From 6726e2600b3ca3a8d68e3e1b95b50da211fa354d Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sun, 14 Nov 2021 23:43:04 +0200 Subject: [PATCH 6/9] rework songInfo pause listener --- plugins/sponsorblock/back.js | 2 +- providers/song-info-front.js | 14 ++++-- providers/song-info.js | 92 ++++++++++++++++-------------------- 3 files changed, 52 insertions(+), 56 deletions(-) diff --git a/plugins/sponsorblock/back.js b/plugins/sponsorblock/back.js index 592d486d..04667b85 100644 --- a/plugins/sponsorblock/back.js +++ b/plugins/sponsorblock/back.js @@ -13,7 +13,7 @@ module.exports = (win, options) => { ...options, }; - ipcMain.on("song-info-request", async (_, data) => { + 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); diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 03a8c6b1..8b3c302f 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -16,11 +16,19 @@ module.exports = () => { document.addEventListener('apiLoaded', apiEvent => { const video = document.querySelector('video'); // name = "dataloaded" and abit later "dataupdated" - apiEvent.detail.addEventListener('videodatachange', (name, dataEvent) => { + apiEvent.detail.addEventListener('videodatachange', (name, _dataEvent) => { if (name !== 'dataloaded') return; video.dispatchEvent(srcChangedEvent); - ipcRenderer.send("song-info-request", JSON.stringify(dataEvent.playerResponse)); + ipcRenderer.send("video-src-changed", JSON.stringify(apiEvent.detail.getPlayerResponse())); }) - + for (const status of ['playing', 'pause']) { + video.addEventListener(status, sendSongInfo); + } + function sendSongInfo() { + const data = apiEvent.detail.getPlayerResponse(); + data.videoDetails.elapsedSeconds = Math.floor(video.currentTime); + data.videoDetails.isPaused = video.paused; + ipcRenderer.send("song-info-request", JSON.stringify(apiEvent.detail.getPlayerResponse())); + } }, { once: true, passive: true }); }; diff --git a/providers/song-info.js b/providers/song-info.js index 1c2c96c7..37954df2 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -4,32 +4,6 @@ const fetch = require("node-fetch"); const config = require("../config"); -// Grab the progress using the selector -const getProgress = async (win) => { - // Get current value of the progressbar element - return win.webContents.executeJavaScript( - 'document.querySelector("#progress-bar").value' - ); -}; - -// Grab the native image using the src -const getImage = async (src) => { - const result = await fetch(src); - const buffer = await result.buffer(); - const output = nativeImage.createFromBuffer(buffer); - if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315) - return getImage(src.slice(0, src.lastIndexOf(".jpg")+4)); - } else { - return output; - } -}; - -// To find the paused status, we check if the title contains `-` -const getPausedStatus = async (win) => { - const title = await win.webContents.executeJavaScript("document.title"); - return !title.includes("-"); -}; - // Fill songInfo with empty values /** * @typedef {songInfo} SongInfo @@ -47,21 +21,48 @@ const songInfo = { url: "", }; +// Grab the native image using the src +const getImage = async (src) => { + const result = await fetch(src); + const buffer = await result.buffer(); + const output = nativeImage.createFromBuffer(buffer); + if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315) + return getImage(src.slice(0, src.lastIndexOf(".jpg") + 4)); + } else { + return output; + } +}; + const handleData = async (responseText, win) => { - let data = JSON.parse(responseText); - songInfo.title = cleanupName(data?.videoDetails?.title); - songInfo.artist =cleanupName(data?.videoDetails?.author); - songInfo.views = data?.videoDetails?.viewCount; - songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url.split("?")[0]; - songInfo.songDuration = data?.videoDetails?.lengthSeconds; - songInfo.image = await getImage(songInfo.imageSrc); - songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate; - songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical?.split("&")[0]; + const data = JSON.parse(responseText); + if (!data) return; - // used for options.resumeOnStart - config.set("url", data?.microformat?.microformatDataRenderer?.urlCanonical); + const microformat = data.microformat?.microformatDataRenderer; + if (microformat) { + songInfo.uploadDate = microformat.uploadDate; + songInfo.url = microformat.urlCanonical?.split("&")[0]; - win.webContents.send("update-song-info", JSON.stringify(songInfo)); + // used for options.resumeOnStart + config.set("url", microformat.urlCanonical); + } + + const videoDetails = data.videoDetails; + if (videoDetails) { + songInfo.title = cleanupName(videoDetails.title); + songInfo.artist = cleanupName(videoDetails.author); + songInfo.views = videoDetails.viewCount; + songInfo.songDuration = videoDetails.lengthSeconds; + songInfo.elapsedSeconds = videoDetails.elapsedSeconds; + songInfo.isPaused = videoDetails.isPaused; + + const oldUrl = songInfo.imageSrc; + songInfo.imageSrc = videoDetails.thumbnail?.thumbnails?.pop()?.url.split("?")[0]; + if (oldUrl !== songInfo.imageSrc) { + songInfo.image = await getImage(songInfo.imageSrc); + } + + win.webContents.send("update-song-info", JSON.stringify(songInfo)); + } }; // This variable will be filled with the callbacks once they register @@ -81,19 +82,6 @@ const registerCallback = (callback) => { }; const registerProvider = (win) => { - win.on("page-title-updated", async () => { - // Get and set the new data - songInfo.isPaused = await getPausedStatus(win); - - const elapsedSeconds = await getProgress(win); - songInfo.elapsedSeconds = elapsedSeconds; - - // Trigger the callbacks - callbacks.forEach((c) => { - c(songInfo); - }); - }); - // This will be called when the song-info-front finds a new request with song data ipcMain.on("song-info-request", async (_, responseText) => { await handleData(responseText, win); @@ -114,7 +102,7 @@ const suffixesToRemove = [ function cleanupName(name) { if (!name) return name; - const lowCaseName = name.toLowerCase(); + const lowCaseName = name.toLowerCase(); for (const suffix of suffixesToRemove) { if (lowCaseName.endsWith(suffix)) { return name.slice(0, -suffix.length); From 185ebbf41792113815dcfcc6dcc654302b0f3150 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sun, 21 Nov 2021 19:44:01 +0200 Subject: [PATCH 7/9] fix downloader playlist download --- plugins/downloader/menu.js | 20 +++++++++----------- providers/song-info.js | 5 ++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/plugins/downloader/menu.js b/plugins/downloader/menu.js index ce7f5643..9ec83d4c 100644 --- a/plugins/downloader/menu.js +++ b/plugins/downloader/menu.js @@ -1,25 +1,23 @@ const { existsSync, mkdirSync } = require("fs"); const { join } = require("path"); -const { URL } = require("url"); -const { dialog } = require("electron"); +const { dialog, ipcMain } = require("electron"); const is = require("electron-is"); const ytpl = require("ytpl"); const chokidar = require('chokidar'); const { setOptions } = require("../../config/plugins"); -const registerCallback = require("../../providers/song-info"); const { sendError } = require("./back"); const { defaultMenuDownloadLabel, getFolder } = require("./utils"); let downloadLabel = defaultMenuDownloadLabel; -let metadataURL = undefined; +let playingPlaylistId = undefined; let callbackIsRegistered = false; module.exports = (win, options) => { if (!callbackIsRegistered) { - registerCallback((info) => { - metadataURL = info.url; + ipcMain.on("video-src-changed", async (_, data) => { + playingPlaylistId = JSON.parse(data)?.videoDetails?.playlistId; }); callbackIsRegistered = true; } @@ -28,17 +26,17 @@ module.exports = (win, options) => { { label: downloadLabel, click: async () => { - const currentURL = metadataURL || win.webContents.getURL(); - const playlistID = new URL(currentURL).searchParams.get("list"); - if (!playlistID) { + const currentPagePlaylistId = new URL(win.webContents.getURL()).searchParams.get("list"); + const playlistId = currentPagePlaylistId || playingPlaylistId; + if (!playlistId) { sendError(win, new Error("No playlist ID found")); return; } - console.log(`trying to get playlist ID: '${playlistID}'`); + console.log(`trying to get playlist ID: '${playlistId}'`); let playlist; try { - playlist = await ytpl(playlistID, { + playlist = await ytpl(playlistId, { limit: options.playlistMaxItems || Infinity, }); } catch (e) { diff --git a/providers/song-info.js b/providers/song-info.js index 37954df2..936ea6ef 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -19,6 +19,8 @@ const songInfo = { songDuration: 0, elapsedSeconds: 0, url: "", + videoId: "", + playlistId: "", }; // Grab the native image using the src @@ -41,7 +43,7 @@ const handleData = async (responseText, win) => { if (microformat) { songInfo.uploadDate = microformat.uploadDate; songInfo.url = microformat.urlCanonical?.split("&")[0]; - + songInfo.playlistId = new URL(microformat.urlCanonical).searchParams.get("list"); // used for options.resumeOnStart config.set("url", microformat.urlCanonical); } @@ -54,6 +56,7 @@ const handleData = async (responseText, win) => { songInfo.songDuration = videoDetails.lengthSeconds; songInfo.elapsedSeconds = videoDetails.elapsedSeconds; songInfo.isPaused = videoDetails.isPaused; + songInfo.videoId = videoDetails.videoId; const oldUrl = songInfo.imageSrc; songInfo.imageSrc = videoDetails.thumbnail?.thumbnails?.pop()?.url.split("?")[0]; From 92452f804f62f19b12f31bcfbf579191006ce85c Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Mon, 22 Nov 2021 18:20:12 +0200 Subject: [PATCH 8/9] fix song-info-request --- providers/song-info-front.js | 18 +++++++++++++----- providers/song-info.js | 9 ++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 8b3c302f..70d47817 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -19,16 +19,24 @@ module.exports = () => { apiEvent.detail.addEventListener('videodatachange', (name, _dataEvent) => { if (name !== 'dataloaded') return; video.dispatchEvent(srcChangedEvent); - ipcRenderer.send("video-src-changed", JSON.stringify(apiEvent.detail.getPlayerResponse())); + sendSongInfo(); }) - for (const status of ['playing', 'pause']) { - video.addEventListener(status, sendSongInfo); - } + + video.addEventListener('pause', e => { + ipcRenderer.send("playPaused", { isPaused: true, elapsedSeconds: Math.floor(e.target.currentTime) }); + }); + + video.addEventListener('playing', e => { + if (e.target.currentTime > 0){ + ipcRenderer.send("playPaused", { isPaused: false, elapsedSeconds: Math.floor(e.target.currentTime) }); + } + }); + function sendSongInfo() { const data = apiEvent.detail.getPlayerResponse(); data.videoDetails.elapsedSeconds = Math.floor(video.currentTime); data.videoDetails.isPaused = video.paused; - ipcRenderer.send("song-info-request", JSON.stringify(apiEvent.detail.getPlayerResponse())); + ipcRenderer.send("video-src-changed", JSON.stringify(apiEvent.detail.getPlayerResponse())); } }, { once: true, passive: true }); }; diff --git a/providers/song-info.js b/providers/song-info.js index 936ea6ef..ddeee4cd 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -86,12 +86,19 @@ const registerCallback = (callback) => { const registerProvider = (win) => { // This will be called when the song-info-front finds a new request with song data - ipcMain.on("song-info-request", async (_, responseText) => { + ipcMain.on("video-src-changed", async (_, responseText) => { await handleData(responseText, win); callbacks.forEach((c) => { c(songInfo); }); }); + ipcMain.on("playPaused", (_, { isPaused, elapsedSeconds }) => { + songInfo.isPaused = isPaused; + songInfo.elapsedSeconds = elapsedSeconds; + callbacks.forEach((c) => { + c(songInfo); + }); + }) }; const suffixesToRemove = [ From 8c5ac17cdfaaf2d4daeafa2ef35d302e6184df7a Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Tue, 23 Nov 2021 18:19:37 +0200 Subject: [PATCH 9/9] fix multiple songInfo calls on start --- providers/song-info-front.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/providers/song-info-front.js b/providers/song-info-front.js index 70d47817..acd41daf 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -22,21 +22,22 @@ module.exports = () => { sendSongInfo(); }) - video.addEventListener('pause', e => { - ipcRenderer.send("playPaused", { isPaused: true, elapsedSeconds: Math.floor(e.target.currentTime) }); - }); - - video.addEventListener('playing', e => { - if (e.target.currentTime > 0){ - ipcRenderer.send("playPaused", { isPaused: false, elapsedSeconds: Math.floor(e.target.currentTime) }); - } - }); + for (const status of ['playing', 'pause']) { + video.addEventListener(status, e => { + if (Math.floor(e.target.currentTime) > 0) { + ipcRenderer.send("playPaused", { + isPaused: status === 'pause', + elapsedSeconds: Math.floor(e.target.currentTime) + }); + } + }); + } function sendSongInfo() { const data = apiEvent.detail.getPlayerResponse(); data.videoDetails.elapsedSeconds = Math.floor(video.currentTime); - data.videoDetails.isPaused = video.paused; - ipcRenderer.send("video-src-changed", JSON.stringify(apiEvent.detail.getPlayerResponse())); + data.videoDetails.isPaused = false; + ipcRenderer.send("video-src-changed", JSON.stringify(data)); } }, { once: true, passive: true }); };