-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
scripts.js
412 lines (320 loc) · 12.9 KB
/
scripts.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
var objects = {};
var register = [];
var program = {};
var processList = pid = [];
if(typeof(setting) != "object") {
var setting = {};
}
var systemRuntime = system.runtime = {
"pidmax": 0,
"zindex": 10,
"pos": {
"mouseX": 0,
"mouseY": 0
},
"clicking": 0,
"clicked": 0,
"timeOfBoot": new Date().getTime(), // Log time of boot
"processList": pid,
"pid": pid,
"documentRoot": location.pathname,
"documentHost": document.getElementById("documentRoot").href,
"time": function() {
// TODO: Add potential offset
var result = new Date();
var toDoubleDigit = function(which) {
while (which.toString().length <= 1) {
which = "0" + which;
}
return which;
}
result.whatTime = function() {
return toDoubleDigit(this.getHours()) + ":" + toDoubleDigit(this.getMinutes());
}
result.whatTimeAndSeconds = function() {
return this.whatTime() + ":" + toDoubleDigit(this.getSeconds());
}
result.whatDate = function() {
return this.getFullYear() + "-" + toDoubleDigit(this.getMonth() + 1) + "-" + toDoubleDigit(this.getDate());
}
return result;
}
};
loadsettings();
var objs, result, iattr, attr;
if(!mainos) {
var mainos = {};
}
objects.content = document.getElementsByClassName("content")[0];
objects.progicons = document.getElementsByClassName("icons")[0];
objects.programs = document.getElementsByClassName("programs")[0];
function jsoncombine(which1, which2) {
try {
which1 = Object.assign(which1, which2);
} catch (e) {
objs = [which1, which2];
result = objs.reduce(function(r, o) {
Object.keys(o).forEach(function(k) {
r[k] = o[k];
});
which1 = r;
}, {});
}
return which1;
}
function setDocumentMeta() { // Overrides some things in the document head
document.head.getElementsByTagName("meta").namedItem("theme-color").content = system.user.settings.themecolor;
document.head.getElementsByTagName("meta").namedItem("msapplication-TileColor").content = system.user.settings.themecolor;
}
function loadsettings() {
// setting.username = loadfile("C:/mainos/system32/settings/username.txt");
// setting.userpath = "C:/users/" + setting.username + "/";
// setting.userdata = setting.userpath + "Program Data/";
// setting.settingpath = "C:/users/" + setting.username + "/settings/";
setting.time = new Date();
// setting.backgroundimage = loadsetting("backgroundimage");
setting.temp = {};
// document.documentElement.style.setProperty("--themecolor", setting.themecolor);
// document.documentElement.style.setProperty("--themecolor2", setting.themecolor2);
// document.documentElement.style.setProperty("--font", setting.font);
// document.documentElement.style.setProperty("--hovercolor",setting.hovercolor);
// document.documentElement.style.setProperty("--hovercolornontransparent",setting.hovercolornontransparent);
if(taskbar) {
taskbar.updateSettings(taskbar);
}
document.documentElement.style.setProperty("--taskbarheight", system.user.settings.taskbar.height + "px");
setDocumentMeta();
}
program = system.user.programs;
if (iofs.exists("C:/mainos/customprograms.txt")) {
try {
program = Object.assign(program, ifjsonparse(iofs.load("C:/mainos/customprograms.txt")));
} catch (e) {
objs = [program, ifjsonparse(iofs.load("C:/mainos/customprograms.txt"))];
result = objs.reduce(function(r, o) {
Object.keys(o).forEach(function(k) {
r[k] = o[k];
});
program = r;
}, {});
}
}
if (system.user.settings.enableRepository) { // Load programs from repository if repository is enabled
try {
var xhr = new XMLHttpRequest();
xhr.open("GET", system.osDetails.serverrepository, true); // TODO: Make async
xhr.onload = function() {
// program = Object.assign(program, ifjsonparse((xhr.responseText)));
const repoList = ifjsonparse(xhr.responseText);
setTimeout(function() {
for (let i in repoList) {
system.user.programs[i] = repoList[i];
if(repoList[i].disabled) {
system.user.programs[i] = [];
system.user.programs[i].maxopen = "0";
system.user.programs[i].disabled = true;
system.user.programs[i].spawnicon = false;
system.user.programs[i].src = "about:blank";
continue;
}
loadProgramMetadata(repoList[i]);
}
}, 100)
}
xhr.send();
} catch (e) {}
}
// Add / handle programs
for(var i = 0; i < Object.keys(system.user.programs).length; i++) {
var myProgram = system.user.programs[Object.keys(system.user.programs).sort()[i]];
if(myProgram.disabled) {
continue;
}
loadProgramMetadata(myProgram);
}
function loadProgramMetadata(which) { // Load program metadata from program file and handle some other stuff
if(typeof(which) == "string") {
which = system.user.programs[which];
}
if(which.src && which.src.includes("//")) { // Don't request metadata from external programs. This would result in access denied.
addDesktopIcon(which);
addProgramIconToFolder(which);
which.metaloaded = true;
return;
}
if(which.metaloaded) {
return; // Don't load metadata twice
}
let xhr = new XMLHttpRequest();
xhr.open("GET", which.src, true);
xhr.onload = function() {
let rawMetaString = xhr.responseText.substring(0, 1200); // Get first 1200 characters of program - Everything relevant must be within this range
if(rawMetaString.includes("<head") && rawMetaString.includes("</head>")) { // Only continue if <head> and </head> are present in range
let metaString = rawMetaString.replaceAll(/\<\!\-\-(.*?)\-\-\>/g, ""); // Remove comments
metaString = metaString.split("</head>")[0] // Extract everything before </head>
metaString = metaString.replace(/\n|\r|\t/g, ''); // Remove empty lines and tabs
metaString = metaString.replaceAll(/\<\/(.*?)\>/g, ""); // Remove all </...> tags
metaString = metaString.split("\<");
metaString.shift(); // Remove first element (everything before first <)
const skipThose = [ // Skip these tags // TODO: Add possibility to filter after space (e.g. <meta name="author" content="..."> -> author)
"script", "!DOCTYPE", "!doctype", "noscript", "style"
];
for(let i = 0; i < metaString.length; i++) {
if(skipThose.includes(metaString[i].split(" ")[0])) {
metaString.splice(i, 1);
}
}
for(myMeta of metaString) {
if(myMeta.includes("title>")) {
// Title
which.title = myMeta.split("title>")[1];
}
if(myMeta.includes("shortcut icon")) {
// Icon
which.icon = myMeta.split("href=\"")[1].split("\"")[0];
}
}
which.metaloaded = true;
} else {
which.hasErrors = true;
}
addDesktopIcon(which);
addProgramIconToFolder(which);
if(which.autostart) { // TODO: Make it work for external programs
// Autostart
if(!system.user.autorun) {
system.user.autorun = [];
}
system.user.autorun.push([which.id,[],which.autostart]);
}
}
xhr.send();
}
function addDesktopIcon(which) {
if(which.devonly && !system.user.settings.developer.enable) {
return;
}
if(which.spawnicon != 0) {
var newProgIcon = document.createElement("button");
newProgIcon.className = "programicon";
newProgIcon.id = which.id;
newProgIcon.innerHTML = `
<img src="${which.icon}" loading="lazy" alt="">
<p>${which.title}</p>
`;
newProgIcon.addEventListener("click", function() {
(async() => {
run(this.id);
})();
});
objects.progicons.appendChild(newProgIcon);
}
}
function addProgramIconToFolder(which) {
iofs.save(system.user.paths.programShortcuts + (which.title).replaceAll("'", "'") + ".run", JSON.stringify(which), "run", 1)
}
/**
* @deprecated
*/
function ifjsonparse(which) { // Parse JSON but only if valid // deprecated
try {
JSON.parse(which);
} catch (e) {
return {};
}
return JSON.parse(which);
}
window.addEventListener("mousemove", function(e) {
systemRuntime.pos.mouseX = e.clientX;
systemRuntime.pos.mouseY = e.clientY;
});
window.addEventListener("touchmove", function(e) {
systemRuntime.pos.mouseX = e.targetTouches[0].pageX;
systemRuntime.pos.mouseY = e.targetTouches[0].pageY;
});
window.addEventListener("touchstart", function(e) {
systemRuntime.pos.mouseX = e.targetTouches[0].pageX;
systemRuntime.pos.mouseY = e.targetTouches[0].pageY;
});
window.addEventListener("mouseup", function() {
systemRuntime.clicking = 0;
});
window.addEventListener("touchend", function() {
systemRuntime.clicking = 0;
});
function gooff() { // Shutdown MainOS
// TODO: Add check so it can't be run by every program
window.close();
self.close();
}
function wait(time) { //deprecated
// TODO: Remove function
if (time > 500) {
time = 0;
console.warn("function wait(): Waiting time may only be 500ms or less.");
}
var starttime = new Date().getTime();
while(new Date().getTime() < starttime + time) {}
console.warn("function wait() is depreciated. Please remove this function from your code.");
}
window.setInterval(function() {
setting.time = new Date();
setting.time = {
hour: setting.time.getHours(),
minute: setting.time.getMinutes(),
year: setting.time.getFullYear(),
month: setting.time.getMonth() + 1,
day: setting.time.getDate()
};
if (setting.time.hour < 10) {
setting.time.hour = "0" + setting.time.hour;
}
if (setting.time.minute < 10) {
setting.time.minute = "0" + setting.time.minute;
}
if (setting.time.month < 10) {
setting.time.month = "0" + setting.time.month;
}
if (setting.time.day < 10) {
setting.time.day = "0" + setting.time.day;
}
setting.time.date = setting.time.year + "-" + setting.time.month + "-" + setting.time.day;
setting.time.time = setting.time.hour + ":" + setting.time.minute;
setting.time.full = setting.time.date + " " + setting.time.time;
/* Automatically update these time displays: */
}, 250);
/**
* Fullscreen system or element
* @param {HTMLElement} element
* @param {boolean} state
*/
function enterFullscreen(element = document.body, state = !document.fullscreenElement) {
window.setTimeout(function() {
if(document.fullscreenElement == null || document.fullscreenElement == undefined || state == true) {
if (element.requestFullscreen) { element.requestFullscreen(); }
else if (element.mozRequestFullScreen) { element.mozRequestFullScreen(); }
else if (element.msRequestFullscreen) { element.msRequestFullscreen(); }
else if (element.webkitRequestFullscreen) { element.webkitRequestFullscreen(); }
else if (element.requestFullscreen) { element.requestFullscreen(); }
} else {
if (document.exitFullscreen) { document.exitFullscreen(); }
else if (document.msExitFullscreen) { document.msExitFullscreen(); }
else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); }
else if (document.exitFullscreen) { document.exitFullscreen(); }
}
}, 0);
}
if (system.user.settings.default_fullscreen == 1) { // Enter fullscreen on start if requested by setting
document.body.getElementsByClassName("content")[0].addEventListener("click", function() {
enterFullscreen();
}, {"once": true});
}
document.getElementById("background").style.backgroundImage = "url(" + iofs.load(system.user.settings.backgroundImage, false) + ")"; // Load Desktop Background
document.getElementById("username").innerText = system.user.name; // Display username on desktop
// Check space on disk
iofs.save("C:/.diskinfo/size_used.txt", JSON.stringify(localStorage).length / 1000, "t=txt");
iofs.save("C:/.diskinfo/size_remaining.txt", iofs.load("C:/.diskinfo/size.txt") - iofs.load("C:/.diskinfo/size_used.txt"), "t=txt");
// Re-Create program shortcuts; Delete them beforehand
iofs.listdir(system.user.paths.programShortcuts).forEach((item) => {
iofs.delete(item, 1);
})