• Public
  • Public/Protected
  • All

Displays html content, similarly to an iframe.


  • Webview




Content settings for the webview.


html: string

HTML contents of the webview.

This should be a complete, valid html document. Changing this property causes the webview to be reloaded.

Webviews are sandboxed from normal extension process, so all communication with the webview must use message passing. To send a message from the extension to the webview, use postMessage. To send message from the webview back to an extension, use the acquireVsCodeApi function inside the webview to get a handle to the editor's api and then call .postMessage():

    const vscode = acquireVsCodeApi(); // acquireVsCodeApi can only be invoked once
    vscode.postMessage({ message: 'hello!' });

To load a resources from the workspace inside a webview, use the asWebviewUri method and ensure the resource's directory is listed in WebviewOptions.localResourceRoots.

Keep in mind that even though webviews are sandboxed, they still allow running scripts and loading arbitrary content, so extensions must follow all standard web security best practices when working with webviews. This includes properly sanitizing all untrusted input (including content from the workspace) and setting a content security policy.

Readonly onDidReceiveMessage

onDidReceiveMessage: Event<any>

Fired when the webview content posts a message.

Webview content can post strings or json serializable objects back to an extension. They cannot post Blob, File, ImageData and other DOM specific objects since the extension that receives the message does not run in a browser environment.

Readonly cspSource

cspSource: string

Content security policy source for webview resources.

This is the origin that should be used in a content security policy rule:

img-src https: ${webview.cspSource} ...;



  • postMessage(message: any): Thenable<boolean>
  • Post a message to the webview content.

    Messages are only delivered if the webview is live (either visible or in the background with retainContextWhenHidden).


    • message: any

      Body of the message. This must be a string or other json serializable object.

      For older versions of vscode, if an ArrayBuffer is included in message, it will not be serialized properly and will not be received by the webview. Similarly any TypedArrays, such as a Uint8Array, will be very inefficiently serialized and will also not be recreated as a typed array inside the webview.

      However if your extension targets vscode 1.57+ in the engines field of its package.json, any ArrayBuffer values that appear in message will be more efficiently transferred to the webview and will also be correctly recreated inside of the webview.

    Returns Thenable<boolean>


  • asWebviewUri(localResource: Uri): Uri
  • Convert a uri for the local file system to one that can be used inside webviews.

    Webviews cannot directly load resources from the workspace or local file system using file: uris. The asWebviewUri function takes a local file: uri and converts it into a uri that can be used inside of a webview to load the same resource:

    webview.html = `<img src="${webview.asWebviewUri(vscode.Uri.file('/Users/codey/workspace/cat.gif'))}">`


    • localResource: Uri

    Returns Uri

VS Code 插件开发中文文档 | VS Code 中文文档 | VS Code 官网文档 | VS Code 扩展市场

Generated by TypeDoc | 站长统计