(() => {
  document.querySelectorAll("[data-copy-button]").forEach((button) => {
    button.addEventListener("click", async () => {
      const id = button.dataset.copyButton;
      const input = document.querySelector(`[data-copy-source="${id}"]`);
      const status = document.querySelector(`[data-copy-status="${id}"]`);
      if (!input) return;
      try {
        await navigator.clipboard.writeText(input.value);
        if (status) status.textContent = "Copied.";
      } catch {
        input.focus();
        input.select();
        const copied = document.execCommand("copy");
        if (status) status.textContent = copied ? "Copied." : "Select and copy the URL manually.";
      }
    });
  });

})();
