--- title: ClipboardItem slug: Web/API/ClipboardItem tags: - API - Clipboard - Clipboard API - ClipboardItem - Cut - Interface - NeedsTranslation - Reference - TopicStub - copy - paste translation_of: Web/API/ClipboardItem ---
The ClipboardItem
interface of the {{domxref('Clipboard API')}} represents a single item format, used when reading or writing data via the {{domxref('Clipboard API')}}. That is {{domxref("clipboard.read()")}} and {{domxref("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 {{domxref("Clipboard.readText()")}} and {{domxref("Clipboard.writeText()")}} methods of the {{domxref("Clipboard")}} interface.
Note: You can only pass in one clipboard item at a time.
ClipboardItem
object, with the {{Glossary("MIME type")}} as the key and {{domxref("Blob")}} as the valueThis interface provides the following properties.
ClipboardItem
.This interface defines the following methods.
Here we're writing a new {{domxref("ClipboardItem.ClipboardItem()")}} to the {{domxref("Clipboard API", "clipboard")}} by requesting a png image using the {{domxref("Fetch API")}}, and in turn, the {{domxref("Body.blob()", "responses' blob()")}} method, to create the new {{domxref("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); } }
Here we're returning all items on the clipboard via the {{domxref("clipboard.read()")}} method. Then utilizing the {{domxref("ClipboardItem.types")}} property to set the {{domxref("ClipboardItem.getType", "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); } }
Specification | Status | Comment |
---|---|---|
{{SpecName('Clipboard API','#clipboarditem','ClipboardItem')}} | {{Spec2('Clipboard API')}} | Initial definition. |
{{Compat("api.ClipboardItem")}}
Note: Image format support varies by browser. See the browser compatibility table for the {{domxref("Clipboard")}} interface.