Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
4 changes: 2 additions & 2 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
48 changes: 48 additions & 0 deletions src/artwork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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);
}

const isImageDataUrl = ({ protocol, pathname }) =>
protocol === "data:" && pathname.toLowerCase().startsWith("image/");

export function isUsableSrc(src) {
const url = URL.parse(src);
if (!url) return false;
return url.protocol === "https:" || isImageDataUrl(url);
}

function largestEdgePx(sizes) {
return parseSizes(sizes).reduce(
(maxPx, { widthPx, heightPx }) => Math.max(maxPx, Math.min(widthPx, heightPx)),
0,
);
}

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, edgePx: largestEdgePx(sizes) }));
if (candidates.length === 0) return null;
const bigEnough = candidates.filter(({ edgePx }) => edgePx >= minPx);
return (bigEnough.length > 0 ? smallest(bigEnough) : largest(candidates)).src;
}
176 changes: 176 additions & 0 deletions test/artwork.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
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 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,<p>hi</p>")).toBe(false);
});

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 whose shorter side is at least minPx", () => {
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 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", () => {
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 big 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 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");
});

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);
});
});