hippofish/packages/backend/src/server/web/bios.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-02-18 20:33:25 +01:00
"use strict";
2021-03-02 17:03:29 +01:00
window.onload = async () => {
2023-02-18 20:33:25 +01:00
const account = JSON.parse(localStorage.getItem("account"));
2021-03-02 17:03:29 +01:00
const i = account.token;
const api = (endpoint, data = {}) => {
const promise = new Promise((resolve, reject) => {
// Append a credential
if (i) data.i = i;
2022-07-07 06:15:47 +02:00
2021-03-02 17:03:29 +01:00
// Send request
2023-02-18 20:33:25 +01:00
fetch(endpoint.indexOf("://") > -1 ? endpoint : `/api/${endpoint}`, {
method: "POST",
2021-03-02 17:03:29 +01:00
body: JSON.stringify(data),
2023-02-18 20:33:25 +01:00
credentials: "omit",
cache: "no-cache",
})
.then(async (res) => {
const body = res.status === 204 ? null : await res.json();
2022-07-07 06:15:47 +02:00
2023-02-18 20:33:25 +01:00
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve();
} else {
reject(body.error);
}
})
.catch(reject);
2021-03-02 17:03:29 +01:00
});
2022-07-07 06:15:47 +02:00
2021-03-02 17:03:29 +01:00
return promise;
};
2023-02-18 20:33:25 +01:00
const content = document.getElementById("content");
2021-03-02 17:03:29 +01:00
2023-02-18 20:33:25 +01:00
document.getElementById("ls").addEventListener("click", () => {
content.innerHTML = "";
2021-03-02 17:03:29 +01:00
2023-02-18 20:33:25 +01:00
const lsEditor = document.createElement("div");
lsEditor.id = "lsEditor";
2021-03-02 17:03:29 +01:00
2023-02-18 20:33:25 +01:00
const adder = document.createElement("div");
adder.classList.add("adder");
const addKeyInput = document.createElement("input");
const addValueTextarea = document.createElement("textarea");
const addButton = document.createElement("button");
addButton.textContent = "Add";
addButton.addEventListener("click", () => {
2021-03-02 17:03:29 +01:00
localStorage.setItem(addKeyInput.value, addValueTextarea.value);
location.reload();
});
adder.appendChild(addKeyInput);
adder.appendChild(addValueTextarea);
adder.appendChild(addButton);
lsEditor.appendChild(adder);
for (let i = 0; i < localStorage.length; i++) {
const k = localStorage.key(i);
2023-02-18 20:33:25 +01:00
const record = document.createElement("div");
record.classList.add("record");
const header = document.createElement("header");
2021-03-02 17:03:29 +01:00
header.textContent = k;
2023-02-18 20:33:25 +01:00
const textarea = document.createElement("textarea");
2021-03-02 17:03:29 +01:00
textarea.textContent = localStorage.getItem(k);
2023-02-18 20:33:25 +01:00
const saveButton = document.createElement("button");
saveButton.textContent = "Save";
saveButton.addEventListener("click", () => {
2021-03-02 17:03:29 +01:00
localStorage.setItem(k, textarea.value);
location.reload();
});
2023-02-18 20:33:25 +01:00
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
removeButton.addEventListener("click", () => {
2021-03-02 17:03:29 +01:00
localStorage.removeItem(k);
location.reload();
});
record.appendChild(header);
record.appendChild(textarea);
record.appendChild(saveButton);
record.appendChild(removeButton);
lsEditor.appendChild(record);
}
content.appendChild(lsEditor);
});
};