{"record":{"id":"b3c88d6c3e3c8311","repo":"Molunerfinn/PicGo","slug":"clipboard-api-is-unavailable","errorCode":null,"errorMessage":"Clipboard API is unavailable.","messagePattern":"Clipboard API is unavailable\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/renderer/components/independent-window/utils.ts","lineNumber":15,"sourceCode":"export function resolveIndependentWindowErrorMessage(\n  error: unknown,\n  fallback: string\n) {\n  if (error instanceof Error) {\n    const message = error.message.trim()\n    return message.length > 0 ? message : fallback\n  }\n\n  return fallback\n}\n\nexport async function copyToClipboard(value: string) {\n  if (!navigator.clipboard?.writeText) {\n    throw new Error(\"Clipboard API is unavailable.\")\n  }\n\n  await navigator.clipboard.writeText(value)\n}\n","sourceCodeStart":1,"sourceCodeEnd":20,"githubUrl":"https://github.com/Molunerfinn/PicGo/blob/07ec7068a5512344da093d49a8408fa63ba3421e/src/renderer/components/independent-window/utils.ts#L1-L20","documentation":"copyToClipboard explicitly throws when the browser Clipboard API (navigator.clipboard.writeText) is missing. This API only exists in secure contexts (HTTPS or localhost) and requires window focus and permission; in Electron it can also be missing in offscreen or unloaded-context windows. The function checks availability before writing and throws this Error otherwise.","triggerScenarios":"Calling copyToClipboard in a window/context where navigator.clipboard is undefined or writeText is absent: insecure origin, iframe without clipboard permission (Permissions-Policy clipboard-write), document not focused, or older/odd webview lacking the API.","commonSituations":"Independent (mini) windows loading content via file:// or http:// instead of the app origin; copying immediately after window open before the document gains focus; embedded webviews with restrictive permissions; browsers (Safari) requiring user-gesture-initiated clipboard writes.","solutions":["Use Electron's clipboard.writeText via IPC (the project's COPY_TEXT RPC / clipboard API) which does not depend on the web Clipboard API","Ensure the window is loaded over a secure context and is focused before calling; call copyToClipboard from a user-gesture handler","Avoid rendering inside cross-origin iframes without clipboard-write permission; set Permissions-Policy to allow clipboard-write","Fall back to a legacy document.execCommand('copy') path when navigator.clipboard is absent"],"exampleFix":"// before\nawait copyToClipboard(value)\n// after\ntry {\n  await copyToClipboard(value)\n} catch {\n  // fallback: main-process clipboard via IPC\n  sendToMain(IRPCActionType.COPY_TEXT, value)\n}","handlingStrategy":"fallback","validationCode":"if (typeof navigator.clipboard?.writeText !== 'function') {\n  // route through Electron main-process clipboard instead\n  sendToMain(IRPCActionType.COPY_TEXT, value)\n  return\n}","typeGuard":"function clipboardAvailable(n: Navigator): n is Navigator & { clipboard: Clipboard } {\n  return typeof n.clipboard?.writeText === 'function'\n}","tryCatchPattern":"try {\n  await copyToClipboard(value)\n} catch (e) {\n  if ((e as Error).message === 'Clipboard API is unavailable.') {\n    sendToMain(IRPCActionType.COPY_TEXT, value)\n  }\n}","preventionTips":["Call clipboard writes from user-gesture handlers and with the document focused","Prefer the Electron IPC clipboard path in independent windows instead of navigator.clipboard","Keep windows on secure contexts (app:// or localhost); avoid file:// for UI windows","Test clipboard behavior in every window type (settings, mini, gallery) you ship"],"tags":["clipboard","browser-api","permissions","secure-context"],"backgroundTag":"clipboard-api-unavailable","analyzedSha":"07ec7068a5512344da093d49a8408fa63ba3421e","analyzedAt":"2026-08-30T01:45:22.289Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}