Skip to content

mlewand/win-clipboard

Repository files navigation

win-clipboard NPM version Build Status Dependency Status

An experimental Node.js module that provides you a full control over host clipboard in Windows environment.

Installation

If you haven't ever messed up with C++ addons, you'll have most likely to install windows-build-tools. It takes a fair amount of time to complete, but simplifies the installation by a lot.

npm install --global --production windows-build-tools

Once you have the above, it's as simple as:

npm install --save-optional win-clipboard

Note that you should install this package an optional dependency, as it won't work on other OSes.

Usage

const clipboard = require( 'win-clipboard' );

clipboard.getText(); // Returns unicode format as a string.

clipboard.getHTML(); // Returns HTML as a string.

clipboard.getData( 'HTML Format' ); // Returns raw content of a "HTML Format".

clipboard.setText( '🙀🙊' ); // Sets some fancy emoji in your unicode format.

clipboard.getFormats(); // Lists formats in the clipboard.

API

  • getText( [format, encoding] )
    • Params:
      • format - string - Format name you want to set. Could be one of the standard builtins. Examples are CF_UNICODETEXT, CF_TEXT, HTML Format etc. Defaults to CF_UNICODETEXT.
      • encoding - string - Encoding used for decoding string in the clipboard. Defaults to utf-16le.
    • Returns:
      • string - String retrieved from the clipboard.
      • null - If no data was found.
  • setText( newText[, format, encoding] )
    • Params:
      • newText - string - Text to be set in the clipboard.
      • format - string - Format name you want to set. Could be one of the standard builtins. Examples are CF_UNICODETEXT, CF_TEXT, HTML Format etc. Defaults to CF_UNICODETEXT.
      • encoding - string - Encoding used for encoding string into the clipboard. Default value depends on format:
        • CF_TEXT - Defaults to ascii.
        • CF_UNICODETEXT - Defaults to utf-16le.
        • In any other case defaults to utf8.
    • Returns:
      • number - Number of bytes written if successful.
      • null - If failed.
  • getHTML( [fullHtml, format] )
    • Params:
      • fullHtml - boolean - If set to true will return outer context, like html, body tags. Defaults to false.
      • format - string - Format name you want to set. Could be one of the standard builtins. Examples are CF_UNICODETEXT, CF_TEXT, HTML Format etc.
  • setHTML( newHtml, [sourceUrl, format] )
    • Params:
      • newHtml - string - HTML code to be set.
      • sourceUrl - string - URL to be set as a SourceURL header. Defaults to null.
      • format - string - Format name you want to set. Could be one of the standard builtins. Examples are CF_UNICODETEXT, CF_TEXT, HTML Format etc.
  • getData( format )
    • Params:
      • format - string - Format name you want to set. Could be one of the standard builtins. Examples are CF_UNICODETEXT, CF_TEXT, HTML Format etc.
    • Returns:
      • Buffer - A raw buffer of what is kept in the memory.
      • null - If nothing is found.
  • setData( newData, format ) - Sets raw data to a given clipboard format.
    • Params:
      • newData - ArrayBuffer - Raw data to be set.
      • format - string - Format name you want to set. Could be one of the standard builtins. Examples are CF_UNICODETEXT, CF_TEXT, HTML Format etc.
    • Returns:
      • number - Number of bytes written if successful.
      • null - If failed.
  • getFormats
    • Returns:
      • string[] - An array of strings with available formats.
  • clear - Simply wipes out your clipboard.

Why?

I needed to put some fancy stuff into a clipboard, and I was surprised that there's no good library for managing the clipboard.

What I needed was an ability to set HTML / RTF / plain text together, which was nowhere to be found.

Other requirement that I had in other side project, was retrieve all the formats in clipboard, for a further inspection.

All the implementation allowed just for setting a plaintext - it was due to the fact that it was based on clip bin.

How?

I implemented it using a Node.js C++ addon, which uses WinAPI.

The implementation turned out to be extremely easy, while having access to the WinAPI it gives all the power in the world to work with the clipboard.

License

MIT © Marek Lewandowski