# Singable: development

Singable romanizes and translates lyrics inside Spotify's web player, restores the
original script when Spotify serves romanized lyrics, and finds lyrics for songs Spotify has
none for. Version 4.0.0 is a clean-slate rebuild of 3.x. Chrome and Edge
use one build; Firefox (142+) has its own MV3 build.

- [PROBLEM.md](../PROBLEM.md): what this is for, in priority order. Every change is judged
  against it.
- [ARCHITECTURE.md](../ARCHITECTURE.md): how it works. The short version: we never rebuild
  Spotify's lyrics view. We change the lyrics Spotify receives (network) and how each
  line's text is drawn (attributes and CSS).

## Build

```bash
npm install
npm run build          # .output/chrome-mv3 (Edge: npm run build:edge)
npm run build:firefox  # .output/firefox-mv3 (Firefox 142+, MV3)
npm run zip && npm run zip:firefox && npm run zip:edge   # store zips + Firefox sources zip
npm test
npm run typecheck
npm run dev            # .output/chrome-mv3-dev, rebuilt and reloaded on every change
npm run build:devkit   # .output/chrome-mv3-devkit, standalone, with the dev kit
```

Dev builds carry a dev kit: `__skDev` in the Spotify page's console reads the extension's
state, changes settings, runs the popup's actions, forgets cached results and reloads the
extension. See [docs/devkit.md](devkit.md).

Load `.output/chrome-mv3` at `chrome://extensions` (Developer mode → Load unpacked).

To rebuild the Firefox add-on from the sources zip (what Mozilla's reviewers do): Node 22,
`npm ci`, then `npm run build:firefox`; the add-on is `.output/firefox-mv3`. The list of
bundled packages and their licences is `public/THIRD_PARTY_NOTICES.txt` (regenerate with
`npm run licenses` after changing dependencies).

## Live testing in Chrome

Branded Chrome ignores `--load-extension`, and Chrome for Testing can't play Spotify (no
Widevine). `tools/live/driver.mjs` therefore starts real Chrome over a debugging pipe and
loads the extension through CDP, using the test profile in `.live/chrome-profile`
(git-ignored).

1. Sign the profile in once, with plain Chrome (Spotify refuses a remote-controlled
   browser at login):
   `open -na "Google Chrome" --args --user-data-dir=$PWD/.live/chrome-profile`
   Sign in, then quit that window.
2. `node tools/live/driver.mjs` (it serves commands on 127.0.0.1:9444). Run driver and
   ctl commands on their own, not chained: Claude Code's allow rules
   (`Bash(node tools/live/driver.mjs:*)`, `Bash(node tools/live/ctl.mjs:*)`, in
   `~/.claude/settings.json`) only match whole commands.
3. `node tools/live/ctl.mjs eval '<js in the Spotify page>'`, `sw '<js in the service
   worker>'`, `shot`, `clickAt`, `reload '{"ext":true}'`, `shotTab '{"url":"popup"}'`,
   `quit`. Also: `swNetwork '{"js":"…","ms":9000}'` (the headers on the worker's own requests
   while the page runs `js`; cookies by name only), `setCookie` / `deleteCookie`
   `'{"name":"…","domain":".youtube.com"}'` (a test cookie, to see that none goes out),
   `stopWorker` (stops the service worker as Chrome does when idle; after it `sw` no longer
   finds the worker, so run worker-side code from the popup page with `inTab`).

Before pressing play, check that nothing says "Playing on …": if the test profile's Spotify
account is playing somewhere else, Spotify Connect moves playback. Mute the test player.

Firefox: `SK_BROWSER=firefox node tools/live/driver.mjs` runs the same driver on Firefox with the
Firefox build (`.output/firefox-mv3`) and its own profile (`.live/firefox-profile`); see the notes at
the top of `tools/live/driver.mjs`.

Word by word, in either browser (one signed-in browser at a time):
`node tools/live/sweep-battery.mjs ["song artist" ...]` plays each song and checks the word timing
and its credit, one line lit per frame, the sweep against the audio (within 60 ms at the 90th
percentile), lines lit ahead of their first word, pause, mode switch, reopening, resume, seek,
Word by word off and on and a reload, then a track change; frames next to page stalls (a
throttled test window) are left out. `tools/live/sweep-soak.mjs "song" 215` plays a song start
to end and checks every painted frame. `tools/live/sweep-release-check.mjs` checks a release
build (no dev kit) from the page alone; after starting the driver on a release build, run
`ctl.mjs reload '{"ext":true}'` first (an older worker can keep running).

`node tools/live/headers.mjs` shows the headers Chrome sends on the extension's Google Translate
request, in a throwaway profile (one real request to Google: run it rarely).

`tools/probe/` is a tiny extension that records Spotify's lyrics-related network traffic
from page start. Use it to see what Spotify changed after a redesign.

## A test build in your everyday Firefox

Release Firefox keeps only Mozilla-signed add-ons across restarts. For testing a build over days,
sign it **unlisted** (private, not on the store) under its own id, so it never touches the
published add-on:

1. Build: `SK_TEST_BUILD=1 SK_TEST_VERSION=4.0.0.1 npm run build:firefox` ("Singable
   (test)", id `spotify-karaoke-test@example.com`). Each signed upload needs a new
   `SK_TEST_VERSION` (4.0.0.2, ...).
2. Sign with your API key from addons.mozilla.org/developers/addon/api/key/ (the secret is read
   without echoing and isn't kept in your shell history; zsh, macOS's default shell):
   `read -r "K?AMO API key: " && read -rs "V?AMO API secret: " && echo && npx --yes web-ext@8 sign --source-dir .output/firefox-mv3 --channel unlisted --artifacts-dir .output/signed --api-key "$K" --api-secret "$V"`
3. Install `.output/signed/*.xpi` (drag it onto Firefox, or about:addons → gear → Install
   Add-on From File). Disable or uninstall the store version meanwhile: two copies would both
   decorate Spotify.
