2024-07-10 15:41:21 -04:00
|
|
|
import {
|
|
|
|
DOMParser,
|
2024-07-11 13:42:31 -04:00
|
|
|
} from "https://deno.land/x/deno_dom@v0.1.46/deno-dom-wasm.ts";
|
2024-07-11 01:12:25 -04:00
|
|
|
import * as css from "https://deno.land/x/css@0.3.0/mod.ts";
|
|
|
|
|
|
|
|
// so, so brittle
|
|
|
|
function is_valid_color(color: string): boolean {
|
|
|
|
try {
|
|
|
|
const { stylesheet } = css.parse(`*{color:${color};}`);
|
2024-07-11 18:39:36 -04:00
|
|
|
return color.toLowerCase() != "example"
|
|
|
|
&& stylesheet.rules.length == 1
|
|
|
|
&& stylesheet.rules[0].declarations.length == 1;
|
2024-07-11 01:12:25 -04:00
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-07-10 15:41:21 -04:00
|
|
|
|
|
|
|
// maybe a bit high, maybe a bit low
|
|
|
|
const MAX_AGE_MS = 10 * 60 * 1000;
|
|
|
|
|
|
|
|
// > "light-dark(oklab(000, 000, 000), oklab(255, 255, 255))".length
|
|
|
|
// 54
|
2024-07-11 01:17:02 -04:00
|
|
|
const MAX_COLOR_LEN = 512;
|
2024-07-10 15:41:21 -04:00
|
|
|
|
|
|
|
const colors: Record<
|
|
|
|
string,
|
|
|
|
{ color: string | undefined; last_checked: Date }
|
|
|
|
> = {};
|
|
|
|
const dom_parser = new DOMParser();
|
|
|
|
|
2024-07-11 13:42:31 -04:00
|
|
|
function parse_color(str: string): string | undefined {
|
|
|
|
const text = str.replace(/^#/, "");
|
|
|
|
if (text.startsWith("name-color:")) {
|
|
|
|
const line = text.split("\n", 1)[0];
|
|
|
|
const color = line.split("name-color:")[1]
|
|
|
|
.trim()
|
|
|
|
.slice(0, MAX_COLOR_LEN)
|
2024-07-11 13:57:00 -04:00
|
|
|
.replaceAll(/[;{}"']|\/\*|\*\//g, "");
|
2024-07-11 13:42:31 -04:00
|
|
|
if (is_valid_color(color)) {
|
|
|
|
return color;
|
|
|
|
} else {
|
|
|
|
console.log(`invalid color!!!! ${color}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-10 15:41:21 -04:00
|
|
|
async function get_name_color(name: string): Promise<string | undefined> {
|
|
|
|
console.log(`fetching ${name}...`);
|
|
|
|
const resp = await fetch(`https://cohost.org/${name}`);
|
|
|
|
if (!resp.ok) {
|
|
|
|
console.log(`fetching https://cohost.org/${name} failed!`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const body = await resp.text();
|
|
|
|
const dom = dom_parser.parseFromString(body, "text/html");
|
|
|
|
if (!dom) {
|
|
|
|
console.log(`parsing https://cohost.org/${name} to the dom failed!`);
|
|
|
|
return;
|
|
|
|
}
|
2024-07-11 13:42:31 -04:00
|
|
|
|
|
|
|
const a = dom.querySelector("a[href*='name-color:']");
|
|
|
|
if (a) {
|
|
|
|
const href = a.getAttribute("href");
|
|
|
|
if (href) {
|
|
|
|
const color = parse_color(href);
|
|
|
|
if (color) return color;
|
2024-07-10 15:41:21 -04:00
|
|
|
}
|
|
|
|
}
|
2024-07-11 13:42:31 -04:00
|
|
|
|
2024-07-25 23:53:48 -04:00
|
|
|
for (const p of dom.querySelectorAll("p, a")) {
|
2024-07-11 17:37:00 -04:00
|
|
|
if (!p.textContent.includes("name-color:")) continue;
|
|
|
|
|
|
|
|
for (const node of p.childNodes) {
|
|
|
|
const color = parse_color(node.textContent);
|
|
|
|
if (color) return color;
|
|
|
|
}
|
2024-07-11 13:42:31 -04:00
|
|
|
}
|
2024-07-10 15:41:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function update_name_color(name: string) {
|
|
|
|
const now = new Date();
|
|
|
|
if (
|
|
|
|
colors[name] &&
|
|
|
|
now.getTime() - colors[name].last_checked.getTime() <= MAX_AGE_MS
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const color = await get_name_color(name);
|
|
|
|
colors[name] = { color, last_checked: now };
|
|
|
|
}
|
|
|
|
|
2024-07-10 16:03:16 -04:00
|
|
|
function cssify({ requester }: { requester?: string }): string {
|
2024-07-10 17:15:21 -04:00
|
|
|
const preamble = `/* customized: ${
|
2024-07-11 01:21:42 -04:00
|
|
|
Object.keys(colors)
|
2024-07-11 01:29:24 -04:00
|
|
|
.filter((name) => colors[name].color)
|
|
|
|
.sort()
|
2024-07-10 17:15:21 -04:00
|
|
|
.join(", ")
|
|
|
|
} */\n/* look, i don't like !important either, but i think it's a better choice than some lame .spec.ific.ity.hack.ing */\n\n`;
|
|
|
|
|
2024-07-10 16:03:16 -04:00
|
|
|
let vars = `:root {\n`;
|
|
|
|
if (requester && colors[requester]) {
|
2024-07-11 02:32:55 -04:00
|
|
|
vars += `--name-color: var(--name-color-${requester.toLowerCase()});\n`;
|
2024-07-10 16:03:16 -04:00
|
|
|
}
|
2024-07-10 17:15:21 -04:00
|
|
|
let classes = "";
|
2024-07-11 01:21:42 -04:00
|
|
|
for (const name of Object.keys(colors).sort()) {
|
|
|
|
const { color } = colors[name];
|
2024-07-10 16:03:16 -04:00
|
|
|
if (!color) continue;
|
2024-07-11 02:32:55 -04:00
|
|
|
vars += `--name-color-${name.toLowerCase()}: ${color};\n`;
|
2024-07-10 17:15:21 -04:00
|
|
|
classes +=
|
2024-07-11 02:32:55 -04:00
|
|
|
`a[href$='/${name}' i]{color: var(--name-color-${name.toLowerCase()}) !important;}\n`;
|
2024-07-10 16:03:16 -04:00
|
|
|
}
|
2024-07-10 17:15:21 -04:00
|
|
|
vars += "}\n\n";
|
|
|
|
return preamble + vars + classes;
|
2024-07-10 16:03:16 -04:00
|
|
|
}
|
|
|
|
|
2024-07-10 15:41:21 -04:00
|
|
|
Deno.serve({ port: 61266 }, async (req) => {
|
|
|
|
const url = new URL(req.url);
|
|
|
|
const names = url.searchParams.getAll("name").filter((name) =>
|
2024-07-11 13:42:31 -04:00
|
|
|
!name.match(/[\/\\?"';{}]/)
|
|
|
|
).map((name) => name.toLowerCase());
|
2024-07-10 15:41:21 -04:00
|
|
|
await Promise.all(names.map(update_name_color));
|
2024-07-10 16:03:16 -04:00
|
|
|
const css = cssify({
|
|
|
|
requester: url.searchParams.get("requester") ?? undefined,
|
|
|
|
});
|
|
|
|
return new Response(css, { headers: { "content-type": "text/css" } });
|
2024-07-10 15:41:21 -04:00
|
|
|
});
|