Skip to content

Commit

Permalink
fix uri of scripts to pass CORS policy (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eskibear authored Nov 3, 2022
1 parent b1c0af3 commit 29796c1
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 37 deletions.
6 changes: 2 additions & 4 deletions src/beginner-tips/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -123,7 +121,7 @@ class BeginnerTipsPage {
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
</body>
</html>`;
Expand Down
11 changes: 6 additions & 5 deletions src/classpath/classpathConfigurationView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ async function initializeWebview(context: vscode.ExtensionContext): Promise<void
}
})));

classpathConfigurationPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/classpath/index.js"));
classpathConfigurationPanel.webview.html = getHtmlForWebview(classpathConfigurationPanel.webview, context.asAbsolutePath("./out/assets/classpath/index.js"));

await checkRequirement();
}

function getHtmlForWebview(scriptPath: string) {
function getHtmlForWebview(webview: vscode.Webview, scriptPath: string) {
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
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();
return `<!DOCTYPE html>
Expand All @@ -115,7 +116,7 @@ function getHtmlForWebview(scriptPath: string) {
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
<div id="content"></div>
</body>
</html>
`;
}
Expand Down Expand Up @@ -148,7 +149,7 @@ async function checkRequirement(): Promise<boolean> {
return false;
}

await javaExt.activate();
await javaExt.activate();
lsApi = javaExt.exports;

if (lsApi) {
Expand Down
21 changes: 11 additions & 10 deletions src/ext-guide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 `<!DOCTYPE html>
Expand Down Expand Up @@ -113,7 +114,7 @@ function getHtmlForWebview(scriptPath: string) {
</div>
<div class="col-5">
<div class="tab-content">
<!-- Basics -->
<div class="tab-pane fade show active" id="panel-basics" role="tabpanel" aria-labelledby="tab-basics">
<table class="table table-borderless table-hover table-sm">
Expand All @@ -125,7 +126,7 @@ function getHtmlForWebview(scriptPath: string) {
<label class="form-check-label" for="chk.redhat.java">
Language Support for Java by Red Hat
</label>
</div>
</td>
</tr>
Expand Down Expand Up @@ -182,7 +183,7 @@ function getHtmlForWebview(scriptPath: string) {
</tbody>
</table>
</div>
<!-- Frameworks -->
<div class="tab-pane fade" id="panel-frameworks" role="tabpanel" aria-labelledby="tab-frameworks">
<table class="table table-borderless table-hover table-sm">
Expand Down Expand Up @@ -220,7 +221,7 @@ function getHtmlForWebview(scriptPath: string) {
</tbody>
</table>
</div>
<!-- Application Servers -->
<div class="tab-pane fade" id="panel-app-servers" role="tabpanel" aria-labelledby="tab-app-servers">
<table class="table table-borderless table-hover table-sm">
Expand All @@ -238,7 +239,7 @@ function getHtmlForWebview(scriptPath: string) {
</tbody>
</table>
</div>
<!-- Keymaps -->
<div class="tab-pane fade" id="panel-keymaps" role="tabpanel" aria-labelledby="tab-keymaps">
<table class="table table-borderless table-hover table-sm">
Expand Down Expand Up @@ -266,7 +267,7 @@ function getHtmlForWebview(scriptPath: string) {
</tbody>
</table>
</div>
</div>
</div>
<div class="col-4">
Expand Down Expand Up @@ -324,7 +325,7 @@ function getHtmlForWebview(scriptPath: string) {
</div>
</div>
</body>
</html>
`;
}
Expand Down
3 changes: 2 additions & 1 deletion src/formatter-settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!DOCTYPE html>
Expand Down
8 changes: 3 additions & 5 deletions src/install-jdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class InstallJdkPage {
webviewPanel?: vscode.WebviewPanel;
beside?: boolean;
}) {

let column = vscode.ViewColumn.Active;
if (options?.beside) {
// "smart" Beside
Expand Down Expand Up @@ -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();
Expand All @@ -157,7 +155,7 @@ class InstallJdkPage {
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
</body>
</html>`;
Expand Down
9 changes: 5 additions & 4 deletions src/java-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();

Expand All @@ -177,7 +178,7 @@ function getHtmlForWebview(scriptPath: string) {
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
<div id="content"></div>
</body>
</html>`;
}

Expand Down
11 changes: 6 additions & 5 deletions src/overview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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 `<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -297,6 +298,6 @@ function getHtmlForWebview(scriptPath: string) {
</div>
</div>
</body>
</html>`;
}
7 changes: 4 additions & 3 deletions src/welcome/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 `<!DOCTYPE html>
Expand Down

0 comments on commit 29796c1

Please sign in to comment.