site.js
897 bytes
1async function copyText(text) {
2 if (navigator.clipboard) {
3 await navigator.clipboard.writeText(text);
4 return;
5 }
6
7 const input = document.createElement("textarea");
8 input.value = text;
9 input.style.position = "fixed";
10 input.style.opacity = "0";
11 document.body.append(input);
12 input.select();
13 const copied = document.execCommand("copy");
14 input.remove();
15 if (!copied) {
16 throw new Error("clipboard copy failed");
17 }
18}
19
20for (const button of document.querySelectorAll("[data-copy]")) {
21 button.addEventListener("click", async () => {
22 const status = document.getElementById(button.getAttribute("aria-describedby"));
23 const label = button.dataset.copyLabel || "URL";
24 try {
25 await copyText(button.dataset.copy);
26 status.textContent = `${label} copied`;
27 } catch {
28 status.textContent = `Could not copy the ${label.toLowerCase()}`;
29 }
30 });
31}