Clipboard API

剪贴板 Clipboard API 提供了响应剪贴板命令(剪切、复制和粘贴)与异步读写系统剪贴板的能力。从权限 Permissions API 获取权限之后,才能访问剪贴板内容;如果用户没有授予权限,则不允许读取或更改剪贴板内容。

该 API 被设计用来取代使用 document.execCommand() 的剪贴板访问方式。

访问剪贴板

除了在实例化中创建一个 Clipboard 对象,你还可以使用全局的 Navigator.clipboard 来访问系统剪贴板。

navigator.clipboard.readText().then(
  clipText => document.querySelector(".editor").innerText += clipText);

上述代码提取了剪贴板的文本并将其附在 class 为 editor 的第一个元素后面。因为当剪贴板中不是文本时, readText() (and read(), for that matter) 会返回一个空字符串,所以这段代码是安全的。

接口

Clipboard Secure context
提供了一个用于读写系统剪贴板上的文本和数据的接口。规范中被称为异步剪贴板 API(Async Clipboard API)。
ClipboardEvent Secure context
表示提供了有关剪贴板修改的信息的事件,也就是 cutcopy,和 paste。规范中被称为剪贴板事件 API(Clipboard Event API)。

规范

规范 状态 备注
Clipboard API and events Working Draft 初步定义

浏览器兼容性

Clipboard

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.

ClipboardEvent

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
ClipboardEvent
Experimental
Chrome Full support YesEdge Full support 12Firefox Full support 22IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
ClipboardEvent() constructor
Experimental
Chrome Full support 58Edge Full support ≤79Firefox Full support 22IE ? Opera Full support 45Safari ? WebView Android Full support 58Chrome Android Full support 58Firefox Android Full support 22Opera Android Full support 43Safari iOS ? Samsung Internet Android Full support 7.0
clipboardData
Experimental
Chrome Full support YesEdge Full support 12Firefox Full support 22IE Full support 5Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

参见