sourcehypertextpublicmusicjukebox.js

function pick(slug, autoplay = true) {
	$(`.favourite-tracks h2`).innerHTML = $(`#sb-${slug} strong`).innerText;
	if ($(`#sb-${slug} strong`).innerText == "singles") {
		$(`.favourite-tracks h2`).innerHTML += `<br><small><small>(${
			$(`#sb-${slug} small`).innerText
		})</small></small>`;
	}
	if ($(`#sb-${slug} strong`).innerText == "comments") {
		$(`.favourite-tracks h2`).innerHTML = "";
	}

	$(".shown").classList.remove("shown");
	$(`#ft-${slug}`).classList.add("shown");

	$$(".select-band.enabled").forEach(el => {
		el.classList.remove(`enabled`);
	});
	$(`#sb-${slug}`).classList.add("enabled");

	if (autoplay) {
		readPlay($(`#ft-${slug} button`));
	}
}

function readPlay(el) {
	const match = el
		.getAttribute("onclick")
		.match(/play\(`(.*?)`(?:, ?([0-9]+))?\)/)
		.slice(1);
	match[1] = parseInt(match[1] || "0");

	play(...match);
}

function play(slug, start = 0) {
	$("#yt-link").setAttribute(
		"src",
		`https://www.youtube-nocookie.com/embed/${slug}?start=${start}&autoplay=1&controls=0&modestbranding=1`
	);

	$$(".favourite-tracks .enabled").forEach(el => {
		el.classList.remove(`enabled`);
	});
	if (start != 0) {
		$(
			`.favourite-tracks button[onclick*="${slug}"][onclick*="${start}"]`
		).classList.add(`enabled`);
	} else {
		$(`.favourite-tracks button[onclick*="${slug}"]`).classList.add(
			`enabled`
		);
	}
}

function randomise() {
	buttons = $$(".favourite-tracks button");
	seed = Math.floor(Math.random() * buttons.length);

	pick(
		buttons[seed].parentElement.parentElement.id.replace("ft-", ""),
		false
	);

	readPlay(buttons[seed]);
}