20

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.

4 Answers 4

45

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

Sign up to request clarification or add additional context in comments.

6 Comments

Is it possible to get a callback when the browser is closed? Doesn't seem like it.
@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
@ShanKhan No. You'd need to write a vscode extension to do that. Take a look at the vscode.previewHtml command for this (for example, use an iframe inside an html preview to load the page)
It's seems that vscode.env.open was renamed to vscode.env.openExternal.
is there a way to open email address in default mail client?
|
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');

1 Comment

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.
2

A neat solution is to employ the Simple Browser extension, which is bundled with modern versions of VS Code. It exposes two APIs, a simple one called simpleBrowser.show that you call like this:

vscode.commands.executeCommand("simpleBrowser.show", "http://google.com");

The second API simpleBrowser.api.open takes a vscode.Uri and an optional configuration object to customize the opening behavior of the WebView, here for example I have it opened beside the current editor:

vscode.commands.executeCommand(
  "simpleBrowser.api.open",
  vscode.Uri.parse("http://google.com"),
  {
    preserveFocus: true,
    viewColumn: vscode.ViewColumn.Beside,
  }
);

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.