The ClipboardItem
interface of the Clipboard API
represents a single item format, used when reading or writing data via the Clipboard API
. That is clipboard.read()
and clipboard.write()
respectively.
The benefit of having the ClipboardItem
interface to represent data, is that it enables developers to cope with the varying scope of file types and data easily.
Access to the contents of the clipboard is gated behind the Permissions API: The clipboard-write
permission is granted automatically to pages when they are in the active tab. The clipboard-read
permission must be requested, which you can do by trying to read data from the clipboard.
Note: You can only pass in one clipboard item at a time.
Constructor
ClipboardItem()
-
Creates a new ClipboardItem
object, with the MIME type as the key and Blob
as the value.
Instance properties
This interface provides the following properties.
-
types
Read only
-
Returns an Array
of MIME types available within the ClipboardItem
.
-
presentationStyle
Read only
-
Returns one of the following: "unspecified"
, "inline"
or "attachment"
.
Instance methods
This interface defines the following methods.
getType()
-
Returns a Promise
that resolves with a Blob
of the requested MIME type, or an error if the MIME type is not found.
Examples
Writing To Clipboard
Here we're writing a new ClipboardItem()
to the clipboard
by requesting a png image using the Fetch API
, and in turn, the responses' blob()
method, to create the new ClipboardItem
.
async function writeClipImg() {
try {
const imgURL = "/myimage.png";
const data = await fetch(imgURL);
const blob = await data.blob();
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob,
}),
]);
console.log("Fetched image copied.");
} catch (err) {
console.error(err.name, err.message);
}
}
Reading From The Clipboard
Here we're returning all items on the clipboard via the clipboard.read()
method. Then utilizing the ClipboardItem.types
property to set the getType()
argument and return the corresponding blob object.
async function getClipboardContents() {
try {
const clipboardItems = await navigator.clipboard.read();
for (const clipboardItem of clipboardItems) {
for (const type of clipboardItem.types) {
const blob = await clipboardItem.getType(type);
}
}
} catch (err) {
console.error(err.name, err.message);
}
}
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
ClipboardItem |
9876–98The ClipboardItem constructor only accepts a blob as the item data, but not strings or Promises that resolve to strings or blobs. See bug 1014310.
|
9879–98The ClipboardItem constructor only accepts a blob as the item data, but not strings or Promises that resolve to strings or blobs. See bug 1014310.
|
87 |
No |
8463–84The ClipboardItem constructor only accepts a blob as the item data, but not strings or Promises that resolve to strings or blobs. See bug 1014310.
|
13.1 |
9884–98The ClipboardItem constructor only accepts a blob as the item data, but not strings or Promises that resolve to strings or blobs. See bug 1014310.
|
9884–98The ClipboardItem constructor only accepts a blob as the item data, but not strings or Promises that resolve to strings or blobs. See bug 1014310.
|
No |
6860–68The ClipboardItem constructor only accepts a blob as the item data, but not strings or Promises that resolve to strings or blobs. See bug 1014310.
|
13.4 |
18.014.0–18.0The ClipboardItem constructor only accepts a blob as the item data, but not strings or Promises that resolve to strings or blobs. See bug 1014310.
|
ClipboardItem |
76 |
79 |
87 |
No |
63 |
13.1 |
84 |
84 |
No |
60 |
13.4 |
14.0 |
getType |
76 |
79 |
87 |
No |
63 |
13.1 |
84 |
84 |
No |
60 |
13.4 |
14.0 |
presentationStyle |
No |
No |
87 |
No |
No |
13.1 |
No |
No |
No |
No |
13.4 |
No |
types |
76 |
79 |
87 |
No |
63 |
13.1 |
84 |
84 |
No |
60 |
13.4 |
14.0 |
See also