/** * Copy `text` to the system clipboard. * * The modern `navigator.clipboard.writeText()` only works in secure * contexts (HTTPS / localhost / file://). This deployment runs over * plain HTTP on the internal network, where that API is gated, so we * fall back to the legacy `textarea + execCommand("copy")` pattern. * `execCommand` is deprecated but every shipping browser still * implements it for the copy command, and it works in non-secure * contexts where the modern API doesn't. * * Returns true on success, false if both paths fail. */ export async function copyToClipboard(text: string): Promise { if (typeof navigator !== "undefined" && navigator.clipboard?.writeText) { try { await navigator.clipboard.writeText(text); return true; } catch { // Fall through to legacy path — secure-context block, permission // denial, or any other modern-API failure. } } if (typeof document === "undefined") return false; const ta = document.createElement("textarea"); ta.value = text; ta.setAttribute("readonly", ""); // Position offscreen but still focusable; some browsers refuse to // copy from elements that are display:none or visibility:hidden. ta.style.position = "fixed"; ta.style.top = "0"; ta.style.left = "0"; ta.style.opacity = "0"; ta.style.pointerEvents = "none"; document.body.appendChild(ta); try { ta.focus(); ta.select(); return document.execCommand("copy"); } catch { return false; } finally { document.body.removeChild(ta); } }