Files
lintunes/lintunes/export/templates/player.js
T
trav 76b941bf68 v0.8.0: export a playlist — as a folder, or as a whole website
File → Export Playlist…, and the same item on a playlist's right-click
menu. A sibling of device_sync: pure plan_export() first, then a worker
on a daemon thread sharing the status-bar progress widgets. The manifest
(index.html / .m3u) is written last, so an interrupted export never
leaves a page naming files that aren't there.

Two destinations. A folder gives the audio under "Artist - Title" names
plus an extended .m3u. A web mix gives a self-contained static site
reproducing the hand-made yearly mixes, with a dialog for title,
description and hero image.

audio.js and jQuery are gone. The old pages shipped ~293 KB: a build of
audio.js whose upstream hasn't moved since 2012, plus jQuery 3.2.1
(CVE-2019-11358, CVE-2020-11022, CVE-2020-11023 — unexploitable on a
static page, since nothing untrusted reaches a jQuery HTML sink, but
dead weight regardless). audio.js never used jQuery; jQuery was there
for ~25 lines of tracklist glue. Both are replaced by a dependency-free
player.js plus a player.css transcribed from the customized audio.js
skin, so the page looks identical — same 250px #c7b563 bar, same
player-graphics.gif (byte-identical: it's an animated GIF whose loading
frame is a spinner), same shortcuts. ~293 KB → ~6 KB. The Flash
fallback went too; audio.js gated it on !canPlayType("audio/mpeg;"),
unreachable since ~2010.

Conversion happens only when a browser genuinely can't decode a file,
never because of bitrate — a 320 kbps MP3 is copied verbatim. Everything
that does convert targets FLAC, so a conversion cannot cost a bit; a
test asserts the exported FLAC's decoded PCM hashes identical to the
ALAC source. Against the real library that's 105 Apple Lossless and 8
AIFF out of 21,382 tracks. DRM'd tracks are reported in a confirmation
dialog, never silently dropped and never attempted. ffmpeg is the CLI
binary here, not Qt's ffmpeg backend, so it's detected at runtime.

Verified in Chromium 151 and Firefox 153 over CDP/Marionette: every
exported format decodes including the converted FLAC, the player builds,
click / space / arrows / scrubber-seek / autoplay-next all work, and the
network log shows no request for jquery, audio.min.js or any .swf.

mix-example/ removed; the template reproduces it.
2026-08-19 19:03:01 -04:00

187 lines
6.7 KiB
JavaScript

