pax_global_header00006660000000000000000000000064144340505240014513gustar00rootroot0000000000000052 comment=2d4fbabdc8298d58d3cd9835cbd35bbeed4cc121 node-copy-paste-1.5.3/000077500000000000000000000000001443405052400145305ustar00rootroot00000000000000node-copy-paste-1.5.3/.gitignore000066400000000000000000000000261443405052400165160ustar00rootroot00000000000000.DS_Store node_modulesnode-copy-paste-1.5.3/README.md000066400000000000000000000047411443405052400160150ustar00rootroot00000000000000# node-copy-paste A command line utility that allows read/write (i.e copy/paste) access to the system clipboard. It does this by wrapping [`pbcopy/pbpaste`](https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/pbcopy.1.html) (for OSX), [`xclip`](http://www.cyberciti.biz/faq/xclip-linux-insert-files-command-output-intoclipboard/) (for Linux, FreeBSD, and OpenBSD), and [`clip`](http://www.labnol.org/software/tutorials/copy-dos-command-line-output-clipboard-clip-exe/2506/) (for Windows). Currently works with node.js v0.8+. ## The API When `require("copy-paste")` is executed, an object with the following properties is returned: - `copy(text[, callback])`: asynchronously replaces the current contents of the clip board with `text`. Takes either a string, array, object, or readable stream. Returns the same value passed in. Optional callback will fire when the copy operation is complete. - `paste([callback])`: if no callback is provided, `paste` synchronously returns the current contents of the system clip board. Otherwise, the contents of the system clip board are passed to the callback as the second parameter. The first one being a potential error. **Note**: The synchronous version of `paste` is not always available. Unfortunately, I'm having a hard time finding a synchronous version of `child_process.exec` that consistently works on all platforms, especially windows. An error message is shown if the synchronous version of `paste` is used on an unsupported platform. That said, the asynchronous version of `paste` is always available. - `require("copy-paste").global()`: adds `copy` and `paste` to the global namespace. Returns an object with `copy` and `paste` as properties. ## Example ```js var ncp = require("copy-paste"); ncp.copy('some text', function () { // complete... }) ``` ## Getting node-copy-paste The easiest way to get node-copy-paste is with [npm](http://npmjs.org/): npm install -g copy-paste Alternatively you can clone this git repository: git clone git://github.com/xavi-/node-copy-paste.git ## Future plans I'm hoping to add various fallbacks for instances when `xclip` or `clip` is not avaiable (see [experimental-fallbacks](https://github.com/xavi-/node-copy-paste/tree/experimental-fallbacks/platform) branch). Also this library needs to be more thoroughly tested on windows. ## Developed by * Xavi Ramirez ## License This project is released under [The MIT License](http://www.opensource.org/licenses/mit-license.php). node-copy-paste-1.5.3/index.js000066400000000000000000000062171443405052400162030ustar00rootroot00000000000000var child_process = require("child_process"); var spawn = child_process.spawn; var execSync = child_process.execSync var util = require("util"); var config; switch(process.platform) { case "darwin": config = require("./platform/darwin"); break; case "win32": config = require("./platform/win32"); break; case "linux": config = require("./platform/linux"); break; case "freebsd": config = require("./platform/linux"); break; case "openbsd": config = require("./platform/linux"); break; case "android": config = require("./platform/android"); break; default: throw new Error("Unknown platform: '" + process.platform + "'. Send this error to xavi.rmz@gmail.com."); } var noop = function() {}; exports.copy = function(text, callback) { const opts = { env: Object.assign({}, process.env, config.copy.env) }; var child = spawn(config.copy.command, config.copy.args, opts); var done = (callback ? function() { callback.apply(this, arguments); done = noop; } : function(err) { if(err) { throw err; } done = noop; } ); var err = []; child.stdin.on("error", function (err) { done(err); }); child .on("exit", function() { done(null, text); }) .on("error", function(err) { done(err); }) .stderr .on("data", function(chunk) { err.push(chunk); }) .on("end", function() { if(err.length === 0) { return; } done(new Error(config.decode(err))); }) ; if(!child.pid) { return text; } if(text?.pipe) { text.pipe(child.stdin); } else { var output, type = Object.prototype.toString.call(text); if(type === "[object String]") { output = text; } else if(type === "[object Object]") { output = util.inspect(text, { depth: null }); } else if(type === "[object Array]") { output = util.inspect(text, { depth: null }); } else if(type === "[object Null]") { output = "null"; } else if(type === "[object Undefined]") { output = "undefined"; } else { output = text.toString(); } child.stdin.end(config.encode(output)); } return text; }; var pasteCommand = [ config.paste.command ].concat(config.paste.args).join(" "); exports.paste = function(callback) { if(execSync && !callback) { return config.decode(execSync(pasteCommand)); } else if(callback) { var child = spawn(config.paste.command, config.paste.args); var done = callback && function() { callback.apply(this, arguments); done = noop; }; var data = [], err = []; child.on("error", function(err) { done(err); }); child.stdout .on("data", function(chunk) { data.push(chunk); }) .on("end", function() { done(null, config.decode(data)); }) ; child.stderr .on("data", function(chunk) { err.push(chunk); }) .on("end", function() { if(err.length === 0) { return; } done(new Error(config.decode(err))); }) ; } else { throw new Error("A synchronous version of paste is not supported on this platform."); } }; exports.silent = function() { throw new Error("DEPRECATED: copy-paste is now always silent."); }; exports.noConflict = function() { throw new Error("DEPRECATED: copy-paste no longer adds global variables by default."); }; exports.global = function() { global.copy = exports.copy; global.paste = exports.paste; return exports; }; node-copy-paste-1.5.3/package.json000066400000000000000000000011521443405052400170150ustar00rootroot00000000000000{ "name": "copy-paste" , "version": "1.5.3" , "description": "A command line utility that allows read/write (i.e copy/paste) access to the system clipboard." , "keywords": [ "copy", "paste", "copy and paste", "clipboard" ] , "maintainers": [ { "name": "Xavi" , "email": "xavi.rmz@gmail.com" , "web": "http://xavi.co" } ] , "main": "./index.js" , "repository": { "type": "git" , "url": "https://github.com/xavi-/node-copy-paste" } , "dependencies": { "iconv-lite": "^0.4.8" } , "devDependencies": { "mocha": "*", "should": ">=8.2.1" } , "scripts": { "test": "node_modules/.bin/mocha -w" } } node-copy-paste-1.5.3/platform/000077500000000000000000000000001443405052400163545ustar00rootroot00000000000000node-copy-paste-1.5.3/platform/android.js000066400000000000000000000006031443405052400203310ustar00rootroot00000000000000exports.copy = { command: "termux-clipboard-set", args: [] }; exports.paste = { command: "termux-clipboard-get", args: [] }; exports.paste.full_command = exports.paste.command; exports.encode = function(str) { return Buffer.from(str, "utf8"); }; exports.decode = function(chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } return Buffer.concat(chunks).toString("utf8"); }; node-copy-paste-1.5.3/platform/darwin.js000066400000000000000000000006061443405052400202000ustar00rootroot00000000000000exports.copy = { command: "pbcopy", args: [], env: { LANG: "en_US.UTF-8" } }; exports.paste = { command: "pbpaste", args: [] }; exports.paste.full_command = exports.paste.command; exports.encode = function(str) { return Buffer.from(str, "utf8"); }; exports.decode = function(chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } return Buffer.concat(chunks).toString("utf8"); }; node-copy-paste-1.5.3/platform/fallbacks/000077500000000000000000000000001443405052400202765ustar00rootroot00000000000000node-copy-paste-1.5.3/platform/fallbacks/paste.vbs000066400000000000000000000025571443405052400221370ustar00rootroot00000000000000Function Base64Encode(sText) Dim oXML, oNode Set oXML = CreateObject("Msxml2.DOMDocument.3.0") Set oNode = oXML.CreateElement("base64") oNode.dataType = "bin.base64" oNode.nodeTypedValue =Stream_StringToBinary(sText) Base64Encode = oNode.text Set oNode = Nothing Set oXML = Nothing End Function 'Stream_StringToBinary Function '2003 Antonin Foller, http://www.motobit.com 'Text - string parameter To convert To binary data Function Stream_StringToBinary(Text) Const adTypeText = 2 Const adTypeBinary = 1 'Create Stream object Dim BinaryStream 'As New Stream Set BinaryStream = CreateObject("ADODB.Stream") 'Specify stream type - we want To save text/string data. BinaryStream.Type = adTypeText 'Specify charset For the source text (unicode) data. BinaryStream.CharSet = "utf-8" 'Open the stream And write text/string data To the object BinaryStream.Open BinaryStream.WriteText Text 'Change stream type To binary BinaryStream.Position = 0 BinaryStream.Type = adTypeBinary 'Ignore first two bytes - sign of BinaryStream.Position = 0 'Open the stream And get binary data from the object Stream_StringToBinary = BinaryStream.Read Set BinaryStream = Nothing End Function Dim objHTML Set objHTML = CreateObject("htmlfile") text = objHTML.ParentWindow.ClipboardData.GetData("Text") Wscript.Echo Base64Encode(text)node-copy-paste-1.5.3/platform/linux.js000066400000000000000000000010231443405052400200450ustar00rootroot00000000000000exports.copy = process.env.WSL_DISTRO_NAME ? { command: "clip.exe", args: [] } : { command: "xclip", args: [ "-selection", "clipboard" ] }; exports.paste = { command: "xclip", args: [ "-selection", "clipboard", "-o" ] }; exports.paste.full_command = [ exports.paste.command ].concat(exports.paste.args).join(" "); exports.encode = function(str) { return Buffer.from(str, "utf8"); }; exports.decode = function(chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } return Buffer.concat(chunks).toString("utf8"); }; node-copy-paste-1.5.3/platform/win32.js000066400000000000000000000013711443405052400176560ustar00rootroot00000000000000var iconv = require("iconv-lite"); var path = require("path"); var vbsPath = path.join(__dirname, ".\\fallbacks\\paste.vbs"); var paste = { command: "cscript", args: [ "/Nologo", vbsPath ] }; paste.full_command = [ paste.command, paste.args[0], '"'+vbsPath+'"' ].join(" "); exports.copy = { command: "clip", args: [] }; exports.paste = paste; exports.encode = function(str) { return iconv.encode(str, "utf16le"); }; exports.decode = function(chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } var b64 = iconv.decode(Buffer.concat(chunks), "cp437"); b64 = b64.substring(0, b64.length - 2); // Chops off extra "\r\n" // remove bom and decode var result = Buffer.from(b64, "base64").subarray(3).toString("utf-8"); return result; }; node-copy-paste-1.5.3/test/000077500000000000000000000000001443405052400155075ustar00rootroot00000000000000node-copy-paste-1.5.3/test/copypaste.js000066400000000000000000000021231443405052400200520ustar00rootroot00000000000000'use strict'; var should = require('should'); var clipboard = require("../index.js"); function copy_and_paste(content, done) { clipboard.copy(content, function(error_when_copy) { should.not.exist(error_when_copy); clipboard.paste(function (error_when_paste, p) { should.not.exist(error_when_paste); should.exist(p); p.should.equal(content); done(); }); }); } describe('copy and paste', function () { it('should work correctly with ascii chars (<128)', function (done) { copy_and_paste("123456789abcdefghijklmnopqrstuvwxyz+-=&_[]<^=>=/{:})-{(`)}", done); }); it('should work correctly with cp437 chars (<256)', function (done) { copy_and_paste("ÉæÆôöòûùÿÖÜ¢£¥₧ƒ", done); }); it('should work correctly with unicode chars (<2^16)', function (done) { copy_and_paste("ĀāĂ㥹ĆćĈĉĊċČčĎ ፰፱፲፳፴፵፶፷፸፹፺፻፼", done); }); it('should work correctly for "±"', function (done) { copy_and_paste("±", done); }); });node-copy-paste-1.5.3/test/mocha.opts000066400000000000000000000000651443405052400175060ustar00rootroot00000000000000--require should --reporter spec --ui bdd --recursive