MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / copyToClipboard

Function copyToClipboard

packages/react/src/lib/clipboard.ts:12–26  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

10// Returns whether the copy succeeded, so callers only show their "copied"
11// confirmation when the value actually reached the clipboard.
12export async function copyToClipboard(text: string): Promise<boolean> {
13 // Preferred path: the async Clipboard API (secure contexts).
14 if (typeof navigator !== "undefined" && navigator.clipboard?.writeText) {
15 // 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
16 try {
17 await navigator.clipboard.writeText(text);
18 return true;
19 } catch {
20 // Permission denied / document not focused — fall through to the legacy
21 // path rather than failing the copy.
22 }
23 }
24
25 return legacyCopy(text);
26}
27
28// Legacy fallback for non-secure origins (plain-HTTP self-host). Select the text
29// via a document Range on a hidden node, then `execCommand("copy")`.

Callers 4

ExpandableCodeBlockFunction · 0.90
handleCopyFunction · 0.90
CodeBlockFunction · 0.90
NpmUpdateCardFunction · 0.90

Calls 1

legacyCopyFunction · 0.85

Tested by

no test coverage detected