Hey guys 😃
A very popular feature on websites is the ability to copy an element to your clipboard. It's easy to find libraries to copy text, BUT what about images?
It is very common to want to use libraries such as Clipboard.js. But, there’s a new JavaScript API for asynchronous clipboard access with a spec that's is not complete, but quite advanced.
The Asynchronous Clipboard API
Note: The asynchronous Clipboard API methods are a recent addition to the specification, and may not be fully implemented to the specification in all browsers. Be sure to review the compatibility tables for each method before using them, to ensure that support is broad enough for your needs.
1. Clipboard Permissions
Because of the potential for abuse, two permissions are defined that allow user agents to give use control over how the Async APIs are used.
To avoid the risk of abuse and as a security measure, 2 permissions are required to use the Clipboard API correctly.
The
clipboard-write
permission controls access to thewrite
method.The
clipboard-read
permission controls access to theread
method.
In this article, we will only focus on the copy
feature to be able to copy a text or an image to the clipboard.
As with many new APIs, navigator.clipboard
is only supported for pages served over HTTPS. To help prevent abuse, clipboard access is only allowed when a page is the active tab.
NOTE: Pages in active tabs can write
to the clipboard without requesting permission.
If you want to access to the data from the clipboard, see here.
Below is a function that shows whether the user can copy text or an image:
2. Copy to clipboard
To copy an item to the clipboard such as image or text, nothing could be simpler. The Clipboard API provides the write()
and writeText()
(only works for text) methods .
Warning: Not all browsers support the methods suggested below. So be careful if you want to use them in production. See here for current compatibility)
2.1 Copy a text (compatibility 94.59%
)
To copy text to the clipboard, call navigator.clipboard.writeText()
. A Promise is resolved once the clipboard's contents have been updated. The promise is rejected if the caller does not have permission to write to the clipboard.
2.2 Copy an image (compatibility 91.57%
)
To copy an image to the clipboard, call navigator.clipboard.write()
(that is the same as the writeText
method but it's more generic and also works for copying text).
Pass an array of ClipboardItem
objects as a parameter to the write()
method.
CAUTION: At the time of writing, only PNG files are supported and you can only pass one image to the clipboard.
2.3 One function to rule them all (compatibility 91.57%
)
That's it, if you want to support certain browsers, you will have to be patient before using this new API (almost finished !)
You can also learn more about the Asynchronous Clipboard API at this article by Jason Miller (@_developit).
Examples:
Example 1: https://copy-to-clipboard.now.sh
Example 2: https://www.meme-studio.io
Source code :
https://gist.github.com/viclafouch/36d3edf58633a25c8b973588cc13480e
Cheers!
Top comments (4)
On the navigator.clipboard.write line I'm getting the following:
Type 'Blob' is missing the following properties from type 'Promise': then, catch, finally, [Symbol.toStringTag] from ts(2739)
Do you have any pointers?
I'm having the same issue, did you find a solution?
Clipboard API write() is not compatible with Firefox, the only way to be able to copy an image is using the deprecated execCommand() function:
Found this blog super helpful!! :)