OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

How to callback to your extension from outside VS Code

Development VSCode
post

This post is over a year old, some of this information may be out of date.

While creating the authentication provider, I discovered a helpful handler in the Visual Studio Code API, which allows you to handle system-wide URIs and callback into your extension.

In the case of the authentication provider, I use it to retrieve the token that gets passed from the callback, but you can use the registerUriHandler for many more scenarios. You can, for instance, integrate a callback from your website into VS Code. Execute scripts that open a file/content you want to get processed by your extension.

How to use the registerUriHandler

It is relatively simple to create your own URI handler. All you need is the following code:

import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
const handleUri = (uri: vscode.Uri) => {
const queryParams = new URLSearchParams(uri.query);
if (queryParams.has('say')) {
vscode.window.showInformationMessage(`URI Handler says: ${queryParams.get('say') as string}`);
}
};
context.subscriptions.push(
vscode.window.registerUriHandler({
handleUri
})
);
}
export function deactivate() {}

Calling back to your extension

To trigger the handleUri method, you need to trigger it from the following URL vscode://<publisher>.<extension name>.

In the case of my example, I use the following URL: vscode://eliostruyf.vscode-urihandler-test?say=How are you doing?, which results in the subsequent notification:

Show image URI Handler showing the notification
URI Handler showing the notification

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code