如何从Visual Studio Code API打开浏览器

我正在探索一种方法,以便从用于开发扩展的Visual Studio Code API中打开默认浏览器。

以下是我的代码:

var disposable = vscode.commands.registerCommand('extension.browser', () => { // The code you place here will be executed every time your command is executed vscode.Uri.call("http://facebook.com"); 

});

如何使用vscode类从API打开浏览器URL。

不需要节点或外部库。 只需使用vscode.open命令:

 vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://google.com')) 

这将在用户的默认浏览器中打开页面

我必须使用npm打开url才能在浏览器中打开。

 var openurl = require('openurl').open; openurl("http://google.com"); 

您不需要安装外部依赖项。 只需使用Node.js库和系统命令即可打开URL。 例如,使用child_process.exec打开一个URL。

声明一个像这样的函数:

 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, '\\\"')}"`); } 

然后从你的代码中调用它

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