diff --git a/CHANGELOG.md b/CHANGELOG.md index 8934b9c..f5cf6ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Test fixture recorded with the spike on Zen 1.24.3b, with tests that pin the media card DOM, metadata and CSP facts the mod relies on. - Artwork selection (`src/artwork.js`): parses Media Session `sizes`, keeps only `https:` and image `data:` sources and picks the smallest image whose shorter side is big enough, falling back to the largest. - Controller matching (`src/controllers.js`): reads metadata from each tab's media controller, treating an inactive controller (`NS_ERROR_NOT_AVAILABLE`) as no session, and matches each media card to a session by its title and artist, giving each session to at most one card. +- Card DOM (`src/card.js`): adds, updates and removes an artwork image at the start of a media card's info container. The image stays hidden until it loads, and only `https:` or image `data:` sources are set. - Claude Code project skills: `unslop` (code style, summarized in the contributing guide), `mine` (attribution policy) and `ship-it` (commit, review and fix loop). [Unreleased]: https://github.com/rodzy/Zen-Media-Viz/commits/main diff --git a/docs/roadmap.md b/docs/roadmap.md index c5607a2..d555f6c 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -8,8 +8,8 @@ The first release is built in milestones. Each milestone is one pull request, wr | 1 | Spike & fixtures | A read-only console snippet that checks Zen's media card DOM and real metadata on the maintainer's Zen version, saved as test fixtures | Done | | 2 | Artwork selection | `src/artwork.js`: parse `sizes`, filter to usable `https:`/`data:` sources, pick the best image | Done | | 3 | Controller matching | `src/controllers.js`: safely read metadata from each tab's media controller and match it to a media card | Done | -| 4 | Card DOM | `src/card.js`: add, update and remove the thumbnail element in a media card | Next | -| 5 | Runtime wiring & Sine | Watch cards and metadata changes, clean teardown, `theme.json`, first run inside Zen | | +| 4 | Card DOM | `src/card.js`: add, update and remove the thumbnail element in a media card | Done | +| 5 | Runtime wiring & Sine | Watch cards and metadata changes, clean teardown, `theme.json`, first run inside Zen | Next | | 6 | Styling & polish | `chrome.css` for the thumbnail, narrow-sidebar and collapsed states, manual checklist | | | 7 | Release v0.1.0 | Install guide, screenshot, version bump and tag | | diff --git a/src/card.js b/src/card.js new file mode 100644 index 0000000..f06ffd0 --- /dev/null +++ b/src/card.js @@ -0,0 +1,36 @@ +import { isUsableSrc } from "./artwork.js"; + +const HTML_NS = "http://www.w3.org/1999/xhtml"; +const ARTWORK_CLASS = "zen-media-viz-artwork"; +const INFO_CONTAINER_SELECTOR = ".zen-media-info-container"; + +const findArtwork = (card) => card.querySelector(`.${ARTWORK_CLASS}`); + +function createArtwork(container) { + const image = container.ownerDocument.createElementNS(HTML_NS, "img"); + image.className = ARTWORK_CLASS; + image.alt = ""; + image.setAttribute("referrerpolicy", "no-referrer"); + image.addEventListener("load", () => { + image.hidden = false; + }); + container.prepend(image); + return image; +} + +export function removeArtwork(card) { + findArtwork(card)?.remove(); +} + +export function showArtwork(card, src) { + if (!isUsableSrc(src)) { + removeArtwork(card); + return; + } + const container = card.querySelector(INFO_CONTAINER_SELECTOR); + if (!container) return; + const image = findArtwork(card) ?? createArtwork(container); + if (image.getAttribute("src") === src) return; + image.hidden = true; + image.setAttribute("src", src); +} diff --git a/test/card.test.js b/test/card.test.js new file mode 100644 index 0000000..024dcdf --- /dev/null +++ b/test/card.test.js @@ -0,0 +1,159 @@ +// @vitest-environment happy-dom +import { describe, expect, it } from "vitest"; +import { removeArtwork, showArtwork } from "../src/card.js"; +import report from "./fixtures/zen-1.24.3b.json" with { type: "json" }; + +const COVER = "https://a.test/cover.jpg"; +const OTHER_COVER = "https://a.test/other.jpg"; + +function recordedCard() { + const toolbar = new DOMParser().parseFromString(report.toolbar.html, "application/xml"); + return toolbar.querySelector("toolbar > .zen-media-card"); +} + +const artworkOf = (card) => card.querySelectorAll(".zen-media-viz-artwork"); + +function loaded(image) { + image.dispatchEvent(new Event("load")); + return image; +} + +describe("showArtwork", () => { + it("adds an image as the first item of the card's info container", () => { + const card = recordedCard(); + + showArtwork(card, COVER); + + const [image] = artworkOf(card); + expect(image.parentElement.classList.contains("zen-media-info-container")).toBe(true); + expect(image.parentElement.firstElementChild).toBe(image); + expect(image.getAttribute("src")).toBe(COVER); + }); + + it("creates an HTML image even inside a XUL card", () => { + const card = recordedCard(); + + showArtwork(card, COVER); + + expect(artworkOf(card)[0].namespaceURI).toBe("http://www.w3.org/1999/xhtml"); + expect(artworkOf(card)[0].localName).toBe("img"); + }); + + it("marks the image as decorative", () => { + const card = recordedCard(); + + showArtwork(card, COVER); + + expect(artworkOf(card)[0].getAttribute("alt")).toBe(""); + }); + + it("keeps the image hidden until it loads", () => { + const card = recordedCard(); + + showArtwork(card, COVER); + const [image] = artworkOf(card); + + expect(image.hidden).toBe(true); + expect(loaded(image).hidden).toBe(false); + }); + + it("keeps the image hidden when it fails to load", () => { + const card = recordedCard(); + showArtwork(card, COVER); + const [image] = artworkOf(card); + + image.dispatchEvent(new Event("error")); + + expect([...artworkOf(card)]).toEqual([image]); + expect(image.hidden).toBe(true); + }); + + it("sends no referrer when fetching the artwork", () => { + const card = recordedCard(); + + showArtwork(card, COVER); + + expect(artworkOf(card)[0].getAttribute("referrerpolicy")).toBe("no-referrer"); + }); + + it("reuses the image when the artwork changes", () => { + const card = recordedCard(); + showArtwork(card, COVER); + const [image] = artworkOf(card); + + showArtwork(card, OTHER_COVER); + + expect([...artworkOf(card)]).toEqual([image]); + expect(image.getAttribute("src")).toBe(OTHER_COVER); + }); + + it("hides the image again while new artwork loads", () => { + const card = recordedCard(); + showArtwork(card, COVER); + const image = loaded(artworkOf(card)[0]); + + showArtwork(card, OTHER_COVER); + + expect(image.hidden).toBe(true); + }); + + it("leaves a loaded image visible when the artwork is unchanged", () => { + const card = recordedCard(); + showArtwork(card, COVER); + const image = loaded(artworkOf(card)[0]); + + showArtwork(card, COVER); + + expect(image.hidden).toBe(false); + }); + + it.each([null, "http://a.test/cover.jpg", "javascript:alert(1)"])( + "removes the image when given %s", + (src) => { + const card = recordedCard(); + showArtwork(card, COVER); + + showArtwork(card, src); + + expect(artworkOf(card)).toHaveLength(0); + }, + ); + + it("does nothing to a card without an info container", () => { + const card = document.createElement("toolbaritem"); + + showArtwork(card, COVER); + + expect(card.children).toHaveLength(0); + }); +}); + +describe("removeArtwork", () => { + it("removes the image from the card", () => { + const card = recordedCard(); + showArtwork(card, COVER); + + removeArtwork(card); + + expect(artworkOf(card)).toHaveLength(0); + }); + + it("leaves the rest of the card untouched", () => { + const card = recordedCard(); + const before = card.outerHTML; + showArtwork(card, COVER); + + removeArtwork(card); + + expect(card.outerHTML).toBe(before); + }); + + it("does nothing to a card without artwork", () => { + const card = recordedCard(); + const before = card.outerHTML; + + removeArtwork(card); + + expect(card.outerHTML).toBe(before); + }); +});