diff options
author | Luca Matei Pintilie <luca@lucamatei.com> | 2023-10-19 20:27:21 +0000 |
---|---|---|
committer | Luca Matei Pintilie <luca@lucamatei.com> | 2023-10-19 20:27:21 +0000 |
commit | 4df2b9df655ac792058611e3d8a27ffdfdc1c846 (patch) | |
tree | 5959e65a6c493a0286c33b73423b533e586b72f5 | |
parent | eeee4128e71b17a59efc93b76d264b7a25cead44 (diff) | |
download | dotfiles-4df2b9df655ac792058611e3d8a27ffdfdc1c846.tar dotfiles-4df2b9df655ac792058611e3d8a27ffdfdc1c846.tar.gz dotfiles-4df2b9df655ac792058611e3d8a27ffdfdc1c846.tar.bz2 dotfiles-4df2b9df655ac792058611e3d8a27ffdfdc1c846.tar.lz dotfiles-4df2b9df655ac792058611e3d8a27ffdfdc1c846.tar.xz dotfiles-4df2b9df655ac792058611e3d8a27ffdfdc1c846.tar.zst dotfiles-4df2b9df655ac792058611e3d8a27ffdfdc1c846.zip |
Add sanitization and xdg-open helper script
-rw-r--r-- | .config/mutt/mailcap | 2 | ||||
-rwxr-xr-x | .local/bin/sanitize | 59 | ||||
-rwxr-xr-x | .local/bin/xdg-open | 23 | ||||
-rwxr-xr-x | .profile | 10 |
4 files changed, 92 insertions, 2 deletions
diff --git a/.config/mutt/mailcap b/.config/mutt/mailcap index 6e5fe57..33eefcf 100644 --- a/.config/mutt/mailcap +++ b/.config/mutt/mailcap @@ -1,3 +1,3 @@ text/html; $BROWSER %s -text/html; w3m -I %{charset} -T text/html -o display_link_number=1 -dump; copiousoutput; +text/html; sanitize | w3m -I %{charset} -T text/html -o display_link_number=1 -dump; copiousoutput; 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 + @@ -69,6 +69,14 @@ then export XDG_SESSION_TYPE fi -PATH="$PATH:$PNPM_HOME:$GOPATH/bin:$DENO_INSTALL/bin:$BUN_INSTALL/bin:$HOME/.cargo/bin:$HOME/.local/bin:$HOME/.luarocks/bin:$HOME/.dotnet/tools" +PATH="$PNPM_HOME:\ +$GOPATH/bin:\ +$DENO_INSTALL/bin:\ +$BUN_INSTALL/bin:\ +$HOME/.cargo/bin:\ +$HOME/.local/bin:\ +$HOME/.luarocks/bin:\ +$HOME/.dotnet/tools:\ +$PATH" export PATH |