diff --git a/javascript/nav-music.js b/javascript/nav-music.js index 1e99f17..7551680 100644 --- a/javascript/nav-music.js +++ b/javascript/nav-music.js @@ -1,6 +1,6 @@ // Volume adjustment for audio elements document.querySelectorAll("audio").forEach((audio) => { - audio.volume = 0.5; + audio.volume = 0.5; // Volume range is from 0.0 (silent) to 1.0 (maximum) }); // Shuffle Playlist (Fisher-Yates) @@ -14,9 +14,10 @@ function fisherYatesShuffle(songs) { // Load the JSON file containing music data directly from chat.veltron.net async function loadMusicData() { - const targetUrl = "https://api.veltron.net/music"; + const targetUrl = 'https://api.veltron.net/music'; // Directly use the target URL const response = await fetch(targetUrl); + // Check if the request was successful if (!response.ok) { console.error("Failed to fetch music data:", response.statusText); return []; @@ -25,37 +26,25 @@ async function loadMusicData() { const data = await response.json(); // Get the "frutigerAeroBliss" section from the JSON - const section = data.ols.find( - (item) => item["@id"] === "frutigerAeroBliss" - ); + const section = data.ols.find(item => item["@id"] === "frutigerAeroBliss"); if (!section) { console.error('Section "frutigerAeroBliss" not found.'); return []; } - // Base domain only — preserve subfolders from data-src-mp3 - const baseUrl = "https://cdn.veltron.net/aero/"; - - const playlist = section.li.map((item) => { - const rawPath = item.a["@data-src-mp3"] || ""; - - // Remove leading slash if present, preserve nested folders like music/maplememory/song.mp3 - const normalizedPath = rawPath.replace(/^\/+/, ""); - - return { - title: item.a.title || "Unknown Title", - - // Full path keeps subfolders instead of only filename - url: baseUrl + normalizedPath, - - background: item.a["@data-background"] || "", - artist: item.a["@data-artist"] || "", - spotifyLink: item.a["@data-spotify"] || "", - youtubeLink: item.a["@data-youtube"] || "", - }; - }); + // Map the songs into a simplified format with direct audio URLs + const baseUrl = "https://cdn.veltron.net/aero/music/"; // Base URL for the music files + const playlist = section.li.map(item => ({ + title: item.a.title, + url: baseUrl + item.a["@data-src-mp3"].split("/").pop(), // Build the full URL from the base path + background: item.a["@data-background"] || '', // Optional background if available + artist: item.a["@data-artist"] || '', + spotifyLink: item.a["@data-spotify"] || '', + youtubeLink: item.a["@data-youtube"] || '' + })); + // Shuffle the playlist return fisherYatesShuffle(playlist); } @@ -70,14 +59,14 @@ const CURRENT_SONG_INFO = document.getElementById("currentSongInfo"); let songs = []; let currentIndex = 0; let currentTime = 0; -let isPlaying = false; +let isPlaying = false; // Track play/pause state -// Save state +// Save state to sessionStorage function saveState() { sessionStorage.setItem("isPlaying", isPlaying); } -// Load state +// Load state from sessionStorage function loadState() { const savedIsPlaying = sessionStorage.getItem("isPlaying"); @@ -86,16 +75,15 @@ function loadState() { } } -// Load song +// Load the current song function loadSong(index) { if (index < 0 || index >= songs.length) return; - - NAV_MUSIC.src = songs[index].url; + NAV_MUSIC.src = songs[index].url; // Directly set the audio URL (no proxy needed) CURRENT_SONG_INFO.innerText = `Featured song: ${songs[index].title}`; NAV_MUSIC.load(); } -// Update buttons +// Update button states based on `isPlaying` function updateButtonStates() { if (isPlaying) { BUTTON_PLAY.classList.add("active"); @@ -106,7 +94,7 @@ function updateButtonStates() { } } -// Play +// Event listeners for playback controls BUTTON_PLAY.addEventListener("click", function () { isPlaying = true; NAV_MUSIC.play(); @@ -114,7 +102,6 @@ BUTTON_PLAY.addEventListener("click", function () { saveState(); }); -// Pause BUTTON_PAUSE.addEventListener("click", function () { isPlaying = false; NAV_MUSIC.pause(); @@ -122,71 +109,59 @@ BUTTON_PAUSE.addEventListener("click", function () { saveState(); }); -// Previous BUTTON_PREVIOUS.addEventListener("click", function () { currentIndex = currentIndex === 0 ? songs.length - 1 : currentIndex - 1; - currentTime = 0; + currentTime = 0; // Reset time when switching songs loadSong(currentIndex); - if (isPlaying) { NAV_MUSIC.play(); } - updateButtonStates(); saveState(); }); -// Next BUTTON_NEXT.addEventListener("click", function () { currentIndex = (currentIndex + 1) % songs.length; - currentTime = 0; + currentTime = 0; // Reset time when switching songs loadSong(currentIndex); - if (isPlaying) { NAV_MUSIC.play(); } - updateButtonStates(); saveState(); }); -// Autoplay next +// Autoplay next song when current ends NAV_MUSIC.addEventListener("ended", function () { currentIndex = (currentIndex + 1) % songs.length; - currentTime = 0; + currentTime = 0; // Reset time when a new song starts loadSong(currentIndex); - if (isPlaying) { NAV_MUSIC.play(); } - saveState(); }); // Initial load window.addEventListener("load", async () => { songs = await loadMusicData(); - if (songs.length > 0) { loadState(); loadSong(currentIndex); - if (isPlaying) { NAV_MUSIC.play(); } - updateButtonStates(); } }); -// Pause all other audio elements +// Pause all audio if user presses play on another audio element document.addEventListener( "play", function (e) { - const audios = document.getElementsByTagName("audio"); - - for (let i = 0; i < audios.length; i++) { - if (audios[i] !== e.target) { + var audios = document.getElementsByTagName("audio"); + for (var i = 0, len = audios.length; i < len; i++) { + if (audios[i] != e.target) { audios[i].pause(); } }