summaryrefslogtreecommitdiffhomepage
path: root/web/assets/js/copy.js
blob: b846048f535fac4a72d2e0738280d63ac9af34f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copy buttons on code blocks. The site works without this file.
document.querySelectorAll("article pre").forEach((pre) => {
  const button = document.createElement("button");
  button.className = "copy";
  button.type = "button";
  button.textContent = "copy";
  button.addEventListener("click", () => {
    const text = pre.querySelector("code")?.textContent ?? pre.textContent;
    navigator.clipboard.writeText(text).then(
      () => { button.textContent = "copied"; },
      () => { button.textContent = "failed"; },
    );
    setTimeout(() => { button.textContent = "copy"; }, 1500);
  });
  pre.append(button);
});