/** * Copy text to clipboard with a non-secure-context fallback. * Returns true if a copy strategy likely succeeded. */ export async function copyText(text: string): Promise { if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) { try { await navigator.clipboard.writeText(text); return true; } catch { // Fall through to legacy fallback. } } if (typeof document === 'undefined') return false; try { const textarea = document.createElement('textarea'); textarea.value = text; textarea.style.position = 'fixed'; textarea.style.opacity = '0'; document.body.appendChild(textarea); textarea.select(); const copied = document.execCommand('copy'); document.body.removeChild(textarea); return copied; } catch { return false; } }