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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

- Project tooling: Vitest, ESLint, Prettier, esbuild bundling and GitHub Actions CI.
- 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.
- 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
9 changes: 5 additions & 4 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ A Zen Browser mod, loaded by the Sine mod manager, that shows album artwork in Z
- `src/*.js` are ES modules. `build.mjs` (esbuild) bundles `src/index.js` into one IIFE, `zen-media-viz.uc.js`, at the repo root. **The bundle is generated but committed**: Sine installs straight from the GitHub repo and only executes `.uc.js` (not `.uc.mjs`). After changing `src/`, run `npm run build` and commit the bundle too. Never hand-edit it.
- Sine integration (`theme.json`, `chrome.css`) arrives in Milestone 5; until then the mod can't be installed. Sine reads `theme.json` at the repo root. Scripts listed under `scripts` with `include: ["chrome://browser/content/browser.xhtml"]` run once per browser window after its `load` event; `chrome.css` is applied as a user stylesheet. Sine can re-run the script into an already-open window when the mod is enabled, so the entry point must tear down any previous instance (`window.ZenMediaViz.destroy()`).
- **Injected globals:** logic modules never read `gBrowser`, `gZenMediaController`, `window` or `document` directly; they take them as parameters. Only `src/index.js` touches the real window. Tests pass fakes. DOM tests opt in with `// @vitest-environment happy-dom` (default environment is `node`).
- Zen/Firefox APIs relied on (from `zen-browser/desktop` `src/zen/media/` and Firefox's `MediaController.webidl`):
- Media cards: `#zen-media-controls-toolbar` > `.zen-media-card` (cloned from `#zen-media-card-template`) > `.zen-media-info-container` > `.zen-media-info-vbox` > `.zen-media-title` / `.zen-media-artist`. Zen sets only title and artist; it ignores artwork.
- `tab.linkedBrowser.browsingContext.mediaController.getMetadata()` returns `{title, artist, album, artwork: [{src, sizes, type}]}` with absolute `src`. It **throws** when the controller is inactive. The controller fires `metadatachange`.
- `browser.xhtml` is a chrome document under a CSP: only `https:`/`data:` image sources are usable.
- Zen/Firefox APIs relied on (from `zen-browser/desktop` `src/zen/media/` and Firefox's `MediaController.webidl`), verified on Zen 1.24.3b (Gecko 160.0.1) by the spike (`docs/spike.md`, `npm run spike`). Its report is the fixture `test/fixtures/zen-1.24.3b.json`, and `test/zen-fixture.test.js` pins the facts below:
- Media cards: `#zen-media-controls-toolbar` > `.zen-media-card` (cloned from `#zen-media-card-template`, which sits inside the toolbar) > `.zen-media-main-vbox` > `vbox` > `.zen-media-info-container` > `.zen-media-info-vbox` > `.zen-media-title` / `.zen-media-artist`. Use descendant selectors, not child chains. Zen sets only title and artist; it ignores artwork. The recorded card had a `playing` class; per Zen's source, cards are created `hidden` and shown when their tab isn't selected.
- Cards carry no attribute naming their controller or browser (only `class`, `style`, `hidden`). Match cards to controllers by title and artist. From Zen's source, not verified by the spike: the card-to-controller map is a private field, and `gZenMediaController.frontCard` returns a card object with `element`, `browser` and `controller`, but only for the front visible card.
- `tab.linkedBrowser.browsingContext.mediaController.getMetadata()` returns `{title, artist, album, artwork: [{src, sizes, type}]}` with absolute `src`. It **throws** `NS_ERROR_NOT_AVAILABLE` when the controller is inactive. The controller fires `metadatachange`.
- `browser.xhtml`'s CSP is `script-src chrome: moz-src: resource:; frame-src chrome:; object-src 'none';`, with no `img-src`, so it shouldn't restrict images (confirm a real artwork image renders once the mod runs in Zen, Milestone 5). Limiting artwork to `https:`/`data:` sources is the mod's own choice: no plain `http:`, and no `blob:` or privileged schemes from the page.
- Anything that can't be tested under Node goes in `docs/manual-testing.md`.

## Workflow
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ npm test
| `npm run format` | Format everything with Prettier (`format:check` only reports) |
| `npm run build` | Bundle `src/` into `zen-media-viz.uc.js` |
| `npm run check:bundle` | Fail if `zen-media-viz.uc.js` doesn't match a fresh build of `src/` |
| `npm run spike` | Print the Browser Console snippet described in `docs/spike.md` |

## How the code is organized

- **`src/`** holds the mod's source as small ES modules.
- **`zen-media-viz.uc.js`** at the repo root is **generated** by `npm run build`. Sine can only run a single classic `.uc.js` file and installs mods straight from the repository, so this bundle is committed. Never edit it by hand. If you change anything in `src/`, run `npm run build` and commit the result; CI fails otherwise.
- **`spike/`** holds the read-only Browser Console snippet that records what a Zen version provides (see [`docs/spike.md`](docs/spike.md)). It isn't part of the mod.
- **`test/`** holds the tests ([Vitest](https://vitest.dev)). Tests that need a DOM add `// @vitest-environment happy-dom` at the top of the file.

### The injected-globals rule
Expand Down
4 changes: 4 additions & 0 deletions docs/manual-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ Items are added as features land.
## Setup

- [ ] The mod installs through Sine and Zen restarts without errors in the Browser Console.

## Spike

- [ ] Pasting the `npm run spike` snippet into the Browser Console logs `[zen-media-viz] Spike report copied to the clipboard.` and the clipboard holds the JSON report.
4 changes: 2 additions & 2 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ The first release is built in milestones. Each milestone is one pull request, wr
| # | Milestone | What it delivers | Status |
| --- | --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | ------ |
| 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 | Next |
| 2 | Artwork selection | `src/artwork.js`: parse `sizes`, filter to usable `https:`/`data:` sources, pick the best image | |
| 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 | |
| 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 | |
Expand Down
17 changes: 17 additions & 0 deletions docs/spike.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Spike: checking Zen's media player

The mod depends on Zen's media card markup and on Firefox's media controller API, neither of which is documented. The spike is a read-only snippet you run in Zen's Browser Console. It reports what your Zen version provides, and the report is saved as test fixtures under `test/fixtures/` so the tests use real data.

The snippet doesn't change anything in the browser. It reads the media toolbar, the active media controllers, the browser window's content-security policy and the member names of `gZenMediaController`.

## Running it

1. In `about:config`, set `devtools.chrome.enabled` to `true` so the Browser Console accepts input.
2. Play something with artwork in one or two tabs, such as a song on [YouTube Music](https://music.youtube.com), then switch to a different tab so Zen shows the media card in the sidebar.
3. Copy the snippet to the clipboard: `npm run -s spike | pbcopy` on macOS, or `npm run -s spike` and copy the output.
4. Open the Browser Console (Cmd/Ctrl+Shift+J), paste the snippet and press Enter. You'll need to type `allow pasting` first if the console asks.
5. The report is now on the clipboard. Paste it into a file and share it in the pull request or issue. A maintainer saves it as `test/fixtures/zen-<version>.json`.

## Before sharing a report

The report has no full page URLs; tabs are identified by host. It does include the title, artist, album and artwork URLs of what's playing, and the media toolbar's markup, which can contain a page's favicon URL. Read it through and remove anything you'd rather not publish.
4 changes: 2 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default [
},
js.configs.recommended,
{
files: ["src/**/*.js"],
files: ["src/**/*.js", "spike/**/*.js"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
Expand All @@ -23,7 +23,7 @@ export default [
},
},
{
files: ["*.js", "*.mjs", "scripts/**/*.js", "scripts/**/*.mjs"],
files: ["*.js", "*.mjs", "scripts/**/*.js", "scripts/**/*.mjs", "spike/**/*.mjs"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"format": "prettier --write .",
"format:check": "prettier --check .",
"build": "node build.mjs",
"check:bundle": "node build.mjs --check"
"check:bundle": "node build.mjs --check",
"spike": "node spike/build.mjs"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
Expand Down
18 changes: 18 additions & 0 deletions spike/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { build } from "esbuild";
import { fileURLToPath } from "node:url";

const {
outputFiles: [snippet],
} = await build({
absWorkingDir: fileURLToPath(new URL("..", import.meta.url)),
entryPoints: ["spike/console.js"],
write: false,
bundle: true,
format: "iife",
target: "firefox140",
charset: "utf8",
legalComments: "none",
logLevel: "warning",
});

process.stdout.write(snippet.text);
12 changes: 12 additions & 0 deletions spike/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* global gBrowser, Services, copy */
import { collectReport } from "./report.js";

const report = collectReport({
document,
gBrowser,
gZenMediaController: window.gZenMediaController,
appinfo: Services.appinfo,
});

copy(JSON.stringify(report, null, 2));
console.log("[zen-media-viz] Spike report copied to the clipboard.", report);
112 changes: 112 additions & 0 deletions spike/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const TOOLBAR_SELECTOR = "#zen-media-controls-toolbar";
const TEMPLATE_SELECTOR = "#zen-media-card-template";
const CARD_SELECTOR = ".zen-media-card";
const CSP_SELECTOR = 'meta[http-equiv="Content-Security-Policy"]';

function describeEnvironment({ name, version, platformVersion, OS }) {
return { name, version, platformVersion, os: OS };
}

function describeElement({ localName, classList }) {
return [localName, ...classList].join(".");
}

function pathBetween(ancestor, element) {
const path = [];
for (let node = element; node && node !== ancestor; node = node.parentElement) {
path.unshift(describeElement(node));
}
return [describeElement(ancestor), ...path];
}

function describeCard(card) {
const title = card.querySelector(".zen-media-title");
return {
hidden: card.hidden,
title: title?.textContent ?? null,
artist: card.querySelector(".zen-media-artist")?.textContent ?? null,
titlePath: title ? pathBetween(card, title) : null,
};
}

function describeToolbar(document) {
const toolbar = document.querySelector(TOOLBAR_SELECTOR);
if (!toolbar) return null;

return {
hasTemplate: toolbar.querySelector(TEMPLATE_SELECTOR) !== null,
cards: [...toolbar.querySelectorAll(CARD_SELECTOR)].map(describeCard),
html: toolbar.outerHTML,
};
}

function readMetadata(controller) {
try {
return { metadata: controller.getMetadata() };
} catch ({ name, message }) {
return { metadataError: { name, message } };
}
}

function controllerOf(tab) {
return tab.linkedBrowser?.browsingContext?.mediaController ?? null;
}

function describeTab(tab) {
const { browserId, currentURI } = tab.linkedBrowser;
const controller = controllerOf(tab);
return {
browserId,
host: URL.parse(currentURI?.spec ?? "")?.host || null,
soundPlaying: tab.hasAttribute("soundplaying"),
id: controller.id,
isActive: controller.isActive,
isPlaying: controller.isPlaying,
isAudible: controller.isAudible,
playbackState: controller.playbackState,
supportedKeys: [...controller.supportedKeys],
...readMetadata(controller),
};
}

function describeTabSafely(tab) {
try {
return describeTab(tab);
} catch ({ name, message }) {
return { browserId: tab.linkedBrowser.browserId, error: { name, message } };
}
}

function describeControllers(tabs) {
const { active = [], inactive = [] } = Object.groupBy([...tabs].filter(controllerOf), (tab) =>
controllerOf(tab).isActive ? "active" : "inactive",
);
const [firstInactive] = inactive;

return {
active: active.map(describeTabSafely),
inactiveCount: inactive.length,
inactiveMetadataError: firstInactive
? (readMetadata(controllerOf(firstInactive)).metadataError ?? null)
: null,
};
}

function describeMembers(object) {
if (!object) return null;

return {
own: Object.getOwnPropertyNames(object).toSorted(),
prototype: Object.getOwnPropertyNames(Object.getPrototypeOf(object)).toSorted(),
};
}

export function collectReport({ document, gBrowser, gZenMediaController, appinfo }) {
return {
environment: describeEnvironment(appinfo),
csp: document.querySelector(CSP_SELECTOR)?.getAttribute("content") ?? null,
toolbar: describeToolbar(document),
zenMediaController: describeMembers(gZenMediaController),
controllers: describeControllers(gBrowser.tabs),
};
}
Loading