草案
此页面不完整。
ClipboardItem
接口在
Clipboard API
represents a single item format, used when reading or writing data via the
Clipboard API
. That is
clipboard.read()
and
clipboard.write()
分别。
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
权限 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.
注意
: To work with text see the
Clipboard.readText()
and
Clipboard.writeText()
methods of the
Clipboard
接口。
注意 : You can only pass in one clipboard item at a time.
ClipboardItem.ClipboardItem()
ClipboardItem
object, with the
MIME 类型
as the key and
Blob
作为值
This interface provides the following properties.
This interface defines the following methods.
getType()
Promise
that resolves with a
Blob
of the requested
MIME 类型
, or an error if the MIME type is not found.
Here we're writing a new
ClipboardItem.ClipboardItem()
到
clipboard
by requesting a png image using the
抓取 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);
}
}
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);
}
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
Clipboard API and events
The definition of 'ClipboardItem' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
ClipboardItem
|
Chrome 66 | Edge ≤79 | Firefox No | IE No | Opera Yes | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android Yes | Safari iOS No | Samsung Internet Android Yes |
ClipboardItem()
构造函数
|
Chrome 66 | Edge ≤79 | Firefox No | IE No | Opera Yes | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android Yes | Safari iOS No | Samsung Internet Android Yes |
getType
|
Chrome 66 | Edge ≤79 | Firefox No | IE No | Opera Yes | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android Yes | Safari iOS No | Samsung Internet Android Yes |
类型
|
Chrome 66 | Edge ≤79 | Firefox No | IE No | Opera Yes | Safari No | WebView Android 66 | Chrome Android 66 | Firefox Android No | Opera Android Yes | Safari iOS No | Samsung Internet Android Yes |
完整支持
不支持
实验。期望将来行为有所改变。
注意
: Image format support varies by browser. See the browser compatibility table for the
Clipboard
接口。