All Projects β†’ lgarron β†’ Clipboard Polyfill

lgarron / Clipboard Polyfill

Licence: other
πŸ“‹ Simple copying on the web, with maximum browser compatibility.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Clipboard Polyfill

Piknik
Copy/paste anything over the network.
Stars: ✭ 2,221 (+196.93%)
Mutual labels:  paste, copy, clipboard
Xcv
βœ‚οΈ Cut, Copy and Paste files with Bash
Stars: ✭ 144 (-80.75%)
Mutual labels:  paste, copy, clipboard
MagicWE2
[MagicWE2] Lag free asynchronous world editor for PMMP with plenty of options
Stars: ✭ 109 (-85.43%)
Mutual labels:  clipboard, copy, paste
Resize Observer
Polyfills the ResizeObserver API.
Stars: ✭ 540 (-27.81%)
Mutual labels:  api, polyfill, ponyfill
netflix-list-exporter
πŸ’«β€Žβ€Žβ€Žβ€β€β€Žβ€An Extension to export your lists from Netflix to Clipboard area and share it with your friends.
Stars: ✭ 60 (-91.98%)
Mutual labels:  clipboard, copy
v-copy
Vue directive to copy to clipboard. (1kB)
Stars: ✭ 88 (-88.24%)
Mutual labels:  clipboard, copy
mini-create-react-context
(A smaller) polyfill for the react context API
Stars: ✭ 34 (-95.45%)
Mutual labels:  polyfill, ponyfill
Abortcontroller Polyfill
Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
Stars: ✭ 273 (-63.5%)
Mutual labels:  polyfill, ponyfill
react-gluejar
Paste images from your clipboard, declaratively
Stars: ✭ 58 (-92.25%)
Mutual labels:  copy, paste
fromentries
Object.fromEntries() ponyfill (in 6 lines)
Stars: ✭ 62 (-91.71%)
Mutual labels:  polyfill, ponyfill
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+405.21%)
Mutual labels:  polyfill, ponyfill
Clipboard Copy
Lightweight copy to clipboard for the web
Stars: ✭ 443 (-40.78%)
Mutual labels:  copy, clipboard
org-rich-yank
πŸ“‹ Rich text clipboard for org-mode: Paste as a #+BEGIN_SRC block of correct mode, with link to where it came from
Stars: ✭ 59 (-92.11%)
Mutual labels:  clipboard, paste
o9n
πŸ–₯ A screen.orientation ponyfill
Stars: ✭ 55 (-92.65%)
Mutual labels:  polyfill, ponyfill
gradle-cpd-plugin
Gradle plugin to find duplicate code using PMDs copy/paste detection (= CPD).
Stars: ✭ 88 (-88.24%)
Mutual labels:  copy, paste
web-streams-polyfill
Web Streams, based on the WHATWG spec reference implementation
Stars: ✭ 198 (-73.53%)
Mutual labels:  polyfill, ponyfill
Gotem
Copy to clipboard for modern browsers in less than 1kb.
Stars: ✭ 439 (-41.31%)
Mutual labels:  copy, clipboard
React Lifecycles Compat
Backwards compatibility polyfill for React class components
Stars: ✭ 457 (-38.9%)
Mutual labels:  api, polyfill
Share Api Polyfill
A polyfill for the sharing that can be used in desktop too, so your users can shere in their twitter, facebook, messenger, linkedin, sms, e-mail, print, telegram or whatsapp.
Stars: ✭ 210 (-71.93%)
Mutual labels:  api, polyfill
Eslint Plugin Compat
Lint the browser compatibility of your code
Stars: ✭ 2,743 (+266.71%)
Mutual labels:  api, polyfill

Logo for clipboard-polyfill: an icon of a clipboard fading into a drafting paper grid.

clipboard-polyfill

Makes copying on the web as easy as:

clipboard.writeText("hello world");

This library is a polyfill for the modern Promise-based asynchronous clipboard API.

