The best way to control Claude Code, Codex and more from any computer or phone

main
ts 120 lines 4.73 KB
Raw
1 import * as Base64 from "effect/encoding/Base64";
2 import * as Result from "effect/Result";
3
4 import type { MobileThemeAppearance } from "./mobileTheme";
5
6 const SVG_DATA_URL_PREFIX = "data:image/svg+xml;base64,";
7 const STYLE_ELEMENT_RE = /<!--[\s\S]*?-->|(<style\b[^>]*>)([\s\S]*?)(<\/style>)/gi;
8 const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//g;
9 const COLOR_SCHEME_MEDIA_RE = /@media\s*\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)\s*\{/gi;
10 const AT_RULE_BLOCK_RE = /@[^{};]*\{/g;
11 const RULE_RE = /([^{}]*)\{([^{}]*)\}/g;
12 const IMPORTANT_RE = /!\s*important$/i;
13 const PLAIN_COLOR_RE = /^(?:#[\da-f]{3,8}|[a-z]+|(?:rgba?|hsla?)\([\d\s.,%/]+\))$/i;
14 const COMMENT_OR_SVG_TAG_RE = /<!--[\s\S]*?-->|<svg\b[^>]*>/gi;
15 const COLOR_ATTRIBUTE_RE = /\s+color\s*=\s*(?:"[^"]*"|'[^']*')/i;
16 const MEDIA_ATTRIBUTE_RE = /\s+media\s*=\s*(?:"([^"]*)"|'([^']*)')/i;
17 const COLOR_SCHEME_CONDITION_RE = /^\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)$/i;
18
19 // CoreSVG ignores root stylesheet colors and misreads @media; resolve both before decoding.
20 export function resolveProjectFaviconColorScheme(url: string, scheme: MobileThemeAppearance) {
21 if (!url.startsWith(SVG_DATA_URL_PREFIX)) return url;
22 const decoded = Base64.decodeString(url.slice(SVG_DATA_URL_PREFIX.length));
23 if (Result.isFailure(decoded)) return url;
24 const svg = decoded.success;
25
26 let stylesheet = "";
27 const styled = svg.replace(
28 STYLE_ELEMENT_RE,
29 (match, open: string, css: string | undefined, close: string) => {
30 if (css === undefined) return match;
31 const mediaAttribute = open.match(MEDIA_ATTRIBUTE_RE);
32 const media = (mediaAttribute?.[1] ?? mediaAttribute?.[2])?.trim();
33 if (media) {
34 const condition = media.match(COLOR_SCHEME_CONDITION_RE);
35 if (!condition) return match;
36 if (condition[1]?.toLowerCase() !== scheme) return "";
37 open = open.replace(MEDIA_ATTRIBUTE_RE, "");
38 }
39 // Comments can hold braces or a commented-out `@media`, which the block matcher would count.
40 const resolvedCss = replaceBlocks(
41 css.replace(CSS_COMMENT_RE, ""),
42 COLOR_SCHEME_MEDIA_RE,
43 (match, body) => (match[1]?.toLowerCase() === scheme ? body : ""),
44 );
45 stylesheet += `${resolvedCss}\n`;
46 return `${open}${resolvedCss}${close}`;
47 },
48 );
49 const rootColor = lastRootColor(stylesheet);
50 const resolved = rootColor === undefined ? styled : withRootColor(styled, rootColor);
51 return resolved === svg ? url : `${SVG_DATA_URL_PREFIX}${Base64.encode(resolved)}`;
52 }
53
54 function withRootColor(svg: string, color: string) {
55 let applied = false;
56 return svg.replace(COMMENT_OR_SVG_TAG_RE, (match) => {
57 if (applied || match.startsWith("<!--")) return match;
58 applied = true;
59 return `${match.slice(0, 4)} color="${color}"${match.slice(4).replace(COLOR_ATTRIBUTE_RE, "")}`;
60 });
61 }
62
63 function replaceBlocks(
64 css: string,
65 opening: RegExp,
66 replace: (match: RegExpExecArray, body: string) => string,
67 ) {
68 let result = "";
69 let cursor = 0;
70 for (const match of css.matchAll(opening)) {
71 if (match.index < cursor) continue;
72 const bodyStart = match.index + match[0].length;
73 const bodyEnd = matchingBrace(css, bodyStart);
74 if (bodyEnd === -1) break;
75 result += css.slice(cursor, match.index) + replace(match, css.slice(bodyStart, bodyEnd));
76 cursor = bodyEnd + 1;
77 }
78 return result + css.slice(cursor);
79 }
80
81 function matchingBrace(css: string, from: number) {
82 let depth = 1;
83 for (let index = from; index < css.length; index++) {
84 if (css[index] === "{") depth++;
85 else if (css[index] === "}" && --depth === 0) return index;
86 }
87 return -1;
88 }
89
90 function lastRootColor(css: string) {
91 let color: string | undefined;
92 let priority = -1;
93 const topLevel = replaceBlocks(css, AT_RULE_BLOCK_RE, () => "");
94 for (const [, selectors = "", declarations = ""] of topLevel.matchAll(RULE_RE)) {
95 const rootSelectors = selectors
96 .replace(/<!\[CDATA\[|\]\]>/g, "")
97 .split(",")
98 .map((selector) => selector.trim().toLowerCase());
99 const specificity = rootSelectors.includes(":root")
100 ? 1
101 : rootSelectors.includes("svg")
102 ? 0
103 : -1;
104 if (specificity === -1) continue;
105 for (const declaration of declarations.split(";")) {
106 const separator = declaration.indexOf(":");
107 if (separator === -1 || declaration.slice(0, separator).trim().toLowerCase() !== "color") {
108 continue;
109 }
110 let value = declaration.slice(separator + 1).trim();
111 const important = IMPORTANT_RE.test(value);
112 if (important) value = value.slice(0, value.lastIndexOf("!")).trim();
113 const nextPriority = specificity + (important ? 2 : 0);
114 if (nextPriority < priority) continue;
115 color = PLAIN_COLOR_RE.test(value) ? value : undefined;
116 priority = nextPriority;
117 }
118 }
119 return color;
120 }