Skip to content

Commit

Permalink
FIX: make sure dynamic colors work on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienMaille committed Oct 16, 2024
1 parent 5ceea45 commit b2c3685
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ curl -fsSL https://raw.githubusercontent.com/JulienMaille/spicetify-dynamic-them
### Manual Install

1. Download the latest [Source code (zip)](https://github.com/JulienMaille/spicetify-dynamic-theme/releases/latest)
2. Extract the files to your [Spicetify/Themes folder](https://spicetify.app/docs/development/customization#themes) (rename the zipped folder to `DefaultDynamic`)
2. Extract the files to your [Spicetify/Themes folder](https://spicetify.app/docs/development/themes/) (rename the zipped folder to `DefaultDynamic`)
3. Copy `default-dynamic.js` and `Vibrant.min.js` to your [Spicetify/Extensions folder](https://spicetify.app/docs/advanced-usage/extensions#installing)
4. Add the 2 lines in `[Patch]` section of the config file (see details below)
5. Run:
Expand Down
37 changes: 10 additions & 27 deletions default-dynamic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let current = "5.2";
let current = "5.3";

function waitForElement(els, func, timeout = 100) {
const queries = els.map((el) => document.querySelector(el));
Expand Down Expand Up @@ -272,18 +272,22 @@ async function songchange() {
nearArtistSpan.innerHTML = nearArtistSpanText;
}
document.documentElement.style.setProperty("--image_url", `url("${bgImage}")`);
registerCoverListener();
pickCoverColor();
}

Spicetify.Player.addEventListener("songchange", songchange);
function pickCoverColor() {
const img = document.querySelector(".main-image-image.cover-art-image");
if (!img) return setTimeout(pickCoverColor, 250); // Check if image exists

function pickCoverColor(img) {
if (Spicetify.Platform.PlatformData.client_version_triple >= "1.2.48") {
if (!img.currentSrc.startsWith("https://i.scdn.co/image")) return;
img.crossOrigin = "Anonymous";
} else {
if (!img.currentSrc.startsWith("spotify:")) return;
}

if (!img.complete) return setTimeout(pickCoverColor, 250); // Check if image is loaded

if (img.complete) {
textColor = "#1db954";
try {
Expand All @@ -301,29 +305,8 @@ function pickCoverColor(img) {
updateColors(textColor);
}

var coverListener;
function registerCoverListener() {
const img = document.querySelector(".main-image-image.cover-art-image");
if (!img) return setTimeout(registerCoverListener, 250); // Check if image exists
if (!img.complete) return setTimeout(registerCoverListener, 250); // Check if image is loaded
pickCoverColor(img);

if (coverListener != null) {
coverListener.disconnect();
coverListener = null;
}

coverListener = new MutationObserver((muts) => {
const img = document.querySelector(".main-image-image.cover-art-image");
if (!img) return registerCoverListener();
pickCoverColor(img);
});
coverListener.observe(img, {
attributes: true,
attributeFilter: ["src"]
});
}
registerCoverListener();
Spicetify.Player.addEventListener("songchange", songchange);
songchange();

(function Startup() {
if (!Spicetify.showNotification) {
Expand Down

0 comments on commit b2c3685

Please sign in to comment.