Can Node.js invoke Chrome?

后端 未结 8 1433
长发绾君心
长发绾君心 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:22

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

    The most salient portion:

    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:

    1. Passing the full path of firefox to child_process.spawn
    2. Escaping of slashes
    3. Passing switches/arguments to firefox.exe: passed as the second parameter of cp.spawn as an array (one entry per switch).

    This call is the equivalent of typing "c:\Program Files (x86)\Mozilla Firefox\firefox.exe" -new-tab http://duckduckgo.com at the windows command line.

    For chrome you'd want something like D:\Users\sequoia\AppData\Local\Google\Chrome\Application\chrome.exe --new-tab http://duckduckgo.com/ I'll let you work out the child_process version on your own ;)

    References:

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

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

提交回复
热议问题