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 website or app permission to access the clipboard. This permission must be obtained
from the Permissions API using the
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
element's contents are cleared. This happens because
readText() returns an empty string if the clipboard is empty or doesn't contain
|Clipboard API and events |
BCD tables only load in the browser