Can Node.js invoke Chrome?
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:
- Use "open -a" with the name of your app from /Applications and append your args
- callback function is called with the output after completion
- Link to API: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
Checkout https://www.npmjs.com/package/chrome-launcher:
Launch chrome:
const chromeLauncher = require('chrome-launcher');
chromeLauncher.launch({
startingUrl: 'https://google.com'
}).then(chrome => {
console.log(`Chrome debugging port running on ${chrome.port}`);
});
Launching headless chrome:
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 a remote debugging port so you can also control browser instance using the DevTools protocol.
Puppeteer is another way to launch Chrome and interact with it using high level APIs.