// NAME: Listening List // AUTHOR: yusufaf // x-release-please-start-version // VERSION: 1.1.0 // x-release-please-end-version // DESCRIPTION: Mark albums and tracks as listened, with inline indicators across Spotify. (function () { 'use strict'; //#region Type Definitions /** * @typedef {Object} ListenedRecord * @property {number} listenedAt - ms epoch when marked * @property {"manual"|"auto-playlist"|"auto-play"|"import"} source */ /** * @typedef {Object} WantedRecord * @property {number} addedAt - ms epoch when added to the want-to-listen list * @property {"manual"|"import"} source */ /** * @typedef {Object} ListenedData * @property {number} schemaVersion * @property {Record} albums - keyed by full URI * @property {Record} tracks - keyed by full URI * @property {Record} wanted - album URIs on the want-to-listen list; never overlaps `albums` */ /** * @typedef {Object} ListenedConfig * @property {number} schemaVersion * @property {{ tracklistRows: boolean, albumHeader: boolean, albumCards: boolean, nowPlaying: boolean }} surfaces * @property {"checkmark"|"dot"|"text"} badgeStyle * @property {{ enabled: boolean, minTracksPerAlbum: number, lastSeededAt: number|null }} autoSeed * @property {{ enabled: boolean, percentThreshold: number }} autoOnPlay */ //#endregion //#region Constants /** LocalStorage keys */ const LL_DATA_KEY = 'listening-list-data'; const LL_CONFIG_KEY = 'listening-list-config'; /** Current schema versions */ const LL_DATA_SCHEMA_VERSION = 2; const LL_CONFIG_SCHEMA_VERSION = 1; /** DOM IDs / classes (unique-prefixed) */ const LL_BADGE_CLASS = 'll-badge'; const LL_BADGE_WANTED_CLASS = 'll-badge--wanted'; const LL_BADGE_TRACKLIST_CLASS = 'll-badge--tracklist'; const LL_BADGE_HEADER_CLASS = 'll-badge--header'; const LL_BADGE_CARD_CLASS = 'll-badge--card'; const LL_BADGE_CARD_WRAPPER_CLASS = 'll-badge-card-wrapper'; const LL_BADGE_NOWPLAYING_CLASS = 'll-badge--nowplaying'; const LL_STYLE_ID = 'll-main-styles'; const LL_MODAL_ROOT_ID = 'll-modal-root'; /** Default configuration */ const LL_DEFAULT_CONFIG = Object.freeze({ schemaVersion: LL_CONFIG_SCHEMA_VERSION, surfaces: { tracklistRows: true, albumHeader: true, albumCards: true, nowPlaying: true, }, badgeStyle: 'checkmark', autoSeed: { enabled: false, minTracksPerAlbum: 3, lastSeededAt: null, }, autoOnPlay: { enabled: false, percentThreshold: 70, }, }); /** Empty data object */ function llEmptyData() { return { schemaVersion: LL_DATA_SCHEMA_VERSION, albums: {}, tracks: {}, wanted: {} }; } /** Checkmark icon (16x16 viewBox) */ const LL_CHECK_SVG_PATH = 'M13.485 1.929a1 1 0 0 1 0 1.414L6.414 10.414a1 1 0 0 1-1.414 0L1.515 6.929a1 1 0 1 1 1.414-1.414L5.707 8.293l6.364-6.364a1 1 0 0 1 1.414 0z'; /** Bookmark icon (16x16 viewBox, Bootstrap Icons bookmark-fill, MIT) — want-to-listen badge */ const LL_BOOKMARK_SVG_PATH = 'M2 2v13.5a.5.5 0 0 0 .74.439L8 13.069l5.26 2.87A.5.5 0 0 0 14 15.5V2a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2z'; /** Gear icon (16x16 viewBox) */ const LL_GEAR_SVG_PATH = 'M9.405 1.05c-.413-1.4-2.397-1.4-2.81 0l-.1.34a1.464 1.464 0 0 1-2.105.872l-.31-.17c-1.283-.698-2.686.705-1.987 1.987l.169.311c.446.82.023 1.841-.872 2.105l-.34.1c-1.4.413-1.4 2.397 0 2.81l.34.1a1.464 1.464 0 0 1 .872 2.105l-.17.31c-.698 1.283.705 2.686 1.987 1.987l.311-.169a1.464 1.464 0 0 1 2.105.872l.1.34c.413 1.4 2.397 1.4 2.81 0l.1-.34a1.464 1.464 0 0 1 2.105-.872l.31.17c1.283.698 2.686-.705 1.987-1.987l-.169-.311a1.464 1.464 0 0 1 .872-2.105l.34-.1c1.4-.413 1.4-2.397 0-2.81l-.34-.1a1.464 1.464 0 0 1-.872-2.105l.17-.31c.698-1.283-.705-2.686-1.987-1.987l-.311.169a1.464 1.464 0 0 1-2.105-.872zM8 10.93a2.929 2.929 0 1 1 0-5.858 2.929 2.929 0 0 1 0 5.858z'; /** Export schema version (matches data schema for now) */ const LL_EXPORT_SCHEMA_VERSION = 2; /** Metadata cache LocalStorage key (name/artist by URI) */ const LL_META_KEY = 'listening-list-meta'; /** Read by the shared footer. release-please keeps `version` in sync with the VERSION banner. */ const EXT = { name: 'Listening List', slug: 'listening-list', // x-release-please-start-version version: '1.1.0', // x-release-please-end-version footerPrefix: 'll-modal', }; // @shared footer.js start — edit shared/footer.js, then run pnpm sync /** * Settings footer shared by every extension: version, Docs, More extensions and a * report-an-issue link. Reads the file's EXT config; class names are built from * EXT.footerPrefix (`${footerPrefix}-footer`, `-footer-links`, `-version`, `-github-link`, `-github-icon`). */ const SHARED_HUB_URL = 'https://spicetify.yusufaf.dev/'; /** GitHub mark icon path (16x16 viewBox, Bootstrap Icons github, MIT) */ const SHARED_GITHUB_ICON_PATH = 'M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z'; /** @param {{ name: string, slug: string, version: string, footerPrefix: string }} ext */ function sharedFooterHtml(ext) { const p = ext.footerPrefix; const issueUrl = `https://github.com/yusufaf/spicetify-extensions/issues/new?template=${ext.slug}.yml`; return ` `; } /** * CSS for sharedFooterHtml, to append to the extension's own stylesheet. * `sticky` pins the footer to the bottom of Spicetify's PopupModal while the content scrolls. * @param {{ footerPrefix: string }} ext * @param {{ sticky?: boolean }} [options] */ function sharedFooterCss(ext, { sticky = false } = {}) { const p = ext.footerPrefix; const stickyRules = sticky ? ` position: sticky; bottom: 0; padding: 10px 0 2px; /* Matches Spicetify's popup container, so the sticky footer blends in under any theme */ background: var(--spice-player, var(--background-elevated-base, #121212));` : ''; return ` .${p}-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; border-top: 1px solid rgba(255, 255, 255, 0.1);${stickyRules} } .${p}-version { color: var(--spice-subtext, #b3b3b3); font-size: 0.75rem; } .${p}-footer-links { display: inline-flex; align-items: center; gap: 12px; flex-shrink: 0; white-space: nowrap; } .${p}-footer a[href] { display: inline-flex; align-items: center; color: var(--spice-subtext, #b3b3b3); font-size: 0.75rem; text-decoration: none; } .${p}-footer a[href]:hover { color: var(--spice-text, #fff); text-decoration: underline; } .${p}-footer a[href]:focus-visible { outline: 2px solid var(--spice-text, #fff); outline-offset: 2px; border-radius: 2px; } .${p}-github-icon { width: 16px; height: 16px; fill: currentColor; } `; } // @shared footer.js end const LL_BASE_CSS = ` .ll-badge { display: inline-flex; align-items: center; justify-content: center; color: var(--spice-button, #1ed760); pointer-events: none; } .ll-badge--tracklist { width: 14px; height: 14px; margin-right: 6px; vertical-align: middle; } .ll-badge--header { width: 18px; height: 18px; margin-right: 8px; flex: none; vertical-align: middle; } /* The metadata line draws "•" before each item that isn't first; the badge takes that slot, so drop the separator it would push onto the next item. */ .ll-badge--header + *::before { content: none !important; } .ll-badge-card-wrapper { position: relative; height: 0; width: 0; overflow: visible; pointer-events: none; z-index: 100; } .ll-badge--card { position: absolute; top: 10px; left: 10px; width: 22px; height: 22px; background: rgba(0,0,0,0.6); border-radius: 50%; padding: 3px; box-sizing: border-box; } .ll-badge--nowplaying { width: 12px; height: 12px; margin-right: 6px; vertical-align: middle; } .ll-badge--style-dot svg { display: none; } .ll-badge--style-dot::after { content: ""; display: block; width: 6px; height: 6px; border-radius: 50%; background: var(--spice-button, #1ed760); } .ll-badge--style-text svg { display: none; } .ll-badge--style-text::after { content: "✓"; font-size: 11px; line-height: 1; color: var(--spice-button, #1ed760); } .ll-badge--wanted { color: var(--spice-subtext, #b3b3b3); } .ll-badge--wanted.ll-badge--style-dot::after { background: transparent; border: 1.5px solid currentColor; box-sizing: border-box; } .ll-badge--wanted.ll-badge--style-text::after { content: "⚑"; color: currentColor; } `; function llEnsureStyles() { if (document.getElementById(LL_STYLE_ID)) return; const s = document.createElement('style'); s.id = LL_STYLE_ID; s.textContent = LL_BASE_CSS; document.head.appendChild(s); } /** * @param {string} extraClass - surface size class * @param {"listened"|"wanted"} [variant] */ function llBadgeMarkup(extraClass, variant = 'listened') { const styleClass = llConfig.badgeStyle === 'dot' ? ' ll-badge--style-dot' : llConfig.badgeStyle === 'text' ? ' ll-badge--style-text' : ''; const wanted = variant === 'wanted'; const variantClass = wanted ? ` ${LL_BADGE_WANTED_CLASS}` : ''; const label = wanted ? 'Want to listen' : 'Listened'; const path = wanted ? LL_BOOKMARK_SVG_PATH : LL_CHECK_SVG_PATH; return ``; } /** Variant of an existing badge element, for comparing against the desired state. */ function llBadgeVariant(el) { return el?.classList.contains(LL_BADGE_WANTED_CLASS) ? 'wanted' : 'listened'; } //#endregion //#region State /** @type {ListenedData} */ let llData = llEmptyData(); /** @type {ListenedConfig} */ let llConfig = JSON.parse(JSON.stringify(LL_DEFAULT_CONFIG)); /** Metadata cache: { albums: { uri: { name, artist } }, tracks: { uri: { name, artist } } } */ let llMeta = { albums: {}, tracks: {} }; /** In-flight fetches to avoid duplicate requests */ const llMetaInflight = new Set(); /** Internal event bus for "data changed" — subscribers re-render their surfaces. */ const llListeners = new Set(); function llEmit() { for (const fn of llListeners) { try { fn(); } catch (e) { console.error('[Listening List] listener error', e); } } } function llSubscribe(fn) { llListeners.add(fn); return () => llListeners.delete(fn); } //#endregion //#region Storage function llLoadData() { const raw = Spicetify.LocalStorage.get(LL_DATA_KEY); if (!raw) return llEmptyData(); try { const parsed = JSON.parse(raw); return llMigrateData(parsed); } catch (e) { console.error('[Listening List] Failed to parse data; using empty. Raw:', raw, e); return llEmptyData(); } } function llSaveData() { try { Spicetify.LocalStorage.set(LL_DATA_KEY, JSON.stringify(llData)); } catch (e) { console.error('[Listening List] Failed to save data', e); Spicetify.showNotification?.('Listening List: failed to save data'); } } function llMigrateData(parsed) { if (!parsed || typeof parsed !== 'object') return llEmptyData(); const v = parsed.schemaVersion ?? 0; if (v > LL_DATA_SCHEMA_VERSION) { console.warn(`[Listening List] Data schema v${v} newer than supported v${LL_DATA_SCHEMA_VERSION}; refusing to write.`); return { ...llEmptyData(), schemaVersion: v, __readOnly: true }; } const side = (o) => (o && typeof o === 'object' ? o : {}); // v0/v1 → v2: `wanted` map added. Older data simply lacks it. const data = { schemaVersion: LL_DATA_SCHEMA_VERSION, albums: side(parsed.albums), tracks: side(parsed.tracks), wanted: v >= 2 ? side(parsed.wanted) : {}, }; // Listened wins: an album can't be both listened and wanted. for (const uri of Object.keys(data.wanted)) if (data.albums[uri]) delete data.wanted[uri]; return data; } function llLoadConfig() { const raw = Spicetify.LocalStorage.get(LL_CONFIG_KEY); if (!raw) return JSON.parse(JSON.stringify(LL_DEFAULT_CONFIG)); try { const parsed = JSON.parse(raw); return llMigrateConfig(parsed); } catch (e) { console.error('[Listening List] Failed to parse config; using defaults. Raw:', raw, e); return JSON.parse(JSON.stringify(LL_DEFAULT_CONFIG)); } } function llSaveConfig() { try { Spicetify.LocalStorage.set(LL_CONFIG_KEY, JSON.stringify(llConfig)); } catch (e) { console.error('[Listening List] Failed to save config', e); } } function llMigrateConfig(parsed) { if (!parsed || typeof parsed !== 'object') return JSON.parse(JSON.stringify(LL_DEFAULT_CONFIG)); return llDeepMerge(LL_DEFAULT_CONFIG, parsed, { schemaVersion: LL_CONFIG_SCHEMA_VERSION }); } function llLoadMeta() { const raw = Spicetify.LocalStorage.get(LL_META_KEY); if (!raw) return { albums: {}, tracks: {} }; try { const parsed = JSON.parse(raw); const albums = parsed.albums && typeof parsed.albums === 'object' ? parsed.albums : {}; const tracks = parsed.tracks && typeof parsed.tracks === 'object' ? parsed.tracks : {}; for (const side of [albums, tracks]) { for (const k of Object.keys(side)) { const m = side[k]; if (!m || typeof m !== 'object' || !('artistUri' in m)) delete side[k]; } } return { albums, tracks }; } catch { return { albums: {}, tracks: {} }; } } function llSaveMeta() { try { Spicetify.LocalStorage.set(LL_META_KEY, JSON.stringify(llMeta)); } catch (e) { console.error('[Listening List] Failed to save meta', e); } } async function llFetchOneAlbumMeta(uri) { const id = uri.split(':').pop(); try { const res = await Spicetify.CosmosAsync.get(`wg://album/v1/album-app/album/${id}/desktop`); if (res?.name) { const a0 = res.artists?.[0]; return { name: res.name, artist: a0?.name || '', artistUri: a0?.uri || '' }; } } catch {} try { if (Spicetify.GraphQL?.Request && Spicetify.GraphQL?.Definitions?.getAlbum) { const res = await Spicetify.GraphQL.Request( Spicetify.GraphQL.Definitions.getAlbum, { uri, locale: Spicetify.Locale?.getLocale?.() || 'en', limit: 1, offset: 0 }, ); const a = res?.data?.albumUnion; if (a?.name) { const ar = a.artists?.items?.[0]; return { name: a.name, artist: ar?.profile?.name || '', artistUri: ar?.uri || '' }; } } } catch {} return null; } async function llFetchOneTrackMeta(uri) { try { if (Spicetify.GraphQL?.Request && Spicetify.GraphQL?.Definitions?.getTrack) { const res = await Spicetify.GraphQL.Request( Spicetify.GraphQL.Definitions.getTrack, { uri, locale: Spicetify.Locale?.getLocale?.() || 'en' }, ); const t = res?.data?.trackUnion; if (t?.name) { const ar = t.artists?.items?.[0] || t.firstArtist?.items?.[0]; return { name: t.name, artist: ar?.profile?.name || '', artistUri: ar?.uri || '' }; } } } catch {} return null; } async function llFetchMetadata(uris, kind, onItem, onDone) { const target = kind === 'albums' ? llMeta.albums : llMeta.tracks; const need = uris.filter((u) => !target[u]); if (need.length === 0) { onDone?.(0); return; } const fetcher = kind === 'albums' ? llFetchOneAlbumMeta : llFetchOneTrackMeta; const concurrency = 4; let cursor = 0; let saveAt = 0; let completed = 0; async function worker() { while (cursor < need.length) { const uri = need[cursor++]; if (target[uri]) { completed++; try { onItem?.(uri, target[uri], completed, need.length); } catch {} continue; } const meta = await fetcher(uri); if (meta) target[uri] = meta; completed++; saveAt++; if (saveAt >= 10) { saveAt = 0; llSaveMeta(); } try { onItem?.(uri, meta, completed, need.length); } catch (e) { console.warn('[Listening List] onItem error', e); } } } try { await Promise.all(Array.from({ length: concurrency }, worker)); } finally { llSaveMeta(); try { onDone?.(need.length); } catch (e) { console.warn('[Listening List] onDone error', e); } } } function llDeepMerge(base, override, ...extras) { const out = Array.isArray(base) ? base.slice() : { ...base }; if (override && typeof override === 'object') { for (const k of Object.keys(override)) { const bv = out[k]; const ov = override[k]; if (bv && typeof bv === 'object' && !Array.isArray(bv) && ov && typeof ov === 'object' && !Array.isArray(ov)) { out[k] = llDeepMerge(bv, ov); } else if (ov !== undefined) { out[k] = ov; } } } for (const extra of extras) Object.assign(out, extra); return out; } //#endregion //#region URI Helpers function llParseUri(uri) { if (typeof uri !== 'string') return null; try { return Spicetify.URI.fromString(uri); } catch { return null; } } function llIsAlbumUri(uri) { const p = llParseUri(uri); return !!p && p.type === Spicetify.URI.Type.ALBUM; } function llIsTrackUri(uri) { const p = llParseUri(uri); return !!p && p.type === Spicetify.URI.Type.TRACK; } function llNormalizeUri(input) { if (!input) return null; if (typeof input === 'string' && input.startsWith('spotify:')) return input; const m = typeof input === 'string' && input.match(/\/(album|track)\/([A-Za-z0-9]+)/); if (m) return `spotify:${m[1]}:${m[2]}`; return null; } //#endregion //#region Marking function llIsAlbumListened(uri) { return !!llData.albums[uri]; } function llIsTrackListened(uri) { return !!llData.tracks[uri]; } function llIsAlbumWanted(uri) { return !!llData.wanted[uri]; } function llRefuseIfReadOnly() { if (!llData.__readOnly) return false; Spicetify.showNotification?.('Listening List: data is read-only (schema newer than this version)'); return true; } function llMarkOne(uri, source) { if (llRefuseIfReadOnly()) return false; const norm = llNormalizeUri(uri); if (!norm) return false; const rec = { listenedAt: Date.now(), source }; if (llIsAlbumUri(norm)) { if (llData.albums[norm]) return false; llData.albums[norm] = rec; delete llData.wanted[norm]; return true; } if (llIsTrackUri(norm)) { if (llData.tracks[norm]) return false; llData.tracks[norm] = rec; return true; } return false; } function llUnmarkOne(uri) { const norm = llNormalizeUri(uri); if (!norm) return false; if (llIsAlbumUri(norm) && llData.albums[norm]) { delete llData.albums[norm]; return true; } if (llIsTrackUri(norm) && llData.tracks[norm]) { delete llData.tracks[norm]; return true; } return false; } /** * @param {string[]} uris * @param {"manual"|"auto-playlist"|"auto-play"|"import"} source */ function llMarkMany(uris, source) { let marked = 0, skipped = 0; for (const u of uris) (llMarkOne(u, source) ? marked++ : skipped++); if (marked > 0) { llSaveData(); llEmit(); } return { marked, skipped }; } function llUnmarkMany(uris) { let removed = 0; for (const u of uris) if (llUnmarkOne(u)) removed++; if (removed > 0) { llSaveData(); llEmit(); } return removed; } /** Add an album to the want-to-listen list. Albums only; already-listened albums are refused. */ function llWantOne(uri, source) { if (llRefuseIfReadOnly()) return false; const norm = llNormalizeUri(uri); if (!norm || !llIsAlbumUri(norm)) return false; if (llData.albums[norm] || llData.wanted[norm]) return false; llData.wanted[norm] = { addedAt: Date.now(), source }; return true; } function llUnwantOne(uri) { const norm = llNormalizeUri(uri); if (!norm || !llIsAlbumUri(norm) || !llData.wanted[norm]) return false; delete llData.wanted[norm]; return true; } /** * @param {string[]} uris * @param {"manual"|"import"} source */ function llWantMany(uris, source) { let added = 0, skipped = 0; for (const u of uris) (llWantOne(u, source) ? added++ : skipped++); if (added > 0) { llSaveData(); llEmit(); } return { added, skipped }; } function llUnwantMany(uris) { let removed = 0; for (const u of uris) if (llUnwantOne(u)) removed++; if (removed > 0) { llSaveData(); llEmit(); } return removed; } /** "listened" beats "wanted"; null when neither. */ function llAlbumState(uri) { if (!uri) return null; if (llIsAlbumListened(uri)) return 'listened'; if (llIsAlbumWanted(uri)) return 'wanted'; return null; } //#endregion //#region Surfaces / Tracklist Rows let llTracklistObserver = null; let llTracklistUnsub = null; let llTracklistHistoryUnlisten = null; function llStartTracklistSurface() { if (!llConfig.surfaces.tracklistRows) return; llEnsureStyles(); llDecorateAllTracklistRows(); llTracklistObserver = new MutationObserver(() => llDecorateAllTracklistRows()); llTracklistObserver.observe(document.body, { childList: true, subtree: true }); llTracklistUnsub = llSubscribe(() => llDecorateAllTracklistRows()); llTracklistHistoryUnlisten = Spicetify.Platform?.History?.listen?.(() => llDecorateAllTracklistRows()) || null; } function llStopTracklistSurface() { llTracklistObserver?.disconnect(); llTracklistObserver = null; llTracklistUnsub?.(); llTracklistUnsub = null; llTracklistHistoryUnlisten?.(); llTracklistHistoryUnlisten = null; document.querySelectorAll(`.${LL_BADGE_TRACKLIST_CLASS}`).forEach((el) => el.remove()); document.querySelectorAll('[data-ll-status]').forEach((row) => delete row.dataset.llStatus); } function llDecorateAllTracklistRows() { const rows = document.querySelectorAll('.main-trackList-trackListRow, [data-testid="tracklist-row"], #main-view [role="grid"] [role="row"][aria-rowindex] > [role="presentation"]'); rows.forEach(llDecorateTracklistRow); } function llDecorateTracklistRow(row) { const anchors = row.querySelectorAll('a[href^="/track/"], a[href^="/album/"]'); let uri = null; for (const a of anchors) { uri = llNormalizeUri(a.getAttribute('href')); if (uri) break; } let existing = row.querySelector(`.${LL_BADGE_TRACKLIST_CLASS}`); const ambientAlbum = llCurrentAlbumUriFromRoute(); const listened = (uri && (llIsAlbumListened(uri) || llIsTrackListened(uri))) || (!!ambientAlbum && llIsAlbumListened(ambientAlbum)); // Want-to-listen is album-level: the row's own album link (playlist/search // rows) or the album page being viewed. const rowAlbum = !listened && llNormalizeUri(row.querySelector('a[href^="/album/"]')?.getAttribute('href')); const wanted = !listened && ((!!rowAlbum && llIsAlbumWanted(rowAlbum)) || (!!ambientAlbum && llIsAlbumWanted(ambientAlbum))); const state = listened ? 'listened' : wanted ? 'wanted' : null; if (existing && state && llBadgeVariant(existing) !== state) { existing.remove(); existing = null; } if (state) { if (!existing) { const titleSlot = row.querySelector('.main-trackList-rowMainContentTitle, [data-testid="internal-track-link"], .main-trackList-rowTitle, [data-testid="tracklist-row-title"], [role="gridcell"][aria-colindex="2"] div[data-encore-id="text"]'); if (titleSlot) { const wrap = document.createElement('span'); wrap.innerHTML = llBadgeMarkup(LL_BADGE_TRACKLIST_CLASS, state); titleSlot.prepend(wrap.firstElementChild); } } } else if (existing) { existing.remove(); } row.dataset.llStatus = listened ? '1' : wanted ? 'w' : '0'; } function llRowNeedsRefresh(row) { return false; } //#endregion //#region Surfaces / Album Header let llHeaderObserver = null; let llHeaderHistoryUnlisten = null; let llHeaderUnsub = null; function llStartAlbumHeaderSurface() { if (!llConfig.surfaces.albumHeader) return; llEnsureStyles(); const tick = () => llDecorateAlbumHeader(); tick(); llHeaderObserver = new MutationObserver(tick); llHeaderObserver.observe(document.body, { childList: true, subtree: true }); llHeaderHistoryUnlisten = Spicetify.Platform?.History?.listen?.(tick) || null; llHeaderUnsub = llSubscribe(tick); } function llStopAlbumHeaderSurface() { llHeaderObserver?.disconnect(); llHeaderObserver = null; llHeaderHistoryUnlisten?.(); llHeaderHistoryUnlisten = null; llHeaderUnsub?.(); llHeaderUnsub = null; document.querySelectorAll(`.${LL_BADGE_HEADER_CLASS}`).forEach((el) => el.remove()); } function llCurrentAlbumUriFromRoute() { const path = Spicetify.Platform?.History?.location?.pathname || location.pathname; const m = path.match(/^\/album\/([A-Za-z0-9]+)/); return m ? `spotify:album:${m[1]}` : null; } function llDecorateAlbumHeader() { const uri = llCurrentAlbumUriFromRoute(); const host = llHeaderBadgeHost(); const state = host ? llAlbumState(uri) : null; const current = host?.querySelector(`.${LL_BADGE_HEADER_CLASS}`) || null; // A badge of the wrong variant (want → listened flip) counts as stale. const existing = current && llBadgeVariant(current) === state ? current : null; // Drop badges stranded on a previous header (navigation) or on an album no // longer marked, but leave a correct badge alone. This tick runs from a // MutationObserver, so removing and re-appending unconditionally would either // loop forever or — with a sticky guard flag — delete the badge and refuse to // rebuild it. document.querySelectorAll(`.${LL_BADGE_HEADER_CLASS}`).forEach((el) => { if (!state || el !== existing) el.remove(); }); if (!state || existing) return; const span = document.createElement('span'); span.innerHTML = llBadgeMarkup(LL_BADGE_HEADER_CLASS, state); const badge = span.firstElementChild; if (state === 'listened') { const rec = llData.albums[uri]; if (rec?.listenedAt) badge.setAttribute('title', `Listened on ${new Date(rec.listenedAt).toLocaleDateString()}`); } else { const rec = llData.wanted[uri]; if (rec?.addedAt) badge.setAttribute('title', `Want to listen · added ${new Date(rec.addedAt).toLocaleDateString()}`); } host.insertBefore(badge, host.firstChild); } /** * Where the album-header badge goes. The metadata line ("artist • year • N songs") * is a centred flex row with room to spare, so an inline badge costs no layout. * The title heading is not usable: it's a `-webkit-box` with `-webkit-line-clamp`, * where every child becomes its own row — a badge there either pushes the whole * page down by its height or falls past the clamp and gets cut off. So there is * no heading fallback: if no metadata line is found the header badge is skipped * and the other surfaces carry the state. */ function llHeaderBadgeHost() { return llQueryFirst(['.main-entityHeader-metaData', '.main-entityHeader-subtitle']); } //#endregion //#region Surfaces / Album Cards let llCardObserver = null; let llCardHistoryUnlisten = null; let llCardUnsub = null; function llStartAlbumCardSurface() { if (!llConfig.surfaces.albumCards) return; llEnsureStyles(); const tick = () => llDecorateAllAlbumCards(); tick(); llCardObserver = new MutationObserver(tick); llCardObserver.observe(document.body, { childList: true, subtree: true }); llCardHistoryUnlisten = Spicetify.Platform?.History?.listen?.(tick) || null; llCardUnsub = llSubscribe(tick); } function llStopAlbumCardSurface() { llCardObserver?.disconnect(); llCardObserver = null; llCardHistoryUnlisten?.(); llCardHistoryUnlisten = null; llCardUnsub?.(); llCardUnsub = null; document.querySelectorAll(`.${LL_BADGE_CARD_WRAPPER_CLASS}, .${LL_BADGE_CARD_CLASS}`).forEach((el) => el.remove()); } function llDecorateAllAlbumCards() { const candidates = document.querySelectorAll('[data-testid="card-click-handler"], [data-encore-id="card"]'); candidates.forEach((c) => llDecorateAlbumCard(c)); document.querySelectorAll('a[href^="/album/"]').forEach((a) => { const card = a.closest('[data-testid="card-click-handler"], [data-encore-id="card"], .main-card-card'); if (card) llDecorateAlbumCard(card, a); }); } function llDecorateAlbumCard(card, anchorHint) { const anchor = anchorHint || card.querySelector('a[href^="/album/"]'); if (!anchor) return; const uri = llNormalizeUri(anchor.getAttribute('href')); if (!uri) return; const state = llAlbumState(uri); let existing = card.querySelector(`.${LL_BADGE_CARD_WRAPPER_CLASS}`); if (existing && state && llBadgeVariant(existing.firstElementChild) !== state) { existing.remove(); existing = null; } if (state && !existing) { const wrapper = document.createElement('div'); wrapper.className = LL_BADGE_CARD_WRAPPER_CLASS; wrapper.innerHTML = llBadgeMarkup(LL_BADGE_CARD_CLASS, state); card.insertBefore(wrapper, card.firstChild); } else if (!state && existing) { existing.remove(); } } //#endregion //#region Surfaces / Now Playing let llNowPlayingHandler = null; let llNowPlayingUnsub = null; function llStartNowPlayingSurface() { if (!llConfig.surfaces.nowPlaying) return; llEnsureStyles(); const tick = () => llDecorateNowPlaying(); tick(); llNowPlayingHandler = tick; Spicetify.Player.addEventListener('songchange', llNowPlayingHandler); llNowPlayingUnsub = llSubscribe(tick); } function llStopNowPlayingSurface() { if (llNowPlayingHandler) { Spicetify.Player.removeEventListener('songchange', llNowPlayingHandler); llNowPlayingHandler = null; } llNowPlayingUnsub?.(); llNowPlayingUnsub = null; document.querySelectorAll(`.${LL_BADGE_NOWPLAYING_CLASS}`).forEach((el) => el.remove()); } function llDecorateNowPlaying() { document.querySelectorAll(`.${LL_BADGE_NOWPLAYING_CLASS}`).forEach((el) => el.remove()); const item = Spicetify.Player?.data?.item; if (!item) return; const trackUri = item.uri; const albumUri = item.album?.uri; const listened = (trackUri && llIsTrackListened(trackUri)) || (albumUri && llIsAlbumListened(albumUri)); const state = listened ? 'listened' : (albumUri && llIsAlbumWanted(albumUri)) ? 'wanted' : null; if (!state) return; const titleEl = llQueryFirst([ '.main-trackInfo-name', '[data-testid="context-item-info-title"]', '[data-testid="now-playing-widget"] a[href^="/album/"]', '[data-testid="now-playing-widget"] a[href^="/track/"]', '[data-testid="now-playing-widget"] a', ]); if (!titleEl) return; const span = document.createElement('span'); span.innerHTML = llBadgeMarkup(LL_BADGE_NOWPLAYING_CLASS, state); const leaf = llTextLeaf(titleEl); leaf.insertBefore(span.firstElementChild, leaf.firstChild); } /** * First selector in the list that matches. `querySelector` with a comma-joined * list returns the match earliest in the document, not the earliest selector, * so it hands back the outer wrapper whenever one is listed alongside its child. */ function llQueryFirst(selectors, root = document) { for (const sel of selectors) { const el = root.querySelector(sel); if (el) return el; } return null; } /** * The deepest descendant that actually holds the text, so an inline badge shares * its line. Appended to a wrapper whose child is a block, the badge gets a line * of its own and grows the container by its own height. Deepest rather than last * so a trailing label ("E", a marquee clone) can't capture the badge. */ function llTextLeaf(el) { const holdsText = (e) => [...e.childNodes].some((n) => n.nodeType === Node.TEXT_NODE && n.data.trim()); let best = null, bestDepth = -1; for (const cand of el.querySelectorAll('*')) { if (!holdsText(cand)) continue; let depth = 0; for (let p = cand.parentElement; p && p !== el; p = p.parentElement) depth++; if (depth > bestDepth) { best = cand; bestDepth = depth; } } return best || el; } //#endregion //#region Auto-Seed async function llRunAutoSeed() { const rootlist = Spicetify.Platform?.RootlistAPI; const plAPI = Spicetify.Platform?.PlaylistAPI; if (!rootlist || !plAPI) { Spicetify.showNotification?.('Playlist API unavailable on this Spotify version'); throw new Error('Platform.RootlistAPI or PlaylistAPI missing'); } const root = await rootlist.getContents(); const playlistUris = llCollectPlaylistUris(root); const albumCounts = new Map(); let playlistsScanned = 0; for (const plUri of playlistUris) { try { const contents = await plAPI.getContents(plUri); const items = contents?.items || contents?.rows || []; for (const it of items) { const albumUri = it?.album?.uri || it?.albumOfTrack?.uri || it?.item?.album?.uri; if (albumUri && albumUri.startsWith('spotify:album:')) { albumCounts.set(albumUri, (albumCounts.get(albumUri) || 0) + 1); } } playlistsScanned++; } catch (e) { console.warn('[Listening List] Failed to read playlist', plUri, e); } } const min = Math.max(1, llConfig.autoSeed.minTracksPerAlbum); const toMark = []; for (const [uri, count] of albumCounts) { if (count >= min) toMark.push(uri); } const { marked } = llMarkMany(toMark, 'auto-playlist'); llConfig.autoSeed.lastSeededAt = Date.now(); llSaveConfig(); return { playlistsScanned, candidates: toMark.length, markedAlbums: marked }; } function llCollectPlaylistUris(node) { const out = []; if (!node) return out; if (node.type === 'playlist' && node.uri) out.push(node.uri); const children = node.items || node.rows || node.children || []; for (const c of children) out.push(...llCollectPlaylistUris(c)); return out; } //#endregion //#region Auto-On-Play let llAOPProgressHandler = null; let llAOPSongChangeHandler = null; let llAOPCurrentMarked = false; function llStartAutoOnPlay() { if (!llConfig.autoOnPlay.enabled) return; llAOPCurrentMarked = false; llAOPSongChangeHandler = () => { llAOPCurrentMarked = false; }; llAOPProgressHandler = (ev) => { if (llAOPCurrentMarked) return; const data = Spicetify.Player?.data; if (!data?.item) return; const dur = data.item.duration?.milliseconds ?? data.item.duration_ms ?? 0; if (!dur) return; const pos = (ev && typeof ev.data === 'number') ? ev.data : Spicetify.Player.getProgress?.() ?? 0; const pct = (pos / dur) * 100; if (pct >= llConfig.autoOnPlay.percentThreshold) { const uri = data.item.uri; if (uri && !llIsTrackListened(uri)) { llMarkMany([uri], 'auto-play'); } llAOPCurrentMarked = true; const albumUri = data.item.album?.uri; if (albumUri && llIsAlbumWanted(albumUri)) { llCheckWantedAlbumCompletion(albumUri).catch((e) => console.warn('[Listening List] Album completion check failed', albumUri, e)); } } }; Spicetify.Player.addEventListener('songchange', llAOPSongChangeHandler); Spicetify.Player.addEventListener('onprogress', llAOPProgressHandler); } function llStopAutoOnPlay() { if (llAOPSongChangeHandler) Spicetify.Player.removeEventListener('songchange', llAOPSongChangeHandler); if (llAOPProgressHandler) Spicetify.Player.removeEventListener('onprogress', llAOPProgressHandler); llAOPSongChangeHandler = null; llAOPProgressHandler = null; llAOPCurrentMarked = false; } function llRestartAutoOnPlay() { llStopAutoOnPlay(); if (llConfig.autoOnPlay.enabled) llStartAutoOnPlay(); } /** Album URI → track URIs, cached for the session. */ const llAlbumTracksCache = new Map(); const llAlbumTracksInflight = new Map(); async function llFetchAlbumTrackUris(uri) { if (llAlbumTracksCache.has(uri)) return llAlbumTracksCache.get(uri); if (llAlbumTracksInflight.has(uri)) return llAlbumTracksInflight.get(uri); const p = (async () => { // The wg:// album endpoint has no track list on current clients, so GraphQL is primary. const out = []; const pageSize = 300; let offset = 0, total = Infinity; while (offset < total && offset < 3000) { const res = await Spicetify.GraphQL.Request( Spicetify.GraphQL.Definitions.getAlbum, { uri, locale: Spicetify.Locale?.getLocale?.() || 'en', limit: pageSize, offset }, ); if (res?.errors?.length) { console.warn('[Listening List] getAlbum returned errors', uri, res.errors); break; } const t = res?.data?.albumUnion?.tracksV2; if (!t?.items?.length) break; for (const it of t.items) if (it?.track?.uri) out.push(it.track.uri); // Advance by what came back, not by the requested page size: the endpoint may clamp `limit`. offset += t.items.length; total = typeof t.totalCount === 'number' ? t.totalCount : out.length; } // Don't cache an empty result: an album that was briefly unresolvable would // otherwise never complete until restart. if (out.length > 0) llAlbumTracksCache.set(uri, out); return out; })(); llAlbumTracksInflight.set(uri, p); try { return await p; } finally { llAlbumTracksInflight.delete(uri); } } /** * Wanted album → listened once enough of its tracks have been heard. * Threshold reuses autoSeed.minTracksPerAlbum. Best-effort: never throws * into the progress handler. */ async function llCheckWantedAlbumCompletion(albumUri) { const tracks = await llFetchAlbumTrackUris(albumUri); if (tracks.length === 0) { console.warn('[Listening List] No tracks resolved for wanted album; completion skipped', albumUri); return; } if (!llIsAlbumWanted(albumUri)) return; const heard = tracks.filter(llIsTrackListened).length; const min = Math.min(tracks.length, Math.max(1, llConfig.autoSeed.minTracksPerAlbum)); if (heard < min) return; const { marked } = llMarkMany([albumUri], 'auto-play'); if (marked) { const name = llMeta.albums[albumUri]?.name; Spicetify.showNotification?.(name ? `Listened: ${name}` : 'Album moved from want-to-listen to listened'); } } //#endregion //#region Import / Export function llExportData() { const payload = { exportSchemaVersion: LL_EXPORT_SCHEMA_VERSION, exportedAt: new Date().toISOString(), data: llData, config: llConfig, }; const blob = new Blob([JSON.stringify(payload, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; const date = new Date().toISOString().slice(0, 10).replace(/-/g, ''); a.download = `listening-list-export-${date}.json`; document.body.appendChild(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(url), 1000); Spicetify.showNotification?.('Exported listening list'); } function llPromptImportData() { const wrap = document.createElement('div'); wrap.innerHTML = `

