ClipboardItem - Web API 接口参考 编辑
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
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.Note: Image format support varies by browser. See the browser compatibility table for the Clipboard
interface.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论