ClipboardItem: phương thức getType()
Baseline
2024
Newly available
Since June 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Phương thức getType() của giao diện ClipboardItem trả về một Promise được giải quyết với một Blob của MIME type được yêu cầu, hoặc lỗi nếu không tìm thấy kiểu MIME.
Cú pháp
getType(type)
Tham số
Giá trị trả về
Ngoại lệ
NotFoundErrorDOMException-
typekhông khớp với một MIME type đã biết. TypeError-
Không chỉ định tham số nào, hoặc
typekhông thuộc vềClipboardItem.
Ví dụ
Trong ví dụ sau, chúng ta trả về tất cả các mục trong clipboard thông qua phương thức clipboard.read(). Với mỗi mục, chúng ta truyền thuộc tính ClipboardItem.types cho phương thức getType(), phương thức này trả về đối tượng Blob tương ứng.
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);
}
}
Thông số kỹ thuật
| Specification |
|---|
| Clipboard API and events> # dom-clipboarditem-gettype> |