18

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 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");
});

How to open Browser URL from API using vscode class.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Shan Khan
  • 9,667
  • 17
  • 61
  • 111

3 Answers3

39

There is no need for node or external libraries.

If you are using VS Code 1.31+, use the vscode.env.openExternal function:

import * as vscode from 'vscode';

vscode.env.openExternal(vscode.Uri.parse('https://example.com'));

For older VS Code versions, use the vscode.open command:

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

Both of these will open the page in the user's default browser

Juan Campa
  • 1,181
  • 2
  • 14
  • 20
Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
  • 1
    Is it possible to get a callback when the browser is closed? Doesn't seem like it. – Michael Flanakin Dec 31 '17 at 07:26
  • @MichaelFlanakin Not with `vscode.open`. You'd need to write a vscode extension that uses node's `child_process` or similar if you need to do that – Matt Bierner Jan 02 '18 at 18:46
  • @ShanKhan No. You'd need to write a vscode extension to do that. Take a look at the [`vscode.previewHtml` command](https://code.visualstudio.com/docs/extensionAPI/vscode-api-commands) for this (for example, use an iframe inside an html preview to load the page) – Matt Bierner Jan 02 '18 at 18:49
  • 1
    It's seems that `vscode.env.open` was renamed to `vscode.env.openExternal`. – yumetodo Feb 21 '19 at 06:12
  • is there a way to open email address in default mail client? – Bandara Mar 19 '19 at 16:57
  • Use try using a mailto uri: `mailto:someone@example.com` – Matt Bierner Mar 19 '19 at 17:31
2

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');
Jakub Synowiec
  • 5,719
  • 1
  • 29
  • 37
  • With Windows 10 I had to add "" (two empty quotes) after the opener for it to work. This is a good solution when you run into VSCode's double encoding bug for certain urls. – boocs Apr 16 '21 at 04:54
0

I had to use npm open url in order to open in browser.

var openurl = require('openurl').open;
    openurl("http://google.com");
Shan Khan
  • 9,667
  • 17
  • 61
  • 111