是否有办法在Node.js中复制到剪贴板?有没有任何模块或想法?我正在一个桌面应用程序中使用Node.js。希望这样可以解释为什么我想要能够实现这个。
是否有办法在Node.js中复制到剪贴板?有没有任何模块或想法?我正在一个桌面应用程序中使用Node.js。希望这样可以解释为什么我想要能够实现这个。
对于 OS X:
function pbcopy(data) {
var proc = require('child_process').spawn('pbcopy');
proc.stdin.write(data); proc.stdin.end();
}
write()
函数可以接受缓冲区或字符串。字符串的默认编码为 utf-8。
请查看clipboardy
。它可以让您在跨平台上复制/粘贴。它比另一个答案中提到的copy-paste
模块更加活跃地维护,并且解决了该模块的许多问题。
const clipboardy = require('clipboardy');
// Copy
clipboardy.writeSync('');
// Paste
clipboardy.readSync();
//
clipboardy
。我有一个按钮在我的网络应用程序中,点击该按钮应该获取剪贴板内容,但它返回 undefined。如有任何建议,请提供。https://dev59.com/P1IH5IYBdhLWcg3wBn7F - soccerwaycopy-paste
最后一个评论更早)。无论如何,这个库与所有项目配置不完全兼容,我正在一个使用node.js和typescript的项目上工作,其中所有的库都能正常工作,只有这个库不能。我发现Xavi的npm i copy-paste
在第一次安装时没有任何兼容性问题,非常好用。如果这个库能够在任何项目中都没有问题,无论它是否被良好配置(从库开发者的角度来看),那将是很棒的。 - Miquel这里有一个提供copy
和paste
功能的模块:https://github.com/xavi-/node-copy-paste
当执行require("copy-paste").global()
命令时,将添加两个全局函数:
> copy("hello") // Asynchronously adds "hello" to clipbroad
> Copy complete
> paste() // Synchronously returns clipboard contents
'hello'
和其他答案提到的一样,要在 Node 中复制和粘贴,您需要调用外部程序。在 node-copy-paste
的情况下,它会调用 pbcopy/pbpaste
(适用于 OSX)、xclip
(适用于 Linux)和 clip
(适用于 Windows)。
当我在一个副业项目的 REPL(交互式命令行界面)中做很多工作时,这个模块非常有帮助。不用说,copy-paste
只是一个命令行实用程序 -- 它 不适用于 服务器工作。
在Windows中最短的方法:
const util = require('util');
require('child_process').spawn('clip').stdin.end(util.inspect('content_for_the_clipboard'));
require
util
才能运行!谢谢…… - LIXerconst util = require("util")
的情况下收到了 ReferenceError: util is not defined
错误。
我在Electon的主进程和渲染进程中都进行了测试,但没有成功。
话虽如此,从node 12.14.1 CLI中测试是可以的。
基于用例,我认为最好定义util以确保安全。 - Benargeevar 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);
});
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
}
);
在Linux中使用相同的代码,但将pbcopy
替换为Xclip(apt get install xclip
)。
echo fooo | clip
。那么,在Linux中如何做到不要求用户安装任何东西呢? - Tower我通过创建一个处理此问题的不同应用程序来实现这一点。虽然这并不是最好的方法,但它能够起作用。
我使用 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
接下来在 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
}
);
我发现没有一个解决方案适用于Windows,也没有明显的地方可行,因此重新发布了这个,在Google搜索的第2页深处找到,使用Windows本地命令行clip
,并避免任何可能凌乱的命令行解决方法,如建议中的echo foo | clip
。
var cp = require("child_process");
var child = cp.spawn('clip');
child.stdin.write(result.join("\n"));
child.stdin.end();
let filePath; // absolute path of the file you'd like to copy
require('child_process').exec(
`osascript -e 'set the clipboard to POSIX file "${filePath}"'`,
function (err, stdout, stderr) {
console.log(stdout); // to confirm the application has been run
}
);
For Windows:
const filePath; // absolute path of the file you'd like to copy
// Make a temporary folder
const tmpFolder = path.join(__dirname, `tmp-${Date.now()}`);
fs.mkdirSync(tmpFolder, { recursive: true });
// Copy the file into the temporary folder
const tmpFilePath = path.join(tmpFolder, path.basename(filePath));
fs.copyFileSync(filePath, tmpFilePath);
// To copy the contents of the folder in PowerShell,
// you need to add a wildcard to the end of the path
const powershellFolder = path.join(tmpFolder, '*');
require('child_process').exec(
`Set-Clipboard -PATH "${powershellFolder}"`, { 'shell': 'powershell.exe' },
function (err, stdout, stderr) {
console.log(stdout); // to confirm the application has been run
}
);
以下是在 Windows 上使用 node-ffi 实现的方法,它可以直接与 Windows 原生剪贴板 API 进行交互。(这意味着您还可以读取/写入不同的剪贴板格式)
var {
winapi,
user32,
kernel32,
constants
} = require('@kreijstal/winffiapi')
const GMEM_MOVEABLE=0x2;
var stringbuffer=Buffer.from("hello world"+'\0');//You need a null terminating string because C api.
var hmem=kernel32.GlobalAlloc(GMEM_MOVEABLE,stringbuffer.length);
var lptstr = kernel32.GlobalLock(hmem);
stringbuffer.copy(winapi.ref.reinterpret(lptstr, stringbuffer.length));
kernel32.GlobalUnlock(hmem);
if (!user32.OpenClipboard(0)){
kernel32.GlobalLock(hmem);
kernel32.GlobalFree(hmem);
kernel32.GlobalUnlock(hmem);
throw new Error("couldn't open clipboard");
}
user32.EmptyClipboard();
user32.SetClipboardData(constants.clipboardFormats.CF_TEXT, hmem);
user32.CloseClipboard();