Files
T
shadowdaoandClaude Sonnet 5.5 e49078ee03 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>
2026-10-04 11:37:38 -07:00

58 lines
1.8 KiB
JavaScript

const { Tray, Menu, nativeImage, shell, clipboard } = require('electron');
const { TRAY_ICON_BASE64 } = require('./tray-icon');
/**
* @param {Object} options
* @param {number} options.port
* @param {() => void} options.onShow
* @param {() => void} options.onQuit
* @returns {{ update: (info: { title: string|null, artist: string|null }) => void, destroy: () => void }}
*/
function createTray({ port, onShow, onQuit }) {
let trayInstance = null;
let currentTitle = null;
let currentArtist = null;
function buildMenu() {
const url = `http://localhost:${port}/`;
const nowPlaying =
currentTitle && currentArtist
? `${currentTitle} — ${currentArtist}`
: currentTitle || 'Nothing playing';
return Menu.buildFromTemplate([
{ label: "What's That Music", enabled: false },
{ label: `Now Playing: ${nowPlaying}`, enabled: false },
{ type: 'separator' },
{ label: 'Show Player', click: () => onShow && onShow() },
{ label: 'Open Overlay in Browser', click: () => shell.openExternal(url) },
{ label: 'Copy OBS URL', click: () => clipboard.writeText(url) },
{ type: 'separator' },
{ label: 'Quit', click: () => onQuit && onQuit() },
]);
}
const icon = nativeImage.createFromDataURL(TRAY_ICON_BASE64);
icon.setTemplateImage(true);
trayInstance = new Tray(icon);
trayInstance.setToolTip("What's That Music");
trayInstance.setContextMenu(buildMenu());
return {
update({ title, artist }) {
currentTitle = title || null;
currentArtist = artist || null;
if (trayInstance) trayInstance.setContextMenu(buildMenu());
},
destroy() {
if (trayInstance) {
trayInstance.destroy();
trayInstance = null;
}
},
};
}
module.exports = { createTray };