Clipboard API adds to the Navigator interface the read-only clipboard property, which returns the Clipboard object used to read and write the clipboard's contents. The Clipboard API can be used to implement cut, copy, and paste features within a web application.

Use of the asynchronous clipboard read and write methods requires that the user grant the web site or app permission to access the clipboard. This permission must be obtained from the 权限 API 使用 "clipboard-read" and/or "clipboard-write" permissions.

句法

theClipboard = navigator.clipboard;
					

Clipboard object used to access the system clipboard.

范例

The following code uses navigator.clipboard to access the system clipboard in order to read the contents of the clipboard.

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

This snippet replaces the contents of the element whose class is "cliptext" with the text contents of the clipboard. Perhaps this code is being used in a browser extension that displays the current clipboard contents, automatically updating periodically or when specific events fire.

If the clipboard is empty or doesn't contain text, the "cliptext" element's contents are cleared. This happens because readText() returns an empty string if the clipboard is empty or doesn't contain text.

规范

规范 状态 注释
Clipboard API and events
The definition of 'navigator.clipboard' in that specification.
工作草案 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
clipboard Chrome 66 Edge ≤79 Firefox 63 IE ? Opera Yes Safari 13.1 WebView Android 66 Chrome Android 66 Firefox Android 63 Opera Android Yes Safari iOS 13.4 Samsung Internet Android 9.0

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

元数据

  • 最后修改: