-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicon-renderer.js
More file actions
41 lines (35 loc) · 1.72 KB
/
Copy pathicon-renderer.js
File metadata and controls
41 lines (35 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Generate the PNG the extension's toolbar icon would be for a given tab URL, by
// loading the REAL extension/icon/toolbar-icon.js into a fake browser (fake-chrome.js),
// letting it register its declarativeContent rules through the worker's own
// installRules() path, then asking the fake what icon it would paint at that URL.
//
// So the icon's variant + pixels are produced by the shipped worker decoding the
// shipped art; only the surrounding browser (the chrome.* APIs, the URL→rule match,
// the default-icon fallback) is faked. The toolbar action only generates 16/32 px
// (all buildRules() decodes), so we assert the most detailed of those, 32.
"use strict";
const fs = require("node:fs");
const path = require("node:path");
const { PNG } = require("pngjs");
const { FakeBrowser } = require("./fake-chrome");
// The shipped extension lives under extension/; the worker's own fetch paths
// ("host-lists.json", "icon/images/…") are extension-root-relative, so the fake browser reads
// from EXT_ROOT.
const EXT_ROOT = path.join(__dirname, "..", "..", "..", "..", "extension");
const WORKER = path.join(EXT_ROOT, "icon/toolbar-icon.js");
const MANIFEST = JSON.parse(fs.readFileSync(path.join(EXT_ROOT, "manifest.json"), "utf8"));
const ASSERT_SIZE = 32;
function encodePng(imageData) {
const png = new PNG({ width: imageData.width, height: imageData.height });
Buffer.from(imageData.data).copy(png.data);
return PNG.sync.write(png);
}
async function iconPngForUrl(url, lists) {
const browser = await new FakeBrowser({
root: EXT_ROOT,
lists,
defaultIcon: MANIFEST.action.default_icon,
}).loadWorker(WORKER);
return encodePng(browser.iconAt(url, ASSERT_SIZE));
}
module.exports = { iconPngForUrl, ASSERT_SIZE };