- Asynchronous Clipboard API
This text introduces these three strategies one after the other. That is my thirty seventh Medium article.
Doc.execCommand() is the normal technique of manipulating the clipboard, which is supported by numerous browsers. It helps the three operations of copy, lower, and paste.
Copy or Reduce operation
When copying, first choose the textual content after which name the
Doc.execCommand('copy'), the chosen textual content will enter the clipboard.
Within the above instance, the script first selects the textual content within the
inputElement of the enter field (
inputElement.choose() ), after which
Doc.execCommand('copy') copies it to the clipboard. Word that the copy operation is finest positioned within the occasion listener perform, triggered by the consumer (for instance, the consumer clicks a button). If the script is executed autonomously, some browsers might report an error. Reduce operation can be just like the copy operation.
When pasting, calling
Doc.execCommand('paste') will output the contents of the clipboard to the present focus component.
Doc.execCommand() technique is handy, it has some disadvantages. First, it will possibly solely copy the chosen content material to the clipboard, and can’t write content material to the clipboard arbitrarily. Secondly, it’s an asynchronous operation. When you copy/paste a considerable amount of knowledge, the web page will freeze. Some browsers may even pop up a immediate field and ask the consumer for permission. At the moment, the web page will turn into unresponsive earlier than the consumer makes a alternative. With a view to remedy these issues, browser distributors have proposed an asynchronous Clipboard API.
Clipboard API is the next-generation clipboard operation technique, which is extra highly effective and cheap than the normal
Doc.execCommand() technique. All its operations are asynchronous and return Promise objects with out inflicting web page jams. Furthermore, it will possibly put arbitrary content material (akin to photos) into the clipboard. The
navigator.clipboard property returns the Clipboard object, and all operations are carried out via this object.
const clipboardObj = navigator.clipboard;
navigator.clipboard property returns
undefined, it signifies that the present browser doesn’t assist this API (you may see the total compatibly desk on Can I exploit…). Since customers might put delicate knowledge (akin to passwords) on the clipboard, permitting scripts to learn them arbitrarily will trigger safety dangers, so this API has extra safety restrictions. To start with, the Chrome browser stipulates that solely HTTPS protocol pages can use this API. Nonetheless, the event setting (
localhost) permits using non-encrypted protocols. Secondly, the consumer’s permission must be clearly obtained when calling. The precise implementation of permissions makes use of the Permissions API. There are two permissions associated to the clipboard:
clipboard-write (write permission) and
clipboard-read (learn permission). The “write permission” is routinely granted to the script, and the “learn permission” should be explicitly granted by the consumer. In different phrases, the script will be routinely accomplished when writing to the clipboard, however when studying the clipboard, the browser will pop up a dialog field asking whether or not the consumer agrees to learn.
As well as, it ought to be famous that what the script reads is all the time the clipboard of the present web page. One downside that this brings is that when you paste the related code into the developer software and run it straight, an error could also be reported as a result of the present web page at the moment is the window of the developer software, not an internet web page.
When you paste the above code into the developer software and run it, an error will probably be reported. As a result of when the code is working, the developer software window is the present web page, and there’s no DOM interface that the Clipboard API is dependent upon this web page. One resolution is to place the related code in setTimeout() to delay working, and shortly click on on the web page window of the browser earlier than calling the perform to show it into the present web page.
After the above code is pasted into the developer software to run, shortly click on on the web page window of the webpage to make it the present web page, in order that no error will probably be reported.
clipboard.readText() technique is used to repeat the textual content knowledge within the clipboard.
Within the above instance, after the consumer clicks on the web page, the textual content within the clipboard will probably be output. Word that the browser will pop up a dialog field at the moment, asking the consumer whether or not to agree with the script to learn the clipboard.
If the consumer disagrees, the script will report an error. At the moment, you should utilize the
attempt...catch construction to deal with errors.
clipboard.learn() technique is used to repeat the info within the clipboard, which will be textual content knowledge or binary knowledge (akin to photos). This technique requires specific permission from the consumer. This technique returns a Promise object. As soon as the state of the thing turns into resolved, an array will be obtained, and every array member is an occasion of a ClipboardItem object.
The ClipboardItem object represents a single clip merchandise and every clip merchandise has a
clipboardItem.sorts property and a
clipboardItem.getType() technique. The
clipboardItem.sorts property returns an array whose members are the MIME sorts out there for the clip merchandise. For instance, a clip merchandise will be pasted in HTML format or in plain textual content format. Then it has two MIME sorts (
textual content/html and
textual content/plain). The
clipboardItem.getType(kind) technique is used to learn the info of the clip merchandise and returns a Promise object. This technique accepts the MIME kind of the clip merchandise as a parameter and returns the info of that kind. This parameter is required, in any other case, an error will probably be reported.
clipboard.writeText() technique is used to jot down textual content content material to the clipboard.
The above instance is that after the consumer clicks on the internet web page, the script writes textual content knowledge to the clipboard. This technique doesn’t require consumer permission, however it’s best to place it in
attempt...catch to forestall errors.
clipboard.write() technique is used to jot down arbitrary knowledge to the clipboard, which will be textual content knowledge or binary knowledge. This technique accepts a ClipboardItem occasion as a parameter, which represents the info written to the clipboard.
Within the above instance, the script writes an image to the clipboard. Word that the Chrome browser at the moment (till this author writes this text) solely helps writing photos in PNG format.
clipboardItem() is a constructor natively offered by the browser to generate an occasion of
clipboardItem. It accepts an object as a parameter. The important thing identify of the thing is the MIME kind of the info, and the important thing worth is the info itself. The next instance is to jot down the worth of the identical clip merchandise in a number of codecs to the clipboard, one is textual content knowledge, and the opposite is binary knowledge for pasting on completely different events.
When the consumer places knowledge into the clipboard, the
copy occasion will probably be triggered. The next instance is to transform the textual content that the consumer places on the clipboard to uppercase.
Within the above instance, the
clipboardData property of the occasion object comprises the clipboard knowledge. It’s an object with the next properties and strategies.
Occasion.clipboardData.setData(kind, knowledge): To change the clipboard knowledge, it’s essential specify the info kind.
Occasion.clipboardData.getData(kind): To acquire clipboard knowledge, it’s essential specify the info kind.
Occasion.clipboardData.clearData([type]): Clear clipboard knowledge, you may specify the info kind. If you don’t specify the sort, all varieties of knowledge will probably be cleared.
Occasion.clipboardData.objects: An array-like object comprises all clip objects, however often there is just one clip merchandise
The next instance is to intercept the consumer’s copy operation and put the required content material into the clipboard.
Within the above instance, first, use
e.preventDefault() to cancel the default operation of the clipboard, after which the script takes over the copy operation. The
lower occasion is triggered when the consumer performs a reducing operation. Its processing is precisely the identical because the
copy occasion, and the lower knowledge can be obtained from the
When the consumer makes use of the clipboard knowledge to stick, the
paste occasion will probably be triggered. The next instance is to intercept the paste operation, the info within the clipboard is taken out by the script.