How to open browser from Visual Studio Code API

前端 未结 3 1755
被撕碎了的回忆
被撕碎了的回忆 2021-02-20 00:38

I am just exploring a way in order to open the default browser from Visual Studio Code API used for developing extensions.

Following is my code :

var dis         


        
3条回答
  •  你的背包
    2021-02-20 01:23

    You don't need to install an external dependency. Opening an URL can be simply done with just the Node.js libraries and system commands. For e.g. use the child_process.exec to open an URL.

    Declare a function like so:

    const exec = require('child_process').exec;
    
    function openURL(url) {
      let opener;
    
      switch (process.platform) {
        case 'darwin':
          opener = 'open';
          break;
        case 'win32':
          opener = 'start';
          break;
        default:
          opener = 'xdg-open';
          break;
      }
    
      return exec(`${opener} "${url.replace(/"/g, '\\\"')}"`);
    }
    

    and then call it from your code

    openURL('http://stackoverflow.com');
    

提交回复
热议问题