🎉 How to Copy an Image or a Text to Clipboard in Javascript (2022) ☘️
Tags: javascript, news, webdev, tutorial
Published: 2020-06-11 11:02:37

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?

***

![Meme Studio](https://dev-to-uploads.s3.amazonaws.com/i/ulzdpv4fixbfa8g0nv97.gif)

***

It is very common to want to use libraries such as [Clipboard.js](https://clipboardjs.com). But, there’s a __new JavaScript API__ for asynchronous clipboard access with a [spec](https://www.w3.org/TR/clipboard-apis/#async-clipboard-api) 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](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#Browser_compatibility) 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 the `write` method.

* The `clipboard-read` permission controls access to the `read` 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.

![Clipboard Permissions](https://dev-to-uploads.s3.amazonaws.com/i/ot7ymvvo276sfyq8asa9.png)

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](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/read).

Below is a function that shows whether the user can copy text or an image:

![Clipboard ask Permissions](https://dev-to-uploads.s3.amazonaws.com/i/0u8d61ttq6825j2og27m.png)

## 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](https://caniuse.com/#search=clipboard%20API))**

#### 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.

![Copy Text Clipboard](https://dev-to-uploads.s3.amazonaws.com/i/zzfake78sf0bqwswcfn4.png)

#### 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.

![Copy Image Clipboard](https://dev-to-uploads.s3.amazonaws.com/i/1efkk3xsmer61vpzp7yq.png)

#### 2.3 One function to rule them all (compatibility `91.57%`)

![Copy Text/Image Clipboard](https://dev-to-uploads.s3.amazonaws.com/i/ons7jazm37qvgbd7dma6.png)

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](https://developers.google.com/web/updates/2018/03/clipboardapi) by Jason Miller ([@_developit](https://twitter.com/_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!