如何将来自VSCODE扩展的消息发布到使用WebViewProvider创建的自定义WebView?
我正在构建一个Vscode扩展程序,其中使用WebView提供商在面板中创建一个自定义选项卡。我想将Extension命令的输出引导到WebView并在HTML脚本中渲染。替代方法是在WebView的HTML脚本内执行VSCODE扩展命令。但是我找不到使用WebView提供商的示例,而是
currentpanel.webview.postmessage({命令:'recactor'});
在我的情况下,这是不确定的,因为我没有创建面板。
Extension.js
let disposable = vscode.commands.registerCommand(
"this is where I want to send data to webview"
...
);
var thisProvider={
resolveWebviewView:function(thisWebview, thisWebviewContext, thisToke){
thisWebview.webview.options={enableScripts:true}
thisWebview.webview.html=`<!DOCTYPE html>
<html>
<body>
<div id="results" style="white-space: pre;" />
<script>
const resultsEl = document.getElementById("results");
window.addEventListener('message', event => {
const message = event.data; // The JSON data our extension sent
switch (message.command) {
case 'results':
console.log(results);
break;
}
});
</script>
</body>
</html>`;
}
}
context.subscriptions.push(
vscode.window.registerWebviewViewProvider("monitor.output", thisProvider)
);
package.json:
"contributes": {
"commands": [
{
"command": "monitor.listen",
"title": "Connect"
}
],
"menus": {
"view/title": [
{
"command": "monitor.listen",
"group": "navigation",
"when": "view == monitor.output"
}
]
},
"viewsContainers": {
"panel": [
{
"id": "monitor",
"title": "Monitor",
"icon": "resources/monitor.jpeg"
}
]
},
"views": {
"monitor": [
{
"type": "webview",
"id": "monitor.output",
"name": "Monitor"
}
]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也一直在寻找这个,但是实际上没有例子说明如何添加它。
您可以在提供商中获取并保存对当前
WebView
内部 resolvewebview 函数。将视图存储在私有实例变量
private _view?:vscode.webviewView;
中并在公共方法中使用它public postmessagetowebview(message:nyy)
提供者代码:
Note :
MEDIA/MAIN.JS
。reset.css
和vscode.css
可以从a vs代码示例console.log
消息,您可以打开DEV。扩展主机与代码实例中的工具通过击中ctrl+shif+shif+p
和typeOpen WebView dev
- 也许您必须打开/关闭WebView才能生成新的控制台日志。您可以在
extension.ts
中使用它:将其添加到
package.json
上,因此命令&amp;视图将可用:可以在此代码的多个位置添加WebViews,它将添加到Explorer视图中。
使用WebView脚本中的以下代码从扩展程序中获取消息:
对于另一个“方向” Web视图到扩展通信。
您可以在WebView Content脚本中使用:
global
tsvscode
变量是在_gethtmlforwebview
中生成的,acepirevscodeapi()
。要修复全局
tsvscode
的打字。 install@types/vscode-webview
with npm asdevDependentys
,然后将global.d.t.ts
添加到您的WebView脚本中,并带有此内容:或者您不喜欢全局变量
tsvscode
和上面的键入。您还可以像以下内容一样创建VS代码API包装器WebView-ui/src/utilities/vscode.ts“ rel =“ nofollow noreferrer”>存储库。只是要运行此代码,创建一个扩展名,如通过在终端中运行
yo Code
,使用Yeoman Generator。您还可以在以下github
I was looking for this too, but there is really no example showing how this could be added.
You can get and save the reference to the current
webView
insideresolveWebView
function in your provider.Store the view in a private instance variable
private _view?: vscode.WebviewView;
and use it in a public methodpublic postMessageToWebview(message: any)
The provider code:
Note:
media/main.js
with the code from the webview snippet below.reset.css
andvscode.css
can be downloaded from a VS Code exampleconsole.log
messages you can open your dev. tools in your extension host VS code instance by hittingctrl+shif+p
and typeOpen webview dev
- maybe you have to open/close your webview to generate new console logs.You can use it in your
extension.ts
:Add it to the
package.json
so the command & view will be available:Webviews can be added at multiple locations in this code it will be added to the explorer view.
Use the following code in your webview script to get the message from the extension:
For the other "direction" web view to extension communication.
You can use in your webview content script:
The global
tsvscode
variable is generated in_getHtmlForWebview
withacquireVsCodeApi()
.To fix the typing for the global
tsvscode
. Install@types/vscode-webview
with npm asdevDependency
and add aglobal.d.ts
to your webview script with this content:Or if you don't like the global variable
tsvscode
and the typing above. You could also create a VS Code API wrapper like in the following repository.Just to get this code running, create an extension as mentioned in the Getting Started Guide with Yeoman generator by running
yo code
in your terminal.You can also find the snippets in the following Github gist.
我遇到了同样的问题。我想在WebView的顶部栏中注册
操作按钮之一
,该网络浏览量显示在“ Explore”选项卡中作为侧边栏显示,以刷新我的WebView中的内容。上面的解决方案在一个警告中非常有效:我需要处理vscode-&gt; webview和webview- vscode互动。这意味着,当我单击刷新按钮时,将触发一个事件到包含一些数据的WebView,摘录下面显示:
此消息,然后需要由WebView处理,因此,为此我必须在内部进行以下内容我的WebView提供商中的脚本:
> resolvewebviewView 函数中如下:
现在,tsvscode可以通过发布消息来接触我的WebView事件侦听器,该消息将其处理在
resolve 页面!
我所描述的内容实现了通信vscode-&gt; webview和webview-&gt; vscode的两个方向。您可能想知道,为什么您不能只在网络浏览量上发布消息,然后在同一WebView上使用ondidrecieve处理?我想知道同一件事,这似乎没有正确的事件侦听器直接注册以直接工作,这就是为什么我们添加
addeventListener
的解决方法似乎可以使用。我希望这会有所帮助!
I ran in to the same problem. I wanted to register one of the
action buttons
in the top bar of a webview that shows up as a sidebar in the explore tab to refresh the content in my webview.The solution above worked very well with one caveat: I needed to both handle the vscode->webview and webview->vscode interaction. What this means is, when I clicked my refresh button, an event is triggered to the webview that contains some data, excerpt shown below:
This message, then needed to be handled by the webview, so for that I had to do the following inside a script in my webview provider:
Now, tsvscode can reach out to my webview event listener by posting the message, which handles it inside the
resolveWebviewView
function as follows:which finally resets the page!
What I have describes implements both directions of communication vscode->webview and webview->vscode. You might be wondering, why couldn't you just post the message on the webview and then handle it with the onDidRecieve on the same webview? I am wondering the same thing, that doesn't seem to have the proper event listener registered to work directly, which is why this workaround where we add
addEventListener
seems to work instead.I hope this helps!