// Copy text to the clipboard, working in BOTH secure and non-secure origins. // // `navigator.clipboard` only exists in a secure context (HTTPS, or localhost). // A self-hosted console is commonly served over plain HTTP on a LAN host/IP — // a non-secure origin — where `navigator.clipboard` is `undefined`, so a bare // `navigator.clipboard.writeText(...)` throws and the copy silently does // nothing. Fall back to a legacy `document.execCommand("copy")` path so the // copy buttons work everywhere. // // Returns whether the copy succeeded, so callers only show their "copied" // confirmation when the value actually reached the clipboard. export async function copyToClipboard(text: string): Promise { // Preferred path: the async Clipboard API (secure contexts). if (typeof navigator !== "undefined" && navigator.clipboard?.writeText) { // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: the Clipboard API rejects (permission denied, blur) and we recover via the legacy path try { await navigator.clipboard.writeText(text); return true; } catch { // Permission denied / document not focused — fall through to the legacy // path rather than failing the copy. } } return legacyCopy(text); } // Legacy fallback for non-secure origins (plain-HTTP self-host). Select the text // via a document Range on a hidden node, then `execCommand("copy")`. // // Crucially this selects a Range WITHOUT moving focus. A throwaway