【问题标题】:How to use nodejs to open default browser and navigate to a specific URL如何使用 nodejs 打开默认浏览器并导航到特定 URL
【发布时间】:2012-01-19 23:34:30
【问题描述】:

我正在使用 Node.js 编写应用程序。

我想要创建的功能之一是打开默认的网络浏览器并导航到特定的 URL。

我希望它是可移植的,以便在 Windows/Mac/Linux 上运行。

【问题讨论】:

  • 我想这个问题就是你要找的:stackoverflow.com/questions/7664605/…
  • 是的,它适用于 Mac。它适用于 Windows 和 Linux 吗?我手头没有开窗机
  • xdg-open 在 Linux 中运行 :)

标签: javascript node.js


【解决方案1】:
#!/usr/bin/env node

const url = 'http://localhost'
require('child_process')
  .exec((process.platform
         .replace('darwin','')
         .replace(/win32|linux/,'xdg-') + 'open ' + url));

【讨论】:

  • 请考虑添加how and why this solves the problem的简要说明。这将有助于读者更好地理解您的解决方案。
  • 仅代码的答案在 SO 上不受欢迎,并且经常被否决。通常需要上下文和解释来理解给定的解决方案,并区分何时应该/不应该应用您的解决方案。带有解释的答案对未来的访问者更有帮助,因此得到更多的支持。 SO 旨在成为一个学习如何解决问题的地方。指出特定答案适用的上下文也很好,例如操作系统平台或特定的边缘情况。还鼓励链接到文档或更多信息(但也必须嵌入实际的解决方案。)
【解决方案2】:

节点打开是deprecated。现在使用open:

const open = require('open')

await open('http://sindresorhus.com') // Opens the url in the default browser

await open('http://sindresorhus.com', {app: 'firefox'}) // Specify the app to open in

【讨论】:

    【解决方案3】:

    Windows + Express

    app.listen(3000, ()=>{
        require('child_process').exec('start http://localhost:3000/');
    });
    

    【讨论】:

      【解决方案4】:

      安装:

      $ npm install open
      

      用法:

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

      【讨论】:

      • 您好,感谢您分享这个答案。一个指向 async/await 文档的链接会很好地添加以供参考。不过代码中的 cmets 不错 :)
      【解决方案5】:

      使用open(以前称为opn),因为它将处理跨平台问题。安装:

      $ npm install open
      

      使用方法:

      const open = require('open');
      
      // opens the url in the default browser 
      open('http://sindresorhus.com');
       
      // specify the app to open in 
      open('http://sindresorhus.com', {app: 'firefox'});
      

      【讨论】:

      • ForbesLindesay 回调被立即调用,而不是在窗口关闭时调用。有什么想法吗?
      • 不确定,可能值得尝试github.com/domenic/opener 作为具有相同 API 的替代模块。看起来它有一个适当的问题跟踪器,你可以在上面打开一个问题。浏览器如何将进程报告为结束可能只是一个奇怪的问题,因此它可能不容易修复。
      • 这是一个很好的建议,非常适合 OP 描述的用例。
      • 看起来 opener 在 Mac / Windows / Linux 上工作,而 open 只在 Mac / Windows 上工作,所以 opener 更可取。
      • 这对我有用,但似乎子进程没有分离,所以当服务器关闭时,我的 firefox 也是如此!不确定如何“打开”以分离子进程......有什么想法吗?
      【解决方案6】:

      您可能需要使用 ... 的值实现一个开关

      require('os').type()
      

      然后根据平台使用spawn("open")spawn("xdg-open")

      【讨论】:

      • 在windows中,我尝试spawn("explorer.exe",['stackoverflow.com']),windows资源管理器会选择默认浏览器打开URL。
      • @QingXu 太棒了! require('child_process').spawn('explorer', ['url']) 是一个不错的单行者!
      【解决方案7】:
      var url = 'http://localhost';
      var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
      require('child_process').exec(start + ' ' + url);
      

      【讨论】:

      • 应该注意,至少在 Windows 上,URL 中的& 应该用^& 转义
      • 我包装了类似的逻辑,承诺它并发布到 npm npmjs.com/package/out-url
      【解决方案8】:

      恕我直言,最简单和最简洁的方法是使用名为 openurl 的 npm 包。做一个npm install openurl。你可以在你的 Nodejs REPL 中快速尝试一下

      require("openurl").open("http://stackoverflow.com/questions/8500326/how-to-use-nodejs-to-open-default-browser-and-navigate-to-a-specific-url")

      如果需要,您也可以使用它发送电子邮件; require("openurl").open("mailto:janedoe@example.com")

      【讨论】:

        猜你喜欢
        • 2014-12-06
        • 2020-05-06
        • 2011-05-30
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        相关资源
        最近更新 更多