- 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 methodology of manipulating the clipboard, which is supported by numerous browsers. It helps the three operations of copy, lower, and paste.
Copy or Minimize 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. Notice that the copy operation is greatest positioned within the occasion listener perform, triggered by the person (for instance, the person clicks a button). If the script is executed autonomously, some browsers could report an error. Minimize operation can also be just like the copy operation.
When pasting, calling
Doc.execCommand('paste') will output the contents of the clipboard to the present focus aspect.
Doc.execCommand() methodology is handy, it has some disadvantages. First, it could actually 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. Should you copy/paste a considerable amount of information, the web page will freeze. Some browsers can even pop up a immediate field and ask the person for permission. At the moment, the web page will turn out to be unresponsive earlier than the person makes a selection. So as to clear up these issues, browser distributors have proposed an asynchronous Clipboard API.
Clipboard API is the next-generation clipboard operation methodology, which is extra highly effective and affordable than the normal
Doc.execCommand() methodology. All its operations are asynchronous and return Promise objects with out inflicting web page jams. Furthermore, it could actually put arbitrary content material (equivalent to photos) into the clipboard. The
navigator.clipboard property returns the Clipboard object, and all operations are carried out by means of this object.
const clipboardObj = navigator.clipboard;
navigator.clipboard property returns
undefined, it implies that the present browser doesn’t help this API (you possibly can see the complete compatibly desk on Can I exploit…). Since customers could put delicate information (equivalent 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 atmosphere (
localhost) permits the usage of non-encrypted protocols. Secondly, the person’s permission must be clearly obtained when calling. The particular 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 robotically granted to the script, and the “learn permission” have to be explicitly granted by the person. In different phrases, the script might be robotically accomplished when writing to the clipboard, however when studying the clipboard, the browser will pop up a dialog field asking whether or not the person agrees to learn.
As well as, it must be famous that what the script reads is at all times the clipboard of the present web page. One drawback that this brings is that when you paste the related code into the developer instrument and run it instantly, an error could also be reported as a result of the present web page presently is the window of the developer instrument, not an online web page.
Should you paste the above code into the developer instrument and run it, an error shall be reported. As a result of when the code is working, the developer instrument window is the present web page, and there’s no DOM interface that the Clipboard API will depend on this web page. One resolution is to place the related code in setTimeout() to delay working, and rapidly 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 instrument to run, rapidly 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.readText() methodology is used to repeat the textual content information within the clipboard.
Within the above instance, after the person clicks on the web page, the textual content within the clipboard shall be output. Notice that the browser will pop up a dialog field presently, asking the person whether or not to agree with the script to learn the clipboard.
If the person disagrees, the script will report an error. At the moment, you need to use the
strive...catch construction to deal with errors.
clipboard.learn() methodology is used to repeat the information within the clipboard, which might be textual content information or binary information (equivalent to photos). This methodology requires express permission from the person. This methodology returns a Promise object. As soon as the state of the item turns into resolved, an array might 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.varieties property and a
clipboardItem.getType() methodology. The
clipboardItem.varieties property returns an array whose members are the MIME varieties obtainable for the clip merchandise. For instance, a clip merchandise might be pasted in HTML format or in plain textual content format. Then it has two MIME varieties (
textual content/html and
textual content/plain). The
clipboardItem.getType(kind) methodology is used to learn the information of the clip merchandise and returns a Promise object. This methodology accepts the MIME kind of the clip merchandise as a parameter and returns the information of that kind. This parameter is required, in any other case, an error shall be reported.
clipboard.writeText() methodology is used to put in writing textual content content material to the clipboard.
The above instance is that after the person clicks on the internet web page, the script writes textual content information to the clipboard. This methodology doesn’t require person permission, however it’s best to place it in
strive...catch to forestall errors.
clipboard.write() methodology is used to put in writing arbitrary information to the clipboard, which might be textual content information or binary information. This methodology 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. Notice that the Chrome browser presently (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 title of the item is the MIME kind of the information, and the important thing worth is the information itself. The next instance is to put in writing 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 totally different events.
When the person places information into the clipboard, the
copy occasion shall be triggered. The next instance is to transform the textual content that the person places on the clipboard to uppercase.
Within the above instance, the
clipboardData property of the occasion object accommodates the clipboard information. It’s an object with the next properties and strategies.
Occasion.clipboardData.setData(kind, information): To change the clipboard information, you have to specify the information kind.
Occasion.clipboardData.getData(kind): To acquire clipboard information, you have to specify the information kind.
Occasion.clipboardData.clearData([type]): Clear clipboard information, you possibly can specify the information kind. If you don’t specify the sort, all varieties of information shall be cleared.
Occasion.clipboardData.objects: An array-like object accommodates all clip objects, however often there is just one clip merchandise
The next instance is to intercept the person’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 person performs a chopping operation. Its processing is precisely the identical because the
copy occasion, and the lower information can also be obtained from the
When the person makes use of the clipboard information to stick, the
paste occasion shall be triggered. The next instance is to intercept the paste operation, the information within the clipboard is taken out by the script.