{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Foundation/resources/exceptions/renderer/scripts.js#L18-L54","documentation":"Thrown by the exception renderer's client-side clipboard helper when document.execCommand('copy') returns a falsy result. The library uses a legacy textarea technique as a fallback for copying stack traces / code snippets. It indicates the browser refused or could not perform the copy operation.","triggerScenarios":"Calling the copy-to-clipboard action from the ignition-style error page when execCommand('copy') fails — e.g. the page is served over an insecure (non-HTTPS, non-localhost) origin, the document lost focus, the browser disabled execCommand, or a Permissions Policy blocks clipboard-write.","commonSituations":"Opening the rendered exception page over plain HTTP on a remote host; the tab being backgrounded at click time; running a browser that has removed execCommand; iframe embedding without allow=\"clipboard-write\".","solutions":["Serve the error page over HTTPS or access it via localhost/127.0.0.1 so the Clipboard API is permitted.","Ensure the document has focus when the copy button is clicked (do not background the tab).","If embedded in an iframe, add allow=\"clipboard-write\" to the iframe element.","Upgrade the browser to one that supports navigator.clipboard.writeText and confirm the renderer's window.copy path.","As a manual fallback, select the text in the rendered stack trace and use Ctrl/Cmd+C."],"exampleFix":"// before — relies on execCommand, fails on insecure origins\nconst result = document.execCommand('copy');\nif (!result) throw new Error('Failed to copy text to clipboard');\n\n// after — prefer async Clipboard API, fall back to execCommand\nasync function copyText(text) {\n  if (navigator.clipboard && window.isSecureContext) {\n    await navigator.clipboard.writeText(text);\n    return;\n  }\n  const ta = document.createElement('textarea');\n  ta.value = text;\n  ta.style.position = 'fixed';\n  ta.style.opacity = '0';\n  document.body.appendChild(ta);\n  ta.select();\n  const ok = document.execCommand('copy');\n  document.body.removeChild(ta);\n  if (!ok) throw new Error('Failed to copy text to clipboard');\n}","handlingStrategy":"try-catch","validationCode":"// guard before invoking the copy helper\nfunction canCopyViaExec() {\n  return typeof document !== 'undefined' && typeof document.execCommand === 'function' && document.hasFocus();\n}\nfunction canCopyViaClipboardApi() {\n  return typeof navigator !== 'undefined' && !!navigator.clipboard && window.isSecureContext;\n}","typeGuard":"function isClipboardAvailable(): boolean {\n  return (typeof navigator !== 'undefined' && !!navigator.clipboard && window.isSecureContext)\n      || (typeof document !== 'undefined' && typeof document.execCommand === 'function');\n}","tryCatchPattern":"try {\n  await copyText(traceText);\n} catch (e) {\n  // graceful degradation: surface the text for manual selection/copy\n  prompt('Copy with Ctrl/Cmd+C:', traceText);\n}","preventionTips":["Serve the error page over HTTPS or use localhost.","Prefer navigator.clipboard.writeText in secure contexts.","Add allow=\"clipboard-write\" if the page is iframe-embedded."],"tags":["clipboard","browser","frontend","security-context"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}