diff options
Diffstat (limited to '.local')
-rwxr-xr-x | .local/bin/sanitize | 59 |
1 files changed, 0 insertions, 59 deletions
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)); -} |