Draft
This page is not complete.
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: To work with text see the Clipboard.readText()
and Clipboard.writeText()
methods of the Clipboard
interface.
Note: You can only pass in one clipboard item at a time.
Constructor
ClipboardItem.ClipboardItem()
- Creates a new
ClipboardItem
object, with the MIME type as the key andBlob
as the value
Properties
This interface provides the following properties.
Methods
This interface defines the following methods.
getType()
- Returns a
Promise
that resolves with aBlob
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.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); // we can now use blob here } } } catch (err) { console.error(err.name, err.message); } }
Specifications
Specification | Status | Comment |
---|---|---|
Clipboard API and events The definition of 'ClipboardItem' in that specification. |
Working Draft | Initial definition. |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
ClipboardItem | Chrome Full support 66 | Edge Full support ≤79 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 66 | Chrome Android Full support 66 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
ClipboardItem() constructor | Chrome Full support 66 | Edge Full support ≤79 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 66 | Chrome Android Full support 66 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
getType | Chrome Full support 66 | Edge Full support ≤79 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 66 | Chrome Android Full support 66 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
types | Chrome Full support 66 | Edge Full support ≤79 | Firefox No support No | IE No support No | Opera Full support Yes | Safari No support No | WebView Android Full support 66 | Chrome Android Full support 66 | Firefox Android No support No | Opera Android Full support Yes | Safari iOS No support No | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
Note: Image format support varies by browser. See the browser compatibility table for the Clipboard
interface.