The State of Copy-Pasting in JavaScript
Tags: javascript, webdev
Published: 2022-01-15 22:55:34

---
title: "The State of Copy-Pasting in JavaScript"
published: true
description: Open the hood and you'll find it's not in great shape.
tags: ["JavaScript", "WebDev"]
cover_image: "https://bbss.dev/images/copy-pasting-javascript/cover.png"
canonical_url: "https://www.bbss.dev/posts/copy-pasting-javascript/"
---

I recently did some work on [a VS Code extension](https://github.com/telesoho/vscode-markdown-paste-image) whose purpose is to handle rich pastes. It prompted me to survey different copy-pasting libraries and the state of the NPM ecosystem as a whole.

## How do clipboards work?

Clipboards across different operating systems work essentially the same. For this reason, we will stick to looking at a single one -- Windows.

A common misconception about how clipboards work is that they contain a single piece of data, such as text or an image. In reality, clipboards hold all the ways target software can represent the data.

Consider if I copy the following [webpage](https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_headers):

![Headings](https://bbss.dev/images/copy-pasting-javascript/headings.png)
_HTML headings as rendered by Firefox_

When pasting it into Microsoft Word, it appears formatted as rich content:

![Microsoft Word](https://bbss.dev/images/copy-pasting-javascript/rich-word.png)
_Microsoft Word displays rich content exactly as Firefox does_

When pasting it into Notepad++, it appears as plain text:

![Notepad++](https://bbss.dev/images/copy-pasting-javascript/notepad.png)
_Notepad++ does not handle rich content_

Pasting different content types depending on the software is possible because Firefox provides multiple representations of the rendered content to the clipboard.

Let's see what types the clipboard is holding using Powershell:

```powershell
$dataObj = [System.Windows.Forms.Clipboard]::GetDataObject()
foreach ($fmt in $dataObj.GetFormats()) {
    [Console]::WriteLine($fmt)
}

## Output: ##
HTML Format
System.String
UnicodeText
Text
Chromium Web Custom MIME Data Format
Locale
OEMText
```

We can look inside some of the clipboard types using Powershell, mainly `HTML Format` and `Text` with the following commands:

```powershell
# Get Text
[System.Windows.Forms.Clipboard]::GetText([System.Windows.Forms.TextDataFormat]::Text)

## Output: ##
This is heading 1
This is heading 2
This is heading 3
This is heading 4
This is heading 5
This is heading 6

# Get HTML Format
[System.Windows.Forms.Clipboard]::GetText([System.Windows.Forms.TextDataFormat]::Text)

## Output: ##
Version:0.9
StartHTML:00000174
EndHTML:00000410
StartFragment:00000208
EndFragment:00000374
SourceURL:https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_headers
<html><body>
<!--StartFragment--><h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<h5>This is heading 5</h5>
<h6>This is heading 6</h6><!--EndFragment-->
</body>
</html>
```

The way to approach clipboards as a consumer is to consume the clipboard formats you know how to, prioritizing some over others. For example, Microsoft Word will prioritize pasting `HTML Format` formats over `Text` formats by default.

While Powershell has some built-in parsers, implementing the format specification for every type of content is a lot of work. Are there libraries that can help us with this?

## Copy-Pasting in the NPM Ecosystem

The simplest clipboard library would let us query the clipboard directly for binary data. This approach would require us to a parser per format per operating system. While such libraries deserve a place in this world, they are not helpful for most applications. Most applications want to handle copying and pasting only for text, rich text, images, and sometimes files. Let's go through the different clipboard management libraries and see how they fare.

We are looking for a library that has the following features:

- Pure JS, with native bindings
- Cross-Platform Support
  - Windows
  - Linux
  - MacOS
- Copy to clipboard
  - Plain Text
  - Rich Text
  - Images
  - Files
- Paste from clipboard
  - Plain Text
  - Rich Text
  - Images
  - Files

Here is a table I made from the collected NPM packages, featuring the most popular clipboard-related libraries on NPM:

Note: This table does not include browser-based copy-paste.

<table>
<tr>
  <th>
  Features / Libraries
  </th>
  <th>
    <a rel="nofollow" href="https://www.npmjs.com/package/clipboardy">clipboardy</a>
  </th>
  <th>
    <a rel="nofollow" href="https://www.npmjs.com/package/copy-paste">copy-paste</a>
  </th>
  <th>
    <a rel="nofollow" href="https://www.npmjs.com/package/clipboard-cli">clipboard-cli</a>
  </th>
  <th>
    <a href="https://www.npmjs.com/package/node-clipboard-wd">node-clipboard-wd</a>
  </th>
  <th>
    <a rel="nofollow" href="https://www.electronjs.org/docs/latest/api/clipboard">Electron</a> (Not a library)
  </th>
</tr>
<tr>
  <th>
  Pure JS
  </th>
  <td style="color: red">
  Uses system utilties
  </td>
  <td style="color: red">
  Uses system utilties
  </td>
  <td style="color: red">
  Uses system utilties
  </td>
  <td style="color: yellow">
  Chromium binary
  </td>
  <td style="color: lightgreen">
  Uses native bindings
  </td>
</tr>
<tr>
  <th>
  Cross-Platform Support
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Windows
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Linux
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  MacOS
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Copy to Clipboard
  </th>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Plain Text
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Rich Text
  </th>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Images
  </th>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Files
  </th>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: yellow">
  Exposes binary API
  </td>
</tr>
<tr>
  <th>
  Paste from Clipboard
  </th>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Plain Text
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Rich Text
  </th>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Images
  </th>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Files
  </th>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: yellow">
  Exposes binary API
  </td>
</tr>

</table>

Honorable mention: [save-clipboard-image](https://www.npmjs.com/package/save-clipboard-image), uses AppleScript to save an image from clipboard to a file.

### Summary

Before writing this post, I was unaware that the result would be this. It appears that all of NPM's clipboard libraries work the same way: they call built-in executables on their host operating system and return the data. Interestingly, none of them handle images, rich text, or files despite no reason they couldn't deal with them the same way.

The only exceptions to this pattern are Electron and [`node-clipboard-wd`](https://github.com/knyzorg/node-clipboard-wd). The former is a framework unsuitable for usage as a library. The latter was written by myself last week as an experiment.

## Where do we go from here?

Software developers have crossed the clipboard bridge in other environments before. In fact, NodeJS developers have _already_ crossed it in Electron, which uses native bindings under the hood. There is no reason we couldn't do the same using a more lightweight C++ library.

I find [clip](https://github.com/dacap/clip) particularly promising in this regard, and I've been looking into writing a NodeJS wrapper around it. For posterity, here is a table of possible native libraries that I have found so far:

<table>
<tr>
  <th>
  Features / Libraries
  </th>
  <th>
    <a rel="nofollow" href="https://github.com/dacap/clip">clip</a>
  </th>
  <th>
    <a rel="nofollow" href="https://crates.io/crates/arboard">arboard</a>
  </th>
  <th>
    <a href="https://github.com/golang-design/clipboard" rel="nofollow">clipboard</a>
  </th>
</tr>
<tr>
  <th>
  Cross-Platform Support
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Windows
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Linux
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  MacOS
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Copy to Clipboard
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: yellow">
  Partial
  </td>
</tr>
<tr>
  <th>
  Plain Text
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Rich Text
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
</tr>
<tr>
  <th>
  Images
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Files
  </th>
  <td style="color: yellow">
  Exposes binary API
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
</tr>
<tr>
  <th>
  Paste from Clipboard
  </th>
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: yellow">
  Partial
  </td>
  <td style="color: yellow">
  Partial
  </td>
</tr>
<tr>
  <th>
  Plain Text
  </th>
  <td style="color: lightgreen">
  Yes
  </td>  
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Rich Text
  </th>
  <td style="color: lightgreen">
  Yes
  </td>  
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
</tr>
<tr>
  <th>
  Images
  </th>
  <td style="color: lightgreen">
  Yes
  </td> 
  <td style="color: lightgreen">
  Yes
  </td>
  <td style="color: lightgreen">
  Yes
  </td>
</tr>
<tr>
  <th>
  Files
  </th>
  <td style="color: yellow">
  Exposes binary API
  </td>
  <td style="color: red">
  No
  </td>
  <td style="color: red">
  No
  </td>
</tr>
</table>