feat: add a built-in browser test client
- serve a single-page client at /ui (and redirect / to it) - login, folder switch, album grid, album songs, search and streaming - enabled by default, disable with WEB_UI=false - document the test client and WEB_UI
This commit is contained in:
1 parent
1571ec65ff
commit
26d31834a5
5 files changed
+334
-1
No files matched your search
@@ -0,0 +1,304 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>MusicManager</title>
|
||||
<style>
|
||||
:root { color-scheme: dark; --bg:#14161a; --panel:#1d2026; --line:#2d323b; --text:#e6e8eb; --muted:#9aa3af; --accent:#4f9cf9; }
|
||||
* { box-sizing: border-box; }
|
||||
body { margin:0; font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; background:var(--bg); color:var(--text); }
|
||||
header { display:flex; align-items:center; gap:12px; padding:12px 18px; border-bottom:1px solid var(--line); background:var(--panel); position:sticky; top:0; z-index:10; }
|
||||
header h1 { font-size:16px; margin:0; font-weight:600; }
|
||||
header .spacer { flex:1; }
|
||||
input, button, select { font:inherit; }
|
||||
input, select { background:#101216; color:var(--text); border:1px solid var(--line); border-radius:6px; padding:7px 9px; }
|
||||
button { background:#262b33; color:var(--text); border:1px solid var(--line); border-radius:6px; padding:7px 11px; cursor:pointer; }
|
||||
button:hover { border-color:var(--accent); }
|
||||
button.primary { background:var(--accent); border-color:var(--accent); color:#04101f; font-weight:600; }
|
||||
button.ghost { background:transparent; }
|
||||
main { padding:18px; padding-bottom:110px; }
|
||||
#login { max-width:360px; margin:12vh auto; background:var(--panel); border:1px solid var(--line); border-radius:10px; padding:22px; display:grid; gap:12px; }
|
||||
#login h2 { margin:0 0 4px; font-size:18px; }
|
||||
#login p { margin:0; color:var(--muted); font-size:13px; }
|
||||
.row { display:flex; gap:8px; flex-wrap:wrap; align-items:center; }
|
||||
.muted { color:var(--muted); font-size:13px; }
|
||||
.grid { display:grid; grid-template-columns:repeat(auto-fill, minmax(160px, 1fr)); gap:14px; margin-top:14px; }
|
||||
.album { background:var(--panel); border:1px solid var(--line); border-radius:8px; overflow:hidden; cursor:pointer; transition:border-color .12s; }
|
||||
.album:hover { border-color:var(--accent); }
|
||||
.album .cover { width:100%; aspect-ratio:1/1; display:block; object-fit:cover; background:#0e1013; }
|
||||
.album .meta { padding:9px 10px 11px; }
|
||||
.album .name { font-size:14px; font-weight:600; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
|
||||
.album .sub { color:var(--muted); font-size:12px; margin-top:2px; }
|
||||
table { width:100%; border-collapse:collapse; margin-top:12px; }
|
||||
th, td { text-align:left; padding:8px 10px; border-bottom:1px solid var(--line); font-size:14px; }
|
||||
th { color:var(--muted); font-weight:500; font-size:12px; text-transform:uppercase; letter-spacing:.04em; }
|
||||
tr.song { cursor:pointer; }
|
||||
tr.song:hover { background:#1b2028; }
|
||||
#player { position:fixed; left:0; right:0; bottom:0; background:var(--panel); border-top:1px solid var(--line); padding:10px 16px; display:flex; gap:14px; align-items:center; z-index:20; }
|
||||
#player .info { min-width:0; flex:1; }
|
||||
#player .title { font-size:14px; font-weight:600; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
|
||||
#player .sub { color:var(--muted); font-size:12px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
|
||||
audio { width:min(520px, 45vw); }
|
||||
#status { color:var(--muted); font-size:13px; min-height:18px; }
|
||||
#status.error { color:#ff7b72; }
|
||||
#crumb { cursor:pointer; color:var(--accent); }
|
||||
.hidden { display:none !important; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header id="topbar" class="hidden">
|
||||
<h1>MusicManager</h1>
|
||||
<button class="ghost" id="homeBtn">Albums</button>
|
||||
<input id="search" placeholder="Search" style="width:220px">
|
||||
<select id="folder"></select>
|
||||
<span class="spacer"></span>
|
||||
<span class="muted" id="whoami"></span>
|
||||
<button class="ghost" id="logout">Log out</button>
|
||||
</header>
|
||||
|
||||
<section id="login">
|
||||
<h2>MusicManager test client</h2>
|
||||
<p>Sign in with a user from users.yaml.</p>
|
||||
<input id="username" placeholder="Username" autocomplete="username">
|
||||
<input id="password" placeholder="Password" type="password" autocomplete="current-password">
|
||||
<button class="primary" id="loginBtn">Connect</button>
|
||||
<div id="loginStatus" class="muted"></div>
|
||||
</section>
|
||||
|
||||
<main id="app" class="hidden">
|
||||
<div class="row">
|
||||
<span id="title" class="muted" style="font-size:15px"></span>
|
||||
<span class="spacer" style="flex:1"></span>
|
||||
<span id="status"></span>
|
||||
</div>
|
||||
<div id="content"></div>
|
||||
</main>
|
||||
|
||||
<div id="player" class="hidden">
|
||||
<div class="info">
|
||||
<div class="title" id="npTitle">Nothing playing</div>
|
||||
<div class="sub" id="npSub"></div>
|
||||
</div>
|
||||
<audio id="audio" controls preload="none"></audio>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const state = {
|
||||
user: sessionStorage.getItem('mm.user') || '',
|
||||
pass: sessionStorage.getItem('mm.pass') || '',
|
||||
folders: [],
|
||||
folderId: '',
|
||||
};
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const esc = (value) => String(value ?? '').replace(/[&<>"']/g, (c) => ({
|
||||
'&': '&', '<': '<', '>': '>', '"': '"', "'": '''
|
||||
}[c]));
|
||||
|
||||
function authParams(extra) {
|
||||
return new URLSearchParams(Object.assign({
|
||||
u: state.user, p: state.pass, v: '1.16.1', c: 'webui', f: 'json',
|
||||
}, extra || {}));
|
||||
}
|
||||
|
||||
function apiUrl(endpoint, extra) {
|
||||
return `/rest/${endpoint}.view?${authParams(extra).toString()}`;
|
||||
}
|
||||
|
||||
async function api(endpoint, extra) {
|
||||
const response = await fetch(apiUrl(endpoint, extra));
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
const body = await response.json();
|
||||
const payload = body['subsonic-response'];
|
||||
if (!payload) throw new Error('Invalid response');
|
||||
if (payload.status !== 'ok') {
|
||||
throw new Error(`Subsonic ${payload.error?.code ?? '?'}: ${payload.error?.message ?? 'failed'}`);
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
function setStatus(message, isError) {
|
||||
const node = $('status');
|
||||
node.textContent = message || '';
|
||||
node.classList.toggle('error', !!isError);
|
||||
}
|
||||
|
||||
function setLoginStatus(message, isError) {
|
||||
const node = $('loginStatus');
|
||||
node.textContent = message || '';
|
||||
node.classList.toggle('error', !!isError);
|
||||
}
|
||||
|
||||
async function login() {
|
||||
state.user = $('username').value.trim();
|
||||
state.pass = $('password').value;
|
||||
if (!state.user || !state.pass) {
|
||||
setLoginStatus('Username and password are required', true);
|
||||
return;
|
||||
}
|
||||
setLoginStatus('Connecting...');
|
||||
try {
|
||||
await api('ping');
|
||||
sessionStorage.setItem('mm.user', state.user);
|
||||
sessionStorage.setItem('mm.pass', state.pass);
|
||||
await start();
|
||||
} catch (error) {
|
||||
setLoginStatus(error.message, true);
|
||||
}
|
||||
}
|
||||
|
||||
async function start() {
|
||||
const folders = (await api('getMusicFolders')).musicFolders?.musicFolder || [];
|
||||
state.folders = folders;
|
||||
const select = $('folder');
|
||||
select.innerHTML = folders.map((folder) =>
|
||||
`<option value="${esc(folder.id)}">${esc(folder.name)}</option>`).join('');
|
||||
state.folderId = folders.length ? String(folders[0].id) : '';
|
||||
select.value = state.folderId;
|
||||
|
||||
$('login').classList.add('hidden');
|
||||
$('app').classList.remove('hidden');
|
||||
$('topbar').classList.remove('hidden');
|
||||
$('player').classList.remove('hidden');
|
||||
$('whoami').textContent = state.user;
|
||||
setLoginStatus('');
|
||||
await showAlbums();
|
||||
}
|
||||
|
||||
async function showAlbums() {
|
||||
setStatus('Loading albums...');
|
||||
try {
|
||||
const params = { type: 'alphabeticalByName', size: '200' };
|
||||
if (state.folderId) params.musicFolderId = state.folderId;
|
||||
const albums = (await api('getAlbumList2', params)).albumList2?.album || [];
|
||||
$('title').textContent = 'Albums';
|
||||
$('content').innerHTML = albums.length ? `<div class="grid">${albums.map(albumCard).join('')}</div>` : '<p class="muted">No albums in this folder.</p>';
|
||||
setStatus(`${albums.length} albums`);
|
||||
} catch (error) {
|
||||
setStatus(error.message, true);
|
||||
}
|
||||
}
|
||||
|
||||
function albumCard(album) {
|
||||
const cover = album.coverArt
|
||||
? `<img class="cover" loading="lazy" src="${esc(apiUrl('getCoverArt', { id: album.coverArt, size: 300 }))}" alt="">`
|
||||
: '<div class="cover"></div>';
|
||||
return `<div class="album" data-album="${esc(album.id)}">
|
||||
${cover}
|
||||
<div class="meta">
|
||||
<div class="name">${esc(album.name)}</div>
|
||||
<div class="sub">${esc(album.artist || '')}${album.year ? ' - ' + esc(album.year) : ''}</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
async function showAlbum(id) {
|
||||
setStatus('Loading album...');
|
||||
try {
|
||||
const album = (await api('getAlbum', { id })).album;
|
||||
const songs = album.song || [];
|
||||
const cover = album.coverArt
|
||||
? `<img src="${esc(apiUrl('getCoverArt', { id: album.coverArt, size: 300 }))}" alt="" style="width:160px;height:160px;object-fit:cover;border-radius:8px">`
|
||||
: '';
|
||||
$('title').innerHTML = `<span id="crumb">Albums</span> / ${esc(album.name)}`;
|
||||
$('crumb').onclick = showAlbums;
|
||||
$('content').innerHTML = `
|
||||
<div class="row" style="margin-top:14px">
|
||||
${cover}
|
||||
<div>
|
||||
<div style="font-size:20px;font-weight:700">${esc(album.name)}</div>
|
||||
<div class="muted">${esc(album.artist || '')}</div>
|
||||
<div class="muted">${songs.length} songs - ${formatDuration(album.duration || 0)}</div>
|
||||
<div class="row" style="margin-top:10px">
|
||||
<button class="primary" id="playAll">Play all</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<table>
|
||||
<thead><tr><th>#</th><th>Title</th><th>Artist</th><th>Duration</th></tr></thead>
|
||||
<tbody>${songs.map(songRow).join('')}</tbody>
|
||||
</table>`;
|
||||
$('playAll').onclick = () => songs.length && playSong(songs[0]);
|
||||
setStatus(`${songs.length} songs`);
|
||||
} catch (error) {
|
||||
setStatus(error.message, true);
|
||||
}
|
||||
}
|
||||
|
||||
function songRow(song) {
|
||||
return `<tr class="song" data-song="${esc(song.id)}">
|
||||
<td>${esc(song.track ?? '')}</td>
|
||||
<td>${esc(song.title)}</td>
|
||||
<td class="muted">${esc(song.artist || '')}</td>
|
||||
<td class="muted">${formatDuration(song.duration || 0)}</td>
|
||||
</tr>`;
|
||||
}
|
||||
|
||||
function playSong(song) {
|
||||
$('audio').src = apiUrl('stream', { id: song.id });
|
||||
$('audio').play().catch(() => {});
|
||||
$('npTitle').textContent = song.title || '';
|
||||
$('npSub').textContent = [song.artist, song.album].filter(Boolean).join(' - ');
|
||||
}
|
||||
|
||||
async function search() {
|
||||
const query = $('search').value.trim();
|
||||
if (!query) return showAlbums();
|
||||
setStatus('Searching...');
|
||||
try {
|
||||
const result = (await api('search3', { query, artistCount: '10', albumCount: '50', songCount: '100' })).searchResult3 || {};
|
||||
const albums = result.album || [];
|
||||
const songs = result.song || [];
|
||||
$('title').textContent = `Search: ${query}`;
|
||||
$('content').innerHTML = `
|
||||
${albums.length ? `<h3>Albums</h3><div class="grid">${albums.map(albumCard).join('')}</div>` : ''}
|
||||
${songs.length ? `<h3>Songs</h3><table><tbody>${songs.map(songRow).join('')}</tbody></table>` : ''}
|
||||
${!albums.length && !songs.length ? '<p class="muted">Nothing found.</p>' : ''}`;
|
||||
setStatus(`${albums.length} albums, ${songs.length} songs`);
|
||||
} catch (error) {
|
||||
setStatus(error.message, true);
|
||||
}
|
||||
}
|
||||
|
||||
function formatDuration(seconds) {
|
||||
const total = Math.max(0, Math.floor(seconds || 0));
|
||||
return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function logout() {
|
||||
sessionStorage.removeItem('mm.user');
|
||||
sessionStorage.removeItem('mm.pass');
|
||||
location.reload();
|
||||
}
|
||||
|
||||
document.addEventListener('click', (event) => {
|
||||
const album = event.target.closest('[data-album]');
|
||||
if (album) { showAlbum(album.dataset.album); return; }
|
||||
const song = event.target.closest('[data-song]');
|
||||
if (song && song.dataset.song) {
|
||||
const id = song.dataset.song;
|
||||
const row = song;
|
||||
playSong({
|
||||
id,
|
||||
title: row.children[1]?.textContent || '',
|
||||
artist: row.children[2]?.textContent || '',
|
||||
album: '',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$('loginBtn').onclick = login;
|
||||
$('password').addEventListener('keydown', (event) => { if (event.key === 'Enter') login(); });
|
||||
$('homeBtn').onclick = showAlbums;
|
||||
$('logout').onclick = logout;
|
||||
$('search').addEventListener('keydown', (event) => { if (event.key === 'Enter') search(); });
|
||||
$('folder').onchange = () => { state.folderId = $('folder').value; showAlbums(); };
|
||||
|
||||
if (state.user && state.pass) start().catch((error) => {
|
||||
$('login').classList.remove('hidden');
|
||||
setLoginStatus(error.message, true);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user