Note: As of late June 2020, you can use navigator.clipboard.writeText("hello world); in the stable versions of all major browsers (see below for compatibility). This library will only be useful to you if you 1) need to copy text/html, or 2) need to target older browsers.

Usage

If you use npm, install:

npm install clipboard-polyfill

Sample app that copies text to the clipboard:

// Using the `clipboard/text` build saves code size.
// This is recommended if you only need to copy text.
import * as clipboard from "clipboard-polyfill/text";

function handler() {
  clipboard.writeText("This text is plain.").then(
    function () {
      console.log("success!");
    },
    function () {
      console.log("error!");
    }
  );
}

window.addEventListener("DOMContentLoaded", function () {
  const button = document.body.appendChild(document.createElement("button"));
  button.textContent = "Copy";
  button.addEventListener("click", handler);
});

Notes:

  • You need to call a clipboard operation in response to a user gesture (e.g. the event handler for a button click).
    • Some browsers may only allow one clipboard operation per gesture.

async/await syntax

import * as clipboard from "clipboard-polyfill/text";

async function handler() {
  console.log("Previous clipboard text:", await clipboard.readText());

  await clipboard.writeText("This text is plain.");
}

window.addEventListener("DOMContentLoaded", function () {
  const button = document.body.appendChild(document.createElement("button"));
  button.textContent = "Copy";
  button.addEventListener("click", handler);
});

More MIME types (data types)

import * as clipboard from "clipboard-polyfill";

async function handler() {
  console.log("Previous clipboard contents:", await clipboard.read());

  const item = new clipboard.ClipboardItem({
    "text/html": new Blob(
      ["<i>Markup</i> <b>text</b>. Paste me into a rich text editor."],
      { type: "text/html" }
    ),
    "text/plain": new Blob(
      ["Fallback markup text. Paste me into a rich text editor."],
      { type: "text/plain" }
    ),
  });
  await clipboard.write([item]);
}

window.addEventListener("DOMContentLoaded", function () {
  const button = document.body.appendChild(document.createElement("button"));
  button.textContent = "Copy";
  button.addEventListener("click", handler);
});

Check the Clipboard API specification for more details.

Notes:

  • You'll need to use async functions for the await syntax.
  • Currently, text/plain and text/html are the only data types that can be written to the clipboard across most browsers.
  • 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.
  • 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.

overwrite-globals version

If 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 into a proper polyfill, so you can write code as if the async clipboard API were already implemented in your browser:

import "clipboard-polyfill/overwrite-globals";

async function handler() {
  const item = new window.ClipboardItem({
    "text/html": new Blob(
      ["<i>Markup</i> <b>text</b>. Paste me into a rich text editor."],
      { type: "text/html" }
    ),
    "text/plain": new Blob(
      ["Fallback markup text. Paste me into a rich text editor."],
      { type: "text/plain" }
    ),
  });

  navigator.clipboard.write([item]);
}

window.addEventListener("DOMContentLoaded", function () {
  const button = document.body.appendChild(document.createElement("button"));
  button.textContent = "Copy";
  button.addEventListener("click", handler);
});

This 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.

Flat-file version with Promise included

If you need to grab a version that "just works", download clipboard-polyfill.promise.js and include it using a <script> tag:

<script src="./clipboard-polyfill.promise.js"></script>
<button onclick="copy()">Copy text!</button>
<script>
  // `clipboard` is defined on the global `window` object.
  function copy() {
    clipboard.writeText("hello world!");
  }
</script>

Why clipboard-polyfill?

Browsers have implemented several clipboard APIs over time, and writing to the clipboard without triggering bugs in various old and current browsers 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.)

See this presentation for for a longer history of clipboard access on the web.

Compatibility

  • β˜‘οΈ: Browser has native async clipboard support.
  • βœ…: clipboard-polyfill provides support.
  • ❌: Support is not possible.
  • Bold browser names indicate the latest functionality changes for stable versions of modern browsers.

Write support by earliest browser version:

Browser writeText() write() (HTML) write() (other formats)
Safari 13.1 β˜‘οΈ β˜‘οΈ β˜‘οΈ (image/uri-list, image/png)
Chrome 76ᡃ / Edge 79 β˜‘οΈ βœ… β˜‘οΈ (image/png)
Chrome 66ᡃ / Firefox 63 β˜‘οΈ βœ… ❌
Safari 10 / Chrome 42ᡃ / Edgeᡈ / Firefox 41 βœ… βœ…α΅‡ ❌
IE 9 βœ…αΆœ ❌ ❌

Read support:

Browser readText() read() (HTML) read() (other formats)
Safari 13.1 β˜‘οΈ β˜‘οΈ β˜‘οΈ (image/uri-list, image/png)
Chrome 76 ᡃ / Edge 79 β˜‘οΈ ❌ β˜‘οΈ (image/png)
Chrome 66ᡃ β˜‘οΈ ❌ ❌
IE 9 βœ…αΆœ ❌ ❌
Firefox ❌ ❌ ❌
  • ᡃ Also includes versions of Edge, Opera, Brave, Vivaldi, etc. based on the corresponding version of Chrome.
  • ᡇ HTML did not work properly on mobile Safari in the first few releases of version 10.
  • ᢜ In Internet Explorer, you will need to polyfill window.Promise if you want the library to work.
  • ᡈ In older versions of Edge (Spartan):
    • It may not be possible to tell if a copy operation succeeded (Edge Bug #14110451, Edge Bug #14080262). clipboard-polyfill will always report success in this case.
    • Only the last data type you specify is copied to the clipboard (Edge Bug #14080506). Consider placing the most important data type last in the object that you pass to the ClipboardItem constructor.
    • 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, #73)

clipboard-polyfill uses a variety of heuristics to work around compatibility bugs. Please let us know if you are running into compatibility issues with any of the browsers listed above.

This is way too complicated!

If you only need to copy text, try this gist for a simpler solution.

Alternatively, if you wait until iOS 14 / macOS 11, navigator.clipboard.writeText() will be supported in the stable versions of all major modern browsers.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].