summaryrefslogtreecommitdiff
path: root/script.js
blob: 9afcb2c4fff52eb9957e276f0fd1abae7cfb6d97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
window.onload = () => {
  document.querySelectorAll(".copy").forEach((element) => {
    const tooltip = document.createElement("span");
    tooltip.textContent = " [copy]";
    tooltip.style.fontSize = "0.8em";
    tooltip.style.opacity = "0.7";
    element.appendChild(tooltip);

    element.addEventListener("click", () => {
      try {
        navigator.clipboard.writeText(element.attributes["copy-text"]?.value);
        tooltip.textContent = " [copied]";
      } catch (error) {
        tooltip.textContent = " [error]";
      }
      setTimeout(() => (tooltip.textContent = " [copy]"), 1000);
    });
  });
};