// Builds the browser extension for each browser from the one source in extension/ (docs/BROWSER.md): // // node tools/extension.mjs [--out ] [--zip] // // writes /firefox/ and /chromium/ (default out: target/extension), each with its manifest.json (the // browser's manifest..json, with the workspace version) and the shared scripts and popup. With --zip // also custos--.zip next to them, reproducible (fixed order and times): the Firefox one is what // AMO signs, the Chromium one what the Edge and Chrome stores take. import fs from 'node:fs'; import path from 'node:path'; import zlib from 'node:zlib'; const root = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..'); const arg = (name) => { const i = process.argv.indexOf(name); return i > 0 ? process.argv[i + 1] : null; }; const out = path.resolve(arg('--out') ?? path.join(root, 'target', 'extension')); const zip = process.argv.includes('--zip'); const cargo = fs.readFileSync(path.join(root, 'Cargo.toml'), 'utf8'); const version = /\[workspace\.package\][^[]*?version = "([^"]+)"/s.exec(cargo)?.[1]; if (!version) throw new Error('no workspace version in Cargo.toml'); const SHARED = ['background.js', 'content.js', 'popup.html', 'popup.js', 'popup.css']; const BROWSERS = ['firefox', 'chromium']; /** A zip of `files` ([name, bytes]), deflated, with a fixed time: the same input gives the same bytes. */ function zipOf(files) { const parts = []; const central = []; let offset = 0; const time = 0, date = (2026 - 1980) << 9 | 1 << 5 | 1; // 2026-01-01 00:00 for (const [name, data] of files) { const n = Buffer.from(name, 'utf8'); const packed = zlib.deflateRawSync(data, { level: 9 }); const crc = zlib.crc32(data); const local = Buffer.alloc(30); local.writeUInt32LE(0x04034b50, 0); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x0800, 6); // UTF-8 names local.writeUInt16LE(8, 8); local.writeUInt16LE(time, 10); local.writeUInt16LE(date, 12); local.writeUInt32LE(crc, 14); local.writeUInt32LE(packed.length, 18); local.writeUInt32LE(data.length, 22); local.writeUInt16LE(n.length, 26); parts.push(local, n, packed); const c = Buffer.alloc(46); c.writeUInt32LE(0x02014b50, 0); c.writeUInt16LE(20, 4); c.writeUInt16LE(20, 6); c.writeUInt16LE(0x0800, 8); c.writeUInt16LE(8, 10); c.writeUInt16LE(time, 12); c.writeUInt16LE(date, 14); c.writeUInt32LE(crc, 16); c.writeUInt32LE(packed.length, 20); c.writeUInt32LE(data.length, 24); c.writeUInt16LE(n.length, 28); c.writeUInt32LE(offset, 42); central.push(c, n); offset += 30 + n.length + packed.length; } const dir = Buffer.concat(central); const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10); end.writeUInt32LE(dir.length, 12); end.writeUInt32LE(offset, 16); return Buffer.concat([...parts, dir, end]); } for (const browser of BROWSERS) { const manifest = JSON.parse(fs.readFileSync(path.join(root, 'extension', `manifest.${browser}.json`), 'utf8')); const withVersion = { manifest_version: manifest.manifest_version, name: manifest.name, version, ...manifest }; const files = [['manifest.json', Buffer.from(JSON.stringify(withVersion, null, 2) + '\n')]]; for (const f of SHARED) files.push([f, fs.readFileSync(path.join(root, 'extension', f))]); const dir = path.join(out, browser); fs.rmSync(dir, { recursive: true, force: true }); fs.mkdirSync(dir, { recursive: true }); for (const [name, data] of files) fs.writeFileSync(path.join(dir, name), data); if (zip) fs.writeFileSync(path.join(out, `custos-${browser}-${version}.zip`), zipOf(files)); console.log(`${browser}: ${dir}${zip ? ` (+ custos-${browser}-${version}.zip)` : ''}`); }