UNPKG

14 kBMarkdownView Raw
1![Logo for clipboard-polyfill: an icon of a clipboard fading into a drafting paper grid.](clipboard-polyfill-logo.svg)
2
3# `clipboard-polyfill`
4
5## ⚠️ You don't need `clipboard-polyfill` to copy text! ⚠️
6
7Note: As of 2020, you can use `navigator.clipboard.writeText(...)` [in the stable versions of all major browsers](https://caniuse.com/mdn-api_clipboard_writetext). This library will only be useful to you if you want to:
8
9- target older browsers (see below for compatibility) for text copy,
10- copy `text/html` in Firefox,
11- use the `ClipboardItem` API in Firefox, or
12- polyfill the API shape in a non-browser environment (e.g. in [`jsdom`](https://github.com/jsdom/jsdom/issues/1568)).
13
14See the [Compatibility section](#compatibility) below for more details.
15
16---
17
18## Summary
19
20Makes copying on the web as easy as:
21
22```js
23clipboard.writeText("hello world");
24```
25
26This library is a [ponyfill](https://github.com/sindresorhus/ponyfill)/polyfill for the modern `Promise`-based [asynchronous clipboard API](https://www.w3.org/TR/clipboard-apis/#async-clipboard-api).
27
28## Usage
29
30If you use `npm`, install:
31
32```shell
33npm install clipboard-polyfill
34```
35
36Sample app that copies text to the clipboard:
37
38```js
39import * as clipboard from "clipboard-polyfill";
40
41function handler() {
42 clipboard.writeText("This text is plain.").then(
43 () => { console.log("success!"); },
44 () => { console.log("error!"); }
45 );
46}
47
48window.addEventListener("DOMContentLoaded", function () {
49 const button = document.body.appendChild(document.createElement("button"));
50 button.textContent = "Copy";
51 button.addEventListener("click", handler);
52});
53```
54
55Notes:
56
57- You need to call a clipboard operation in response to a user gesture (e.g. the event handler for a `button` click).
58 - Some browsers may only allow one clipboard operation per gesture.
59
60## `async`/`await` syntax
61
62```js
63import * as clipboard from "clipboard-polyfill";
64
65async function handler() {
66 console.log("Previous clipboard text:", await clipboard.readText());
67
68 await clipboard.writeText("This text is plain.");
69}
70
71window.addEventListener("DOMContentLoaded", function () {
72 const button = document.body.appendChild(document.createElement("button"));
73 button.textContent = "Copy";
74 button.addEventListener("click", handler);
75});
76```
77
78## More MIME types (data types)
79
80```js
81import * as clipboard from "clipboard-polyfill";
82
83async function handler() {
84 console.log("Previous clipboard contents:", await clipboard.read());
85
86 const item = new clipboard.ClipboardItem({
87 "text/html": new Blob(
88 ["<i>Markup</i> <b>text</b>. Paste me into a rich text editor."],
89 { type: "text/html" }
90 ),
91 "text/plain": new Blob(
92 ["Fallback markup text. Paste me into a rich text editor."],
93 { type: "text/plain" }
94 ),
95 });
96 await clipboard.write([item]);
97}
98
99window.addEventListener("DOMContentLoaded", function () {
100 const button = document.body.appendChild(document.createElement("button"));
101 button.textContent = "Copy";
102 button.addEventListener("click", handler);
103});
104```
105
106Check [the Clipboard API specification](https://www.w3.org/TR/clipboard-apis/#clipboard-interface) for more details.
107
108Notes:
109
110- You'll need to use `async` functions for the `await` syntax.
111- Currently, `text/plain` and `text/html` are the only data types that can be written to the clipboard across most browsers.
112- If you try to copy unsupported data types, they may be silently dropped (e.g. Safari 13.1) or the call may throw an error (e.g. Chrome 83). In general, it is not possible to tell when data types are dropped.
113- In some current browsers, `read()` may only return a subset of supported data types, even if the clipboard contains more data types. There is no way to tell if there were more data types.
114
115### `overwrite-globals` version
116
117If you want the library to overwrite the global clipboard API with its implementations, import `clipboard-polyfill/overwrite-globals`. This will turn the library from a [ponyfill](https://ponyfill.com/) into a proper polyfill, so you can write code as if the async clipboard API were already implemented in your browser:
118
119```js
120import "clipboard-polyfill/overwrite-globals";
121
122async function handler() {
123 const item = new window.ClipboardItem({
124 "text/html": new Blob(
125 ["<i>Markup</i> <b>text</b>. Paste me into a rich text editor."],
126 { type: "text/html" }
127 ),
128 "text/plain": new Blob(
129 ["Fallback markup text. Paste me into a rich text editor."],
130 { type: "text/plain" }
131 ),
132 });
133
134 navigator.clipboard.write([item]);
135}
136
137window.addEventListener("DOMContentLoaded", function () {
138 const button = document.body.appendChild(document.createElement("button"));
139 button.textContent = "Copy";
140 button.addEventListener("click", handler);
141});
142```
143
144This approach is not recommended, because it may break any other code that interacts with the clipboard API globals, and may be incompatible with future browser implementations.
145
146### Flat-file version with `Promise` included
147
148If you need to grab a version that "just works", download [`clipboard-polyfill.window-var.promise.es5.js`](https://unpkg.com/clipboard-polyfill/dist/es5/window-var/clipboard-polyfill.window-var.promise.es5.js) and include it using a `<script>` tag:
149
150```html
151<script src="./clipboard-polyfill.window-var.promise.es5.js"></script>
152<button onclick="copy()">Copy text!</button>
153<script>
154 // `clipboard` is defined on the global `window` object.
155 function copy() {
156 clipboard.writeText("hello world!");
157 }
158</script>
159```
160
161### Bundling / tree shaking / minification / CommonJS
162
163Thanks to the conveniences of the modern JS ecosystem, we do not provide tree shaken, minified, or CommonJS builds anymore. To get such builds without losing compatibility, pass `clipboard-polyfill` builds through `esbuild`. For example:
164
165```shell
166mkdir temp && cd temp && npm install clipboard-polyfill esbuild
167
168# Minify the ES6 build:
169echo 'export * from "clipboard-polyfill";' | npx esbuild --format=esm --target=es6 --bundle --minify
170
171# Include just the `writeText()` export and minify:
172echo 'export { writeText } from "clipboard-polyfill";' | npx esbuild --format=esm --target=es6 --bundle --minify
173
174# Minify an ES5 build:
175cat node_modules/clipboard-polyfill/dist/es5/window-var/clipboard-polyfill.window-var.promise.es5.js | npx esbuild --format=esm --target=es5 --bundle --minify
176
177# Get a CommonJS build:
178echo 'export * from "clipboard-polyfill";' | npx esbuild --format=cjs --target=es6 --bundle
179```
180
181## Why `clipboard-polyfill`?
182
183Browsers have implemented several clipboard APIs over time, and writing to the clipboard without [triggering bugs in various old and current browsers](https://github.com/lgarron/clipboard-polyfill/blob/master/experiment/Conclusions.md) is fairly tricky. In every browser that supports copying to the clipboard in some way, `clipboard-polyfill` attempts to act as close as possible to the async clipboard API. (See above for disclaimers and limitations.)
184
185See [this presentation](https://docs.google.com/presentation/d/1Ix2rYi67hbZoIQsd85kspkUPLi8Q-PZopy_AtfafHW0) for for a longer history of clipboard access on the web.
186
187## Compatibility
188
189- ☑️: Browser has native async clipboard support.
190- ✅: `clipboard-polyfill` adds support.
191- ❌: Support is not possible.
192- **Bold browser names** indicate the latest functionality changes for stable versions of modern browsers.
193
194Write support by earliest browser version:
195
196| Browser | `writeText()` | `write()` (HTML) | `write()` (other formats) |
197| ------------------------------------------- | ------------- | ---------------- | ---------------------------------- |
198| **Safari 13.1** | ☑️ | ☑️ | ☑️ (`image/uri-list`, `image/png`) |
199| **Chrome 86**ᵃ / **Edge 86** | ☑️ | ☑️ | ☑️ (`image/png`) |
200| Chrome 76ᵃ / Edge 79 | ☑️ | ✅ | ☑️ (`image/png`) |
201| Chrome 66ᵃ / **Firefox 63** | ☑️ | ✅ | ❌ |
202| Safari 10 / Chrome 42ᵃ / Edgeᵈ / Firefox 41 | ✅ | ✅ᵇ | ❌ |
203| IE 9 | ✅ᶜ | ❌ | ❌ |
204
205Read support:
206
207| Browser | `readText()` | `read()` (HTML) | `read()` (other formats) |
208| ----------------------------------------------------------------------------------- | ------------ | --------------- | ---------------------------------- |
209| **Safari 13.1** | ☑️ | ☑️ | ☑️ (`image/uri-list`, `image/png`) |
210| **Chrome [76](https://web.dev/image-support-for-async-clipboard/)** ᵃ / **Edge 79** | ☑️ | ❌ | ☑️ (`image/png`) |
211| Chrome [66](https://developers.google.com/web/updates/2018/03/clipboardapi)ᵃ | ☑️ | ❌ | ❌ |
212| IE 9 | ✅ᶜ | ❌ | ❌ |
213| **Firefox** | ❌ | ❌ | ❌ |
214
215- ᵃ Also includes versions of Edge, Opera, Brave, Vivaldi, etc. based on the corresponding version of Chrome.
216- ᵇ HTML did not work properly on mobile Safari in the first few releases of version 10.
217- ᶜ In Internet Explorer, you will need to polyfill `window.Promise` if you want the library to work.
218- ᵈ In older versions of Edge (Spartan):
219 - It may not be possible to tell if a copy operation succeeded ([Edge Bug #14110451](https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/14110451/), [Edge Bug #14080262](https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/14080262/)). `clipboard-polyfill` will always report success in this case.
220 - Only the _last_ data type you specify is copied to the clipboard ([Edge Bug #14080506](https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/14080506/)). Consider placing the most important data type last in the object that you pass to the `ClipboardItem` constructor.
221 - The `text/html` data type is not written using the expected `CF_HTML` format. `clipboard-polyfill` does _not_ try to work around this, since 1) it would require fragile browser version sniffing, 2) users of Edge are not generally stuck on version < 17, and 3) the failure mode for other browsers would be that invalid clipboard HTML is copied. ([Edge Bug #14372529](https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/14372529/), [#73](https://github.com/lgarron/clipboard-polyfill/issues/73))
222
223`clipboard-polyfill` uses a variety of heuristics to work around compatibility bugs. Please [let us know](https://github.com/lgarron/clipboard-polyfill/issues/new) if you are running into compatibility issues with any of the browsers listed above.
224
225## History
226
227### Browser history
228
229| Browser | First version supporting<br>`navigator.clipboard.writeText(...)` | Release Date |
230| ------- | ------------------------------------------------------------- | ------------ |
231| Chrome | 66+ | April 2018 |
232| Firefox | 53+ | October 2018 |
233| Edge | 79+ (first Chromium-based release) | January 2020 |
234| Safari | 13.1+ | March 2020 |
235
236### Project history
237
238This project dates from a time when clipboard access in JS was barely becoming possible, and [ergonomic clipboard API efforts were stalling](https://lists.w3.org/Archives/Public/public-webapps/2015JulSep/0235.html). (See [this presentation](https://docs.google.com/presentation/d/1Ix2rYi67hbZoIQsd85kspkUPLi8Q-PZopy_AtfafHW0/) for a bit more context.) Fortunately, [an ergonomic API with the same functionality](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard) is now available in all modern browsers since 2020:
239
240- 2015: Browsers [start supporting](https://caniuse.com/mdn-api_document_execcommand_copy) the [defunct](https://w3c.github.io/editing/docs/execCommand/) `document.execCommand("copy")` call (with [many, many issues](./experiment/Conclusions.md)).
241- 2015: Started this project as `clipboard.js` (half a year before @zenorocha picked [the same name](https://github.com/zenorocha/clipboard.js) 😛).
242- 2016: Renewed discussions about an async clipboard API (e.g. [proposal doc](https://docs.google.com/document/d/1QI5rKJSiYeD9ekP2NyCYJuOnivduC9-tqEOn-GsCGS4/edit#), [`crbug.com/593475`](https://bugs.chromium.org/p/chromium/issues/detail?id=593475)).
243- 2017: Renamed this project to `clipboard-polyfill` to reflect a `v2` API overhaul aligned with the draft spec.
244- 2018: Browsers [start supporting](https://caniuse.com/mdn-api_clipboard_writetext) `navigator.clipboard.writeText()`.
245- 2020: Browsers [start supporting](https://caniuse.com/mdn-api_clipboard_write) `navigator.clipboard.write()` (including `text/html` support).
246
247Thanks to Gary Kacmarcik, Hallvord Steen, and others for helping to bring the [async clipboard API](https://w3c.github.io/clipboard-apis/) to life!
248
249## This is way too complicated!
250
251If you only need to copy text in modern browsers, consider using `navigator.clipboard.writeText()` directly: <https://caniuse.com/mdn-api_clipboard_writetext>
252
253If you need copy text in older browsers as well, you could also try [this gist](https://gist.github.com/lgarron/d1dee380f4ed9d825ca7) for a simple hacky solution.