forked from Darin755/browser-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.js
More file actions
350 lines (317 loc) · 11.6 KB
/
startup.js
File metadata and controls
350 lines (317 loc) · 11.6 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
if(!window.WebAssembly) {//if no web assembly
alert("Your browser is not supported because it doesn't support WebAssembly");
document.write("unsupported - no Web Assembly");
}
//start message
console.log("Welcome to Browser Windows");
//version
window.version = 1.4; //buildroot 2022.8
//parse url
var url = window.location.search.substring(1);
window.params = new URLSearchParams(url);
//mem (MB)
if(window.params.has("mem")) {
if(typeof(window.params.get("mem") == 'number')) {
window.mem = window.params.get("mem");
} else {
alert("mem invalid. reverting to 256mb");
}
} else {
window.mem = 512; //mb
}
//initial state
if(window.params.has("initial")) {
window.initial_state = window.params.get("initial");
console.log("Using " + window.initial_state + " as initial");
} else {
if(checkExistence("state.bin.zst")) {
console.log("Using state.bin.zst as initial");
window.initial_state = "state.bin.zst"
} else {
console.log("No initial State found");
window.initial_state = "";
}
}
//iso
if(window.params.has("iso") != true) {
if(checkExistence("ipxe.iso")) {
window.iso = "ipxe.iso"+"?version="+window.version;
console.log("using "+window.iso+" as iso");
} else {
console.log("ipxe.iso not found. IPXE will NOT BOOT.");
window.iso = "";
}
} else {
if(window.initial_state == "" && checkExistence(window.params.get("iso"))) { //if it doesn't exist
window.iso = window.params.get("iso")+"?version="+window.version;
window.initial_state = "";
} else {
console.log("invalid iso");
alert("user supplied iso not found");
window.iso = "";
}
}
//screen
window.screen = false; //default value
if(window.params.has("screen")) {
if(window.params.get("screen") == "true") {
window.screen = true; //don't hide the screen after boot
} else {
document.getElementById("screen_container").style.display = "none";
document.getElementById("screenButton").innerHTML = "show screen";
}
}
//cmd
if(window.params.has("cmd") && window.params.get("cmd") != "") {
if(window.cmd == undefined) {
window.cmd = window.params.get("cmd");
} else {
window.cmd = window.cmd + "&&" + window.params.get("cmd");
}
}
//autosave
if(window.params.has("autosave")) {
console.log("enabling web storage persist");
window.persist = true;
if(typeof(window.params.get("autosave") == 'number') && window.params.get("autosave") > 0) {
window.autosaveTime = window.params.get("autosave");
console.log("using "+ window.autosaveTime + "seconds as autosave interval");
} else {
console.log("no options were provided using default 30 seconds");
window.autosaveTime = 30; //30 seconds
}
}
//embed
if(window.params.has("embed") && window.params.get("embed") == "true") {
console.log("running in embeded mode");
var fluffs = document.getElementsByClassName("fluff");
for(var i = 0;i<fluffs.length;i++) {
fluffs[i].style.display = "none";
}
document.body.append(document.getElementById("save"));
document.body.append(document.getElementById("clear_save"));
document.body.append(document.getElementById("autosave_toggle"));
window.persist = false;
}
//autosave restore
if(localStorage.getItem("autosave") == 'true') {
document.getElementById("autosave_toggle").innerHTML = "disable autosave";
window.persist = true;
}
window.boot = false; //not booted
//warn if not initial state or iso
if(window.iso == "" && window.initial_state == "") {
alert("No bootable devices found");
}
//start v86
var emulator = window.emulator = new V86Starter({
wasm_path: "lib/v86/v86.wasm",
memory_size: window.mem * 1024 * 1024,
vga_memory_size: 16 * 1024 * 1024,
network_relay_url: "wss://relay.widgetry.org/",
screen_container: document.getElementById("screen_container"),
serial_container_xtermjs: document.getElementById("terminal"),
bios: {
url: "lib/bios/seabios.bin",
},
vga_bios: {
url: "lib/bios/vgabios.bin",
},
filesystem:{
basefs: "contents.json",
baseurl: "flat/",
},
cdrom: {
url: ipxe.iso,
async: (window.params.has("async") && (window.params.get("async") == "true")) ,//async
},
initial_state: { url: window.initial_state },
autostart: true,
preserve_mac_from_state_image: true,
//disable_keyboard: true
});
//configure page
var term_div = document.getElementById("terminal");
var waiting_text = document.getElementById("waiting_text");
//commented out because of issues with slow devices
// setTimeout(function() {//wait for xterm
// document.getElementById("terminal").style.display = "none";
// },1200);//hide screen
waiting_text.style.display = "block";
document.getElementById("toolbox_div").style.display = "none";//hide toolbox
var data = "";
//check for save
loadSaves();//run the function below
function loadSaves() {
if(emulator.is_running()) {//check if it is loaded
window.autosave_lock = false; //prevent race conditions
localforage.getItem("snapshot-"+window.params.get("iso")).then(function(value) {
if(value != null) {
state = value;
emulator.restore_state(state);
if(window.boot != true) { //if there is a open app, refresh it
emulator.serial0_send("\033"+"\x0c"); //esc followed by ctrl-l to refresh terminal
document.getElementById("screen_container").style.display = "none";//hide the screen
document.getElementById("screenButton").innerHTML = "show screen";
}
document.getElementById("save_time").innerHTML = "restored from save at "+getTimestamp();
document.getElementById("storage_error").style.display = "none";
if(localStorage.getItem("noupdates") != 'true') {
checkForUpdates();
} else {
console.log("user opted out of rootfs updates - not checking");
}
}
}).catch(function(err) {//error
console.log("error with web storage: "+err);
window.persist = false;//no autosave
document.getElementById("save").disabled = true;
document.getElementById("autosave_toggle").disabled = true;
document.getElementById("clear_save").disabled = true;
document.getElementById("storage_error").style.display = "block";
});
//send enter for the loading initial state
if(window.initial_state != "") {
emulator.serial0_send('\n');
}
} else {//v86 isn't ready
setTimeout(loadSaves,1000);//check every second until ready
}
}
//wait for boot
emulator.add_listener("serial0-output-char", function(char) {
if(char !== "\r")
{
data += char;
}
if(data.endsWith("$ ") || data.endsWith("# "))
{
//time to boot
if(window.boot == false) {
console.log("Boot successful");//boot successful
emulator.serial0_send("$HOME/.profile\n");//input after restore
document.getElementById("terminal").style.display = "block";//show the terminal
if(!window.screen) {//continue showing the screen if false
document.getElementById("screen_container").style.display = "none";//hide the screen and waiting text
document.getElementById("waiting_text").style.display = "none";
document.getElementById("screenButton").innerHTML = "show screen";
}
waiting_text.style.display = "none";
document.getElementById("boot_time").innerHTML = (Math.round(performance.now()/100)/10);
window.boot = true;
if(window.cmd != undefined) {//cmd
emulator.serial0_send(window.cmd + "\n");
}
if(window.persist == true) {
window.autosave_loop = setInterval(function() {
startAutosave(true); //run autosave every 30 seconds
console.log("autosaved");
}, window.autosaveTime*1000);
}
}
}
});
var state;
function save() {
emulator.save_state(function(error, new_state){
if(error){
console.log(error);
}
state = new_state;
document.getElementById("save_time").innerHTML = "saved to temporary at "+getTimestamp();
});
}
function restore() {
emulator.restore_state(state);
document.getElementById("save_time").innerHTML = "restored from temporary at "+getTimestamp();
}
function getTimestamp() {
var d = new Date();
var hours;
var ampm;
var min;
if(d.getHours() > 12) {
hours = d.getHours() - 12;
ampm = "pm";
} else {
hours = d.getHours();
ampm = "am"
}
if(d.getMinutes() < 10) {
min = "0" + d.getMinutes();
} else {
min = d.getMinutes();
}
return hours+":"+min+" "+ampm;
}
//autosave
async function startAutosave(auto) {
if(!window.autosave_lock) {
window.autosave_lock = true;
document.getElementById("save_time").innerHTML = "saving . . .";
localStorage.setItem("version", window.version);
var temp_state = await emulator.save_state();
localforage.setItem("snapshot-"+window.params.get("iso"), temp_state).then(function () {
window.autosave_lock = false;
});
if(auto) {
document.getElementById("save_time").innerHTML = "autosaved at "+getTimestamp();
} else { //just a normal save
document.getElementById("save_time").innerHTML = "saved at "+getTimestamp();
console.log("saved");
}
}
}
function checkForUpdates() {//updates
console.log("checking for updates . . .");
var current_version = localStorage.getItem("version");
if(current_version != null) {
if(current_version < window.version) {
console.log("newer version detected. Delete saved data to get latest");
if(window.confirm("There is a newer version of the rootfs available. Delete all data and reload Now?") ) {
delete_data();
} else if (!window.confirm("Would you like to see this message again?")) {
localStorage.setItem("noupdates",true);
console.log("user opted out of updates");
alert("You will no longer be receiving any rootfs updates - Delete save to opt back in");
}
} else {
console.log("no updates available");
}
} else {
console.log("no version number (old save?)");
localStorage.setItem("version",window.version); //blindly set it but that the best that we can do
}
}
//listen for postMessage
window.addEventListener("message", (event) => {
var data = event.data;
if(data == "toggleScreen") {
toggleScreen();
} else if(data == "save") {
startAutosave();
} else if(data == "pause") {
emulator.stop();
} else if(data == "play") {
emulator.run();
} else if(data == "togglePause") {
if(emulator.is_running()) {
emulator.stop();
} else {
emulator.run();
}
} else if(data.indexOf("cmd=") == 0) {
emulator.serial0_send(data.substring(4)+'\n');
}
});
//check if file exists on server
function checkExistence(filename) {
var http = new XMLHttpRequest();
http.open('HEAD', window.location.href.split('?')[0] +filename, false);
http.send();
if(http.status == 200) {
return true;
} else {
return false;
}
}