From 708bc3090845be767c92b0fe53fe855a0fdca985 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Fri, 13 Sep 2019 16:37:37 -0700 Subject: [PATCH] Use `asWebviewUri` instead of vscode-resource directly --- webview-sample/package-lock.json | 6 +++--- webview-sample/package.json | 4 ++-- webview-sample/src/extension.ts | 21 +++++++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/webview-sample/package-lock.json b/webview-sample/package-lock.json index 460b7e11..33a619f1 100644 --- a/webview-sample/package-lock.json +++ b/webview-sample/package-lock.json @@ -31,9 +31,9 @@ "dev": true }, "@types/vscode": { - "version": "1.33.0", - "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.33.0.tgz", - "integrity": "sha512-JSmGiValbrcG5g20jjCfKakLiuWyrcjVezj+SEAEZ4klXQktE5EtowuGlkLVqbkiBK4iY5wy/4yW8OjecuHnjQ==", + "version": "1.38.0", + "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.38.0.tgz", + "integrity": "sha512-aGo8LQ4J1YF0T9ORuCO+bhQ5sGR1MXa7VOyOdEP685se3wyQWYUExcdiDi6rvaK61KUwfzzA19JRLDrUbDl7BQ==", "dev": true }, "ansi-styles": { diff --git a/webview-sample/package.json b/webview-sample/package.json index 6d590694..0e60167e 100644 --- a/webview-sample/package.json +++ b/webview-sample/package.json @@ -4,7 +4,7 @@ "version": "0.0.1", "publisher": "vscode-samples", "engines": { - "vscode": "^1.32.0" + "vscode": "^1.38.0" }, "categories": [ "Other" @@ -44,6 +44,6 @@ "@types/node": "^10.5.2", "tslint": "^5.16.0", "typescript": "^3.5.1", - "@types/vscode": "^1.32.0" + "@types/vscode": "^1.38.0" } } diff --git a/webview-sample/src/extension.ts b/webview-sample/src/extension.ts index c8403708..b85edab1 100644 --- a/webview-sample/src/extension.ts +++ b/webview-sample/src/extension.ts @@ -137,37 +137,38 @@ class CatCodingPanel { } private _update() { - const z = 1 + 2; + const webview = this._panel.webview; + // Vary the webview's content based on where it is located in the editor. switch (this._panel.viewColumn) { case vscode.ViewColumn.Two: - this._updateForCat('Compiling Cat'); + this._updateForCat(webview, 'Compiling Cat'); return; case vscode.ViewColumn.Three: - this._updateForCat('Testing Cat'); + this._updateForCat(webview, 'Testing Cat'); return; case vscode.ViewColumn.One: default: - this._updateForCat('Coding Cat'); + this._updateForCat(webview, 'Coding Cat'); return; } } - private _updateForCat(catName: keyof typeof cats) { + private _updateForCat(webview: vscode.Webview, catName: keyof typeof cats) { this._panel.title = catName; - this._panel.webview.html = this._getHtmlForWebview(cats[catName]); + this._panel.webview.html = this._getHtmlForWebview(webview, cats[catName]); } - private _getHtmlForWebview(catGif: string) { + private _getHtmlForWebview(webview: vscode.Webview, catGifPath: string) { // Local path to main script run in the webview const scriptPathOnDisk = vscode.Uri.file( path.join(this._extensionPath, 'media', 'main.js') ); // And the uri we use to load this script in the webview - const scriptUri = scriptPathOnDisk.with({ scheme: 'vscode-resource' }); + const scriptUri = webview.asWebviewUri(scriptPathOnDisk); // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); @@ -181,13 +182,13 @@ class CatCodingPanel { Use a content security policy to only allow loading images from https or from our extension directory, and only allow scripts that have a specific nonce. --> - + Cat Coding - +

0