Send a variable from Chrome to VSCode

Viewed 24

I want to send a variable from Chrome, during a click event, and receive that variable in VSCode, through a VSCode extension.

The click event is not the problem. I'm struggling to receive data in VSCode.

This looked promising, but I'm struggling to get it to work.

Question: In your opinion, which is the best solution?

Update:

It turns out that the solution in the link is a good solution. And I managed to get it to work but not in the way it suggests.

Suggested code:

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    const handleUri = (uri: vscode.Uri) => {
        // this doesn't log
        console.log('testing', 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() {}

However, only this example will log the desired result immediately:

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    const handleUri = (uri: vscode.Uri) => {
        // this doesn't log
        console.log('testing', 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
        })
    );
    let disposable = vscode.commands.registerCommand('my-org.myExtension', function () {
        vscode.window.showInformationMessage('Hello World');
        vscode.window.registerUriHandler({ handleUri })
    });

    context.subscriptions.push(disposable);
}

export function deactivate() {}

Update 2

In the first example above, the handleUri function is invoked as intended, but the logs are printed late (I had to invoke the command in the second example to see 'testing' and the uri logged).

I hope this helps someone.

0 Answers
Related