question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Transfering data from window to extension

See original GitHub issue

Hey, i have a question. I would like to use this implementation of vscode in browser as frontend for lowcode platform. I want to use this in iframe and i want to send data through window.postMessage.

However vscode extension has no access to window object. I tried creating custom command similarly to github1s.vscode.get-browser-url and i am able to register eventListener on window in this way, but i have no clue how to pass information from callback to extension

Is this even possible?

Thanks for info

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
conwnetcommented, May 11, 2021

and therefore i can’t listen to events that are occuring outside extension (events comming from window)

I think we can do that:

// in your extension, for example:
// demo-extension/src/extension.ts

import * as vscode from 'vscode';

vscode.commands.registerCommand('my-command-register-by-extension', (data) => {
    // do something related extension
    console.log(data); // once user click `document.body`, the extension should print 'click'
});

// in vscode, for example:
// src/vs/code/browser/workbench/workbench.ts

import { commands } from 'vs/workbench/workbench.web.api';

document.body.onclick = (e) => {
    commands.executeCommand('my-command-register-by-extension', e.type);
}
1reaction
conwnetcommented, May 5, 2021

@mecirmartin

However vscode extension has no access to window object.

Yes, the extension runs inside a webworker, so we can’t visit the window object directly. And the extension host service also runs inside an isolated iframe:

https://github.com/conwnet/github1s/blob/master/resources/index.html#L180-L181

We can also use the postMessage API inside a webworker (just contact with the extension host service directly), but I think the vscode command maybe better in this case.

I tried creating custom command similarly to github1s.vscode.get-browser-url and i am able to register eventListener on window in this way, but i have no clue how to pass information from callback to extension

You can register a command like github1s.vscode.get-browser-url in vscode main service, and then the extension can get the data from vscode main service by using the extension API (vscode.commands.executeCommand('github1s.vscode.get-browser-url', ...arguments)). Noted the Returns of the executeCommand API is the returned value of the given command(the window object works here) and the arguments is the data we can get inside an extension context (webworker).

https://code.visualstudio.com/api/references/vscode-api#commands

You can also register a command in your extension context using the API (vscode.commands.registerCommand). So that we can call the function which comes from the extension context once the vscode is ready when we are in vscode main service.

Hope the information above can help you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chrome extension: sending data to window created with ...
windows.create in my extension. I've got the wiring between content script and background script right. I can right click an element and send...
Read more >
Migrate data of a Chrome extension from one computer to ...
Fortunately, it is possible to individually migrate the extension's specific data in Brave browser, as per this question.
Read more >
Chrome Extensions Message passing
How to pass messages between extensions and content scripts.
Read more >
How to install and manage Azure CLI extensions
To see the Azure CLI extensions provided and maintained by Microsoft, use the az extension list-available command. Azure CLI Copy.
Read more >
Visual Studio Code Frequently Asked Questions
If you don't want to send usage data to Microsoft, you can set the telemetry.telemetryLevel user setting to off . From File >...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found