Javascript 在 Node.js 中复制到剪贴板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7778539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:34:00  来源:igfitidea点击:

Copy to clipboard in Node.js?

javascriptnode.jsclipboard

提问by Tower

Is there a way you can copy to clipboard in Node.js? Any modules or ideas what so ever? I'm using Node.js on a desktop application. Hopefully that clears up why I want it to be able to achieve this.

有没有办法在 Node.js 中复制到剪贴板?任何模块或想法是什么?我在桌面应用程序上使用 Node.js。希望这能说明为什么我希望它能够实现这一目标。

采纳答案by chjj

A clipboard is not inherent to an operating system. It's a construct of whatever window system the operating system happens to be running. So if you wanted this to work on X for example, you would need bindings to Xlib and/or XCB. Xlib bindings for node actually exist: https://github.com/mixu/nwm. Although I'm not sure whether it gives you access to the X clipboard, you might end up writing your own. You'll need separate bindings for windows.

剪贴板不是操作系统固有的。它是操作系统碰巧运行的任何窗口系统的构造。因此,如果您希望它在 X 上运行,则需要绑定到 Xlib 和/或 XCB。节点的 Xlib 绑定​​实际上存在:https: //github.com/mixu/nwm。虽然我不确定它是否能让您访问 X 剪贴板,但您最终可能会编写自己的剪贴板。您将需要单独的窗口绑定。

edit: If you want to do something hacky, you could also use xclip:

编辑:如果你想做一些hacky,你也可以使用xclip:

var exec = require('child_process').exec;

var getClipboard = function(func) {
  exec('/usr/bin/xclip -o -selection clipboard', function(err, stdout, stderr) {
    if (err || stderr) return func(err || new Error(stderr));
    func(null, stdout);
  });
};

getClipboard(function(err, text) {
  if (err) throw err;
  console.log(text);
});

回答by Benjamin Atkin

For OS X:

对于 OS X:

function pbcopy(data) {
    var proc = require('child_process').spawn('pbcopy'); 
    proc.stdin.write(data); proc.stdin.end();
}

write()can take a buffer or a string. The default encoding for a string will be utf-8.

write()可以采用缓冲区或字符串。字符串的默认编码为 utf-8。

回答by Sindre Sorhus

Check out clipboardy. It lets you copy/paste cross-platform. It is more actively maintained than the copy-pastemodule mentionedin another answer and it fixes many of that module's issues.

退房clipboardy。它允许您跨平台复制/粘贴。它比另一个答案中提到copy-paste模块得到更积极的维护,并且解决了该模块的许多问题。

const clipboardy = require('clipboardy');

// Copy
clipboardy.writeSync('');

// Paste
clipboardy.readSync();
//

回答by Xavi

Here's a module that provide copyand pastefunctions: https://github.com/xavi-/node-copy-paste

这是一个提供copypaste运行的模块:https: //github.com/xavi-/node-copy-paste

When require("copy-paste").global()is executed, two global functions are added:

require("copy-paste").global()被执行时,两个全局的功能添加:

> copy("hello") // Asynchronously adds "hello" to clipbroad
> Copy complete
> paste() // Synchronously returns clipboard contents
'hello'

Like many of the other answer mentioned, to copy and paste in node you need to call out to an external program. In the case of node-copy-paste, it calls out to pbcopy/pbpaste(for OSX), xclip(for linux), and clip(for windows).

像提到的许多其他答案一样,要在节点中复制和粘贴,您需要调用外部程序。在 的情况下node-copy-paste,它调用pbcopy/pbpaste(对于 OSX)、xclip(对于 linux)和clip(对于 Windows)。

This module was very helpful when I was doing a lot of work in the REPL for a side project. Needless to say, copy-pasteis only a command line utility -- it is notmeant for server work.

当我在 REPL 中为一个副项目做大量工作时,这个模块非常有用。不用说,copy-paste它只是一个命令行实用程序——它并不适用于服务器工作。

回答by Ernst Ernst

Shortest way in Windows:

Windows 中的最短路径:

const util = require("util");
require('child_process').spawn('clip').stdin.end(util.inspect("content_for_the_clipboard"));

回答by pimvdb

I managed to do so by creating a different application which handles this. It's certainly not the best way, but it works.

我通过创建一个不同的应用程序来处理这个问题。这当然不是最好的方法,但它有效。

I'm on Windows and created a VB.NET application:

我在 Windows 上创建了一个 VB.NET 应用程序:

Module Module1

    Sub Main()
        Dim text = My.Application.CommandLineArgs(0)
        My.Computer.Clipboard.SetText(text)
        Console.Write(text) ' will appear on stdout
    End Sub
End Module

Then in Node.js, I used child_process.execto run the VB.NET application, with the data to be copied passed as a command line argument:

然后在 Node.js 中,我曾经child_process.exec运行 VB.NET 应用程序,将要复制的数据作为命令行参数传递:

require('child_process').exec(
    "CopyToClipboard.exe \"test foo bar\"",

    function(err, stdout, stderr) {
        console.log(stdout); // to confirm the application has been run
    }
);

回答by FGRibreau

Mac has a native command line pbcopyfor this usecase:

Mac 有一个pbcopy用于此用例的本机命令行:

require('child_process').exec(
    'echo "test foo bar" | pbcopy',

    function(err, stdout, stderr) {
        console.log(stdout); // to confirm the application has been run
    }
);

Same code for Linux but replace pbcopywith Xclip(apt get install xclip)

相同的 Linux 代码,但替换pbcopyXclip( apt get install xclip)

回答by Damodaran

check this zeroclipboard

检查这个零剪贴板

npm install zeroclipboard

npm 安装零剪贴板