Is it possible for Node.js running on a desktop to spawn a Chrome Browser window? I would like to start a Chrome Browser providing the window size and location when Node.js receives an event.
Is sys shell commands only methodology?
Chrome V8 executes JavaScript code. Node. js is built on top of Chrome V8 and is a widely used runtime environment for serverless JavaScript functions.
js is a server-side JavaScript run-time environment. It's open-source, including Google's V8 engine, libuv for cross-platform compatibility, and a core library. Notably, Node. js does not expose a global "window" object, since it does not run within a browser.
Node allows you to create a child process in four different ways: spawn() , fork() , exec() , and execFile() . We will be using the exec() call to launch a browser in a child process. The exec() call buffers the command's generated output and passes all output to a callback function.
Node uses the same JS "engine" that runs chrome. An engine in this case, is a piece of software that compiles, or "translates" your JS code into machine code; or the 0s and 1s your computer can understand.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With