Can Node.js invoke Chrome?

后端 未结 8 1415
长发绾君心
长发绾君心 2021-02-04 09:54

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 rece

8条回答
  •  孤街浪徒
    2021-02-04 10:31

    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.

提交回复
热议问题