Paste exported JSON or choose a file. Existing entries are kept; new entries are merged (earliest listenedAt / addedAt wins per URI; listened beats want-to-listen).

`; wrap.querySelector('#ll-import-file').addEventListener('change', async (e) => { const f = e.target.files?.[0]; if (!f) return; wrap.querySelector('#ll-import-text').value = await f.text(); }); wrap.querySelector('#ll-import-cancel').addEventListener('click', () => Spicetify.PopupModal.hide()); wrap.querySelector('#ll-import-go').addEventListener('click', () => { const raw = wrap.querySelector('#ll-import-text').value; try { const parsed = JSON.parse(raw); const result = llMergeImport(parsed); Spicetify.showNotification?.(`Imported ${result.addedAlbums} albums, ${result.addedTracks} tracks, ${result.addedWanted} to listen`); Spicetify.PopupModal.hide(); } catch (e) { console.error('[Listening List] Import failed', e); Spicetify.showNotification?.('Import failed (see console)'); } }); Spicetify.PopupModal.display({ title: 'Import Listening List', content: wrap, isLarge: true }); } function llMergeImport(payload) { if (llRefuseIfReadOnly()) throw new Error('Data is read-only'); if (!payload || typeof payload !== 'object') throw new Error('Not an object'); const incoming = payload.data || payload; if (!incoming.albums || !incoming.tracks) throw new Error('Missing albums/tracks'); if (payload.exportSchemaVersion && payload.exportSchemaVersion > LL_EXPORT_SCHEMA_VERSION) { throw new Error(`Export schema v${payload.exportSchemaVersion} newer than supported v${LL_EXPORT_SCHEMA_VERSION}`); } let addedAlbums = 0, addedTracks = 0; const mergeSide = (target, source, counterKey) => { for (const [uri, rec] of Object.entries(source)) { if (!rec || typeof rec.listenedAt !== 'number') continue; const incomingRec = { listenedAt: rec.listenedAt, source: rec.source === 'manual' || rec.source === 'auto-playlist' || rec.source === 'auto-play' || rec.source === 'import' ? rec.source : 'import' }; if (!target[uri]) { target[uri] = incomingRec; if (counterKey === 'a') addedAlbums++; else addedTracks++; } else if (incomingRec.listenedAt < target[uri].listenedAt) { target[uri] = incomingRec; } } }; mergeSide(llData.albums, incoming.albums, 'a'); mergeSide(llData.tracks, incoming.tracks, 't'); // `wanted` is absent from v1 exports. Listened wins on both sides. let addedWanted = 0; if (incoming.wanted && typeof incoming.wanted === 'object') { for (const [uri, rec] of Object.entries(incoming.wanted)) { if (!rec || typeof rec.addedAt !== 'number' || !llIsAlbumUri(uri) || llData.albums[uri]) continue; const incomingRec = { addedAt: rec.addedAt, source: rec.source === 'manual' ? 'manual' : 'import' }; if (!llData.wanted[uri]) { llData.wanted[uri] = incomingRec; addedWanted++; } else if (incomingRec.addedAt < llData.wanted[uri].addedAt) { llData.wanted[uri] = incomingRec; } } } for (const uri of Object.keys(llData.wanted)) if (llData.albums[uri]) delete llData.wanted[uri]; llSaveData(); llEmit(); return { addedAlbums, addedTracks, addedWanted }; } //#endregion //#region Modal const LL_MODAL_CSS = ` #${LL_MODAL_ROOT_ID} { color: var(--spice-text); } #${LL_MODAL_ROOT_ID} .ll-tabs { display: flex; gap: 8px; border-bottom: 1px solid var(--spice-subtext, #999); margin-bottom: 12px; } #${LL_MODAL_ROOT_ID} .ll-tab { background: none; border: none; color: var(--spice-subtext, #999); padding: 8px 12px; cursor: pointer; border-bottom: 2px solid transparent; } #${LL_MODAL_ROOT_ID} .ll-tab.is-active { color: var(--spice-text); border-bottom-color: var(--spice-button, #1ed760); } #${LL_MODAL_ROOT_ID} .ll-section { margin-bottom: 16px; } #${LL_MODAL_ROOT_ID} .ll-section h3 { margin: 0 0 8px 0; font-size: 14px; } #${LL_MODAL_ROOT_ID} .ll-row { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; } #${LL_MODAL_ROOT_ID} input[type="number"], #${LL_MODAL_ROOT_ID} input[type="range"] { background: var(--spice-card, #222); color: var(--spice-text); border: 1px solid var(--spice-subtext, #555); border-radius: 4px; padding: 4px; } #${LL_MODAL_ROOT_ID} button.ll-btn { background: var(--spice-button, #1ed760); color: var(--spice-button-text, #000); border: none; border-radius: 16px; padding: 6px 14px; cursor: pointer; } #${LL_MODAL_ROOT_ID} button.ll-btn.ll-btn--ghost { background: transparent; color: var(--spice-text); border: 1px solid var(--spice-subtext, #555); } #${LL_MODAL_ROOT_ID} button.ll-btn.ll-btn--icon { background: transparent; color: var(--spice-subtext, #999); border: none; border-radius: 50%; padding: 6px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; } #${LL_MODAL_ROOT_ID} button.ll-btn.ll-btn--icon:hover { background: var(--spice-card, #222); color: var(--spice-text); } #${LL_MODAL_ROOT_ID} button.ll-btn.ll-btn--icon svg { width: 16px; height: 16px; fill: currentColor; display: block; } #${LL_MODAL_ROOT_ID} table { width: 100%; border-collapse: collapse; font-size: 12px; } #${LL_MODAL_ROOT_ID} th, #${LL_MODAL_ROOT_ID} td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--spice-card, #222); } #${LL_MODAL_ROOT_ID} th { cursor: pointer; user-select: none; } #${LL_MODAL_ROOT_ID} td a { text-decoration: none; color: var(--spice-text); } #${LL_MODAL_ROOT_ID} td a:hover { text-decoration: underline; } #${LL_MODAL_ROOT_ID} .ll-artist-link { color: inherit; } #${LL_MODAL_ROOT_ID} .ll-artist-link:hover { color: var(--spice-text); text-decoration: underline; } /* Spicetify's isLarge PopupModal stretches its dialog to fill the whole viewport regardless of content length; scope the cap to just our modal (:has) so other isLarge modals in Spotify/other extensions are untouched. */ .main-embedWidgetGenerator-container:has(#${LL_MODAL_ROOT_ID}) { height: auto !important; max-height: min(80vh, 720px) !important; } .main-trackCreditsModal-mainSection:has(#${LL_MODAL_ROOT_ID}) { overflow-y: auto !important; } ${sharedFooterCss(EXT, { sticky: true })} #${LL_MODAL_ROOT_ID} .ll-modal-footer { margin-top: 16px; } `; let llActiveTab = 'viewer'; function llOpenModal(tab) { llActiveTab = tab || llActiveTab; const root = document.createElement('div'); root.id = LL_MODAL_ROOT_ID; const style = document.createElement('style'); style.textContent = LL_MODAL_CSS; root.appendChild(style); const tabs = document.createElement('div'); tabs.className = 'll-tabs'; const tabButtons = {}; for (const [key, label] of [['viewer', 'Viewer'], ['stats', 'Stats'], ['settings', 'Settings']]) { const b = document.createElement('button'); b.className = 'll-tab' + (llActiveTab === key ? ' is-active' : ''); b.textContent = label; b.addEventListener('click', () => { llActiveTab = key; for (const [k, btn] of Object.entries(tabButtons)) btn.classList.toggle('is-active', k === key); body.innerHTML = ''; body.appendChild(renderActive()); }); tabButtons[key] = b; tabs.appendChild(b); } root.appendChild(tabs); const body = document.createElement('div'); body.className = 'll-body'; body.appendChild(renderActive()); root.appendChild(body); function renderActive() { if (llActiveTab === 'viewer') return llRenderViewerTab(); if (llActiveTab === 'stats') return llRenderStatsTab(); return llRenderSettingsTab(); } root.insertAdjacentHTML('beforeend', sharedFooterHtml(EXT)); Spicetify.PopupModal.display({ title: 'Listening List', content: root, isLarge: true }); } function llRenderSettingsTab() { const root = document.createElement('div'); root.appendChild(llSettingsSurfaces()); root.appendChild(llSettingsBadgeStyle()); root.appendChild(llSettingsAutoSeed()); root.appendChild(llSettingsAutoOnPlay()); root.appendChild(llSettingsData()); return root; } function llSettingsSurfaces() { const sec = document.createElement('div'); sec.className = 'll-section'; sec.innerHTML = '

