Clipboard
interface implements the
Clipboard API
, providing—if the user grants permission—both read and write access to the contents of the system clipboard.
The Clipboard API can be used to implement cut, copy, and paste features within a web application.
The system clipboard is exposed through the global
Navigator.clipboard
特性。
Calls to the methods of the
Clipboard
object will not succeed if the user hasn't granted the needed permissions using the
权限 API
和
"clipboard-read"
or
"clipboard-write"
permission as appropriate.
注意: In reality, at this time browser requirements for access to the clipboard vary significantly. Please see the section Clipboard availability 了解细节。
All of the Clipboard API methods operate asynchronously; they return a
Promise
which is resolved once the clipboard access has been completed. The promise is rejected if clipboard access is denied.
clipboard
is a data buffer that is used for short-term, data storage and/or data transfers, this can be between documents or applications
It is usually implemented as an anonymous, temporary
data buffer
, sometimes called the paste buffer, that can be accessed from most or all programs within the environment via defined
programming interfaces
.
A typical application accesses clipboard functionality by mapping user input 譬如 keybindings , menu selections , etc. to these interfaces.
Clipboard
is based on the
EventTarget
interface, and includes its methods.
read()
Promise
. When the data has been retrieved, the promise is resolved with a
DataTransfer
object that provides the data.
readText()
Promise
which is resolved with a
DOMString
containing the clipboard's text once it's available.
write()
Promise
.
writeText()
Promise
which is resolved once the text is fully copied into the clipboard.
The asynchronous clipboard API is a relatively recent addition, and the process of implementing it in browsers is not yet complete. Due to both potential security concerns and technical complexities, the process of integrating this API is happening gradually in most browsers.
For example, Firefox does not yet support the
"clipboard-read"
and
"clipboard-write"
permissions, so access to the methods that access and change the contents of the clipboard are restricted in other ways.
For WebExtensions, you can request the clipboardRead and clipboardWrite permissions to be able to use clipboard.readText() and clipboard.writeText(). Content scripts applied on HTTP sites do not have access to the clipboard object. See extensions in Firefox 63 .
此外,
read()
and
write()
are disabled by default and require changing a preference to enable them. Check the compatibility tables for each method before using it.
| 规范 | 状态 | 注释 |
|---|---|---|
|
Clipboard API and events
The definition of 'Clipboard' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Clipboard
|
Chrome 66 | Edge 79 | Firefox 63 | IE No | Opera 53 | Safari 13.1 | WebView Android 66 | Chrome Android 66 | Firefox Android 63 | Opera Android 47 | Safari iOS 13.4 | Samsung Internet Android 9.0 |
read
|
Chrome
部分支持
76
|
Edge 79 |
Firefox
63
Disabled
|
IE No | Opera 63 | Safari 13.1 |
WebView Android
部分支持
84
|
Chrome Android
部分支持
84
|
Firefox Android
63
Disabled
|
Opera Android 54 | Safari iOS 13.4 | Samsung Internet Android 12.0 |
readText
|
Chrome 66 | Edge 79 |
Firefox
63
|
IE No | Opera 53 | Safari 13.1 | WebView Android 66 | Chrome Android 66 |
Firefox Android
63
|
Opera Android 47 | Safari iOS 13.4 | Samsung Internet Android 9.0 |
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 |
writeText
|
Chrome 66 | Edge 79 |
Firefox
63
|
IE No | Opera 53 | Safari 13.1 | WebView Android 66 | Chrome Android 66 |
Firefox Android
63
|
Opera Android 47 | Safari iOS 13.4 | Samsung Internet Android 9.0 |
完整支持
部分支持
不支持
见实现注意事项。
用户必须明确启用此特征。
Clipboard