Thursday, July 25, 2024

Discover Clipboard Operation in JavaScript | by Sabesan Sathananthan | Geek Tradition

Clipboard API is the next-generation clipboard operation technique, which is extra highly effective and affordable than the normal Doc.execCommand() technique. All its operations are asynchronous and return Promise objects with out inflicting web page jams. Furthermore, it may possibly put arbitrary content material (comparable 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;

If the navigator.clipboard property returns undefined, it implies that the present browser doesn’t help this API (you may see the complete compatibly desk on Can I take advantage of…). Since customers could put delicate information (comparable to passwords) on the clipboard, permitting scripts to learn them arbitrarily will trigger safety dangers, so this API has extra safety restrictions. To begin with, the Chrome browser stipulates that solely HTTPS protocol pages can use this API. Nevertheless, 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 could 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.

The permission immediate for the Clipboard API.

As well as, it must 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 in the event you paste the related code into the developer device 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 device, not an internet web page.

Should you paste the above code into the developer device and run it, an error shall be reported. As a result of when the code is working, the developer device window is the present web page, and there’s no DOM interface that the Clipboard API will depend on this web page. One answer 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 operate to show it into the present web page.

After the above code is pasted into the developer device 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 shall be reported.

Clipboard object


The clipboard.readText() technique is used to repeat the textual content information within the clipboard.

Within the above instance, after the consumer clicks on the web page, the textual content within the clipboard shall 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 use the attempt...catch construction to deal with errors.


The clipboard.learn() technique is used to repeat the information within the clipboard, which could be textual content information or binary information (comparable to photos). This technique requires express permission from the consumer. This technique returns a Promise object. As soon as the state of the thing turns into resolved, an array could 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 obtainable for the clip merchandise. For instance, a clip merchandise could 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(sort) technique is used to learn the information of the clip merchandise and returns a Promise object. This technique accepts the MIME sort of the clip merchandise as a parameter and returns the information of that sort. This parameter is required, in any other case, an error shall be reported.


The clipboard.writeText() technique is used to write 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 information to the clipboard. This technique doesn’t require consumer permission, however it’s best to place it in attempt...catch to forestall errors.


The clipboard.write() technique is used to write down arbitrary information to the clipboard, which could be textual content information or binary information. This technique accepts a ClipboardItem occasion as a parameter, which represents the information written to the clipboard.

Within the above instance, the script writes an image to the clipboard. Word that the Chrome browser presently (till this author writes this text) solely helps writing photographs in PNG format. clipboardItem() is a constructor natively supplied by the browser to generate an occasion of clipboardItem. It accepts an object as a parameter. The important thing title of the thing is the MIME sort of the information, and the important thing worth is the information itself. The next instance is to write down the worth of the identical clip merchandise in a number of codecs to the clipboard, one is textual content information, and the opposite is binary information for pasting on completely different events.

Related Articles


Please enter your comment!
Please enter your name here

Stay Connected

- Advertisement -spot_img

Latest Articles