From 7c594f80302c8631ecb122f85344218fab8668ee Mon Sep 17 00:00:00 2001 From: mehbark Date: Thu, 11 Jul 2024 13:42:31 -0400 Subject: [PATCH] name-color: color in href + a tiny bit less brittle --- deno.lock | 22 +++++++++++++++++++ serverside/name-color.ts | 46 +++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/deno.lock b/deno.lock index 5be9f89..b95681e 100644 --- a/deno.lock +++ b/deno.lock @@ -275,6 +275,28 @@ "https://deno.land/x/deno_dom@v0.1.43/src/dom/utils-types.ts": "96db30e3e4a75b194201bb9fa30988215da7f91b380fca6a5143e51ece2a8436", "https://deno.land/x/deno_dom@v0.1.43/src/dom/utils.ts": "4c6206516fb8f61f37a209c829e812c4f5a183e46d082934dd14c91bde939263", "https://deno.land/x/deno_dom@v0.1.43/src/parser.ts": "e06b2300d693e6ae7564e53dfa5c9a9e97fdb8c044c39c52c8b93b5d60860be3", + "https://deno.land/x/deno_dom@v0.1.46/build/deno-wasm/deno-wasm.js": "d6841a06342eb6a2798ef28de79ad69c0f2fa349fa04d3ca45e5fcfbf50a9340", + "https://deno.land/x/deno_dom@v0.1.46/deno-dom-wasm.ts": "a33d160421bbb6e3104285ea5ebf33352b7ad50d82ea8765e3cf65f972b25119", + "https://deno.land/x/deno_dom@v0.1.46/src/api.ts": "0ff5790f0a3eeecb4e00b7d8fbfa319b165962cf6d0182a65ba90f158d74f7d7", + "https://deno.land/x/deno_dom@v0.1.46/src/constructor-lock.ts": "0e7b297e8b9cf921a3b0d3a692ec5fb462c5afc47ec554292e20090b9e16b40a", + "https://deno.land/x/deno_dom@v0.1.46/src/deserialize.ts": "1cf4096678d8afed8ed28dbad690504c4d2c28149ba768b26eacd1416873425b", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/document-fragment.ts": "1c7352a3c816587ed7fad574b42636198f680f17abc3836fcfe7799b31e7718f", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/document.ts": "0b07049fd614c1d460240d1bf3e051084a58105e54887af90f45bc615965f1c6", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/dom-parser.ts": "784ee0e766d4a01e14420f328053fd3a0016c6b40ee442edc3ae80f5d9777927", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/element.ts": "d1a006e4f7cd1eb050838a96ba93a254e5cf4136236c6454ab6e8ffedcf395fd", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/elements/html-template-element.ts": "740b97a5378c9a14cccf3429299846eda240b613013e2d2d7f20b393897453c2", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/html-collection.ts": "eedc0b097612ef420d975df6924850a36a4829b35aafa4c92078609a15a52f08", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/node-list.ts": "aa5d4c2297fadfb1b392ffa0e5c33a6fb8c198e8875bb853c2fc3fc49807701d", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/node.ts": "4ee9bc98f1d5b31a9a36674a9f3e1d6e25b9125b1532c42f84c4624816fe8435", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/selectors/custom-api.ts": "852696bd58e534bc41bd3be9e2250b60b67cd95fd28ed16b1deff1d548531a71", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/selectors/nwsapi-types.ts": "c43b36c36acc5d32caabaa54fda8c9d239b2b0fcbce9a28efb93c84aa1021698", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/selectors/nwsapi.js": "985d7d8fc1eabbb88946b47a1c44c1b2d4aa79ff23c21424219f1528fa27a2ff", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/selectors/selectors.ts": "83eab57be2290fb48e3130533448c93c6c61239f2a2f3b85f1917f80ca0fdc75", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/selectors/sizzle-types.ts": "78149e2502409989ce861ed636b813b059e16bc267bb543e7c2b26ef43e4798b", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/selectors/sizzle.js": "c3aed60c1045a106d8e546ac2f85cc82e65f62d9af2f8f515210b9212286682a", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/utils-types.ts": "96db30e3e4a75b194201bb9fa30988215da7f91b380fca6a5143e51ece2a8436", + "https://deno.land/x/deno_dom@v0.1.46/src/dom/utils.ts": "4c6206516fb8f61f37a209c829e812c4f5a183e46d082934dd14c91bde939263", + "https://deno.land/x/deno_dom@v0.1.46/src/parser.ts": "e06b2300d693e6ae7564e53dfa5c9a9e97fdb8c044c39c52c8b93b5d60860be3", "https://deno.land/x/imagescript@1.2.15/ImageScript.js": "9ce10c37a0e4fe43699689088c24f386fd401874057932bd57079ac729205038", "https://deno.land/x/imagescript@1.2.15/png/src/crc.mjs": "5cf50de181d61dd00e66a240d811018ba5070afa8bba302f393604404604de84", "https://deno.land/x/imagescript@1.2.15/png/src/mem.mjs": "4968d400dae069b4bf0ef4767c1802fd2cc7d15d90eda4cfadf5b4cd19b96c6d", diff --git a/serverside/name-color.ts b/serverside/name-color.ts index f806fee..10b6283 100644 --- a/serverside/name-color.ts +++ b/serverside/name-color.ts @@ -1,6 +1,6 @@ import { DOMParser, -} from "https://deno.land/x/deno_dom@v0.1.38/deno-dom-wasm.ts"; +} from "https://deno.land/x/deno_dom@v0.1.46/deno-dom-wasm.ts"; import * as css from "https://deno.land/x/css@0.3.0/mod.ts"; // so, so brittle @@ -27,6 +27,22 @@ const colors: Record< > = {}; const dom_parser = new DOMParser(); +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) + .replaceAll(/[;{}"']/g, ""); + if (is_valid_color(color)) { + return color; + } else { + console.log(`invalid color!!!! ${color}`); + } + } +} + async function get_name_color(name: string): Promise { console.log(`fetching ${name}...`); const resp = await fetch(`https://cohost.org/${name}`); @@ -40,20 +56,20 @@ async function get_name_color(name: string): Promise { console.log(`parsing https://cohost.org/${name} to the dom failed!`); return; } - for (const p of dom.querySelectorAll("p")) { - const text = p.textContent; - if (text.startsWith("name-color: ")) { - const line = text.split("\n", 1)[0]; - const color = line.split("name-color: ")[1] - .slice(0, MAX_COLOR_LEN) - .replaceAll(/[;{}"']/g, ""); - if (is_valid_color(color)) { - return color; - } else { - console.log(`invalid color!!!! ${color}`); - } + + 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; } } + + for (const p of dom.querySelectorAll("p")) { + const color = parse_color(p.textContent); + if (color) return color; + } } async function update_name_color(name: string) { @@ -95,8 +111,8 @@ function cssify({ requester }: { requester?: string }): string { Deno.serve({ port: 61266 }, async (req) => { const url = new URL(req.url); const names = url.searchParams.getAll("name").filter((name) => - !name.match(/\/\?/) - ).map(name => name.toLowerCase()); + !name.match(/[\/\\?"';{}]/) + ).map((name) => name.toLowerCase()); await Promise.all(names.map(update_name_color)); const css = cssify({ requester: url.searchParams.get("requester") ?? undefined,