From 9a8bd0f24a28b62e099b72b5dd0776d0d55f6c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 01:11:34 -0600 Subject: [PATCH 1/7] Add artwork selection for Media Session images parseSizes reads Media Session sizes strings, isUsableSrc limits sources to https and data URLs, and pickArtwork chooses the smallest image wide enough for the card, falling back to the widest. --- src/artwork.js | 41 +++++++++++ test/artwork.test.js | 157 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 src/artwork.js create mode 100644 test/artwork.test.js diff --git a/src/artwork.js b/src/artwork.js new file mode 100644 index 0000000..358f97f --- /dev/null +++ b/src/artwork.js @@ -0,0 +1,41 @@ +const USABLE_PROTOCOLS = new Set(["https:", "data:"]); +const SIZE_PATTERN = /^([1-9]\d*)x([1-9]\d*)$/i; +const SCALABLE_SIZE = { widthPx: Infinity, heightPx: Infinity }; + +function parseSize(token) { + if (token.toLowerCase() === "any") return SCALABLE_SIZE; + const match = SIZE_PATTERN.exec(token); + if (!match) return null; + const [, width, height] = match; + return { widthPx: Number(width), heightPx: Number(height) }; +} + +export function parseSizes(sizes = "") { + return sizes + .split(/\s+/) + .filter(Boolean) + .map(parseSize) + .filter((size) => size !== null); +} + +export function isUsableSrc(src) { + return USABLE_PROTOCOLS.has(URL.parse(src)?.protocol); +} + +function largestWidthPx(sizes) { + return Math.max(0, ...parseSizes(sizes).map(({ widthPx }) => widthPx)); +} + +const narrowest = (images) => + images.reduce((best, image) => (image.widthPx < best.widthPx ? image : best)); +const widest = (images) => + images.reduce((best, image) => (image.widthPx > best.widthPx ? image : best)); + +export function pickArtwork(artwork, { minPx }) { + const candidates = artwork + .filter(({ src }) => isUsableSrc(src)) + .map(({ src, sizes }) => ({ src, widthPx: largestWidthPx(sizes) })); + if (candidates.length === 0) return null; + const wideEnough = candidates.filter(({ widthPx }) => widthPx >= minPx); + return (wideEnough.length > 0 ? narrowest(wideEnough) : widest(candidates)).src; +} diff --git a/test/artwork.test.js b/test/artwork.test.js new file mode 100644 index 0000000..172545d --- /dev/null +++ b/test/artwork.test.js @@ -0,0 +1,157 @@ +import { describe, expect, it } from "vitest"; +import { isUsableSrc, parseSizes, pickArtwork } from "../src/artwork.js"; +import report from "./fixtures/zen-1.24.3b.json" with { type: "json" }; + +function image(src, sizes = "") { + return { src, sizes, type: "image/jpeg" }; +} + +describe("parseSizes", () => { + it("parses a single WxH size", () => { + expect(parseSizes("60x60")).toEqual([{ widthPx: 60, heightPx: 60 }]); + }); + + it("parses space-separated sizes", () => { + expect(parseSizes("16x16 32x48")).toEqual([ + { widthPx: 16, heightPx: 16 }, + { widthPx: 32, heightPx: 48 }, + ]); + }); + + it("accepts an uppercase X separator", () => { + expect(parseSizes("48X48")).toEqual([{ widthPx: 48, heightPx: 48 }]); + }); + + it("treats any as an infinitely scalable size", () => { + expect(parseSizes("ANY")).toEqual([{ widthPx: Infinity, heightPx: Infinity }]); + }); + + it("skips malformed and zero sizes", () => { + expect(parseSizes("0x0 60x 012x12 axb 1.5x2 64x64")).toEqual([{ widthPx: 64, heightPx: 64 }]); + }); + + it("returns no sizes for an empty or missing value", () => { + expect(parseSizes("")).toEqual([]); + expect(parseSizes(undefined)).toEqual([]); + }); +}); + +describe("isUsableSrc", () => { + it("accepts https URLs", () => { + expect(isUsableSrc("https://example.com/cover.jpg")).toBe(true); + }); + + it("accepts data URLs", () => { + expect(isUsableSrc("data:image/png;base64,iVBORw0KGgo=")).toBe(true); + }); + + it.each([ + "http://example.com/cover.jpg", + "blob:https://example.com/2f6c1e9a", + "chrome://browser/content/icon.png", + "resource://gre/res/icon.png", + "moz-extension://abc/cover.png", + "file:///etc/passwd", + "javascript:alert(1)", + ])("rejects %s", (src) => { + expect(isUsableSrc(src)).toBe(false); + }); + + it("rejects relative and malformed URLs", () => { + expect(isUsableSrc("/cover.jpg")).toBe(false); + expect(isUsableSrc("")).toBe(false); + expect(isUsableSrc(undefined)).toBe(false); + }); +}); + +describe("pickArtwork", () => { + it("prefers the smallest image at least minPx wide", () => { + const artwork = [ + image("https://a.test/544", "544x544"), + image("https://a.test/60", "60x60"), + image("https://a.test/226", "226x226"), + image("https://a.test/120", "120x120"), + ]; + + expect(pickArtwork(artwork, { minPx: 100 })).toBe("https://a.test/120"); + }); + + it("falls back to the largest image when none is wide enough", () => { + const artwork = [image("https://a.test/60", "60x60"), image("https://a.test/120", "120x120")]; + + expect(pickArtwork(artwork, { minPx: 500 })).toBe("https://a.test/120"); + }); + + it("measures an image by the largest of its sizes", () => { + const artwork = [ + image("https://a.test/multi", "16x16 256x256"), + image("https://a.test/128", "128x128"), + ]; + + expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/multi"); + }); + + it("prefers a sized image that is wide enough over a scalable one", () => { + const artwork = [ + image("https://a.test/any.svg", "any"), + image("https://a.test/300", "300x300"), + ]; + + expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/300"); + }); + + it("prefers a scalable image when no sized image is wide enough", () => { + const artwork = [image("https://a.test/60", "60x60"), image("https://a.test/any.svg", "any")]; + + expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/any.svg"); + }); + + it("prefers a sized image over one without sizes", () => { + const artwork = [image("https://a.test/unsized"), image("https://a.test/60", "60x60")]; + + expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/60"); + }); + + it("picks the first unsized image when no image declares sizes", () => { + const artwork = [image("https://a.test/first"), image("https://a.test/second")]; + + expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/first"); + }); + + it("keeps the first of equally sized images", () => { + const artwork = [ + image("https://a.test/first", "120x120"), + image("https://a.test/second", "120x120"), + ]; + + expect(pickArtwork(artwork, { minPx: 100 })).toBe("https://a.test/first"); + }); + + it("ignores images with unusable sources", () => { + const artwork = [ + image("http://a.test/120", "120x120"), + image("blob:https://a.test/1", "120x120"), + image("https://a.test/544", "544x544"), + ]; + + expect(pickArtwork(artwork, { minPx: 100 })).toBe("https://a.test/544"); + }); + + it("returns null when no image is usable", () => { + const artwork = [image("http://a.test/120", "120x120")]; + + expect(pickArtwork(artwork, { minPx: 100 })).toBeNull(); + }); + + it("returns null for empty artwork", () => { + expect(pickArtwork([], { minPx: 100 })).toBeNull(); + }); + + it("picks from the artwork recorded on Zen 1.24.3b", () => { + const [{ metadata }] = report.controllers.active; + + const src = pickArtwork(metadata.artwork, { minPx: 96 }); + + expect(src).toBe(metadata.artwork.find(({ sizes }) => sizes === "120x120").src); + }); +}); From 3c08fc99f32fe31ce6a74d351127bb650a64b4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 01:11:34 -0600 Subject: [PATCH 2/7] Mark milestone 2 done in the roadmap --- docs/roadmap.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/roadmap.md b/docs/roadmap.md index c08deff..446e8e3 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -6,8 +6,8 @@ The first release is built in milestones. Each milestone is one pull request, wr | --- | --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | ------ | | 0 | Open-source & tooling shell | Test, lint, format and bundle tooling, CI, contributor docs and templates | Done | | 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 | Next | -| 3 | Controller matching | `src/controllers.js`: safely read metadata from each tab's media controller and match it to a media card | | +| 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 | Next | | 4 | Card DOM | `src/card.js`: add, update and remove the thumbnail element in a media card | | | 5 | Runtime wiring & Sine | Watch cards and metadata changes, clean teardown, `theme.json`, first run inside Zen | | | 6 | Styling & polish | `chrome.css` for the thumbnail, narrow-sidebar and collapsed states, manual checklist | | From 2deac45fc0eaf03a3de39c61146a4dd1c2625b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 01:11:34 -0600 Subject: [PATCH 3/7] Add changelog entry for artwork selection --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf6bc24..1a56949 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Contributor docs: contributing guide, code of conduct, security policy, issue and pull request templates. - Spike: `npm run spike` prints a read-only Browser Console snippet that reports Zen's media card DOM and media controller metadata ([docs/spike.md](docs/spike.md)). - 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 `data:` sources and picks the smallest image at least as wide as needed, falling back to the largest. - 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 From d715866e029ccedf6d1bf25a06718ce6fd25b9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 01:16:57 -0600 Subject: [PATCH 4/7] Accept only image data URLs for artwork A data: URL of any media type was passed on as artwork. Only data: URLs with an image/* media type are usable now. --- src/artwork.js | 8 ++++++-- test/artwork.test.js | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/artwork.js b/src/artwork.js index 358f97f..b39125d 100644 --- a/src/artwork.js +++ b/src/artwork.js @@ -1,4 +1,3 @@ -const USABLE_PROTOCOLS = new Set(["https:", "data:"]); const SIZE_PATTERN = /^([1-9]\d*)x([1-9]\d*)$/i; const SCALABLE_SIZE = { widthPx: Infinity, heightPx: Infinity }; @@ -18,8 +17,13 @@ export function parseSizes(sizes = "") { .filter((size) => size !== null); } +const isImageDataUrl = ({ protocol, pathname }) => + protocol === "data:" && pathname.toLowerCase().startsWith("image/"); + export function isUsableSrc(src) { - return USABLE_PROTOCOLS.has(URL.parse(src)?.protocol); + const url = URL.parse(src); + if (!url) return false; + return url.protocol === "https:" || isImageDataUrl(url); } function largestWidthPx(sizes) { diff --git a/test/artwork.test.js b/test/artwork.test.js index 172545d..1857402 100644 --- a/test/artwork.test.js +++ b/test/artwork.test.js @@ -41,10 +41,14 @@ describe("isUsableSrc", () => { expect(isUsableSrc("https://example.com/cover.jpg")).toBe(true); }); - it("accepts data URLs", () => { + it("accepts image data URLs", () => { expect(isUsableSrc("data:image/png;base64,iVBORw0KGgo=")).toBe(true); }); + it("rejects data URLs that are not images", () => { + expect(isUsableSrc("data:text/html,

hi

")).toBe(false); + }); + it.each([ "http://example.com/cover.jpg", "blob:https://example.com/2f6c1e9a", From 236403ddd1c790859f8f02b8b4af8e9485357ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 01:16:57 -0600 Subject: [PATCH 5/7] Avoid RangeError on very long artwork sizes lists Spreading every parsed size into Math.max threw a RangeError for a page-supplied sizes string with hundreds of thousands of tokens. Reduce over the sizes instead. --- src/artwork.js | 2 +- test/artwork.test.js | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/artwork.js b/src/artwork.js index b39125d..fa7af1d 100644 --- a/src/artwork.js +++ b/src/artwork.js @@ -27,7 +27,7 @@ export function isUsableSrc(src) { } function largestWidthPx(sizes) { - return Math.max(0, ...parseSizes(sizes).map(({ widthPx }) => widthPx)); + return parseSizes(sizes).reduce((maxPx, { widthPx }) => Math.max(maxPx, widthPx), 0); } const narrowest = (images) => diff --git a/test/artwork.test.js b/test/artwork.test.js index 1857402..372d2ad 100644 --- a/test/artwork.test.js +++ b/test/artwork.test.js @@ -86,7 +86,14 @@ describe("pickArtwork", () => { expect(pickArtwork(artwork, { minPx: 500 })).toBe("https://a.test/120"); }); - it("measures an image by the largest of its sizes", () => { + it("handles an image that declares a very long list of sizes", () => { + const artwork = [image("https://a.test/many", Array(300_000).fill("1x1").join(" "))]; + + expect(pickArtwork(artwork, { minPx: 96 })).toBe("https://a.test/many"); + }); + + it("measures an image by the largest of its sizes" +, () => { const artwork = [ image("https://a.test/multi", "16x16 256x256"), image("https://a.test/128", "128x128"), From 07268c1b73aa4e8bdd88351c01f57661dbe3593a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 01:16:58 -0600 Subject: [PATCH 6/7] Measure artwork by the shorter side of its size Measuring by width let a wide banner such as 200x40 beat a square cover. Compare the shorter edge so an image counts as big enough only when both dimensions are. --- src/artwork.js | 21 ++++++++++++--------- test/artwork.test.js | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/artwork.js b/src/artwork.js index fa7af1d..247d8ae 100644 --- a/src/artwork.js +++ b/src/artwork.js @@ -26,20 +26,23 @@ export function isUsableSrc(src) { return url.protocol === "https:" || isImageDataUrl(url); } -function largestWidthPx(sizes) { - return parseSizes(sizes).reduce((maxPx, { widthPx }) => Math.max(maxPx, widthPx), 0); +function largestEdgePx(sizes) { + return parseSizes(sizes).reduce( + (maxPx, { widthPx, heightPx }) => Math.max(maxPx, Math.min(widthPx, heightPx)), + 0, + ); } -const narrowest = (images) => - images.reduce((best, image) => (image.widthPx < best.widthPx ? image : best)); -const widest = (images) => - images.reduce((best, image) => (image.widthPx > best.widthPx ? image : best)); +const smallest = (images) => + images.reduce((best, image) => (image.edgePx < best.edgePx ? image : best)); +const largest = (images) => + images.reduce((best, image) => (image.edgePx > best.edgePx ? image : best)); export function pickArtwork(artwork, { minPx }) { const candidates = artwork .filter(({ src }) => isUsableSrc(src)) - .map(({ src, sizes }) => ({ src, widthPx: largestWidthPx(sizes) })); + .map(({ src, sizes }) => ({ src, edgePx: largestEdgePx(sizes) })); if (candidates.length === 0) return null; - const wideEnough = candidates.filter(({ widthPx }) => widthPx >= minPx); - return (wideEnough.length > 0 ? narrowest(wideEnough) : widest(candidates)).src; + const bigEnough = candidates.filter(({ edgePx }) => edgePx >= minPx); + return (bigEnough.length > 0 ? smallest(bigEnough) : largest(candidates)).src; } diff --git a/test/artwork.test.js b/test/artwork.test.js index 372d2ad..98bf411 100644 --- a/test/artwork.test.js +++ b/test/artwork.test.js @@ -69,7 +69,7 @@ describe("isUsableSrc", () => { }); describe("pickArtwork", () => { - it("prefers the smallest image at least minPx wide", () => { + it("prefers the smallest image whose shorter side is at least minPx", () => { const artwork = [ image("https://a.test/544", "544x544"), image("https://a.test/60", "60x60"), @@ -80,20 +80,28 @@ describe("pickArtwork", () => { expect(pickArtwork(artwork, { minPx: 100 })).toBe("https://a.test/120"); }); - it("falls back to the largest image when none is wide enough", () => { + it("falls back to the largest image when none is big enough", () => { const artwork = [image("https://a.test/60", "60x60"), image("https://a.test/120", "120x120")]; expect(pickArtwork(artwork, { minPx: 500 })).toBe("https://a.test/120"); }); + it("measures a size by its shorter side", () => { + const artwork = [ + image("https://a.test/banner", "200x40"), + image("https://a.test/300", "300x300"), + ]; + + expect(pickArtwork(artwork, { minPx: 96 })).toBe("https://a.test/300"); + }); + it("handles an image that declares a very long list of sizes", () => { const artwork = [image("https://a.test/many", Array(300_000).fill("1x1").join(" "))]; expect(pickArtwork(artwork, { minPx: 96 })).toBe("https://a.test/many"); }); - it("measures an image by the largest of its sizes" -, () => { + it("measures an image by the largest of its sizes", () => { const artwork = [ image("https://a.test/multi", "16x16 256x256"), image("https://a.test/128", "128x128"), @@ -102,7 +110,7 @@ describe("pickArtwork", () => { expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/multi"); }); - it("prefers a sized image that is wide enough over a scalable one", () => { + it("prefers a sized image that is big enough over a scalable one", () => { const artwork = [ image("https://a.test/any.svg", "any"), image("https://a.test/300", "300x300"), @@ -111,7 +119,7 @@ describe("pickArtwork", () => { expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/300"); }); - it("prefers a scalable image when no sized image is wide enough", () => { + it("prefers a scalable image when no sized image is big enough", () => { const artwork = [image("https://a.test/60", "60x60"), image("https://a.test/any.svg", "any")]; expect(pickArtwork(artwork, { minPx: 200 })).toBe("https://a.test/any.svg"); From 7b5b2d475be485f0500415bd1a2d1aee2050bfb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Rodr=C3=ADguez?= Date: Fri, 27 Nov 2026 01:16:58 -0600 Subject: [PATCH 7/7] Describe artwork selection rules in the changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a56949..02e5f10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Contributor docs: contributing guide, code of conduct, security policy, issue and pull request templates. - Spike: `npm run spike` prints a read-only Browser Console snippet that reports Zen's media card DOM and media controller metadata ([docs/spike.md](docs/spike.md)). - 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 `data:` sources and picks the smallest image at least as wide as needed, falling back to the largest. +- 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. - 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