diff --git a/javascript/music-player.js b/javascript/music-player.js index e48abd2..cb4e2dc 100644 --- a/javascript/music-player.js +++ b/javascript/music-player.js @@ -13,6 +13,31 @@ let NAV_NEXT = document.getElementById("navMusicNext"); let NAV_PREV = document.getElementById("navMusicPrevious"); +// =============================== +// URL SANITIZER +// =============================== +function sanitizeMp3Url(raw) { + if (!raw) return null; + + // Reject invalid placeholders + if (raw === "#" || raw === "/" || raw === "./" || raw === "../") return null; + + // Reject URLs that are not MP3 files + if (!raw.endsWith(".mp3")) return null; + + // If JSON gives absolute URL + if (raw.startsWith("http")) return raw; + + // If JSON gives /music/... or /audio/... + if (raw.startsWith("/")) { + return "https://frutigeraeroarchive.org" + raw; + } + + // Otherwise treat as relative filename + return "https://frutigeraeroarchive.org/music/" + raw; +} + + // =============================== // LOAD MUSIC DATA // =============================== @@ -23,16 +48,15 @@ async function loadMusicData() { const section = data.ols.find(item => item["@id"] === "frutigerAeroBliss"); if (!section) return []; - return section.li.map(item => { - const raw = item.a["@data-src-mp3"]; - - const fullUrl = raw.startsWith("/") - ? "https://frutigeraeroarchive.org" + raw - : "https://frutigeraeroarchive.org/music/" + raw; + // KEEP JSON ORDER EXACTLY, but filter invalid URLs + return section.li + .map(item => { + const fixedUrl = sanitizeMp3Url(item.a["@data-src-mp3"]); + if (!fixedUrl) return null; return { title: item.a.title, - url: fullUrl, + url: fixedUrl, background: item.a["@data-background"] || "", artist: item.a["@data-artist"] || "", spotify: item.a["@data-spotify"] || "", @@ -41,9 +65,11 @@ async function loadMusicData() { copyright: item.a["@data-copyright"] || "", date: item.a["@data-date"] || "" }; - }); + }) + .filter(Boolean); // remove null entries } + // =============================== // CREATE PLAYLIST // =============================== @@ -110,8 +136,14 @@ function updateSong(index) { }); const song = SONG_LINKS[index]; + const src = song.getAttribute("data-src-mp3"); - AUDIO_PLAYER.src = song.getAttribute("data-src-mp3"); + if (!src) { + console.warn("Invalid song URL, skipping."); + return; + } + + AUDIO_PLAYER.src = src; TRACK_NAME_DISPLAY.textContent = song.getAttribute("data-title"); AUDIO_PLAYER.play().catch(console.error);