npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

jg-clipboard

v0.3.2

Published

Copy and read text, HTML, and images on the browser clipboard, with support detection and coded errors that say why the browser refused.

Readme

jg-clipboard

Copy and read text, HTML, and images on the browser clipboard, with support detection and coded errors that say why the browser refused.

Install

npm install jg-clipboard

Usage

Copy from a click or key press handler. Each copy calls the clipboard before its first await, so the browser still sees the gesture:

import { copyText, ERRORS } from 'jg-clipboard';
import { hasErrorCode } from 'jg-exception';

copyButton.addEventListener('click', async () => {
  try {
    await copyText('npm install jg-clipboard');
    showToast('Copied');
  } catch (error) {
    if (hasErrorCode(error, ERRORS.CLIPBOARD_PERMISSION_DENIED)) {
      showToast('Allow clipboard access to copy');
    }
  }
});

Content that loads after the click can be a promise. It goes through a ClipboardItem that waits for it, which Safari needs:

shareButton.addEventListener('click', () => {
  void copyText(fetch('/share-link').then((response) => response.text()));
});

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:

import { copyHtml, copyImage } from 'jg-clipboard';

await copyHtml('<strong>Order #42</strong> shipped', { text: 'Order #42 shipped' });
await copyImage(fetch('/chart.jpg').then((response) => response.blob()));

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:

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:

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, or content that still loads was copied outside one | | 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.
  • Content that still loads, such as a promise or an image to redraw as PNG, needs the click or key press. Outside one, the copy is refused at once with CLIPBOARD_NO_USER_GESTURE, where the browser reports navigator.userActivation, because Chromium would wait for such a write forever and other browsers refuse it.

Documentation

The full API reference ships in the package as API.md, generated from its TSDoc for the installed version, and is at https://jg-packages.jesusgraterol.dev/packages/jg-clipboard/.

Recipes for common jobs ship as RECIPES.md, with every block typechecked against the release, and are at https://jg-packages.jesusgraterol.dev/packages/jg-clipboard/recipes/.

License

MIT