/* Mix player — no dependencies.
*
* Replaces the audio.js + jQuery pair the hand-made mixes used to ship. That
* stack existed to skin an <audio> element back when you couldn't style
* `<audio controls>`, and to fall back to Flash where HTML5 audio was
* missing. The Flash branch has been unreachable since roughly 2010 (audio.js
* gated it on `!canPlayType("audio/mpeg;")`, which no current browser
* answers falsely), and everything else here is standard DOM.
*
* The generated wrapper markup and the class names match audio.js exactly, so
* player.css is a straight transcription of the old skin.
*
* Expected page markup:
* <audio preload></audio>
* <ol><li><a href="#" data-src="./audios/x.mp3">Title — Artist</a></li>…</ol>
*/
(function () {
"use strict";
var WRAPPER =
'<div class="play-pause">' +
'<p class="play"></p><p class="pause"></p>' +
'<p class="loading"></p><p class="error"></p>' +
'</div>' +
'<div class="scrubber"><div class="progress"></div><div class="loaded"></div></div>' +
'<div class="time"><em class="played">00:00</em>/<strong class="duration">00:00</strong></div>' +
'<div class="error-message"></div>';
function clock(seconds) {
if (!isFinite(seconds) || seconds < 0) seconds = 0;
var m = Math.floor(seconds / 60), s = Math.floor(seconds % 60);
return (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s;
}
function build(audio) {
var wrap = document.createElement("div");
wrap.className = "audiojs";
audio.parentNode.insertBefore(wrap, audio);
wrap.appendChild(audio);
// insertAdjacentHTML rather than innerHTML: appending would re-parse and
// replace the <audio> node we just moved in, losing its state.
wrap.insertAdjacentHTML("beforeend", WRAPPER);
var ui = {
wrap: wrap,
progress: wrap.querySelector(".progress"),
loaded: wrap.querySelector(".loaded"),
scrubber: wrap.querySelector(".scrubber"),
played: wrap.querySelector(".played"),
duration: wrap.querySelector(".duration"),
error: wrap.querySelector(".error-message")
};
function state(name) {
wrap.classList.remove("playing", "loading", "error");
if (name) wrap.classList.add(name);
}
function start() {
// play() rejects when a fresh load() interrupts it — which is just
// what holding an arrow key down does. Not an error worth reporting.
var p = audio.play();
if (p && p.catch) p.catch(function () {});
}
wrap.querySelector(".play-pause").addEventListener("click", function () {
if (wrap.classList.contains("error")) return;
if (audio.paused) start(); else audio.pause();
});
ui.scrubber.addEventListener("click", function (e) {
if (!isFinite(audio.duration)) return;
var box = ui.scrubber.getBoundingClientRect();
audio.currentTime = ((e.clientX - box.left) / box.width) * audio.duration;
});
audio.addEventListener("loadstart", function () { state("loading"); });
audio.addEventListener("play", function () { state("playing"); });
audio.addEventListener("pause", function () { state(null); });
audio.addEventListener("error", function () {
state("error");
ui.error.textContent = 'Error loading: "' +
(audio.currentSrc || audio.src || "") + '"';
});
audio.addEventListener("loadedmetadata", function () {
ui.duration.textContent = clock(audio.duration);
if (!audio.paused) state("playing"); else state(null);
});
audio.addEventListener("timeupdate", function () {
ui.played.textContent = clock(audio.currentTime);
if (isFinite(audio.duration) && audio.duration > 0) {
ui.progress.style.width =
(audio.currentTime / audio.duration) * 100 + "%";
}
});
function paintLoaded() {
// `buffered` is a list of ranges; the bar shows the leading one, as
// audio.js did. Driven from four events rather than `progress` alone
// because a local or well-cached file can finish buffering before the
// first `progress` ever fires, leaving the bar stuck at zero.
if (audio.buffered.length && isFinite(audio.duration) && audio.duration > 0) {
ui.loaded.style.width =
(audio.buffered.end(audio.buffered.length - 1) / audio.duration) * 100 + "%";
}
}
["progress", "loadeddata", "canplay", "timeupdate"].forEach(function (name) {
audio.addEventListener(name, paintLoaded);
});
return {
element: audio,
load: function (src) {
ui.progress.style.width = "0%";
ui.loaded.style.width = "0%";
ui.played.textContent = "00:00";
ui.duration.textContent = "00:00";
audio.src = src;
audio.load();
},
play: start,
playPause: function () {
if (audio.paused) start(); else audio.pause();
}
};
}
function init() {
var audio = document.querySelector("audio");
var items = Array.prototype.slice.call(document.querySelectorAll("ol li"));
if (!audio || !items.length) return;
var player = build(audio);
function select(li, autoplay) {
items.forEach(function (other) { other.classList.remove("playing"); });
li.classList.add("playing");
player.load(li.querySelector("a").getAttribute("data-src"));
if (autoplay) player.play();
}
items.forEach(function (li) {
li.addEventListener("click", function (e) {
e.preventDefault();
select(li, true);
});
});
// Advance to the next track, stopping at the end of the mix — the old
// page had the wrap-around commented out, so this matches it.
audio.addEventListener("ended", function () {
var current = document.querySelector("ol li.playing");
var next = current && current.nextElementSibling;
if (next) select(next, true);
});
document.addEventListener("keydown", function (e) {
var target = e.target || {};
var tag = (target.tagName || "").toLowerCase();
if (tag === "input" || tag === "textarea" || target.isContentEditable) return;
var current = document.querySelector("ol li.playing");
if (e.key === "ArrowRight") {
e.preventDefault();
select((current && current.nextElementSibling) || items[0], true);
} else if (e.key === "ArrowLeft") {
e.preventDefault();
select((current && current.previousElementSibling) || items[items.length - 1], true);
} else if (e.key === " " || e.key === "Spacebar") {
e.preventDefault();
player.playPause();
}
});
// Cue the first track without playing it, as the original page did.
select(items[0], false);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();