Clipboard.readText()

Baseline 2024

Newly available

Since June 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Clipboard 接口的 readText() 方法解析系统剪贴板的文本内容返回一个Promise

语法

var promise = navigator.clipboard.readText()

参数

None.

返回值

A Promise that resolves with a DOMString containing the textual contents of the clipboard. Returns an empty string if the clipboard is empty, does not contain text, or does not include a textual representation among the DataTransfer objects representing the clipboard's contents.

要从剪贴板中读取非文本内容,请改用read()方法。你可以使用 writeText()将文本写入剪贴板

例子

此示例检索剪贴板的文本内容,并将返回的文本插入元素的内容中。

js
navigator.clipboard
  .readText()
  .then((clipText) => (document.getElementById("outbox").innerText = clipText));

规范

Specification
Clipboard API and events
# dom-clipboard-readtext

浏览器兼容性

BCD tables only load in the browser

See also