Skip to content

Commit 29796c1

Browse files
authored
fix uri of scripts to pass CORS policy (#1103)
1 parent b1c0af3 commit 29796c1

File tree

8 files changed

+39
-37
lines changed

8 files changed

+39
-37
lines changed

src/beginner-tips/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ class BeginnerTipsPage {
105105

106106
private _getHtmlForWebview() {
107107
const scriptPathOnDisk = vscode.Uri.file(path.join(this._extensionPath, 'out', "assets", "beginner-tips", "index.js"));
108-
109-
// const scriptUri = this._panel.webview.asWebviewUri(scriptPathOnDisk);
110-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
108+
const scriptUri = this._panel?.webview.asWebviewUri(scriptPathOnDisk);
111109

112110
// Use a nonce to whitelist which scripts can be run
113111
const nonce = getNonce();
@@ -123,7 +121,7 @@ class BeginnerTipsPage {
123121
<body>
124122
<noscript>You need to enable JavaScript to run this app.</noscript>
125123
<div id="root"></div>
126-
124+
127125
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
128126
</body>
129127
</html>`;

src/classpath/classpathConfigurationView.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ async function initializeWebview(context: vscode.ExtensionContext): Promise<void
9393
}
9494
})));
9595

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

9898
await checkRequirement();
9999
}
100100

101-
function getHtmlForWebview(scriptPath: string) {
101+
function getHtmlForWebview(webview: vscode.Webview, scriptPath: string) {
102102
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
103-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
103+
const scriptUri = webview.asWebviewUri(scriptPathOnDisk);
104+
104105
// Use a nonce to whitelist which scripts can be run
105106
const nonce = getNonce();
106107
return `<!DOCTYPE html>
@@ -115,7 +116,7 @@ function getHtmlForWebview(scriptPath: string) {
115116
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
116117
<div id="content"></div>
117118
</body>
118-
119+
119120
</html>
120121
`;
121122
}
@@ -148,7 +149,7 @@ async function checkRequirement(): Promise<boolean> {
148149
return false;
149150
}
150151

151-
await javaExt.activate();
152+
await javaExt.activate();
152153
lsApi = javaExt.exports;
153154

154155
if (lsApi) {

src/ext-guide/index.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function initializeJavaExtGuideView(context: vscode.ExtensionContext, webv
3535
dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg"))
3636
};
3737

38-
webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/ext-guide/index.js"));
38+
webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/ext-guide/index.js"));
3939

4040
context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback));
4141
context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage(async (e) => {
@@ -68,9 +68,10 @@ async function initializeJavaExtGuideView(context: vscode.ExtensionContext, webv
6868
syncExtensionStatus();
6969
}
7070

71-
function getHtmlForWebview(scriptPath: string) {
71+
function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) {
7272
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
73-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
73+
const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk);
74+
7475
// Use a nonce to whitelist which scripts can be run
7576
const nonce = getNonce();
7677
return `<!DOCTYPE html>
@@ -113,7 +114,7 @@ function getHtmlForWebview(scriptPath: string) {
113114
</div>
114115
<div class="col-5">
115116
<div class="tab-content">
116-
117+
117118
<!-- Basics -->
118119
<div class="tab-pane fade show active" id="panel-basics" role="tabpanel" aria-labelledby="tab-basics">
119120
<table class="table table-borderless table-hover table-sm">
@@ -125,7 +126,7 @@ function getHtmlForWebview(scriptPath: string) {
125126
<label class="form-check-label" for="chk.redhat.java">
126127
Language Support for Java by Red Hat
127128
</label>
128-
129+
129130
</div>
130131
</td>
131132
</tr>
@@ -182,7 +183,7 @@ function getHtmlForWebview(scriptPath: string) {
182183
</tbody>
183184
</table>
184185
</div>
185-
186+
186187
<!-- Frameworks -->
187188
<div class="tab-pane fade" id="panel-frameworks" role="tabpanel" aria-labelledby="tab-frameworks">
188189
<table class="table table-borderless table-hover table-sm">
@@ -220,7 +221,7 @@ function getHtmlForWebview(scriptPath: string) {
220221
</tbody>
221222
</table>
222223
</div>
223-
224+
224225
<!-- Application Servers -->
225226
<div class="tab-pane fade" id="panel-app-servers" role="tabpanel" aria-labelledby="tab-app-servers">
226227
<table class="table table-borderless table-hover table-sm">
@@ -238,7 +239,7 @@ function getHtmlForWebview(scriptPath: string) {
238239
</tbody>
239240
</table>
240241
</div>
241-
242+
242243
<!-- Keymaps -->
243244
<div class="tab-pane fade" id="panel-keymaps" role="tabpanel" aria-labelledby="tab-keymaps">
244245
<table class="table table-borderless table-hover table-sm">
@@ -266,7 +267,7 @@ function getHtmlForWebview(scriptPath: string) {
266267
</tbody>
267268
</table>
268269
</div>
269-
270+
270271
</div>
271272
</div>
272273
<div class="col-4">
@@ -324,7 +325,7 @@ function getHtmlForWebview(scriptPath: string) {
324325
</div>
325326
</div>
326327
</body>
327-
328+
328329
</html>
329330
`;
330331
}

src/formatter-settings/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi
149149

150150
private getHtmlForWebview(scriptPath: string) {
151151
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
152-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
152+
const scriptUri = this.webviewPanel?.webview.asWebviewUri(scriptPathOnDisk);
153+
153154
// Use a nonce to whitelist which scripts can be run
154155
const nonce = getNonce();
155156
return `<!DOCTYPE html>

src/install-jdk/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class InstallJdkPage {
3737
webviewPanel?: vscode.WebviewPanel;
3838
beside?: boolean;
3939
}) {
40-
40+
4141
let column = vscode.ViewColumn.Active;
4242
if (options?.beside) {
4343
// "smart" Beside
@@ -139,9 +139,7 @@ class InstallJdkPage {
139139

140140
private _getHtmlForWebview() {
141141
const scriptPathOnDisk = vscode.Uri.file(path.join(this._extensionPath, 'out', "assets", "install-jdk", "index.js"));
142-
143-
// const scriptUri = this._panel.webview.asWebviewUri(scriptPathOnDisk);
144-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
142+
const scriptUri = this._panel?.webview.asWebviewUri(scriptPathOnDisk);
145143

146144
// Use a nonce to whitelist which scripts can be run
147145
const nonce = getNonce();
@@ -157,7 +155,7 @@ class InstallJdkPage {
157155
<body>
158156
<noscript>You need to enable JavaScript to run this app.</noscript>
159157
<div id="root"></div>
160-
158+
161159
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
162160
</body>
163161
</html>`;

src/java-runtime/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function initializeJavaRuntimeView(context: vscode.ExtensionContext, webvi
4141
light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")),
4242
dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg"))
4343
};
44-
webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/java-runtime/index.js"));
44+
webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/java-runtime/index.js"));
4545

4646
context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback));
4747
context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage(async (e) => {
@@ -159,9 +159,10 @@ async function initializeJavaRuntimeView(context: vscode.ExtensionContext, webvi
159159
}
160160
}
161161

162-
function getHtmlForWebview(scriptPath: string) {
162+
function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) {
163163
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
164-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
164+
const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk);
165+
165166
// Use a nonce to whitelist which scripts can be run
166167
const nonce = getNonce();
167168

@@ -177,7 +178,7 @@ function getHtmlForWebview(scriptPath: string) {
177178
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
178179
<div id="content"></div>
179180
</body>
180-
181+
181182
</html>`;
182183
}
183184

src/overview/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function initializeOverviewView(context: vscode.ExtensionContext, webviewP
5151
light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")),
5252
dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg"))
5353
};
54-
webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/overview/index.js"));
54+
webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/overview/index.js"));
5555

5656
context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback));
5757

@@ -108,12 +108,13 @@ export class OverviewViewSerializer implements vscode.WebviewPanelSerializer {
108108
}
109109
}
110110

111-
function getHtmlForWebview(scriptPath: string) {
111+
function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) {
112112
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
113-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
113+
const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk);
114+
114115
// Use a nonce to whitelist which scripts can be run
115116
const nonce = getNonce();
116-
117+
117118
return `<!DOCTYPE html>
118119
<html lang="en">
119120
<head>
@@ -297,6 +298,6 @@ function getHtmlForWebview(scriptPath: string) {
297298
</div>
298299
</div>
299300
</body>
300-
301+
301302
</html>`;
302303
}

src/welcome/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async function initializeWelcomeView(context: vscode.ExtensionContext, webviewPa
7474
light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")),
7575
dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg"))
7676
};
77-
webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/welcome/index.js"));
77+
webviewPanel.webview.html = getHtmlForWebview(webviewPanel, context.asAbsolutePath("./out/assets/welcome/index.js"));
7878
context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback));
7979
context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage((message => {
8080
switch (message.command) {
@@ -98,9 +98,10 @@ async function initializeWelcomeView(context: vscode.ExtensionContext, webviewPa
9898
}));
9999
}
100100

101-
function getHtmlForWebview(scriptPath: string) {
101+
function getHtmlForWebview(webviewPanel: vscode.WebviewPanel, scriptPath: string) {
102102
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
103-
const scriptUri = (scriptPathOnDisk).with({ scheme: "vscode-resource" });
103+
const scriptUri = webviewPanel.webview.asWebviewUri(scriptPathOnDisk);
104+
104105
// Use a nonce to whitelist which scripts can be run
105106
const nonce = getNonce();
106107
return `<!DOCTYPE html>

0 commit comments

Comments
 (0)