From 759f0406608464cb7514914de9119d4eaff2eaac Mon Sep 17 00:00:00 2001 From: Luca Matei Pintilie Date: Sat, 25 Nov 2023 11:03:55 +0100 Subject: Remove sanitize in favor of https://hg.sr.ht/~aninternettroll/sanitize --- .local/bin/sanitize | 59 ----------------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100755 .local/bin/sanitize (limited to '.local') diff --git a/.local/bin/sanitize b/.local/bin/sanitize deleted file mode 100755 index dbde059..0000000 --- a/.local/bin/sanitize +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env -S deno run --allow-read --ext ts -// vi: ft=typescript -import { - DOMParser, - Element, -} from "https://deno.land/x/deno_dom@v0.1.41-alpha-artifacts/deno-dom-wasm.ts"; - -function sanitizeLink(link: string): string { - try { - let url = new URL(link); - url.searchParams.delete("tracking_data"); - url.searchParams.delete("utm_source"); - url.searchParams.delete("utm_medium"); - url.searchParams.delete("utm_campaign"); - if ( - url.host.includes("safelinks.protection.outlook.com") && - url.searchParams.has("url") - ) { - url = new URL(url.searchParams.get("url")!); - } - return url.href; - } catch (err) { - console.error(err); - return link; - } -} - -function sanitizeEl(el: Element) { - const href = el.getAttribute("href"); - if (href) { - el.setAttribute("href", sanitizeLink(href)); - } - const src = el.getAttribute("src"); - if (src) { - el.setAttribute("src", sanitizeLink(src)); - } -} - -function sanitize(html: string): string { - const document = new DOMParser().parseFromString(html, "text/html"); - - const aTags = document?.querySelectorAll("a"); - aTags?.forEach((a) => sanitizeEl(a as Element)); - const imgTags = document?.querySelectorAll("a"); - imgTags?.forEach((img) => sanitizeEl(img as Element)); - - if (document?.documentElement?.outerHTML) { - return document.documentElement.outerHTML; - } else throw new Error("No document available"); -} - -if (import.meta.main) { - let file: string; - const [path] = Deno.args; - if (!path || path === "-") { - file = await new Response(Deno.stdin.readable).text(); - } else file = await Deno.readTextFile(path); - console.log(sanitize(file)); -} -- cgit v1.2.3