Surfaces

'; for (const [key, label] of [ ['tracklistRows', 'Tracklist rows'], ['albumHeader', 'Album page header'], ['albumCards', 'Album cards / tiles'], ['nowPlaying', 'Now-playing bar'], ]) { const row = document.createElement('label'); row.className = 'll-row'; row.innerHTML = `${label}`; const cb = document.createElement('input'); cb.type = 'checkbox'; cb.checked = !!llConfig.surfaces[key]; cb.addEventListener('change', () => { llConfig.surfaces[key] = cb.checked; llSaveConfig(); llRestartSurface(key); }); row.appendChild(cb); sec.appendChild(row); } return sec; } function llRestartSurface(key) { const startStop = { tracklistRows: [llStartTracklistSurface, llStopTracklistSurface], albumHeader: [llStartAlbumHeaderSurface, llStopAlbumHeaderSurface], albumCards: [llStartAlbumCardSurface, llStopAlbumCardSurface], nowPlaying: [llStartNowPlayingSurface, llStopNowPlayingSurface], }[key]; if (!startStop) return; const [start, stop] = startStop; stop(); if (llConfig.surfaces[key]) start(); } function llSettingsBadgeStyle() { const sec = document.createElement('div'); sec.className = 'll-section'; sec.innerHTML = '

