HearthSim/clipboard-polyfill

Name: clipboard-polyfill

Owner: HearthSim

Description: Forked for droppable console.log statements required by Twitch Extensions

Created: 2018-04-08 00:37:02.0

Updated: 2018-04-08 00:53:30.0

Pushed: 2018-04-08 00:53:29.0

Homepage:

Size: 221

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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

clipboard-polyfill

Make copying on the web as easy as:

clipboard.writeText("hello world");

As of October 2017, this library is a polyfill for the modern Promise-based asynchronous clipboard API.

Usage

Get the source using one of the following:

Plain Text

Copy text to the clipboard (all modern browsers):

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

Read text from the clipboard (IE 9-11 and Chrome 65+):

clipboard.readText().then(console.log, console.error);

Caveats:

Other Data Types (e.g. HTML)

Write (all modern browsers):

var dt = new clipboard.DT();
dt.setData("text/plain", "Fallback markup text.");
dt.setData("text/html", "<i>Markup</i> <b>text</b>.");
clipboard.write(dt);

Read (IE 9-11, Chrome 65+):

// The success callback receives a clipboard.DT object.
clipboard.read().then(console.log, console.error);

Caveats:

Interface
clipboard {
  static write:     (data: clipboard.DT)  => Promise<void>
  static writeText: (s: string) => Promise<void>
  static read:      () => Promise<clipboard.DT>
  static readText:  () => Promise<string>
  static suppressWarnings: () => void
}

clipboard.DT {
  constructor()
  setData: (type: string, value: string): void
  getData: (type: string): string | undefined
}
A note on clipboard.DT

The asynchronous clipboard API works like this:

var dt = new DataTransfer();
dt.setData("text/plain", "plain text");
navigator.clipboard.write(dt);

Ideally, clipboard-polyfill would take a DataTransfer, so that the code above works verbatim when you replace navigator.clipboard with clipboard. However, the DataTransfer constructor cannot be called in most browsers. Thus, this library uses a light-weight alternative to DataTransfer, exposed as clipboard.DT:

var dt = new clipboard.DT();
dt.setData("text/plain", "plain text");
clipboard.write(dt);
This is way too complicated!

Try this gist for a simpler solution.

Can I use it?

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

Limitations

clipboard.write() was called without a text/plain data type. On some platforms, this may result in an empty clipboard. Call clipboard.suppressWarnings() to suppress this warning.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.