From 76245544314f951ed9f08ed871cc5a035ee0bd2f Mon Sep 17 00:00:00 2001 From: Julien Maille Date: Tue, 12 Oct 2021 21:35:38 +0200 Subject: [PATCH] IMP: properly hook cover event listener when needed --- dribbblish-dynamic.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/dribbblish-dynamic.js b/dribbblish-dynamic.js index f9ea1f0..5584fc7 100644 --- a/dribbblish-dynamic.js +++ b/dribbblish-dynamic.js @@ -299,7 +299,8 @@ async function songchange() { updateColors(textColor, textColor) coverListenerInstalled = false } - hookCoverChange(true) + if (!coverListenerInstalled) + hookCoverChange(true) if (album_uri !== undefined && !album_uri.includes('spotify:show')) { const albumInfo = await getAlbumInfo(album_uri.replace("spotify:album:", "")) @@ -341,7 +342,6 @@ async function songchange() { Spicetify.Player.addEventListener("songchange", songchange) function pickCoverColor(img) { - //img.setAttribute('crossOrigin', '') var swatches = new Vibrant(img, 5).swatches() lightCols = ["Vibrant", "DarkVibrant", "Muted", "LightVibrant"] darkCols = ["Vibrant", "LightVibrant", "Muted", "DarkVibrant"] @@ -363,16 +363,26 @@ function pickCoverColor(img) { updateColors(textColor, sidebarColor, true) } +waitForElement([".main-nowPlayingBar-left"], (queries) => { + var observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.removedNodes.length>0) + coverListenerInstalled = false + }); + }); + observer.observe(queries[0], { childList: true }) +}); + function hookCoverChange(pick) { waitForElement([".cover-art-image"], (queries) => { - coverListenerInstalled = true; + coverListenerInstalled = true if (pick && queries[0].complete && queries[0].naturalHeight !== 0) pickCoverColor(queries[0]); queries[0].addEventListener("load", function () { try { - pickCoverColor(queries[0]); + pickCoverColor(queries[0]) } catch (error) { - console.error(error); - setTimeout(pickCoverColor, 300, queries[0]); + console.error(error) + setTimeout(pickCoverColor, 300, queries[0]) } }); });