Badge style

'; for (const v of ['checkmark', 'dot', 'text']) { const row = document.createElement('label'); row.className = 'll-row'; row.innerHTML = `${v.charAt(0).toUpperCase() + v.slice(1)}`; const rb = document.createElement('input'); rb.type = 'radio'; rb.name = 'll-badge-style'; rb.checked = llConfig.badgeStyle === v; rb.addEventListener('change', () => { if (rb.checked) { llConfig.badgeStyle = v; llSaveConfig(); llEmit(); } }); row.appendChild(rb); sec.appendChild(row); } return sec; } function llSettingsAutoSeed() { const sec = document.createElement('div'); sec.className = 'll-section'; sec.innerHTML = '

Auto-seed from playlists

'; const enabled = document.createElement('label'); enabled.className = 'll-row'; enabled.innerHTML = 'Enabled'; const cb = document.createElement('input'); cb.type = 'checkbox'; cb.checked = llConfig.autoSeed.enabled; cb.addEventListener('change', () => { llConfig.autoSeed.enabled = cb.checked; llSaveConfig(); }); enabled.appendChild(cb); sec.appendChild(enabled); const thr = document.createElement('label'); thr.className = 'll-row'; thr.innerHTML = 'Min tracks per album'; const n = document.createElement('input'); n.type = 'number'; n.min = '1'; n.max = '20'; n.value = String(llConfig.autoSeed.minTracksPerAlbum); n.addEventListener('change', () => { const v = Math.max(1, Math.min(20, Number(n.value) || 3)); llConfig.autoSeed.minTracksPerAlbum = v; llSaveConfig(); }); thr.appendChild(n); sec.appendChild(thr); const runRow = document.createElement('div'); runRow.className = 'll-row'; const last = llConfig.autoSeed.lastSeededAt ? new Date(llConfig.autoSeed.lastSeededAt).toLocaleString() : 'never'; runRow.innerHTML = `Last seeded: ${last}`; const btn = document.createElement('button'); btn.className = 'll-btn'; btn.textContent = 'Seed now'; btn.addEventListener('click', async () => { btn.disabled = true; btn.textContent = 'Seeding...'; try { const summary = await llRunAutoSeed(); Spicetify.showNotification?.(`Seeded ${summary.markedAlbums} albums from ${summary.playlistsScanned} playlists`); } catch (e) { console.error('[Listening List] Auto-seed failed', e); Spicetify.showNotification?.('Auto-seed failed (see console)'); } finally { btn.disabled = false; btn.textContent = 'Seed now'; } }); runRow.appendChild(btn); sec.appendChild(runRow); return sec; } function llSettingsAutoOnPlay() { const sec = document.createElement('div'); sec.className = 'll-section'; sec.innerHTML = '

