In Safari browser, you can directly use navigator.clipboard.writeText to copy text without any issues. However, if you use navigator.clipboard.writeText after a promise, it will throw a permission error. The specific error is as follows:
Reasons:
- The copied content in development is requested through an interface, which Safari rejects. Due to security restrictions, the above method only works when the user interacts with the page. If your copy operation is not triggered by the user (for example, through a timer or other non-interactive means), it may be restricted by the browser.
- Accessing the clipboard through
document.execCommand()is synchronous and can only read and write the DOM.
Clipboard API#
A new asynchronous copy and paste API. It provides (if the user grants permission) access to read and write system clipboard content. The Clipboard API can be used to implement cut, copy, and paste functionality in web applications by simply calling the
writeTextmethod to successfully copy a piece of text. This API is intended to replace accessing the clipboard using document.execCommand.
Copy: Writing content to the clipboard#
writeText()
To write text to the system clipboard, you can call writeText(). The text will be successfully copied and can be pasted using Ctrl+V.
write()
In fact, writeText() is just a shorthand for the generic write() method, which also allows you to copy images to the clipboard. After successfully copying an image, it needs to be read using the read() method. The write() method accepts an array of ClipboardItem objects as a parameter. To create a new ClipboardItem object, you need to use the MIME type as the key and a Blob as the value.
Currently, support for multiple ClipboardItems is not implemented, but support for multiple key-value pairs of data is supported.
Note: Since the Clipboard API is still an experimental API, it behaves differently in Safari browser.
- It must be in an HTTPS environment to be debugged and used. Otherwise, an error will occur: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
- The
blobproperty value ofClipboardItemneeds to return a Promise.
Paste: Reading data from the clipboard#
readText()
To read text from the clipboard, you can call readText().
read()
read() returns a list of ClipboardItems objects. To read images from the clipboard, you can iterate through the list of ClipboardItems to retrieve them.
Event Listening#
The Clipboard API inherits from EventTarget (a DOM interface implemented by objects that can receive events and create listeners), so it also has event listening capabilities. Currently, it has not been discovered how to listen for events, so you can use the previous document.addEventListener for listening.
This article is synchronized and updated to xLog by Mix Space
The original link is https://liu-wb.com/posts/default/clipboard