{"record":{"id":"1c7eed3617e4e6f4","repo":"semaphoreui/semaphore","slug":"fallback-copy-failed","errorCode":null,"errorMessage":"Fallback copy failed","messagePattern":"Fallback copy failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"web/src/lib/copyToClipboard.js","lineNumber":25,"sourceCode":"    el.setAttribute('readonly', '');\n    el.style.position = 'absolute';\n    el.style.left = '-9999px';\n    document.body.appendChild(el);\n    const selected = document.getSelection().rangeCount > 0\n      ? document.getSelection().getRangeAt(0) : false;\n    el.select();\n    document.execCommand('copy');\n    document.body.removeChild(el);\n    if (selected) {\n      document.getSelection().removeAllRanges();\n      document.getSelection().addRange(selected);\n    }\n\n    const successful = document.execCommand('copy');\n    // document.body.removeChild(textArea);\n\n    if (!successful) {\n      throw new Error('Fallback copy failed');\n    }\n\n    EventBus.$emit('i-snackbar', {\n      color: 'success',\n      text: message,\n    });\n  } catch (e) {\n    EventBus.$emit('i-snackbar', {\n      color: 'error',\n      text: `Can't copy to clipboard: ${e.message}`,\n    });\n  }\n}\n","sourceCodeStart":7,"sourceCodeEnd":39,"githubUrl":"https://github.com/semaphoreui/semaphore/blob/1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa/web/src/lib/copyToClipboard.js#L7-L39","documentation":"copyToClipboard first tries the async Clipboard API and falls back to a hidden-textarea + document.execCommand('copy') trick for older browsers. When execCommand returns false (the browser refused or could not perform the copy), the function throws 'Fallback copy failed' instead of emitting the success snackbar. It signals that no clipboard mechanism succeeded in the current browser context.","triggerScenarios":"Calling copyToClipboard in a browser where the Clipboard API is unavailable (non-secure context, older browser) AND document.execCommand('copy') returns false — e.g. the call is not triggered by a user gesture, the page lacks focus, or an iframe lacks the clipboard permission.","commonSituations":"Programmatic/automatic copy attempts without a click; page copied from inside a sandboxed iframe without allow-same-origin/clipboard permissions; Safari or older mobile browsers; running the page over plain HTTP so navigator.clipboard is undefined; browser window blurred when the copy runs.","solutions":["Ensure copyToClipboard is invoked synchronously from a real user event (click handler) so execCommand is allowed.","Serve the app over HTTPS (or localhost) so the modern navigator.clipboard API is available and the fallback is rarely needed.","Check document.hasFocus() before copying and defer the call (e.g. after focusing the window or awaiting user interaction).","Catch the error and show an instructive snackbar telling the user to copy manually (select + Ctrl/Cmd-C).","Avoid calling it from inside restricted iframes, or add clipboard permissions to the iframe sandbox."],"exampleFix":"// before\nthis.copyResult = result; // copy happens outside a user gesture\n\n// after\nbutton.addEventListener('click', async () => {\n  try {\n    await copyToClipboard(result, 'Copied!');\n  } catch (e) {\n    EventBus.$emit('i-snackbar', { color: 'error', text: 'Copy failed — please copy manually' });\n  }\n});","handlingStrategy":"try-catch","validationCode":"if (!document.hasFocus() || (!navigator.clipboard && !document.queryCommandSupported('copy'))) {\n  showManualCopyHint();\n  return;\n}","typeGuard":"function canCopyToClipboard() {\n  return typeof navigator !== 'undefined' &&\n    (navigator.clipboard != null || document.queryCommandSupported?.('copy') === true);\n}","tryCatchPattern":"try {\n  await copyToClipboard(text, 'Copied!');\n} catch (e) {\n  if (e.message === 'Fallback copy failed') {\n    EventBus.$emit('i-snackbar', { color: 'error', text: 'Copy blocked — please copy manually' });\n    selectTextForManualCopy(text);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Only trigger clipboard writes from direct user gestures (click/keydown handlers).","Serve the app on HTTPS so navigator.clipboard is available.","Guard with document.hasFocus() and clipboard feature detection before copying.","Always wrap clipboard calls in try/catch and offer a manual copy UI as fallback.","Avoid embedded iframe contexts without clipboard permissions."],"tags":["clipboard","browser","javascript","fallback"],"backgroundTag":"clipboard-copy-failed","analyzedSha":"1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa","analyzedAt":"2026-09-07T11:00:33.293Z","contentChangedAt":"2026-09-07T11:00:33.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}