mirror of
https://git.ari.lt/ari.lt/ari.lt.git
synced 2025-02-04 17:49:24 +01:00
840ec8a17a
Signed-off-by: Ari Archer <ari.web.xyz@gmail.com>
115 lines
2.9 KiB
JavaScript
115 lines
2.9 KiB
JavaScript
function sleep(ms) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
async function add_boot_entry(entry_object) {
|
|
for (const entry in entry_object) {
|
|
if (do_sleep) {
|
|
let sleep_time;
|
|
if (typeof entry_object[entry]["sleep_time"] !== "undefined") {
|
|
sleep_time = entry_object[entry]["sleep_time"];
|
|
} else {
|
|
sleep_time = Math.floor(Math.random() * 2000 + 500);
|
|
}
|
|
await sleep(sleep_time);
|
|
}
|
|
|
|
let new_entry = document.createElement("p");
|
|
new_entry.innerHTML = entry_object[entry]["text"];
|
|
new_entry.classList.add("bmsg");
|
|
|
|
let entry_class;
|
|
switch (entry_object[entry]["type"]) {
|
|
case "error":
|
|
entry_class = "error";
|
|
break;
|
|
|
|
case "warning":
|
|
entry_class = "warn";
|
|
break;
|
|
|
|
case "ok":
|
|
entry_class = "ok";
|
|
break;
|
|
|
|
default:
|
|
throw `Type '${entry_object[entry]["type"]}' not found.`;
|
|
}
|
|
|
|
new_entry.setAttribute("bmsg_type", entry_class);
|
|
boot.appendChild(new_entry);
|
|
}
|
|
}
|
|
|
|
function hash(string) {
|
|
return string.split("").reduce(function (a, b) {
|
|
a = (a << 5) - a + b.charCodeAt(0);
|
|
return a & a;
|
|
}, 0);
|
|
}
|
|
|
|
function to_filename(filename) {
|
|
return filename.replaceAll(" ", "_");
|
|
}
|
|
|
|
function to_storage(filename) {
|
|
return `file.${to_filename(filename)}`;
|
|
}
|
|
|
|
function file_exists(filename) {
|
|
return to_storage(filename) in window.localStorage;
|
|
}
|
|
|
|
function save_file(filename, content) {
|
|
try {
|
|
window.localStorage.setItem(to_storage(filename), btoa(content));
|
|
} catch (e) {
|
|
alert(`Failed to save ${to_filename(filename)}: ${e}`);
|
|
}
|
|
}
|
|
|
|
function get_file(filename) {
|
|
let file = window.localStorage.getItem(to_storage(filename));
|
|
return file ? atob(file) : "";
|
|
}
|
|
|
|
function remove_file(filename) {
|
|
window.localStorage.removeItem(to_storage(filename));
|
|
}
|
|
|
|
function list_files() {
|
|
let files = [];
|
|
|
|
Object.keys(localStorage).forEach((key) => {
|
|
if (key.startsWith("file.")) files.push(key.slice(5));
|
|
});
|
|
|
|
return files;
|
|
}
|
|
|
|
function disable(element) {
|
|
element.setAttribute("disabled", "disabled");
|
|
element.setAttribute("readonly", "true");
|
|
}
|
|
|
|
function enable(element) {
|
|
element.removeAttribute("disabled");
|
|
element.removeAttribute("readonly");
|
|
}
|
|
|
|
function invoke_download(filename, data) {
|
|
let blob = new Blob([data], { type: "text/plain" });
|
|
|
|
if (window.navigator.msSaveOrOpenBlob)
|
|
window.navigator.msSaveBlob(blob, filename);
|
|
else {
|
|
let elem = window.document.createElement("a");
|
|
|
|
elem.href = window.URL.createObjectURL(blob);
|
|
elem.download = filename;
|
|
|
|
document.body.appendChild(elem);
|
|
elem.click();
|
|
document.body.removeChild(elem);
|
|
}
|
|
}
|