Auto-mark on play

'; const en = document.createElement('label'); en.className = 'll-row'; en.innerHTML = 'Enabled'; const cb = document.createElement('input'); cb.type = 'checkbox'; cb.checked = llConfig.autoOnPlay.enabled; cb.addEventListener('change', () => { llConfig.autoOnPlay.enabled = cb.checked; llSaveConfig(); llRestartAutoOnPlay(); }); en.appendChild(cb); sec.appendChild(en); const thr = document.createElement('label'); thr.className = 'll-row'; thr.innerHTML = `Threshold: ${llConfig.autoOnPlay.percentThreshold}%`; const r = document.createElement('input'); r.type = 'range'; r.min = '0'; r.max = '100'; r.value = String(llConfig.autoOnPlay.percentThreshold); r.addEventListener('input', () => { const v = Number(r.value); llConfig.autoOnPlay.percentThreshold = v; const val = thr.querySelector('#ll-aop-val'); if (val) val.textContent = String(v); }); r.addEventListener('change', () => llSaveConfig()); thr.appendChild(r); sec.appendChild(thr); return sec; } function llSettingsData() { const sec = document.createElement('div'); sec.className = 'll-section'; sec.innerHTML = '

Data

'; const row = document.createElement('div'); row.className = 'll-row'; const exp = document.createElement('button'); exp.className = 'll-btn ll-btn--ghost'; exp.textContent = 'Export JSON'; exp.addEventListener('click', llExportData); const imp = document.createElement('button'); imp.className = 'll-btn ll-btn--ghost'; imp.textContent = 'Import JSON'; imp.addEventListener('click', llPromptImportData); const clr = document.createElement('button'); clr.className = 'll-btn ll-btn--ghost'; clr.textContent = 'Clear all'; clr.addEventListener('click', () => { if (!confirm('Clear all listened data and the want-to-listen list? This cannot be undone.')) return; llData = llEmptyData(); llSaveData(); llEmit(); Spicetify.showNotification?.('Listening List cleared'); }); row.append(exp, imp, clr); sec.appendChild(row); return sec; } let llViewerState = { kind: 'albums', sortKey: 'listenedAt', sortDir: 'desc', filter: '' }; const LL_VIEWER_KINDS = [['albums', 'Albums'], ['tracks', 'Tracks'], ['wanted', 'Want to listen']]; /** Per-kind table wiring: which data map, which metadata cache, which date field. */ function llViewerKindInfo(kind) { if (kind === 'wanted') return { source: llData.wanted, metaKind: 'albums', dateKey: 'addedAt', dateLabel: 'Added', titleLabel: 'Album', path: 'album' }; if (kind === 'tracks') return { source: llData.tracks, metaKind: 'tracks', dateKey: 'listenedAt', dateLabel: 'Listened', titleLabel: 'Track', path: 'track' }; return { source: llData.albums, metaKind: 'albums', dateKey: 'listenedAt', dateLabel: 'Listened', titleLabel: 'Album', path: 'album' }; } function llRenderViewerTab() { const root = document.createElement('div'); const subTabs = document.createElement('div'); subTabs.className = 'll-tabs'; for (const [k, label] of LL_VIEWER_KINDS) { const b = document.createElement('button'); b.className = 'll-tab' + (llViewerState.kind === k ? ' is-active' : ''); b.textContent = label; b.addEventListener('click', () => { const prevDate = llViewerKindInfo(llViewerState.kind).dateKey; llViewerState.kind = k; // Date sort follows the kind's date field; name/artist sorts carry over. if (llViewerState.sortKey === prevDate) llViewerState.sortKey = llViewerKindInfo(k).dateKey; renderBody(); refreshTabs(); }); subTabs.appendChild(b); } root.appendChild(subTabs); const filter = document.createElement('input'); filter.type = 'text'; filter.placeholder = 'Filter by name, artist, or URI'; filter.value = llViewerState.filter; filter.style.width = '100%'; filter.style.margin = '8px 0'; filter.style.padding = '6px'; filter.style.background = 'var(--spice-card, #222)'; filter.style.color = 'var(--spice-text)'; filter.style.border = '1px solid var(--spice-subtext, #555)'; filter.style.borderRadius = '4px'; filter.addEventListener('input', () => { llViewerState.filter = filter.value; renderBody(); }); root.appendChild(filter); const bodyHost = document.createElement('div'); root.appendChild(bodyHost); function refreshTabs() { subTabs.querySelectorAll('.ll-tab').forEach((el, i) => { el.classList.toggle('is-active', LL_VIEWER_KINDS[i][0] === llViewerState.kind); }); } function renderBody() { bodyHost.innerHTML = ''; bodyHost.appendChild(llRenderViewerTable()); } renderBody(); return root; } function llRenderViewerTable() { const kind = llViewerState.kind; const { source, metaKind, dateKey, dateLabel, titleLabel, path: pathKind } = llViewerKindInfo(kind); const metaSide = metaKind === 'albums' ? llMeta.albums : llMeta.tracks; const f = llViewerState.filter.trim().toLowerCase(); let entries = Object.entries(source).filter(([uri]) => { if (!f) return true; if (uri.toLowerCase().includes(f)) return true; const m = metaSide[uri]; return !!m && ((m.name || '').toLowerCase().includes(f) || (m.artist || '').toLowerCase().includes(f)); }); const wrap = document.createElement('div'); if (entries.length === 0) { wrap.innerHTML = kind === 'wanted' ? '

Nothing on your list yet. Right-click an album → "Want to listen".

' : '

Nothing here yet.

'; return wrap; } const status = document.createElement('div'); status.style.cssText = 'margin:0 0 8px 0; font-size:12px; opacity:.75; display:flex; align-items:center; gap:8px; min-height:18px;'; wrap.appendChild(status); const scroll = document.createElement('div'); scroll.style.cssText = 'max-height:50vh; overflow-y:auto;'; wrap.appendChild(scroll); // Re-sort with name support if requested entries.sort((a, b) => { let aKey, bKey; if (llViewerState.sortKey === 'name') { aKey = (metaSide[a[0]]?.name || '').toLowerCase(); bKey = (metaSide[b[0]]?.name || '').toLowerCase(); } else if (llViewerState.sortKey === 'artist') { aKey = (metaSide[a[0]]?.artist || '').toLowerCase(); bKey = (metaSide[b[0]]?.artist || '').toLowerCase(); } else { aKey = a[1][dateKey]; bKey = b[1][dateKey]; } const cmp = aKey > bKey ? 1 : aKey < bKey ? -1 : 0; return llViewerState.sortDir === 'asc' ? cmp : -cmp; }); const table = document.createElement('table'); const thead = document.createElement('thead'); const arrow = (key) => llViewerState.sortKey === key ? (llViewerState.sortDir === 'asc' ? ' ▲' : ' ▼') : ''; thead.innerHTML = ` ${titleLabel}${arrow('name')} Artist${arrow('artist')} ${dateLabel}${arrow(dateKey)} Source `; thead.querySelectorAll('th[data-sort]').forEach((th) => { th.style.cursor = 'pointer'; th.addEventListener('click', (e) => { e.stopPropagation(); const key = th.getAttribute('data-sort'); if (llViewerState.sortKey === key) { llViewerState.sortDir = llViewerState.sortDir === 'asc' ? 'desc' : 'asc'; } else { llViewerState.sortKey = key; llViewerState.sortDir = key === dateKey ? 'desc' : 'asc'; } wrap.replaceWith(llRenderViewerTable()); }); }); table.appendChild(thead); const tbody = document.createElement('tbody'); const max = 500; const visible = entries.slice(0, max); const escHtml = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); const buildArtistCell = (m) => { if (!m) return ''; const artistName = escHtml(m.artist || ''); if (m.artistUri && m.artistUri.startsWith('spotify:artist:')) { const aid = m.artistUri.split(':').pop(); return `${artistName}`; } return artistName; }; const trashSvg = ''; const checkSvg = ``; const actionsCell = kind === 'wanted' ? `` + `` : ``; for (const [uri, rec] of visible) { const tr = document.createElement('tr'); tr.dataset.uri = uri; const id = uri.split(':').pop(); const path = `/${pathKind}/${id}`; const m = metaSide[uri]; const nameCell = m?.name ? escHtml(m.name) : '…'; tr.innerHTML = ` ${nameCell} ${buildArtistCell(m)} ${new Date(rec[dateKey]).toLocaleDateString()} ${rec.source} ${actionsCell} `; tr.querySelector('.ll-name-cell a').addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); Spicetify.Platform?.History?.push(path); Spicetify.PopupModal.hide(); }); const artistLink = tr.querySelector('.ll-artist-link'); if (artistLink) { artistLink.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); Spicetify.Platform?.History?.push(artistLink.getAttribute('data-artist-path')); Spicetify.PopupModal.hide(); }); } const rowActions = { unmark: () => llUnmarkMany([uri]) > 0, unwant: () => llUnwantMany([uri]) > 0, mark: () => llMarkMany([uri], 'manual').marked > 0, }; tr.querySelectorAll('button[data-act]').forEach((btn) => { btn.addEventListener('click', (e) => { e.stopPropagation(); if (rowActions[btn.getAttribute('data-act')]?.()) tr.remove(); }); }); tbody.appendChild(tr); } table.appendChild(tbody); scroll.appendChild(table); const missing = visible.map(([u]) => u).filter((u) => !metaSide[u]); if (missing.length > 0) { status.innerHTML = `Loading 0 / ${missing.length}…`; if (!document.getElementById('ll-spinner-css')) { const s = document.createElement('style'); s.id = 'll-spinner-css'; s.textContent = '@keyframes ll-spin { to { transform: rotate(360deg); } }'; document.head.appendChild(s); } const patchRow = (uri) => { const tr = tbody.querySelector(`tr[data-uri="${CSS.escape(uri)}"]`); if (!tr) return; const m = metaSide[uri]; if (!m) return; const a = tr.querySelector('.ll-name-cell a'); if (a) a.textContent = m.name || a.textContent; const art = tr.querySelector('.ll-artist-cell'); if (art) { art.innerHTML = buildArtistCell(m); const newLink = art.querySelector('.ll-artist-link'); if (newLink) { newLink.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); Spicetify.Platform?.History?.push(newLink.getAttribute('data-artist-path')); Spicetify.PopupModal.hide(); }); } } }; llFetchMetadata( missing, metaKind, (uri, _meta, done, total) => { patchRow(uri); const txt = status.querySelector('.ll-status-text'); if (txt) txt.textContent = `Loading ${done} / ${total}…`; }, () => { status.innerHTML = ''; }, ); } if (entries.length > max) { const more = document.createElement('p'); more.style.opacity = '.6'; more.textContent = `Showing first ${max} of ${entries.length}. Use the filter to narrow.`; wrap.appendChild(more); } return wrap; } function llRenderStatsTab() { const root = document.createElement('div'); const albums = Object.values(llData.albums); const tracks = Object.values(llData.tracks); const now = Date.now(); const monthAgo = now - 30 * 24 * 3600 * 1000; const albumsThisMonth = albums.filter((r) => r.listenedAt >= monthAgo).length; const tracksThisMonth = tracks.filter((r) => r.listenedAt >= monthAgo).length; const bySource = (rows) => rows.reduce((acc, r) => { acc[r.source] = (acc[r.source] || 0) + 1; return acc; }, {}); const a = bySource(albums); const t = bySource(tracks); const earliest = [...albums, ...tracks].reduce((min, r) => r.listenedAt < min ? r.listenedAt : min, Infinity); const latest = [...albums, ...tracks].reduce((max, r) => r.listenedAt > max ? r.listenedAt : max, 0); const fmt = (n) => Number.isFinite(n) && n > 0 ? new Date(n).toLocaleDateString() : '—'; root.innerHTML = `

Totals

Albums listened${albums.length}
Tracks listened${tracks.length}
Albums to listen${Object.keys(llData.wanted).length}

Last 30 days

Albums${albumsThisMonth}
Tracks${tracksThisMonth}

By source — albums

Manual${a.manual || 0}
Auto-playlist${a['auto-playlist'] || 0}
Auto-play${a['auto-play'] || 0}
Import${a.import || 0}

By source — tracks

Manual${t.manual || 0}
Auto-playlist${t['auto-playlist'] || 0}
Auto-play${t['auto-play'] || 0}
Import${t.import || 0}

Range

Earliest${fmt(earliest)}
Latest${fmt(latest)}
`; return root; } //#endregion //#region Context Menu function llShouldShowMark(uris) { return uris.some((u) => { const norm = llNormalizeUri(u); if (!norm) return false; return (llIsAlbumUri(norm) && !llIsAlbumListened(norm)) || (llIsTrackUri(norm) && !llIsTrackListened(norm)); }); } function llShouldShowUnmark(uris) { return uris.some((u) => { const norm = llNormalizeUri(u); if (!norm) return false; return (llIsAlbumUri(norm) && llIsAlbumListened(norm)) || (llIsTrackUri(norm) && llIsTrackListened(norm)); }); } function llShouldShowWant(uris) { return uris.some((u) => { const norm = llNormalizeUri(u); return !!norm && llIsAlbumUri(norm) && !llAlbumState(norm); }); } function llShouldShowUnwant(uris) { return uris.some((u) => { const norm = llNormalizeUri(u); return !!norm && llIsAlbumUri(norm) && llIsAlbumWanted(norm); }); } function llRegisterContextMenu() { const wantItem = new Spicetify.ContextMenu.Item( 'Want to listen', (uris) => { const { added, skipped } = llWantMany(uris, 'manual'); if (added > 0) Spicetify.showNotification?.(`Added ${added} to want-to-listen${skipped ? ` (${skipped} skipped)` : ''}`); else if (!llData.__readOnly) Spicetify.showNotification?.('Nothing added (already listened or on the list)'); }, llShouldShowWant, ); const unwantItem = new Spicetify.ContextMenu.Item( 'Remove from want-to-listen', (uris) => { const removed = llUnwantMany(uris); Spicetify.showNotification?.(`Removed ${removed} from want-to-listen`); }, llShouldShowUnwant, ); const markItem = new Spicetify.ContextMenu.Item( 'Mark as listened', (uris) => { const { marked, skipped } = llMarkMany(uris, 'manual'); Spicetify.showNotification?.(`Marked ${marked} as listened${skipped ? ` (${skipped} already)` : ''}`); }, llShouldShowMark, ); const unmarkItem = new Spicetify.ContextMenu.Item( 'Unmark as listened', (uris) => { const removed = llUnmarkMany(uris); Spicetify.showNotification?.(`Unmarked ${removed}`); }, llShouldShowUnmark, ); markItem.register(); unmarkItem.register(); wantItem.register(); unwantItem.register(); } //#endregion //#region Profile Menu function llRegisterProfileMenu() { const item = new Spicetify.Menu.Item( 'Listening List', false, () => llOpenModal('viewer'), ); item.register(); } //#endregion //#region Main function llHealth() { return { ready: true, anchors: { rows: document.querySelectorAll('[data-ll-status]').length, badges: document.querySelectorAll(`.${LL_BADGE_CLASS}:not(.${LL_BADGE_NOWPLAYING_CLASS})`).length, }, errors: [ ...(llData?.__readOnly ? [`Data schema v${llData.schemaVersion} is newer than supported v${LL_DATA_SCHEMA_VERSION}; writes disabled`] : []), ...llMissingBadgeErrors(), ], }; } function llMissingBadgeErrors() { const missing = document.querySelectorAll( `[data-ll-status="1"]:not(:has(.${LL_BADGE_TRACKLIST_CLASS})), [data-ll-status="w"]:not(:has(.${LL_BADGE_TRACKLIST_CLASS}))` ).length; return missing ? [`Badge slot not found on ${missing} tracklist row(s)`] : []; } async function main() { while ( !Spicetify?.Platform?.History || !Spicetify?.LocalStorage || !Spicetify?.ContextMenu?.Item || !Spicetify?.Menu?.Item || !Spicetify?.URI || !Spicetify?.Player || !Spicetify?.React || !Spicetify?.ReactJSX?.jsx || !Spicetify?.ReactComponent?.MenuItem || !Spicetify?.CosmosAsync || !Spicetify?.GraphQL?.Definitions?.getAlbum ) { await new Promise((r) => setTimeout(r, 100)); } if (window.__listeningListActive) { console.warn('[Listening List] Already active; skipping double-load.'); return; } window.__listeningListActive = true; llData = llLoadData(); llConfig = llLoadConfig(); llSaveConfig(); llMeta = llLoadMeta(); llRegisterContextMenu(); llStartTracklistSurface(); llStartAlbumHeaderSurface(); llStartAlbumCardSurface(); llStartNowPlayingSurface(); llRegisterProfileMenu(); llStartAutoOnPlay(); (window.__spicetifyHealth ??= {})['listening-list'] = llHealth; if (llConfig.autoSeed.enabled && !llConfig.autoSeed.lastSeededAt) { llRunAutoSeed() .then((s) => Spicetify.showNotification?.(`Listening List: seeded ${s.markedAlbums} albums`)) .catch((e) => console.warn('[Listening List] First-run auto-seed failed', e)); } console.log('[Listening List] Booted.'); } main(); //#endregion })();