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/read/index.html | 84 +++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 files/zh-cn/web/api/clipboard/read/index.html (limited to 'files/zh-cn/web/api/clipboard/read') diff --git a/files/zh-cn/web/api/clipboard/read/index.html b/files/zh-cn/web/api/clipboard/read/index.html new file mode 100644 index 0000000000..7b0ffa7bda --- /dev/null +++ b/files/zh-cn/web/api/clipboard/read/index.html @@ -0,0 +1,84 @@ +--- +title: Clipboard.read() +slug: Web/API/Clipboard/read +tags: + - API + - Clip + - Clipboard + - Clipboard API + - read +translation_of: Web/API/Clipboard/read +--- +
{{APIRef("Clipboard API")}}
+ +

The read() method of the {{domxref("Clipboard")}} interface requests a copy of the clipboard's contents, delivering the data to the returned {{jsxref("Promise")}} when the promise is resolved. Unlike {{domxref("Clipboard.readText", "readText()")}}, the read() method can return arbitrary data, such as images.

+ +

To read from the clipboard, you must first have the "clipboard-read" permission.

+ +
+

Note: The asynchronous Clipboard and Permissions APIs are still in the process of being integrated into most browsers, so they often deviate from the official rules for permissions and the like. Be sure to review the {{anch("Browser compatibility", "compatibility table")}} before using these methods.

+
+ +

语法

+ +
var promise = navigator.clipboard.read();
+ +

Parameters

+ +

None.

+ +

Return value

+ +

A {{jsxref("Promise")}} that resolves with a {{domxref("DataTransfer")}} object containing the clipboard's contents. The promise is rejected if permission to access the clipboard is not granted.

+ +

例子

+ +

After using {{domxref("Permissions.query", "navigator.permissions.query()")}} to find out if we have (or if the user will be prompted to allow) "clipboard-read" access, this example fetches the data currently on the clipboard. If it's not plain text, an error message is presented. Otherwise, an element referred to using the variable textElem has its contents replaced with the clipboard's contents.

+ +
// First, ask the Permissions API if we have some kind of access to
+// the "clipboard-read" feature.
+
+navigator.permissions.query({name: "clipboard-read"}).then(result => {
+  // If permission to read the clipboard is granted or if the user will
+  // be prompted to allow it, we proceed.
+
+  if (result.state == "granted" || result.state == "prompt") {
+    navigator.clipboard.read().then(data => {
+      for (let i=0; i<data.items.length; i++) {
+        if (data.items[i].type != "text/plain") {
+          alert("Clipboard contains non-text data. Unable to access it.");
+        } else {
+          textElem.innerText = data.items[i].getAs("text/plain");
+        }
+      }
+    });
+  }
+});
+
+ +
+

Note: At this time, while Firefox does implement read(), it does not recognize the "clipboard-read" permission, so attempting to use the Permissions API to manage access to the API will not work.

+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Clipboard API','#h-clipboard-read','read()')}}{{Spec2('Clipboard API')}}Initial definition.
+ +

浏览器兼容性

+ + + +

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

-- cgit v1.2.3-54-g00ecf