-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathwebview.ts
66 lines (56 loc) · 2.38 KB
/
webview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
export function createWebviewPanel(context: vscode.ExtensionContext) {
const extPath = context.extensionPath;
const panel = vscode.window.createWebviewPanel(
"liveHTMLPreviewer",
"JSON Crack",
vscode.ViewColumn.Beside,
{
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [
vscode.Uri.file(path.join(extPath, "build")),
vscode.Uri.file(path.join(extPath, "build", "static")),
vscode.Uri.file(path.join(extPath, "build", "static", "js")),
vscode.Uri.file(path.join(extPath, "build", "static", "css")),
vscode.Uri.file(path.join(extPath, "assets")),
],
}
);
panel.iconPath = vscode.Uri.file(path.join(extPath, "build", "assets", "favicon.ico"));
const manifest = JSON.parse(
fs.readFileSync(path.join(extPath, "build", "asset-manifest.json"), "utf-8")
);
const mainScript = manifest.files["main.js"];
const mainStyle = manifest.files["main.css"];
const scriptPathOnDisk = vscode.Uri.file(path.join(extPath, "build", mainScript));
const stylePathOnDisk = vscode.Uri.file(path.join(extPath, "build", mainStyle));
const stylesMainUri = panel.webview.asWebviewUri(stylePathOnDisk);
const scriptUri = panel.webview.asWebviewUri(scriptPathOnDisk);
const nonce = getNonce();
panel.webview.html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="${panel.webview.asWebviewUri(vscode.Uri.file(path.join(extPath, "build")))}/">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' ${panel.webview.cspSource} blob:; connect-src ${panel.webview.cspSource} blob:; script-src 'unsafe-eval' 'unsafe-inline' ${panel.webview.cspSource}; style-src ${panel.webview.cspSource} 'unsafe-inline';">
<link href="${stylesMainUri}" rel="stylesheet">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
return panel;
}
function getNonce() {
let text = "";
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 32; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}