From 29796c11b4d538e756c04b8700cbf87b45bd894f Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Thu, 3 Nov 2022 23:41:17 +0800 Subject: [PATCH] fix uri of scripts to pass CORS policy (#1103) --- src/beginner-tips/index.ts | 6 ++---- src/classpath/classpathConfigurationView.ts | 11 ++++++----- src/ext-guide/index.ts | 21 +++++++++++---------- src/formatter-settings/index.ts | 3 ++- src/install-jdk/index.ts | 8 +++----- src/java-runtime/index.ts | 9 +++++---- src/overview/index.ts | 11 ++++++----- src/welcome/index.ts | 7 ++++--- 8 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/beginner-tips/index.ts b/src/beginner-tips/index.ts index 83098e04..d0029465 100644 --- a/src/beginner-tips/index.ts +++ b/src/beginner-tips/index.ts @@ -105,9 +105,7 @@ class BeginnerTipsPage { private _getHtmlForWebview() { const scriptPathOnDisk = vscode.Uri.file(path.join(this._extensionPath, 'out', "assets", "beginner-tips", "index.js")); - - // const scriptUri = this._panel.webview.asWebviewUri(scriptPathOnDisk); - const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" }); + const scriptUri = this._panel?.webview.asWebviewUri(scriptPathOnDisk); // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); @@ -123,7 +121,7 @@ class BeginnerTipsPage {
- + `; diff --git a/src/classpath/classpathConfigurationView.ts b/src/classpath/classpathConfigurationView.ts index feed1bf6..db883b58 100644 --- a/src/classpath/classpathConfigurationView.ts +++ b/src/classpath/classpathConfigurationView.ts @@ -93,14 +93,15 @@ async function initializeWebview(context: vscode.ExtensionContext): Promise @@ -115,7 +116,7 @@ function getHtmlForWebview(scriptPath: string) {
- + `; } @@ -148,7 +149,7 @@ async function checkRequirement(): Promise { return false; } - await javaExt.activate(); + await javaExt.activate(); lsApi = javaExt.exports; if (lsApi) { diff --git a/src/ext-guide/index.ts b/src/ext-guide/index.ts index 85270369..a42837b7 100644 --- a/src/ext-guide/index.ts +++ b/src/ext-guide/index.ts @@ -35,7 +35,7 @@ async function initializeJavaExtGuideView(context: vscode.ExtensionContext, webv dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg")) }; - webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/ext-guide/index.js")); + webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/ext-guide/index.js")); context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback)); context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage(async (e) => { @@ -68,9 +68,10 @@ async function initializeJavaExtGuideView(context: vscode.ExtensionContext, webv syncExtensionStatus(); } -function getHtmlForWebview(scriptPath: string) { +function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) { const scriptPathOnDisk = vscode.Uri.file(scriptPath); - const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" }); + const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk); + // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); return ` @@ -113,7 +114,7 @@ function getHtmlForWebview(scriptPath: string) {
- +
@@ -125,7 +126,7 @@ function getHtmlForWebview(scriptPath: string) { - + @@ -182,7 +183,7 @@ function getHtmlForWebview(scriptPath: string) {
- +
@@ -220,7 +221,7 @@ function getHtmlForWebview(scriptPath: string) {
- +
@@ -238,7 +239,7 @@ function getHtmlForWebview(scriptPath: string) {
- +
@@ -266,7 +267,7 @@ function getHtmlForWebview(scriptPath: string) {
- +
@@ -324,7 +325,7 @@ function getHtmlForWebview(scriptPath: string) {
- + `; } diff --git a/src/formatter-settings/index.ts b/src/formatter-settings/index.ts index 961458cf..c58e6a75 100644 --- a/src/formatter-settings/index.ts +++ b/src/formatter-settings/index.ts @@ -149,7 +149,8 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi private getHtmlForWebview(scriptPath: string) { const scriptPathOnDisk = vscode.Uri.file(scriptPath); - const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" }); + const scriptUri = this.webviewPanel?.webview.asWebviewUri(scriptPathOnDisk); + // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); return ` diff --git a/src/install-jdk/index.ts b/src/install-jdk/index.ts index 63201fcb..96a44c89 100644 --- a/src/install-jdk/index.ts +++ b/src/install-jdk/index.ts @@ -37,7 +37,7 @@ class InstallJdkPage { webviewPanel?: vscode.WebviewPanel; beside?: boolean; }) { - + let column = vscode.ViewColumn.Active; if (options?.beside) { // "smart" Beside @@ -139,9 +139,7 @@ class InstallJdkPage { private _getHtmlForWebview() { const scriptPathOnDisk = vscode.Uri.file(path.join(this._extensionPath, 'out', "assets", "install-jdk", "index.js")); - - // const scriptUri = this._panel.webview.asWebviewUri(scriptPathOnDisk); - const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" }); + const scriptUri = this._panel?.webview.asWebviewUri(scriptPathOnDisk); // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); @@ -157,7 +155,7 @@ class InstallJdkPage {
- + `; diff --git a/src/java-runtime/index.ts b/src/java-runtime/index.ts index 9ef6d584..69e99108 100644 --- a/src/java-runtime/index.ts +++ b/src/java-runtime/index.ts @@ -41,7 +41,7 @@ async function initializeJavaRuntimeView(context: vscode.ExtensionContext, webvi light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")), dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg")) }; - webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/java-runtime/index.js")); + webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/java-runtime/index.js")); context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback)); context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage(async (e) => { @@ -159,9 +159,10 @@ async function initializeJavaRuntimeView(context: vscode.ExtensionContext, webvi } } -function getHtmlForWebview(scriptPath: string) { +function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) { const scriptPathOnDisk = vscode.Uri.file(scriptPath); - const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" }); + const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk); + // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); @@ -177,7 +178,7 @@ function getHtmlForWebview(scriptPath: string) {
- + `; } diff --git a/src/overview/index.ts b/src/overview/index.ts index f70f1ca2..85b492e3 100644 --- a/src/overview/index.ts +++ b/src/overview/index.ts @@ -51,7 +51,7 @@ async function initializeOverviewView(context: vscode.ExtensionContext, webviewP light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")), dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg")) }; - webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/overview/index.js")); + webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/overview/index.js")); context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback)); @@ -108,12 +108,13 @@ export class OverviewViewSerializer implements vscode.WebviewPanelSerializer { } } -function getHtmlForWebview(scriptPath: string) { +function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) { const scriptPathOnDisk = vscode.Uri.file(scriptPath); - const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" }); + const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk); + // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); - + return ` @@ -297,6 +298,6 @@ function getHtmlForWebview(scriptPath: string) { - + `; } diff --git a/src/welcome/index.ts b/src/welcome/index.ts index ec5f16b5..03206d65 100644 --- a/src/welcome/index.ts +++ b/src/welcome/index.ts @@ -74,7 +74,7 @@ async function initializeWelcomeView(context: vscode.ExtensionContext, webviewPa light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")), dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg")) }; - webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/welcome/index.js")); + webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/welcome/index.js")); context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback)); context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage((message => { switch (message.command) { @@ -98,9 +98,10 @@ async function initializeWelcomeView(context: vscode.ExtensionContext, webviewPa })); } -function getHtmlForWebview(scriptPath: string) { +function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) { const scriptPathOnDisk = vscode.Uri.file(scriptPath); - const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" }); + const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk); + // Use a nonce to whitelist which scripts can be run const nonce = getNonce(); return `