Fix now-playing scraper, quit hang, tray icon, and tray race (v1.0.1)

- Read the track from the Media Session API, falling back to the legacy
  ytmusic-player-bar scrape. Current YT Music layouts render no player bar,
  so the API and tray stayed empty while music played. Artist is also clean
  now (no "• views • likes" suffix).
- Terminate connected WebSocket clients before closing the server. ws's
  close() waits for every client to disconnect, so quitting with an overlay
  open hung forever, leaving the window playing and the port bound.
- Replace the tray icon with a valid PNG (the old one had a bad IDAT CRC and
  decoded to an empty image) and test that its chunks are well formed.
- Create the tray before the window so early state updates are not dropped.
- Bump version to 1.0.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-04 11:37:38 -07:00
co-authored by Claude Sonnet 5.5
parent f73ea93dde
commit e49078ee03
11 changed files with 192 additions and 54 deletions
+60
View File
@@ -0,0 +1,60 @@
const { test } = require('node:test');
const assert = require('node:assert');
const { scrapeNowPlaying } = require('../now-playing');
const emptyDoc = { querySelector: () => null };
function nav(metadata, playbackState) {
return { mediaSession: { metadata, playbackState } };
}
test('reads track info from the Media Session when the player bar is absent', () => {
const result = scrapeNowPlaying(
emptyDoc,
nav(
{
title: 'DOMINO',
artist: 'Stray Kids',
album: '',
artwork: [
{ src: 'small.jpg', sizes: '60x60' },
{ src: 'big.jpg', sizes: '544x544' },
],
},
'playing'
)
);
assert.deepStrictEqual(result, {
title: 'DOMINO',
artist: 'Stray Kids',
albumArt: 'big.jpg',
isPlaying: true,
});
});
test('reports isPlaying false when the Media Session is paused', () => {
const result = scrapeNowPlaying(emptyDoc, nav({ title: 'T', artist: 'A', artwork: [] }, 'paused'));
assert.strictEqual(result.isPlaying, false);
assert.strictEqual(result.albumArt, null);
});
test('returns null when nothing is loaded anywhere', () => {
assert.strictEqual(scrapeNowPlaying(emptyDoc, nav(null, 'none')), null);
assert.strictEqual(scrapeNowPlaying(emptyDoc, {}), null);
});
test('falls back to the legacy player-bar DOM when Media Session has no metadata', () => {
const els = {
'ytmusic-player-bar .title': { textContent: ' Old Song ' },
'ytmusic-player-bar .byline': { textContent: 'Old Artist' },
'ytmusic-player-bar img.image': { src: 'art.jpg' },
'#play-pause-button': { getAttribute: (n) => (n === 'title' ? 'Pause' : null) },
};
const doc = { querySelector: (s) => els[s] || null };
assert.deepStrictEqual(scrapeNowPlaying(doc, nav(null, 'none')), {
title: 'Old Song',
artist: 'Old Artist',
albumArt: 'art.jpg',
isPlaying: true,
});
});
+20
View File
@@ -0,0 +1,20 @@
const { test } = require('node:test');
const assert = require('node:assert');
const WebSocket = require('ws');
const { startServer } = require('../server');
test('stop() resolves even while an overlay WebSocket client is connected', async () => {
const handle = await startServer({ port: 9300 });
const client = new WebSocket(`ws://127.0.0.1:${handle.port}`);
client.on('error', () => {});
await new Promise((resolve) => client.on('open', resolve));
let timer;
const result = await Promise.race([
handle.stop().then(() => 'stopped'),
new Promise((resolve) => { timer = setTimeout(() => resolve('hung'), 2000); }),
]);
clearTimeout(timer);
assert.strictEqual(result, 'stopped');
});
+25
View File
@@ -0,0 +1,25 @@
const { test } = require('node:test');
const assert = require('node:assert');
const zlib = require('node:zlib');
const { TRAY_ICON_BASE64 } = require('../tray-icon');
test('tray icon is a structurally valid PNG (every chunk CRC checks out)', () => {
const prefix = 'data:image/png;base64,';
assert.ok(TRAY_ICON_BASE64.startsWith(prefix));
const png = Buffer.from(TRAY_ICON_BASE64.slice(prefix.length), 'base64');
assert.strictEqual(png.subarray(0, 8).toString('hex'), '89504e470d0a1a0a');
const types = [];
let off = 8;
while (off < png.length) {
const len = png.readUInt32BE(off);
const type = png.subarray(off + 4, off + 8);
const body = png.subarray(off + 8, off + 8 + len);
const crc = png.readUInt32BE(off + 8 + len);
assert.strictEqual(zlib.crc32(Buffer.concat([type, body])), crc, `bad CRC in ${type}`);
types.push(type.toString());
off += 12 + len;
}
assert.strictEqual(off, png.length, 'trailing bytes after last chunk');
assert.deepStrictEqual([types[0], types.at(-1)], ['IHDR', 'IEND']);
});