{"name":"copy-to-clipboard","version":"4.0.0","keywords":["clipboard","copy","browser"],"author":{"name":"sudodoki","email":"smd.deluzion@gmail.com"},"license":"MIT","_id":"copy-to-clipboard@4.0.0","maintainers":[{"name":"sudodoki","email":"smd.deluzion@gmail.com"}],"contributors":[{"url":"https://github.com/shvaikalesh","name":"Aleksej Shvajka"}],"homepage":"https://github.com/sudodoki/copy-to-clipboard#readme","bugs":{"url":"https://github.com/sudodoki/copy-to-clipboard/issues"},"dist":{"shasum":"5ab7da2b01e088bcfec1e7f8567d8495aebff738","tarball":"http://registry-npm.ywwl.com/copy-to-clipboard/download/copy-to-clipboard-4.0.0.tgz","fileCount":8,"integrity":"sha512-mfN771XnlPSUl0eCw4QoGcXfbK2205n52j4JuXINgUi0WLiFp/YtZ5at8tO9DZSmpHzoyx83ytca/NJqCcdzRw==","signatures":[{"sig":"MEUCIQDDU3iXlbMQCOjtB1rFibWylVzXVpC+z/Uq5OPcNr82AgIgafiZ1QINreEnPLetZ0bQjWw1sAj9TiCmjp6rLkO460U=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":32237,"size":7617,"noattachment":false,"key":"/copy-to-clipboard/-/copy-to-clipboard-4.0.0.tgz"},"main":"dist/index.cjs","type":"commonjs","types":"dist/index.d.ts","module":"dist/index.mjs","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.cjs"}}},"gitHead":"bbb5d59f2bc8eb04d957070b81df5b0a127f7429","scripts":{"test":"nightwatch --env chrome","build":"tsup && cp dist/index.global.js example/index.js","check":"publint","pretest":"npm run build","test:all":"nightwatch --env chrome,firefox,edge,safari","test:edge":"nightwatch --env edge","typecheck":"tsc --noEmit","test:chrome":"nightwatch --env chrome","test:lt:all":"nightwatch --env lambdatest_firefox,lambdatest_chrome,lambdatest_safari,lambdatest_edge","test:safari":"nightwatch --env safari","test:firefox":"nightwatch --env firefox","test:lt:edge":"nightwatch --env lambdatest_edge","test:lt:chrome":"nightwatch --env lambdatest_chrome","test:lt:safari":"nightwatch --env lambdatest_safari","test:lt:firefox":"nightwatch --env lambdatest_firefox"},"_npmUser":{"name":"sudodoki","email":"smd.deluzion@gmail.com"},"repository":{"url":"git+https://github.com/sudodoki/copy-to-clipboard.git","type":"git"},"_npmVersion":"11.11.0","description":"Copy stuff into clipboard using JS with fallbacks","directories":{"example":"example"},"sideEffects":false,"_nodeVersion":"25.8.0","_hasShrinkwrap":false,"devDependencies":{"tsup":"^8.0.0","publint":"^0.3.18","minimist":"^1.2.6","nightwatch":"^3.15.0","typescript":"^5.0.0","geckodriver":"^6.1.0","chromedriver":"^146.0.6","toggle-selection":"^2.0.0","@brettz9/node-static":"^0.1.1"},"_npmOperationalInternal":{"tmp":"tmp/copy-to-clipboard_4.0.0_1776805022907_0.009731450517104223","host":"s3://npm-registry-packages-npm-production"},"readme":"# Copy to clipboard [![CI](https://github.com/sudodoki/copy-to-clipboard/actions/workflows/github-ci.yml/badge.svg)](https://github.com/sudodoki/copy-to-clipboard/actions/workflows/github-ci.yml)\n\nSimple module exposing an async `copy` function that uses the [Async Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) (`navigator.clipboard`) in secure contexts (HTTPS / localhost), with automatic fallback to `document.execCommand('copy')` for non-secure contexts or older browsers.\n\n> If you are building using [Electron](http://electronjs.org/), use [their API](https://www.electronjs.org/docs/latest/api/clipboard).\n\n# Example\n\n```js\nimport copy from 'copy-to-clipboard';\n\nawait copy('Text');\n\n// Copy with options\nawait copy('Text', {\n  debug: true,\n  message: 'Press #{key} to copy',\n});\n\n// Copy as HTML (text/html + text/plain written simultaneously)\nawait copy('<b>Hello <i>world</i></b>', { format: 'text/html' });\n\n// Custom plain-text fallback via onCopy\nawait copy('<b>Hello <i>world</i></b>', {\n  format: 'text/html',\n  onCopy: () => new ClipboardItem({\n    'text/html': new Blob(['<b>Hello <i>world</i></b>'], { type: 'text/html' }),\n    'text/plain': new Blob(['Hello world'], { type: 'text/plain' }),\n  }),\n});\n```\n\n# API\n\n`copy(text: string, options?: object): Promise<boolean>` — copies text to clipboard. Returns `true` on success, `false` if all paths failed (no additional keystrokes were required from the user).\n\n> **v4 breaking change:** `copy()` is now async and returns `Promise<boolean>`. Use `await copy(...)` to get the result.\n\n## Options\n\n| Value | Default | Notes |\n|-------|---------|-------|\n| `options.debug` | `false` | `Boolean`. Enable output to console. |\n| `options.message` | `'Copy to clipboard: #{key}, Enter'` | `String`. Prompt message used when `fallbackToPrompt` is enabled. All occurrences of `#{key}` are replaced with `⌘+C` on macOS or `Ctrl+C` otherwise. |\n| `options.format` | — | `String`. MIME type to copy as. Use `'text/html'` to copy rich text; `'text/plain'` to strip inherited styles when pasting into rich-text editors. When set alongside `'text/html'`, both `text/html` and `text/plain` are written simultaneously via `ClipboardItem`. |\n| `options.onCopy` | — | `(clipboardData: ClipboardItem \\| DataTransfer) => ClipboardItem \\| void`. Called before the write. On the async path, receives the constructed `ClipboardItem` and may return a new one to replace it (useful for custom MIME types or a different plain-text fallback). On the `execCommand` fallback path, receives the `DataTransfer` object; return value is ignored. |\n| `options.fallbackToPrompt` | `false` | `Boolean`. If `true`, shows a `window.prompt()` as a last resort when both `navigator.clipboard` and `execCommand` fail. Off by default in v4. |\n\n## Copy path selection\n\n1. **`navigator.clipboard.writeText(text)`** — used when the page is a secure context (HTTPS / localhost), `navigator.clipboard` is available, and neither `options.format` nor `options.onCopy` is set.\n2. **`navigator.clipboard.write([ClipboardItem])`** — used in a secure context when `options.format` or `options.onCopy` is set. Builds a `ClipboardItem` with `text/plain` always present; adds the requested MIME type alongside it. `onCopy` may return a replacement `ClipboardItem`.\n3. **`execCommand('copy')` fallback** — used on non-HTTPS pages, when `navigator.clipboard` is unavailable, or when the async write throws. Uses a hidden `<span>` element. `preventDefault` is only called when `options.format` is set.\n4. **`window.prompt()` fallback** — opt-in via `options.fallbackToPrompt: true`.\n\n# Recipes\n\n## HTML copy with stripped plain-text fallback\n\nBy default, `copy(html, { format: 'text/html' })` puts the raw HTML string in the `text/plain` slot of the `ClipboardItem`. If you want apps that only accept plain text to receive readable content instead of markup, use `onCopy` to supply a stripped version:\n\n```js\nfunction stripHtml(html) {\n  const div = document.createElement('div');\n  div.innerHTML = html;\n  return div.textContent || div.innerText || '';\n}\n\nconst html = '<b>Hello <i>world</i></b>';\n\nawait copy(html, {\n  format: 'text/html',\n  onCopy: () => new ClipboardItem({\n    'text/html': new Blob([html], { type: 'text/html' }),\n    'text/plain': new Blob([stripHtml(html)], { type: 'text/plain' }),\n  }),\n});\n// text/html  → '<b>Hello <i>world</i></b>'\n// text/plain → 'Hello world'\n```\n\n## Custom MIME type\n\n```js\nawait copy('col1,col2\\nval1,val2', {\n  format: 'text/csv',\n});\n// text/csv   → 'col1,col2\\nval1,val2'\n// text/plain → 'col1,col2\\nval1,val2'  (always included as fallback)\n```\n\n---\n\n# Browser support\n\n| Browser | Minimum | Notes |\n|---------|---------|-------|\n| Chrome | 76+ | Full `ClipboardItem` + `write()` support |\n| Firefox | 127+ | Full `ClipboardItem` + `write()` landed in Firefox 127 (mid-2024) |\n| Safari | 13.1+ | `writeText()` and `write()` available |\n| Edge | 79+ | Chromium-based; same as Chrome |\n\n`execCommand` fallback retains support for non-HTTPS contexts and any browser that reaches the catch path.\n\n> **Note:** The async clipboard write must occur within a user gesture (click, keydown, etc.). This library is designed to be called from event handlers, so this constraint is normally satisfied automatically.\n\n# Installation\n\n```\nnpm i copy-to-clipboard\n```\n\nAvailable as CommonJS, ES module, and IIFE (for `<script>` tags):\n\n```js\n// ESM\nimport copy from 'copy-to-clipboard';\n\n// CommonJS\nconst copy = require('copy-to-clipboard');\n```\n\n```html\n<!-- IIFE via CDN — exposes window.copyToClipboard -->\n<script src=\"https://unpkg.com/copy-to-clipboard/dist/index.global.js\"></script>\n```\n\n# TypeScript\n\nBuilt-in declarations are included for both CommonJS and ESM consumers.\n\n```ts\nimport copy from 'copy-to-clipboard';\nimport type { Options } from 'copy-to-clipboard';\n\nconst result: boolean = await copy('text');\n```\n\n# UI components based on this package\n\n- [react-copy-to-clipboard](https://github.com/nkbt/react-copy-to-clipboard)\n- [copy-button](https://github.com/sudodoki/copy-button)\n\n# See also\n\n- [clipboard-copy](https://github.com/feross/clipboard-copy) by [@feross](https://github.com/feross)\n- [Async Clipboard API (MDN)](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API)\n- [ClipboardItem (MDN)](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem)\n\n# Running Tests\n\nEnd-to-end tests are powered by [Nightwatch](https://nightwatchjs.org/) using native browser drivers.\n\n```\nnpm i\nnpm test               # Chrome (default)\nnpm run test:firefox\nnpm run test:safari\nnpm run test:edge\nnpm run test:all       # all local browsers\n```\n\n**Safari prerequisite:** enable \"Allow Remote Automation\" in Safari's Develop menu. See [Testing with WebDriver in Safari](https://developer.apple.com/documentation/webkit/testing-with-webdriver-in-safari).\n\n## CI\n\n**Chrome, Firefox, and Edge** tests run automatically on every push to `master` and on pull requests via GitHub Actions (Ubuntu runner, headless).\n\n**Cross-browser** tests (Chrome, Firefox, Safari, Edge) run on [LambdaTest](https://www.lambdatest.com/) automatically on every version tag (`v*`) and can be triggered manually from the Actions tab. Requires `LT_USERNAME` and `LT_ACCESS_KEY` repository secrets.\n\n```\nnpm run test:lt:chrome\nnpm run test:lt:firefox\nnpm run test:lt:safari\nnpm run test:lt:edge\nnpm run test:lt:all\n```\n\n# Breaking changes in v4\n\n1. **`copy()` is now async** — returns `Promise<boolean>` instead of `boolean`. Wrap call sites with `await`.\n2. **IE11 support dropped** — `window.clipboardData` path removed.\n3. **`window.prompt()` fallback is opt-in** — set `options.fallbackToPrompt: true` to enable.\n4. **Build output moved to `dist/`** — direct `require('copy-to-clipboard/index.js')` paths will break; use the package name only.\n","publish_time":1776805023061,"_cnpm_publish_time":1776805023061,"dist-tags":{"latest":"4.0.2"}}