Navigator.clipboard - Web APIs 编辑
The 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 Permissions API using the "clipboard-read"
and/or "clipboard-write"
permissions.
Syntax
theClipboard = navigator.clipboard;
Value
The Clipboard
object used to access the system clipboard.
Examples
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.
Specifications
Specification | Status | Comment |
---|---|---|
Clipboard API and events The definition of 'navigator.clipboard' in that specification. | Working Draft | Initial definition. |
Browser compatibility
BCD tables only load in the browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论