Skip to main content

clipboard

Read from and write to the system clipboard via the async Clipboard API.

Import: @rtorcato/browser-common/clipboard

๐Ÿ“– MDN: Clipboard API ยท ๐Ÿ“Š caniuse: async clipboard

Requires a secure context (HTTPS, or localhost in dev). Writing is generally allowed from a user gesture; reading may prompt for permission.

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
Clipboard.writeTextโœ… 66+ [1,2]โœ… 79+ [1,2]โœ… 63+ [3,4]โœ… 13.1+ [3]โœ… 13.4+ [3]Widely available
Clipboard.readTextโœ… 66+ [5]โœ… 79+ [5]โœ… 125+ [3,6,7]โœ… 13.1+โœ… 13.4+Newly available
  1. Chrome, Edge: From version 107, this method must be called within user gesture event handlers, or the user must grant the clipboard-write permission.
  2. Chrome, Edge: Before version 107, the user must grant the clipboard-write permission.
  3. Firefox, Safari, iOS Safari: This method must be called within user gesture event handlers.
  4. Firefox: Web extensions with the clipboardWrite permission in their manifest can write data without a user gesture.
  5. Chrome, Edge: The user must grant the clipboard-read permission.
  6. Firefox: A paste prompt is displayed when the clipboard is read. If the clipboard contains same-origin content, the prompt is suppressed.
  7. Firefox: Web extensions with the clipboardRead permission in their manifest can read data without the paste prompt displaying. Prior to Firefox 147, extensions without this permission were unable to read clipboard data.

Needs: a user gesture, a permission grant

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Clipboard.writeText ยท Clipboard.readText

Exampleโ€‹

import {
isClipboardApiAvailable,
copyToClipboard,
readFromClipboard,
} from '@rtorcato/browser-common/clipboard'

if (isClipboardApiAvailable()) {
await copyToClipboard('Copied from the app')
const text = await readFromClipboard()
console.log(text)
}

Exportsโ€‹

  • isClipboardApiAvailable() โ€” feature check
  • copyToClipboard(text) โ€” write a string to the clipboard
  • copyElementTextToClipboard(element) โ€” copy an element's text content
  • readFromClipboard() โ€” read the clipboard's text

See the API reference for full signatures.