From 9bd8992a2074aafbc4e492b616371421a664cbbf Mon Sep 17 00:00:00 2001 From: withthelog Date: Sun, 13 Oct 2024 16:07:56 +0200 Subject: [PATCH] added progress bar and patched some minor bugs --- index.html | 3 ++- lanyard.js | 73 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/index.html b/index.html index 8816700..0977715 100644 --- a/index.html +++ b/index.html @@ -49,7 +49,8 @@
-

Activity: Loading...

+

loading...

+
diff --git a/lanyard.js b/lanyard.js index de5e381..41bba9f 100644 --- a/lanyard.js +++ b/lanyard.js @@ -1,4 +1,6 @@ let ws; +let progressInterval; // To hold the interval reference for updating progress +let songEndTimeout; // To reset the UI when the song ends function connectToLanyard() { ws = new WebSocket("wss://api.lanyard.rest/socket"); @@ -25,7 +27,12 @@ function connectToLanyard() { // Update HTML content dynamically const activityElement = document.getElementById("activity"); const albumArtElement = document.getElementById("albumArt"); - const separatorElement = document.querySelector(".separator"); // Select the separator element + const separatorElement = document.querySelector(".separator"); + const progressBar = document.getElementById("progressBar"); + + // Clear any existing progress interval or song end timeout + if (progressInterval) clearInterval(progressInterval); + if (songEndTimeout) clearTimeout(songEndTimeout); // Update the activity text and display Spotify data if (presence.listening_to_spotify) { @@ -33,21 +40,46 @@ function connectToLanyard() { activityElement.textContent = `${spotifyData.song} - ${spotifyData.artist}`; // Load album art with CORS - albumArtElement.crossOrigin = "Anonymous"; // Set CORS attribute - albumArtElement.src = spotifyData.album_art_url; // Update album art URL - albumArtElement.style.display = "block"; // Show the album art - separatorElement.style.display = "block"; // Show the separator when a song is playing + albumArtElement.crossOrigin = "Anonymous"; + albumArtElement.src = spotifyData.album_art_url; + albumArtElement.style.display = "block"; + separatorElement.style.display = "block"; + progressBar.style.display = "block"; + + // Get the start and end timestamps of the song + const startTime = spotifyData.timestamps.start; + const endTime = spotifyData.timestamps.end; + const duration = endTime - startTime; + + // Update the progress bar in real time + const updateProgress = () => { + const currentTime = Date.now(); + const elapsed = currentTime - startTime; + const progress = (elapsed / duration) * 100; + progressBar.value = progress; + + if (progress >= 100) clearInterval(progressInterval); // Stop updating when the song is over + }; + + // Update the progress bar every second + updateProgress(); + progressInterval = setInterval(updateProgress, 1000); - // Wait for the album art to fully load before extracting the colors + // Set a timeout to reset the UI when the song ends + songEndTimeout = setTimeout(() => { + resetSongDisplay(); + }, duration); // Reset after the song's duration + + // Extract colors and set gradient background albumArtElement.onload = function () { try { const colorThief = new ColorThief(); if (albumArtElement.complete && albumArtElement.naturalHeight !== 0) { const dominantColor = colorThief.getColor(albumArtElement); - const palette = colorThief.getPalette(albumArtElement, 2); + const palette = colorThief.getPalette(albumArtElement, 3); const gradient = `linear-gradient(135deg, rgb(${dominantColor[0]}, ${dominantColor[1]}, ${dominantColor[2]}), rgb(${palette[1][0]}, ${palette[1][1]}, ${palette[1][2]}))`; - document.body.style.background = gradient; // Set the new gradient background + document.body.style.background = gradient; } else { console.error("Image not loaded properly."); } @@ -56,20 +88,29 @@ function connectToLanyard() { } }; - // Handle image load errors - albumArtElement.onerror = function () { - console.error("Failed to load the album art image."); - }; } else { - activityElement.textContent = "offline :("; - albumArtElement.style.display = "none"; // Hide the album art if not listening to Spotify - separatorElement.style.display = "none"; // Hide the separator when no song is playing + // Reset if no Spotify activity + resetSongDisplay(); } } }; } +// Function to reset the song display (called when the song ends) +function resetSongDisplay() { + const activityElement = document.getElementById("activity"); + const albumArtElement = document.getElementById("albumArt"); + const separatorElement = document.querySelector(".separator"); + const progressBar = document.getElementById("progressBar"); + + activityElement.textContent = "offline :("; + albumArtElement.style.display = "none"; + separatorElement.style.display = "none"; + progressBar.style.display = "none"; + progressBar.value = 0; // Reset progress bar +} + // Start the WebSocket connection after the DOM is fully loaded document.addEventListener("DOMContentLoaded", () => { connectToLanyard(); -}); +}); \ No newline at end of file