Clipboard

Clipboard 接口实现了 Clipboard API,如果用户授予了相应的权限,就能提供系统剪贴板的读写访问。在 Web 应用程序中,Clipboard API 可用于实现剪切、复制和粘贴功能。

系统剪贴板暴露于全局属性 Navigator.clipboard 之中。

如果用户没有适时使用 Permissions API 授予相应权限和"clipboard-read" 或 "clipboard-write" 权限,调用 Clipboard 对象的方法不会成功。

注意:实际上,现在浏览器对于访问剪贴板权限的索取各有不同,在章节 剪贴板可用性 查看更多细节。

所有剪贴板 API 方法都是异步的;它们返回一个 Promise 对象,在剪贴板访问完成后被执行。如果剪贴板访问被拒绝,promise 也会被拒绝。

剪贴板 是用于短期数据储存或转移的数据缓存区,数据转移可以发生在文档和应用程序之间。剪贴板常常实现为一个匿名的、临时的 数据缓存,有时也叫做粘贴缓存,可由绝大部分位于已定义编程接口的环境中的程序访问。

一个典型的应用程序常通过将 用户输入 如 组合键, 菜单选择 等映射到这些接口来访问剪贴板。

方法

Clipboard 继承自 EventTarget 接口,因此拥有它的方法。

read()
从剪贴板读取数据(比如图片),返回一个 Promise 对象。When the data has been retrieved, the promise is resolved with a DataTransfer object that provides the data。
readText()
从操作系统读取文本;returns a Promise which is resolved with a DOMString containing the clipboard's text once it's available。
write()
写入任意数据至操作系统剪贴板。This asynchronous operation signals that it's finished by resolving the returned Promise
writeText()
写入文本至操作系统剪贴板。returning a Promise which is resolved once the text is fully copied into the clipboard。

剪贴板可用性

异步剪贴板 API 是一个相对较新的 API,浏览器仍在逐渐实现它。由于潜在的安全问题和技术复杂性,大多数浏览器正在逐步集成这个 API。

例如,Firefox 不支持 "clipboard-read" 和 "clipboard-write" 权限,所以使用其他方式访问或改变剪贴板中的内容会受限。

对于浏览器扩展来说,你可以要求 clipboardRead 和 clipboardWrite 权限以使用 clipboard.readText() 和 clipboard.writeText()。但基于 HTTP 的网站中包含的脚本则不能获得剪贴板对象。参考 extensions in Firefox 63

除此之外, read() 以及write() 是默认禁用且需要修改偏好设置来启用的。在使用之前请先确认浏览器兼容性表格。

规范

规范 状态 备注
Clipboard API and events
Clipboard
Working Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
ClipboardChrome Full support 66Edge Full support 79Firefox Full support 63IE No support NoOpera Full support 53Safari Full support 13.1WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 63Opera Android Full support 47Safari iOS Full support 13.4Samsung Internet Android Full support 9.0
readChrome Partial support 76
Notes
Partial support 76
Notes
Notes From version 76, the image/png MIME type is supported.
Partial support 66
Notes
Notes Images are not supported.
Edge Full support 79Firefox Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Currently works just like readText(); non-text content is not currently supported.
Disabled From version 63: this feature is behind the dom.events.asyncClipboard.dataTransfer preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 63Safari Full support 13.1WebView Android Partial support 84
Notes
Partial support 84
Notes
Notes From version 84, the image/png MIME type is supported.
Partial support 66
Notes
Notes Images are not supported.
Chrome Android Partial support 84
Notes
Partial support 84
Notes
Notes From version 84, the image/png MIME type is supported.
Partial support 66
Notes
Notes Images are not supported.
Firefox Android Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Currently works just like readText(); non-text content is not currently supported.
Disabled From version 63: this feature is behind the dom.events.asyncClipboard.dataTransfer preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 54Safari iOS Full support 13.4Samsung Internet Android Full support 12.0
readTextChrome Full support 66Edge Full support 79Firefox Full support 63
Notes
Full support 63
Notes
Notes Firefox only supports reading the clipboard in browser extensions, using the "clipboardRead" extension permission.
IE No support NoOpera Full support 53Safari Full support 13.1WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 63
Notes
Full support 63
Notes
Notes Firefox only supports reading the clipboard in browser extensions, using the "clipboardRead" extension permission.
Opera Android Full support 47Safari iOS Full support 13.4Samsung Internet Android Full support 9.0
writeChrome Full support 66
Notes
Full support 66
Notes
Notes From version 76, the image/png MIME type is supported.
Edge Full support 79Firefox Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Currently works exactly like writeText(), including the availability limitations currently imposed by Firefox.
Disabled From version 63: this feature is behind the dom.events.asyncClipboard.dataTransfer preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 63Safari Full support 13.1WebView Android Full support 66
Notes
Full support 66
Notes
Notes From version 84, the image/png MIME type is supported.
Chrome Android Full support 66
Notes
Full support 66
Notes
Notes From version 84, the image/png MIME type is supported.
Firefox Android Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Currently works exactly like writeText(), including the availability limitations currently imposed by Firefox.
Disabled From version 63: this feature is behind the dom.events.asyncClipboard.dataTransfer preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 54Safari iOS Full support 13.4Samsung Internet Android Full support 12.0
writeTextChrome Full support 66Edge Full support 79Firefox Full support 63
Notes
Full support 63
Notes
Notes Writing to the clipboard is available without permission in secure contexts and browser extensions, but only from user-initiated event callbacks. Browser extensions with the "clipboardWrite" permission can write to the clipboard at any time.
IE No support NoOpera Full support 53Safari Full support 13.1WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 63
Notes
Full support 63
Notes
Notes Writing to the clipboard is available without permission in secure contexts and browser extensions, but only from user-initiated event callbacks. Browser extensions with the "clipboardWrite" permission can write to the clipboard at any time.
Opera Android Full support 47Safari iOS Full support 13.4Samsung Internet Android Full support 9.0

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

参见