【问题标题】:How would you launch a browser from the a node.js command line script [duplicate]您将如何从 node.js 命令行脚本启动浏览器 [重复]
【发布时间】:2011-12-01 15:38:13
【问题描述】:

可能重复:
How to use nodejs to open default browser and navigate to a specific URL

我不知道这是否重要,但我在 OSX 上。

我知道您可以通过键入以下命令从命令行本身启动浏览器:

open http://www.stackoverflow.com

但是有没有办法从 nodejs 命令行脚本中打开浏览器?

【问题讨论】:

    标签: shell command-line node.js


    【解决方案1】:

    Open 现在存在,使用它。 :)

    安装方式:

    $ npm install --save open
    

    用于:

    const open = require('open');
    
    // Opens the image in the default image viewer
    (async () => {
        await open('unicorn.png', {wait: true});
        console.log('The image viewer app closed');
    
        // Opens the url in the default browser
        await open('https://sindresorhus.com');
    
        // Specify the app to open in
        await open('https://sindresorhus.com', {app: 'firefox'});
    
        // Specify app arguments
        await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
    })();
    

    app: ... 选项:

    Type: string | string[]

    指定要打开目标的应用程序,或应用程序的数组和 应用参数。

    应用名称取决于平台。不要将其硬编码为可重用 模块。例如,Chrome 是 macOS 上的 google chrome,google-chrome Linux 和 Windows 上的 chrome。

    您也可以传入应用的完整路径。例如在 WSL 上,这可以 为 /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe Chrome 的 Windows 安装。

    例子:

    open('http://localhost', {app: "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"});

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2023-04-02
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多