diff options
Diffstat (limited to '.local')
-rwxr-xr-x | .local/bin/sanitize | 59 | ||||
-rwxr-xr-x | .local/bin/xdg-open | 23 |
2 files changed, 82 insertions, 0 deletions
diff --git a/.local/bin/sanitize b/.local/bin/sanitize new file mode 100755 index 0000000..dbde059 --- /dev/null +++ b/.local/bin/sanitize @@ -0,0 +1,59 @@ +#!/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)); +} diff --git a/.local/bin/xdg-open b/.local/bin/xdg-open new file mode 100755 index 0000000..8cb094e --- /dev/null +++ b/.local/bin/xdg-open @@ -0,0 +1,23 @@ +#!/usr/bin/env sh + +URL="$1" +xdgopen="$(which --all xdg-open | tac | head --lines 1)" + +if [ "$URL" = "--help" ] +then + echo "Help here" + exit 1 +fi + +term() { + footclient $@ || alacritty -e $@ +} + +if echo "$URL" | grep -qE "https?://.*youtu" +then + mpv "$URL" +else + $xdgopen "$URL" + # term w3m "$URL" +fi + |