Clipboard
方法
write()
writes arbitrary data, such as images, to the clipboard.
This can be used to implement cut and copy functionality.
"clipboard-write"
permission of the
权限 API
, is granted automatically to pages when they are in the active tab.
注意: Browser support for the asynchronous clipboard APIs is still in the process of being implemented. Be sure to check the 兼容性表格 及 Clipboard availability in Clipboard 了解更多信息。
var promise = navigator.clipboard.write(data)
data
ClipboardItem
objects containing data to be written to the clipboard.
A
Promise
which is resolved when the data has been written to the clipboard. The promise is rejected if the clipboard is unable to complete the clipboard access.
This example function replaces the current contents of the clipboard with a specified string.
function setClipboard(text) {
let data = [new ClipboardItem({ "text/plain": text })];
navigator.clipboard.write(data).then(function() {
/* success */
}, function() {
/* failure */
});
}
The code begins by creating a new
ClipboardItem
object into which the text will be placed for sending to the clipboard. The key of the object passed to the
ClipboardItem
constructor indicates the content type, the value indicates the content. The content could be a text or even a Blob (e.g. for copying images to the clipboard). Then
write()
is called, specifying both a fulfilment function and an error function.
function copyCanvasContentsToClipboard(canvas, onDone, onError) {
canvas.toBlob(function (blob) {
let data = [new ClipboardItem({ [blob.type]: blob })];
navigator.clipboard.write(data).then(function () {
onDone();
}, function (err) {
onError(err);
})
});
}
注意 : You can only pass in one clipboard item at a time.
| 规范 | 状态 | 注释 |
|---|---|---|
|
Clipboard API and events
The definition of 'write()' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
write
|
Chrome
66
|
Edge 79 |
Firefox
63
Disabled
|
IE No | Opera 63 | Safari 13.1 |
WebView Android
66
|
Chrome Android
66
|
Firefox Android
63
Disabled
|
Opera Android 54 | Safari iOS 13.4 | Samsung Internet Android 12.0 |
完整支持
不支持
见实现注意事项。
用户必须明确启用此特征。
Clipboard
read()
readText()
write()
writeText()