From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/clipboard/index.html | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 files/zh-cn/web/api/clipboard/index.html (limited to 'files/zh-cn/web/api/clipboard/index.html') diff --git a/files/zh-cn/web/api/clipboard/index.html b/files/zh-cn/web/api/clipboard/index.html new file mode 100644 index 0000000000..834376f01e --- /dev/null +++ b/files/zh-cn/web/api/clipboard/index.html @@ -0,0 +1,89 @@ +--- +title: Clipboard +slug: Web/API/Clipboard +tags: + - API + - Clipboard + - 剪切 + - 剪切板 + - 剪贴板 + - 参考 + - 复制 + - 接口 + - 粘贴 + - 编辑 +translation_of: Web/API/Clipboard +--- +
{{APIRef("Clipboard API")}}
+ +

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

+ +

系统剪贴板暴露于全局属性 {{domxref("Navigator.clipboard")}} 之中。

+ +

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

+ +
+

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

+
+ +

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

+ +
+

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

+ +

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

+
+ +

方法

+ +

Clipboard 继承自 {{domxref("EventTarget")}} 接口,因此拥有它的方法。

+ +
+
{{domxref("Clipboard.read()","read()")}}
+
从剪贴板读取数据(比如图片),返回一个 {{jsxref("Promise")}} 对象。When the data has been retrieved, the promise is resolved with a {{domxref("DataTransfer")}} object that provides the data。
+
{{domxref("Clipboard.readText()","readText()")}}
+
从操作系统读取文本;returns a Promise which is resolved with a {{domxref("DOMString")}} containing the clipboard's text once it's available。
+
{{domxref("Clipboard.write()","write()")}}
+
写入任意数据至操作系统剪贴板。This asynchronous operation signals that it's finished by resolving the returned Promise
+
{{domxref("Clipboard.writeText()","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

+ +

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

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Clipboard API','#clipboard-interface','Clipboard')}}{{Spec2('Clipboard API')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.Clipboard")}}

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf