Can Node.js call Chrome?

Is it possible that a Chrome browser window appears on the Node.js desktop? I would like to launch Chrome Browser, providing the size and location of the window when Node.js receives the event.

Are sys shell commands just a methodology?

+8
source share
7 answers
var exec = require('child_process').exec exec('open firefox www.google.pt' , function(err) { if(err){ //process error } else{ console.log("success open") } }) 

This opens firefox on google page from node_s script node, for chrome should be the same

-4
source

On 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); 

A bit more:

+13
source

Now I open a new firefox tab on the windows: https://github.com/Sequoia/FTWin/blob/master/FTWin.n.js

The most important part:

 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]); 

Note:

  • Passing the full path of firefox to child_process.spawn
  • Removing Slashes
  • Passing switches / arguments to firefox.exe: passed as the second cp.spawn parameter as an array (one entry for each switch).

This call is equivalent to entering "c:\Program Files (x86)\Mozilla Firefox\firefox.exe" -new-tab http://duckduckgo.com at the Windows command prompt.

For chrome, you need something like D:\Users\sequoia\AppData\Local\Google\Chrome\Application\chrome.exe --new-tab http://duckduckgo.com/ , I will let you develop a version of child_process yourself; )

Literature:

http://peter.sh/experiments/chromium-command-line-switches/

http://nodejs.org/docs/v0.3.1/api/child_processes.html

+5
source

With opn :

 const opn = require('opn'); opn('http://siteurl.com/', {app: ['google chrome']}); 
+5
source

Checkout https://www.npmjs.com/package/chrome-launcher :

Run chrome:

 const chromeLauncher = require('chrome-launcher'); chromeLauncher.launch({ startingUrl: 'https://google.com' }).then(chrome => { console.log(`Chrome debugging port running on ${chrome.port}`); }); 

Headless Chrome Launch:

 const chromeLauncher = require('chrome-launcher'); chromeLauncher.launch({ startingUrl: 'https://google.com', chromeFlags: ['--headless', '--disable-gpu'] }).then(chrome => { console.log(`Chrome debugging port running on ${chrome.port}`); }); 

chrome-launcher opens the remote port for debugging, so you can also control the browser instance using DevTools .

Puppeteer is another way to launch Chrome and interact with it using a high-level API.

+2
source

Yes, I think you will need to escape into the shell and then open the chrome.

0
source

Node can only do this when calling the UNIX / Windows command, so only the sys shell command.

0
source

All Articles