{"name":"copy-to-clipboard","version":"4.0.2","description":"Copy stuff into clipboard using JS with fallbacks","main":"dist/index.cjs","module":"dist/index.mjs","types":"dist/index.d.ts","exports":{".":{"import":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"require":{"types":"./dist/index.d.ts","default":"./dist/index.cjs"}}},"type":"commonjs","sideEffects":false,"scripts":{"changelog":"npx changelogen@0.6.2 --no-bump --output CHANGELOG.md","release:patch":"npx changelogen@0.6.2 --release --patch","release:minor":"npx changelogen@0.6.2 --release --minor","release:major":"npx changelogen@0.6.2 --release --major","build":"tsup && cp dist/index.global.js example/index.js","typecheck":"tsc --noEmit","check":"publint","pretest":"npm run build","test":"nightwatch --env chrome","test:chrome":"nightwatch --env chrome","test:firefox":"nightwatch --env firefox","test:safari":"nightwatch --env safari","test:edge":"nightwatch --env edge","test:all":"nightwatch --env chrome,firefox,edge,safari","test:lt:firefox":"nightwatch --env lambdatest_firefox","test:lt:chrome":"nightwatch --env lambdatest_chrome","test:lt:safari":"nightwatch --env lambdatest_safari","test:lt:edge":"nightwatch --env lambdatest_edge","test:lt:all":"nightwatch --env lambdatest_firefox,lambdatest_chrome,lambdatest_safari,lambdatest_edge"},"keywords":["clipboard","copy","browser"],"author":{"name":"sudodoki","email":"smd.deluzion@gmail.com"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/sudodoki/copy-to-clipboard.git"},"contributors":[{"name":"Aleksej Shvajka","url":"https://github.com/shvaikalesh"}],"directories":{"example":"example"},"devDependencies":{"@brettz9/node-static":"^0.1.1","chromedriver":"^146.0.6","geckodriver":"^6.1.0","minimist":"^1.2.6","nightwatch":"^3.15.0","publint":"^0.3.18","toggle-selection":"^2.0.0","tsup":"^8.0.0","typescript":"^5.0.0"},"gitHead":"bf69f76573537765769849e2b9f4efc6b8b51177","_id":"copy-to-clipboard@4.0.2","bugs":{"url":"https://github.com/sudodoki/copy-to-clipboard/issues"},"homepage":"https://github.com/sudodoki/copy-to-clipboard#readme","_nodeVersion":"25.8.0","_npmVersion":"11.11.0","dist":{"integrity":"sha512-gklSft7IuhriZKHKpuoA1fpJSLPNgvUMWMo5BlnzAJm0zNKnznoSv23IjtNqclx8eKi6ZcdvFFzYEER/+U1LoQ==","shasum":"fbd05a471231962c25486c1dc5019adf0b4bdc71","tarball":"http://registry-npm.ywwl.com/copy-to-clipboard/download/copy-to-clipboard-4.0.2.tgz","fileCount":8,"unpackedSize":33538,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIH3hGvkXsP5oGnccmZluSKUic8Fo5RGYsAB21Q//A3D4AiEAg9GZVzFIWDoWpAU87zs1VVYmdu0gCTztVxU1NHxZ0zs="}],"size":7854,"noattachment":false,"key":"/copy-to-clipboard/-/copy-to-clipboard-4.0.2.tgz"},"_npmUser":{"name":"sudodoki","email":"smd.deluzion@gmail.com"},"maintainers":[{"name":"sudodoki","email":"smd.deluzion@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/copy-to-clipboard_4.0.2_1777068918798_0.0253539332576862"},"_hasShrinkwrap":false,"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":1777068918933,"_cnpm_publish_time":1777068918933,"dist-tags":{"latest":"4.0.2"}}