summaryrefslogtreecommitdiff
path: root/static/main.js
blob: 002a4fe1ecf91b97c2374e93323a2dea38ef3a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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");

  // Trigger like button animation
  d.querySelector("#like-post").addEventListener("click", function() {
    setTimeout(() => this[l].remove("like-anim"), 700);
    this[l].add("like-anim");
    this[l].add("liked");
  });

  /*
  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(""));
  //*/
});