summaryrefslogtreecommitdiff
path: root/static/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/main.js')
-rw-r--r--static/main.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/static/main.js b/static/main.js
new file mode 100644
index 0000000..5e7c18c
--- /dev/null
+++ b/static/main.js
@@ -0,0 +1,41 @@
+document.addEventListener("DOMContentLoaded", () => {
+ const d = document, m = Math,
+ h = d.documentElement,
+ c = d.cookie.split("; "),
+ t = c.find(r => r.startsWith("theme=")),
+ o = "path=/; samesite=strict; expires",
+ q = "querySelectorAll",
+ l = "classList";
+
+ h[l][(t && t.split("=")[1] == "invert")?"add":"remove"]("invert");
+ d.querySelector("#invert").addEventListener("click", () =>
+ d.cookie = h[l].toggle("invert")?
+ `theme=invert; ${o}=Fri, 31 Dec 9999 23:59:59 GMT`:
+ `theme=; ${o}=Thu, 01 Jan 1970 00:00:00 GMT`);
+
+ // Add line numbers to code blocks
+ [...d[q]("pre.hljs code")].forEach(e => e.innerHTML =
+ e.innerHTML.split("\n").map((l,i,a) => l &&
+ `<div class="code-line" data-line-number="${
+ (i+1).toString().padStart(Math.ceil(Math.log10(a.length)), 0)
+ }">${l}</div>`).join("\n"));
+
+ // Disable Android Chrome "tap to search" when revealing spoiler text
+ [...d[q]("mark")].forEach(e => e.tabIndex = "-1");
+
+ const ZLM_UNICODE_START = 0xED80;
+ const LERFU_INDEX = "ptkflscmx.' 1234bdgvrzjn`-,~ aeiouy qw AEIOUY";
+ const lerfuLookup = c => String.fromCodePoint(ZLM_UNICODE_START + LERFU_INDEX.indexOf(c));
+
+ function latinToZlm(c) {
+ if (c.codePointAt(0) == 0x2019) c = "'";
+ if (c.codePointAt(0) >= 0xED80) return c;
+ if (" \n\t".includes(c)) return c;
+ if (c == "h" || c == "H") c = "'";
+ if (LERFU_INDEX.indexOf(c) >= 0) return lerfuLookup(c);
+ if (LERFU_INDEX.indexOf(c.toLowerCase()) >= 0) return lerfuLookup(c.toLowerCase());
+ return c;
+ }
+
+ [...d[q]("[lang=jbo]")].forEach(e => e.innerText = e.innerText.split("").map(latinToZlm).join(""));
+});