Files
WebSpeak-client-for-TeamSpeak/web/scripts/build-host-chrome-demo-skin.mjs
T
OddTurtleHead 881354e283 feat(skin): dock recovery as host-owned toolbar buttons
Add chrome.json slot mode "toolbar" with grouped and per-action recovery buttons, page-specific anchor candidates and sanitized toolbar style tokens. Keep the floating capsule as the bad-anchor fallback, hide the duplicate plugin launcher, and preserve legacy capsule slots. Document the full contract, add a minimal toolbar example package, and cover the new behavior in unit and browser tests.
2026-11-26 23:06:43 +08:00

126 lines
5.1 KiB
JavaScript

import { readFile, writeFile } from "node:fs/promises";
import { deflateSync } from "node:zlib";
import { zipSync, strToU8 } from "fflate";
const source = new URL("../../docs/examples/host-chrome-demo/", import.meta.url);
const toolbarSource = new URL("../../docs/examples/host-chrome-toolbar/", import.meta.url);
const examples = new URL("../../docs/examples/", import.meta.url);
const publicDir = new URL("../public/", import.meta.url);
// Keep rebuilt archives byte-stable when their inputs do not change.
const archiveMtime = new Date(2026, 10, 15, 16, 33, 0);
const crcTable = (() => {
const table = new Uint32Array(256);
for (let n = 0; n < 256; n += 1) {
let c = n;
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
table[n] = c >>> 0;
}
return table;
})();
function crc32(bytes) {
let c = 0xffffffff;
for (const byte of bytes) c = crcTable[(c ^ byte) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
}
function chunk(type, data) {
const out = Buffer.alloc(data.length + 12);
out.writeUInt32BE(data.length, 0);
out.write(type, 4, "ascii");
data.copy(out, 8);
out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
return out;
}
/** Build a tiny 24x24 RGBA PNG without any native image dependency. */
function iconPng(base, accent) {
const size = 24;
const stride = size * 4 + 1;
const raw = Buffer.alloc(stride * size);
for (let y = 0; y < size; y += 1) {
raw[y * stride] = 0;
for (let x = 0; x < size; x += 1) {
const border = x < 2 || y < 2 || x > size - 3 || y > size - 3;
const glyph = (x > 6 && x < 18 && y > 6 && y < 10) || (x > 10 && x < 14 && y > 6 && y < 18);
const color = border ? accent : glyph ? [255, 255, 255] : base;
const offset = y * stride + 1 + x * 4;
raw[offset] = color[0]; raw[offset + 1] = color[1]; raw[offset + 2] = color[2]; raw[offset + 3] = 255;
}
}
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(size, 0);
ihdr.writeUInt32BE(size, 4);
ihdr[8] = 8; ihdr[9] = 6; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0;
return Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", ihdr),
chunk("IDAT", deflateSync(raw, { level: 9 })),
chunk("IEND", Buffer.alloc(0)),
]);
}
const icons = {
"assets/icons/language.png": iconPng([29, 79, 156], [16, 46, 96]),
"assets/icons/skin.png": iconPng([22, 130, 106], [12, 76, 62]),
"assets/icons/apps.png": iconPng([208, 108, 32], [128, 62, 12]),
"assets/icons/settings.png": iconPng([92, 92, 118], [48, 48, 68]),
"assets/icons/status.png": iconPng([150, 60, 90], [84, 28, 52]),
};
const [manifestText, cssText, chromeText, toolbarManifestText, toolbarCssText, toolbarChromeText] = await Promise.all([
readFile(new URL("manifest.json", source), "utf8"),
readFile(new URL("skin.css", source)),
readFile(new URL("chrome.json", source), "utf8"),
readFile(new URL("manifest.json", toolbarSource), "utf8"),
readFile(new URL("skin.css", toolbarSource)),
readFile(new URL("chrome.json", toolbarSource), "utf8"),
]);
const toolbarManifest = JSON.parse(toolbarManifestText);
if (toolbarManifest.author !== "WebSpeak Community") {
throw new Error("host-chrome-toolbar manifest author must be WebSpeak Community");
}
const chrome = JSON.parse(chromeText);
const sharedFiles = {
"skin.css": new Uint8Array(cssText),
...Object.fromEntries(Object.entries(icons).map(([path, bytes]) => [path, new Uint8Array(bytes)])),
};
// Variant A: theming + bitmap icons only (watermark / hints / launcher visible).
const themedManifest = JSON.stringify({ ...JSON.parse(manifestText), id: "community.host-chrome-themed", name: "Host Chrome Themed" });
const themedChrome = JSON.stringify({ schemaVersion: 1, visibleParts: chrome.visibleParts, icons: chrome.icons });
const themed = zipSync({
"manifest.json": strToU8(themedManifest),
"chrome.json": strToU8(themedChrome),
...sharedFiles,
}, { level: 9, mtime: archiveMtime });
// Variant B: the same package plus declarative hiding of non-critical chrome.
const hidden = zipSync({
"manifest.json": strToU8(manifestText),
"chrome.json": strToU8(chromeText),
...sharedFiles,
}, { level: 9, mtime: archiveMtime });
// Minimal toolbar-mode sample: host-owned recovery buttons docked into the
// header, with page-wide anchor candidates and toolbar tokens.
const toolbar = zipSync({
"manifest.json": strToU8(toolbarManifestText),
"chrome.json": strToU8(toolbarChromeText),
"skin.css": new Uint8Array(toolbarCssText),
}, { level: 9, mtime: archiveMtime });
await Promise.all([
writeFile(new URL("host-chrome-themed.wskin", examples), themed),
writeFile(new URL("host-chrome-demo.wskin", examples), hidden),
writeFile(new URL("host-chrome-toolbar.wskin", examples), toolbar),
writeFile(new URL("host-chrome-themed.wskin", publicDir), themed),
writeFile(new URL("host-chrome-demo.wskin", publicDir), hidden),
writeFile(new URL("host-chrome-toolbar.wskin", publicDir), toolbar),
]);
console.log(`Built host chrome demo skins (${themed.byteLength} / ${hidden.byteLength} / ${toolbar.byteLength} bytes) into docs/examples and web/public.`);