Node.js可以调用Chrome吗?
运行在桌面上的Node.js是否可以生成Chrome浏览器窗口? 我想在Node.js接收事件时启动一个提供窗口大小和位置的Chrome浏览器。
sys shell命令是唯一的方法吗?
var exec = require('child_process').exec exec('open firefox www.google.pt' , function(err) { if(err){ //process error } else{ console.log("success open") } })
这从一个nodejs脚本在谷歌页面打开Firefox,铬应该是相同的
在MacOSX上
var childProc = require('child_process'); childProc.exec('open -a "Google Chrome" http://your_url', callback); //Or could be: childProc.exec('open -a firefox http://your_url', callback);
多一点:
- 在/ Applications中使用“open -a”作为你的应用程序的名称,并附加你的参数
- callback函数在完成后用输出调用
- 链接到API: https : //nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
我在这里打开一个新的Firefox的标签: https : //github.com/Sequoia/FTWin/blob/master/FTWin.n.js
最显着的部分:
var cp = require('child_process'), url_to_open = 'http://duckduckgo.com/'; cp.spawn('c:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe', ['-new-tab', url_to_open]);
注意:
- 将firefox的完整path传递给child_process.spawn
- 逃避斜杠
- 将开关/parameter passing给firefox.exe:作为cp.spawn的第二个参数作为数组(每个开关一个条目)传递。
这个调用相当于在Windows命令行键入"c:\Program Files (x86)\Mozilla Firefox\firefox.exe" -new-tab http://duckduckgo.com
。
对于铬你想要的东西像D:\Users\sequoia\AppData\Local\Google\Chrome\Application\chrome.exe --new-tab http://duckduckgo.com/
我会让你解决child_process版本自己;)
参考文献:
用opn :
const opn = require('opn'); opn('http://siteurl.com/', {app: ['google chrome']});
是的,我想你会需要逃离壳体,然后打开铬。
只有在调用UNIX / Windows命令时,节点才能做到这一点,所以只能使用sys shell命令。