export async function copyTextToClipboard(text: string): Promise { if ( typeof navigator !== "undefined" && typeof window !== "undefined" && window.isSecureContext !== false && navigator.clipboard?.writeText ) { try { await navigator.clipboard.writeText(text); return true; } catch { // Fall through to execCommand fallback. } } if (typeof document === "undefined" || !document.body) { return false; } const textarea = document.createElement("textarea"); textarea.value = text; textarea.setAttribute("readonly", ""); textarea.style.position = "fixed"; textarea.style.left = "-9999px"; textarea.style.top = "0"; textarea.style.opacity = "0"; textarea.style.pointerEvents = "none"; document.body.appendChild(textarea); try { textarea.focus(); textarea.select(); textarea.setSelectionRange(0, text.length); return document.execCommand("copy"); } catch { return false; } finally { textarea.remove(); } }