forked from snaplet/postgres-wasm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
320 lines (274 loc) · 9.28 KB
/
index.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>Postgres in browser</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/css/xterm.css" />
<link rel="stylesheet" href="./styles.css" />
<script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@2/dist/zero-md.min.js"></script>
<script src="https://unpkg.com/[email protected]/lib/xterm-addon-fit.js"></script>
<script src="https://unpkg.com/[email protected]/lib/xterm.js"></script>
<script src="./lib/libv86.js"></script>
<script>
window.onload = () => {
const baseOptions = {
wasm_path: "./lib/v86.wasm",
memory_size: 128 * 1024 * 1024,
filesystem: {
basefs: "filesystem/filesystem.json",
baseurl: "filesystem/",
},
screen_container: document.getElementById("screen_container"),
serial_container_xtermjs: document.getElementById("terminal"),
network_relay_url: "wss://relay.widgetry.org/",
autostart: true,
disable_keyboard: true,
disable_mouse: true,
disable_speaker: true,
acpi: true,
};
const params = new URL(document.location).searchParams;
const boot = params.get("boot") === "true";
const sql = params.get("sql");
const md = params.get("md");
if (sql) {
fetch(sql, { method: 'GET' }).then(
async i => {
if (!i.ok) throw new Error("Failed to fetch file");
let m = new Uint8Array(await i.arrayBuffer());
emulator.add_listener("emulator-ready", function () {
initTerm();
setTimeout(() => {
emulator.create_file("/mnt/data.sql", m);
}, 0);
setTimeout(() => {
emulator.serial0_send(`\\i /mnt/data.sql\n`);
}, 0);
});
}).catch(
e => console.error(e)
);
}
if (md) {
const app = document.querySelector('zero-md')
const run = async () => {
app.src = md
await app.render({
// The class `line-numbers` will be added to the markdown-body container
classes: 'line-numbers',
// These are Marked options
gfm: false,
mangle: false
})
}
run()
}
if (boot) {
document.getElementById("screen_container").style = "display:block";
document.getElementById("terminal").style = "";
}
const bzimageUrl = "./filesystem/27d6e559.bin";
const options = {
...baseOptions,
...(boot
? {
bzimage: {
url: bzimageUrl,
},
cmdline: [
"rw",
"root=host9p rootfstype=9p rootflags=version=9p2000.L,trans=virtio,cache=loose quiet acpi=off console=ttyS0 tsc=reliable mitigations=off random.trust_cpu=on nowatchdog page_poison=on",
].join(" "),
bios: {
url: "./bios/seabios.bin",
},
vga_bios: {
url: "./bios/vgabios.bin",
},
}
: { initial_state: { url: "./state/v86state.bin.zst" } }),
};
var emulator = new V86Starter(options);
function initTerm() {
const fitAddon = new FitAddon.FitAddon();
emulator.serial_adapter.term.loadAddon(fitAddon);
fitAddon.fit();
window.addEventListener("resize", () => fitAddon.fit());
}
function sendBackgroundCommands(commands) {
const filename = `${(Math.random() + 1).toString(36).substring(7)}.sh`;
emulator.create_file(
`/inbox/${filename}`,
new TextEncoder("UTF-8").encode(commands.join("\n"))
);
}
if (!boot) {
emulator.add_listener("emulator-ready", function () {
initTerm();
setTimeout(() => {
sendBackgroundCommands(["/etc/init.d/S40network restart"]);
emulator.serial0_send(
`\\! stty rows ${emulator.serial_adapter.term.rows} cols ${emulator.serial_adapter.term.cols} && reset\n`
);
emulator.serial_adapter.term.focus();
}, 0);
});
}
var state;
document.getElementById("save_restore").onclick = async function () {
var button = this;
if (state) {
button.value = "Save state";
await emulator.restore_state(state);
state = undefined;
} else {
const new_state = await emulator.save_state();
console.log("Saved state of " + new_state.byteLength + " bytes");
button.value = "Restore state";
state = new_state;
}
button.blur();
};
document.getElementById("save_file").onclick = async function () {
const new_state = await emulator.save_state();
var a = document.createElement("a");
a.download = "v86state.bin";
a.href = window.URL.createObjectURL(new Blob([new_state]));
a.dataset.downloadurl =
"application/octet-stream:" + a.download + ":" + a.href;
a.click();
this.blur();
};
document.getElementById("restore_file").onchange = function () {
if (this.files.length) {
var filereader = new FileReader();
emulator.stop();
filereader.onload = async function (e) {
await emulator.restore_state(e.target.result);
emulator.run();
};
filereader.readAsArrayBuffer(this.files[0]);
this.value = "";
}
this.blur();
};
document.getElementById("upload_files").onchange = function (e) {
var files = e.target.files;
for (var i = 0; i < files.length; i++) {
var reader = new FileReader();
reader.onload = (function (file) {
return function (e) {
var data = new TextEncoder("UTF-8").encode(e.target.result);
emulator.create_file("/" + file.name, data);
console.log("uploaded " + file.name);
};
})(files[i]);
reader.readAsText(files[i]);
}
};
const uploadBtns = document.getElementsByClassName("upload-btn");
for (let btn of uploadBtns) {
btn.addEventListener("click", (event) => {
const input = btn.getElementsByTagName("input")[0];
input.click(event);
});
}
};
function copyCode(num) {
const shadowContainer = document.getElementById('container').shadowRoot;
const codes = shadowContainer.querySelectorAll('code');
const btnElement = shadowContainer.querySelectorAll('button');
// Create a temporary textarea element to copy the text to
const tempTextArea = document.createElement('textarea');
tempTextArea.value = codes[num].textContent;
shadowContainer.appendChild(tempTextArea);
// Select the text in the textarea and execute the copy command
tempTextArea.select();
document.execCommand('copy');
// Clean up by removing the temporary textarea
shadowContainer.removeChild(tempTextArea);
// Update the button text
btnElement[num].innerText = 'copied';
setTimeout(function() {
btnElement[num].innerText = 'copy';
}, 800);
}
</script>
</head>
<body>
<div id="screen_container" style="display: none">
<div></div>
<canvas></canvas>
</div>
<div class="container">
<zero-md id="container"
manual-render="true">
<template>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.1.0/github-markdown-dark.min.css" />
<style>
:host {
display: block;
position: relative;
contain: content;
box-sizing: border-box;
min-width: 200px;
max-width: 850px;
margin: 0 auto;
padding: 0px;
width: calc(50% - 20px * 2);
height: 100vh;
overflow: scroll;
overflow-x: hidden;
}
.markdown-body {
height: inherit;
font-size: small;
}
h1{
color: white;
padding: 20px 20px;
}
ol>li{
margin-left: 30px;
font-size: 1.2em;
}
pre{
width: 80%;
margin-left: 60px;
border: 1px solid rgb(124, 124, 124);
}
p{
margin-left: 40px;
}
strong{
color: rgb(38, 114, 255);
}
img{
margin-left: 20px;
width: 90%
}
span{
color: rgb(86, 117, 255);
}
button{
position: absolute;
margin-left: 73%;
margin-top: 20px;
width: 70px;
height: 30px;
background-color: rgb(0, 89, 255);
border: 1px solid rgb(0, 89, 255);
border-radius: 0.4em;
}
button:hover{
background-color: rgb(0, 58, 167);
border: rgb(0, 58, 167);
}
</style>
</template>
</zero-md>
<div class="xterminal" style="width: calc(60% + 16px * 2); height: calc(100% - 18px * 2 );">
<div id="terminal"></div>
</div>
</div>
</body>