An experimental Node.js module that provides you a full control over host clipboard in Windows environment.
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.
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.
getText( [format, encoding] )
- Params:
format
-string
- Format name you want to set. Could be one of the standard builtins. Examples areCF_UNICODETEXT
,CF_TEXT
,HTML Format
etc. Defaults toCF_UNICODETEXT
.encoding
-string
- Encoding used for decoding string in the clipboard. Defaults toutf-16le
.
- Returns:
string
- String retrieved from the clipboard.null
- If no data was found.
- Params:
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 areCF_UNICODETEXT
,CF_TEXT
,HTML Format
etc. Defaults toCF_UNICODETEXT
.encoding
-string
- Encoding used for encoding string into the clipboard. Default value depends on format:CF_TEXT
- Defaults toascii
.CF_UNICODETEXT
- Defaults toutf-16le
.- In any other case defaults to
utf8
.
- Returns:
number
- Number of bytes written if successful.null
- If failed.
- Params:
getHTML( [fullHtml, format] )
- Params:
fullHtml
-boolean
- If set totrue
will return outer context, likehtml
,body
tags. Defaults tofalse
.format
-string
- Format name you want to set. Could be one of the standard builtins. Examples areCF_UNICODETEXT
,CF_TEXT
,HTML Format
etc.
- Params:
setHTML( newHtml, [sourceUrl, format] )
- Params:
newHtml
-string
- HTML code to be set.sourceUrl
-string
- URL to be set as a SourceURL header. Defaults tonull
.format
-string
- Format name you want to set. Could be one of the standard builtins. Examples areCF_UNICODETEXT
,CF_TEXT
,HTML Format
etc.
- Params:
getData( format )
- Params:
format
-string
- Format name you want to set. Could be one of the standard builtins. Examples areCF_UNICODETEXT
,CF_TEXT
,HTML Format
etc.
- Returns:
Buffer
- A raw buffer of what is kept in the memory.null
- If nothing is found.
- Params:
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 areCF_UNICODETEXT
,CF_TEXT
,HTML Format
etc.
- Returns:
number
- Number of bytes written if successful.null
- If failed.
- Params:
getFormats
- Returns:
string[]
- An array of strings with available formats.
- Returns:
clear
- Simply wipes out your clipboard.
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.
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.
MIT ยฉ Marek Lewandowski