comicsite/graveyard/tmp-hypnos/musicloader.js
etherware-novice 8d353ea284
Some checks failed
Deploy to neocities / deploy (push) Has been cancelled
dumping everything that wasnt saved before i delete this folder
2025-07-28 20:47:54 -05:00

72 lines
1.7 KiB
JavaScript

AUTOplay = {}
AUTOplay.player = null;
AUTOplay.curSong = "";
AUTOplay.dispSong = "";
// fetch the song data on startup!
window.addEventListener("DOMContentLoaded", getPageSong);
async function getPageSong()
{
if (!document.getElementById("autoplay-text-box"))
{ return; }
let req = await fetch("musiclist.csv");
if (!req.ok)
{ throw new Error(`HTTP error" ${req.status}`); }
list = await req.text();
list = list.split('\n');
list.shift(); // getting rid of the csv header
var pgsong = "def"
for (const line of list)
{
let songline = line.split(',');
if(songline[1] === undefined)
{ continue; }
if(!window.location.pathname.startsWith(songline[0]))
{ continue; }
pgsong = songline[1];
}
if(pgsong == "def")
{
console.warn("No page song defined! Defaulting to relaxation");
pgsong = "relaxation";
}
let namereq = await fetch(`audio/${pgsong}.txt`);
if (!req.ok)
AUTOplay.dispSong = `${pgsong}.mp3`;
else
AUTOplay.dispSong = await namereq.text();
AUTOplay.curSong = pgsong;
AUTOplay.player = new Audio(`audio/${pgsong}.mp3`);
AUTOplay.player.addEventListener("play", playMus);
AUTOplay.player.addEventListener("pause", stopMus);
AUTOplay.player.play(); // will most likely fail but we tried :)
}
function playMus()
{
document.getElementById("autoplay-text-box").style.left = "-50%";
document.getElementById("autoplay-text-text").innerHTML = AUTOplay.dispSong;
}
function stopMus()
{
document.getElementById("autoplay-text-box").style.left = "-90.5%";
document.getElementById("autoplay-text-text").innerHTML = "";
}
function toggleMus()
{
if (AUTOplay.player.paused)
{ AUTOplay.player.play(); }
else
{ AUTOplay.player.pause(); }
}