Node Clip It is a Node.js library for clipboard operations with support for text and buffers. It provides both synchronous and asynchronous methods for reading from and writing to the clipboard. The library works on Windows, Linux, and macOS.
- Cross-platform: Works on Windows, Linux, and macOS.
- Text and Buffer Support: Copy and paste text or binary data (buffers).
- Synchronous and Asynchronous Methods: Choose between
writeSync
/readSync
andwriteAsync
/readAsync
. - Easy to Use: Simple API for clipboard operations.
Clip It supports copying and pasting all file types as buffers, including:
- Text files (
.txt
,.json
, etc.) - PDFs (
.pdf
) - Images (
.png
,.jpg
, etc.) - Videos (
.mp4
,.mov
, etc.) - Audio files (
.mp3
,.wav
, etc.) - Binary files (
.bin
,.exe
, etc.)
import ClipIt from 'node-clip-it';
import fs from 'fs';
const clip = new ClipIt();
// Read a PDF file as a buffer
const pdfBuffer = fs.readFileSync('example.pdf');
// Write the PDF buffer to the clipboard
clip.writeSync(pdfBuffer);
// Read the PDF buffer from the clipboard
const clipboardContent = clip.readSync();
fs.writeFileSync('output.pdf', clipboardContent); // Save the buffer to a file
---
## Requirements
- **Node.js**: Version 12 or higher.
- **Platform-Specific Tools**:
- **Windows**: `clip` and `powershell` (pre-installed).
- **Linux**: `xclip` (install with `sudo apt-get install xclip`).
- **macOS**: `pbcopy` and `pbpaste` (pre-installed).
---
## Installation
1. Clone the repository:
```bash
git clone https://github.com/Ferencz/clip-it.git
cd node-clip-it
Install the package using npm:
```bash
npm install node-node-clip-it
// Using ES Modules
import ClipIt from 'node-clip-it';
// Using CommonJS
const ClipIt = require('node-clip-it');
const clip = new ClipIt();
writeSync(content: string | Buffer)
: Writes text or buffer content to the clipboard synchronously.readSync(): string | Buffer
: Reads text or buffer content from the clipboard synchronously.
// Write text to the clipboard
clip.writeSync('Hello, Clip It!');
// Read text from the clipboard
const textContent = clip.readSync();
console.log(textContent); // Output: Hello, Clip It!
// Write buffer to the clipboard
const buffer = Buffer.from('Hello, Buffer!');
clip.writeSync(buffer);
// Read buffer from the clipboard
const bufferContent = clip.readSync();
console.log(bufferContent.toString()); // Output: Hello, Buffer!
writeAsync(content: string | Buffer): Promise<void>
: Writes text or buffer content to the clipboard asynchronously.readAsync(): Promise<string | Buffer>
: Reads text or buffer content from the clipboard asynchronously.
// Write text to the clipboard
await clip.writeAsync('Hello, Clip It Async!');
// Read text from the clipboard
const textContent = await clip.readAsync();
console.log(textContent); // Output: Hello, Clip It Async!
// Write buffer to the clipboard
const buffer = Buffer.from('Hello, Buffer Async!');
await clip.writeAsync(buffer);
// Read buffer from the clipboard
const bufferContent = await clip.readAsync();
console.log(bufferContent.toString()); // Output: Hello, Buffer Async!
You can also copy and paste file contents as buffers.
import fs from 'fs';
// Read a file as a buffer
const fileBuffer = fs.readFileSync('example.txt');
// Write the file buffer to the clipboard
clip.writeSync(fileBuffer);
// Read the file buffer from the clipboard
const clipboardContent = clip.readSync();
fs.writeFileSync('output.txt', clipboardContent); // Save the buffer to a file
You can check if the clipboard already contains content before performing operations.
// Read preloaded content from the clipboard
const preloadedContent = clip.readSync();
if (preloadedContent) {
console.log('Preloaded clipboard content:', preloadedContent);
} else {
console.log('Clipboard is empty.');
}
// Overwrite preloaded content
clip.writeSync('New clipboard content');
const updatedContent = clip.readSync();
console.log('Updated clipboard content:', updatedContent);
Method | Description |
---|---|
writeSync(content) |
Writes text or buffer content to the clipboard. |
readSync() |
Reads text or buffer content from the clipboard. |
writeAsync(content) |
Writes text or buffer content to the clipboard. |
readAsync() |
Reads text or buffer content from the clipboard. |
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
If you encounter any issues or have questions, please open an issue.
Enjoy using Clip It! 🎉