135 lines
3.4 KiB
JavaScript
135 lines
3.4 KiB
JavaScript
let clock = document.getElementById("clock");
|
|
let date = document.getElementById("date");
|
|
let input = document.getElementById("search-bar");
|
|
let result = document.getElementById("search-results");
|
|
|
|
/* Allow toggling between military and 12 hr time */
|
|
let military_time = false
|
|
clock.onclick = function() {
|
|
military_time = !military_time;
|
|
time();
|
|
}
|
|
|
|
function time(timeout) {
|
|
let d = new Date();
|
|
let m = d.getMinutes();
|
|
let h = d.getHours();
|
|
|
|
/* if it hasn't been set then set it here */
|
|
if (military_time) {
|
|
clock.textContent = `${("0" + h).substr(-2)}:${("0" + m).substr(-2)}`;
|
|
} else {
|
|
let hours = h % 12;
|
|
let minutes = ("0" + m).substr(-2);
|
|
hours = hours ? hours : 12;
|
|
|
|
clock.textContent = `${hours}:${minutes} ${(h >= 12 ? "PM" : "AM")}`;
|
|
}
|
|
|
|
/* set the time every second */
|
|
timeout && setTimeout(() => time(true), (60 - d.getSeconds()) * 1000);
|
|
}
|
|
|
|
let last_date;
|
|
/* Sun 31st, May */
|
|
function cal(force) {
|
|
const days = [ "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" ];
|
|
const months = [ "January", "Febuary", "March", "April", "May", "June",
|
|
"July", "August", "September", "November", "December", ];
|
|
|
|
let d = new Date();
|
|
let day = days[d.getDay()];
|
|
let day_of_month = d.getDate();
|
|
let month = months[d.getMonth()];
|
|
let day_suffix
|
|
|
|
/* get a suffix for the day of the month */
|
|
if (day_of_month > 3 && day_of_month < 21) {
|
|
day_suffix = "th";
|
|
} else if ((day_of_month / 1) % 10 == 1) {
|
|
day_suffix = "st";
|
|
} else if ((day_of_month / 1) % 10 == 2) {
|
|
day_suffix = "nd";
|
|
} else if ((day_of_month / 1) % 10 == 3) {
|
|
day_suffix = "rd";
|
|
} else {
|
|
day_suffix = "th";
|
|
}
|
|
|
|
/* check if this date has already been set before */
|
|
if (!force && last_date == `${d.getDay()}${d.getMonth()}`) {
|
|
return;
|
|
}
|
|
|
|
date.textContent = `${day} ${day_of_month}${day_suffix}, ${month}`;
|
|
|
|
/* store the last date that was set */
|
|
last_date = `${d.getDay()}${d.getMonth()}`;
|
|
}
|
|
|
|
/* show the time on the page */
|
|
time(true);
|
|
|
|
/* just incase something dumb happens the time should update when the user
|
|
* focus's the page */
|
|
window.addEventListener('focus', time);
|
|
|
|
cal();
|
|
setInterval(cal, 1000);
|
|
|
|
function shell(event) {
|
|
var env = {
|
|
path: "/startpage/root/bin/",
|
|
};
|
|
|
|
/* convert a directory such as /startpage/root/bin/ to /startpage/root/.bin
|
|
* to read static fs data.
|
|
*/
|
|
function convertPath(path) {
|
|
if (path.endsWith('/')) {
|
|
path = path.slice(0, -1);
|
|
}
|
|
const parts = path.split('/');
|
|
const hidden = '.' + parts.pop();
|
|
return parts.concat(hidden).join('/');
|
|
}
|
|
|
|
async function run(path) {
|
|
try {
|
|
const response = await fetch(convertPath(env.path));
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to load file: ${response.status}`);
|
|
}
|
|
|
|
const text = await response.text();
|
|
const lines = text.split(/\r?\n/);
|
|
const matchedLine = lines.find(line => line.split(".")[0] == path);
|
|
if (!matchedLine) {
|
|
throw new Error(`${path} cannot be found in path.`);
|
|
}
|
|
|
|
const file = env.path + matchedLine;
|
|
const { cmd } = await import(file);
|
|
return await cmd(input.value, event.key);
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
if (input.value == "") {
|
|
input.style.color = "white";
|
|
} else {
|
|
run(input.value.substring(0, input.value.indexOf(" ")) || input.value)
|
|
.then(res => {
|
|
result.innerHTML = res;
|
|
input.style.color = "var(--blue)";
|
|
})
|
|
.catch(err => {
|
|
input.style.color = "white";
|
|
});
|
|
}
|
|
}
|
|
|
|
input.addEventListener('keyup', shell);
|
|
shell({ key: "" });
|