title | short-title | slug | page-type | browser-compat |
---|---|---|---|---|
ClipboardItem: getType() method |
getType() |
Web/API/ClipboardItem/getType |
web-api-instance-method |
api.ClipboardItem.getType |
{{APIRef("Clipboard API")}} {{securecontext_header}}
The getType()
method of the {{domxref("ClipboardItem")}} interface returns a {{jsxref("Promise")}} that resolves with a {{domxref("Blob")}} of the requested {{Glossary("MIME type")}} or an error if the MIME type is not found.
getType(type)
type
- : A valid {{Glossary("MIME type")}}.
A {{jsxref("Promise")}} that resolves with a {{domxref("Blob")}} object.
NotFoundError
{{domxref("DOMException")}}- : The
type
does not match a known {{Glossary("MIME type")}}.
- : The
- {{jsxref("TypeError")}}
- : No parameter is specified or the
type
is not that of the {{domxref("ClipboardItem")}}.
- : No parameter is specified or the
In the following example, we're returning all items on the clipboard via the {{domxref("clipboard.read()")}} method.
Then utilizing the {{domxref("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}}
{{Compat}}