Copy HTML with its plain text, and images. A PNG is copied as it is, and other types the browser can decode, such as JPEG or WebP, are redrawn as PNG, the only image type browsers accept on the clipboard:
Read from a paste button. readClipboard reads the text, HTML, and image with one call, so the person pasting sees one prompt at most. An image the browser can’t read comes back as null when text or HTML was copied with it, so the text still pastes:
ts
import { readClipboard, readText } from 'jg-clipboard';pasteButton.addEventListener('click', async () => { const { text, html, image } = await readClipboard(); // each null when the clipboard lacks it input.value = text ?? (await readText());});
Show only what the browser supports. getClipboardSupport never throws, and reports everything as false during server rendering:
ts
import { getClipboardSupport } from 'jg-clipboard';const support = getClipboardSupport(); // { isSecureContext, copyText, copyHtml, copyImage, readText, read }copyImageButton.hidden = !support.copyImage;
Errors
Every failure is an Exception with one of these codes, and the browser’s error as its cause:
Code
When
CLIPBOARD_UNSUPPORTED
The page isn’t a secure context, such as HTTPS or localhost, or the browser lacks the API
CLIPBOARD_NOT_FOCUSED
The browser refused because the page doesn’t have focus
CLIPBOARD_NO_USER_GESTURE
The browser refused because the call didn’t follow a click or a key press
CLIPBOARD_PERMISSION_DENIED
The browser refused for any other reason, such as a denied or dismissed permission; data.permission holds the Permissions API’s state, or null
CLIPBOARD_INVALID_IMAGE
The image can’t be decoded as PNG: an image to copy, or one readClipboard read without text or HTML beside it
CLIPBOARD_WRITE_FAILED
A copy failed otherwise, such as content whose promise rejected
CLIPBOARD_READ_FAILED
A read failed otherwise
Browser limits
The clipboard needs a secure context: HTTPS, or localhost while developing.
Copy and read from a click or key press handler. Safari refuses calls that don’t follow one directly, and Chromium needs the page to have focus.
Reading asks first. Safari and Firefox show a paste prompt each time, and Chromium asks once for the clipboard-read permission.
Images go on the clipboard as PNG only. Converting another type needs createImageBitmap, so it works in every current browser, but not in very old ones.
Without ClipboardItem, a promised text loads before the copy starts, and Safari may then refuse it.
Generated from TSDoc
API reference
Every export of [email protected], 10 in all, with its signature, its errors, and an example.
Copies HTML to the clipboard, with plain text for places that can't paste HTML: options.text, or else the HTML's text content. Call it from a click or key press handler; like copyText, it calls the clipboard before its first await, and either value can be a promise.
Copies an image to the clipboard as PNG, the only image type browsers accept there. A PNG is written as it is, and other types the browser can decode, such as JPEG or WebP, are redrawn as PNG. Call it from a click or key press handler; it calls the clipboard before its first await, and the image can be a promise, such as a fetched Blob.
Copies text to the clipboard. Call it from a click or key press handler. It calls the clipboard before its first await, so the browser still sees that gesture. Text that loads after the click, such as from a request, can be a promise: it goes through a ClipboardItem that waits for it, which Safari needs. Browsers without ClipboardItem wait for the promise first, and may then refuse the copy.
import { copyText } from 'jg-clipboard';// the text loads after the clickbutton.addEventListener('click', () => { void copyText(fetch('/share-link').then((response) => response.text()));});
Tells what the browser lets the page do with the clipboard, from feature detection and ClipboardItem.supports where the browser has it. It never throws, and it's safe to call during server rendering, where everything is false. A supported feature can still be refused, such as without a click or a permission.
Signature
getClipboardSupport(): IClipboardSupport
Returns
IClipboardSupport
Whether the page is a secure context, and whether each function can run.
Example
import { getClipboardSupport } from 'jg-clipboard';const support = getClipboardSupport();if (support.copyImage) { showCopyImageButton();}
Reads the clipboard's text, HTML, and image with one read() call, so the person pasting sees one prompt at most. Call it from a click or key press handler, which Safari requires. An image the browser can't read never hides the text or HTML copied with it, such as the picture of spreadsheet cells that comes with their text: the image is then null.
Signature
readClipboard(): Promise<IClipboardContent>
Returns
Promise<IClipboardContent>
A promise that resolves to each kind of content, with null for the kinds the clipboard doesn't hold or the image it can't read. The image is PNG.
Reads the text on the clipboard. Browsers ask before a page reads it: Safari and Firefox show a paste prompt, and Chromium asks for the clipboard-read permission once. Call it from a click or key press handler, which Safari requires.
Signature
readText(): Promise<string>
Returns
Promise<string>
A promise that resolves to the text, or '' when the clipboard holds none.
Throws
CLIPBOARD_UNSUPPORTEDThe clipboard can't read text here.
CLIPBOARD_NOT_FOCUSEDThe page must have focus to use the clipboard.
CLIPBOARD_NO_USER_GESTUREThe browser only allows this right after a click or a key press.
CLIPBOARD_PERMISSION_DENIEDThe permission to use the clipboard was denied.
CLIPBOARD_READ_FAILEDThe clipboard couldn't be read.