From ec12ce588fee892592d8c009605cc00400b5cd46 Mon Sep 17 00:00:00 2001 From: mehbark Date: Wed, 10 Jul 2024 16:03:16 -0400 Subject: [PATCH] name-color serverside gens css :D --- serverside/name-color.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/serverside/name-color.ts b/serverside/name-color.ts index f60227d..aca3296 100644 --- a/serverside/name-color.ts +++ b/serverside/name-color.ts @@ -31,7 +31,9 @@ async function get_name_color(name: string): Promise { for (const p of dom.querySelectorAll("p")) { const text = p.textContent; if (text.startsWith("name-color: ")) { - return text.split("name-color: ")[1].slice(0, MAX_COLOR_LEN); + return text.split("name-color: ")[1] + .slice(0, MAX_COLOR_LEN) + .replaceAll(";", ""); } } } @@ -48,6 +50,21 @@ async function update_name_color(name: string) { colors[name] = { color, last_checked: now }; } +function cssify({ requester }: { requester?: string }): string { + let vars = `:root {\n`; + if (requester && colors[requester]) { + vars += `--name-color: var(--name-color-${requester});\n`; + } + let classes = ".name { color: var(--name-color); }\n"; + for (const [name, { color }] of Object.entries(colors)) { + if (!color) continue; + vars += `--name-color-${name}: ${color};\n`; + classes += `.name-${name}{--name-color:var(--name-color-${name})}`; + } + vars += "}\n"; + return vars + classes; +} + // TODO: ONE BIG REQUEST/RESPONSE DUHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHh Deno.serve({ port: 61266 }, async (req) => { const url = new URL(req.url); @@ -55,5 +72,8 @@ Deno.serve({ port: 61266 }, async (req) => { !name.match(/\/\?/) ); await Promise.all(names.map(update_name_color)); - return Response.json(colors); + const css = cssify({ + requester: url.searchParams.get("requester") ?? undefined, + }); + return new Response(css, { headers: { "content-type": "text/css" } }); });