{"id":"6208705c99606be5","repo":"laravel/framework","slug":"failed-to-copy-text-to-clipboard","errorCode":null,"errorMessage":"Failed to copy text to clipboard","messagePattern":"Failed to copy text to clipboard","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/Illuminate/Foundation/resources/exceptions/renderer/scripts.js","lineNumber":36,"sourceCode":"});\n\nwindow.copyToClipboard = async function (text) {\n    if (navigator.clipboard) {\n        await navigator.clipboard.writeText(text);\n    } else {\n        const textarea = document.createElement('textarea');\n        textarea.value = text;\n        textarea.style.position = 'fixed';\n        textarea.style.opacity = '0';\n        textarea.style.pointerEvents = 'none';\n        document.body.appendChild(textarea);\n        textarea.select();\n\n        const result = document.execCommand('copy');\n        document.body.removeChild(textarea);\n\n        if (!result) {\n            throw new Error('Failed to copy text to clipboard');\n        }\n    }\n};\n\nconst highlighter = createHighlighterCoreSync({\n    themes: [lightPlus, darkPlus],\n    langs: [php, sql, json],\n    engine: createJavaScriptRegexEngine(),\n});\n\nwindow.highlight = function (\n    code,\n    language,\n    truncate = false,\n    editor = false,\n    startingLine = 1,\n    highlightedLine = null\n) {","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Foundation/resources/exceptions/renderer/scripts.js#L18-L54","documentation":"Thrown by the exception renderer's copy-to-clipboard helper when the legacy fallback path (document.execCommand('copy')) fails. It only triggers when navigator.clipboard is unavailable (insecure context) AND execCommand returns false, meaning the browser refused the copy. This is a frontend convenience error in Laravel's Whoops-style exception page, not application code.","triggerScenarios":"Calling window.copyToClipboard(text) on a page served over plain HTTP (not localhost), inside a sandboxed iframe without allow-popups/clipboard-write, or when the document is not focused so execCommand('copy') returns false.","commonSituations":"Running the app on http:// (non-TLS) staging where the Async Clipboard API is gated behind secure contexts; iframe-embedded previews; automated test runners where the document lacks focus.","solutions":["Serve the page over HTTPS (or via localhost) so navigator.clipboard is available and the fallback is never reached.","If embedded in an iframe, add allow=\"clipboard-write\" to the iframe element and ensure the parent grants permission.","Ensure the document is focused (window.focus()) before invoking copyToClipboard.","Wrap the call in try/catch and show a manual fallback (e.g. an already-selected textarea) when it throws."],"exampleFix":"// before\nwindow.copyToClipboard = async function (text) {\n  if (navigator.clipboard) {\n    await navigator.clipboard.writeText(text);\n  } else {\n    // execCommand fallback that may throw\n  }\n};\n\n// after\ntry {\n  await window.copyToClipboard(text);\n} catch (e) {\n  // reveal a textarea with the text pre-selected so the user can press Ctrl+C\n  showManualCopyFallback(text);\n}","handlingStrategy":"try-catch","validationCode":"function canUseClipboardApi() {\n  return typeof navigator !== 'undefined' && navigator.clipboard && window.isSecureContext;\n}","typeGuard":"function isClipboardAvailable() {\n  return typeof navigator !== 'undefined' && !!navigator.clipboard && window.isSecureContext;\n}","tryCatchPattern":"try {\n  await window.copyToClipboard(text);\n} catch (e) {\n  // graceful degradation: show a pre-selected textarea fallback\n  showManualCopyFallback(text);\n}","preventionTips":["Serve the exception page over HTTPS or localhost so navigator.clipboard is used.","Grant clipboard-write permission to any embedding iframe.","Never assume copyToClipboard succeeds; always provide a manual-copy fallback."],"tags":["clipboard","browser","frontend","javascript","exceptions-renderer"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}