Coder Social home page Coder Social logo

win-clipboard's Introduction

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

win-clipboard's People

Contributors

mlewand avatar ksx123 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.