Easy-Session-Manager/src/scripts/background.js

133 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-02-29 21:30:44 +00:00
const message2 = "Name too long or none provided; or, unacceptable character used.";
2020-02-01 02:43:21 +00:00
const regexp = /^[a-zA-Z0-9-_]+$/; // Alphanumeric, dash, underscore
2020-02-29 21:30:44 +00:00
let data = null;
2020-02-26 08:21:16 +00:00
2020-02-29 21:30:44 +00:00
const prePprocessor = (obj, enteryName = '', message = "") => {
let inputTag = document.getElementsByName("toSaveNameImport")[0];
inputTag.value = enteryName.replace(/ /g, "_");
data = obj.target.result;
document.getElementsByName("toSaveImportErrMessage")[0].innerText = message;
}
2020-02-26 08:21:16 +00:00
2020-02-29 21:30:44 +00:00
const processor = () => {
let inputTag = document.getElementsByName("toSaveNameImport")[0];
enteryName = inputTag.value.replace(/ /g, "_");
inputTag.value = enteryName;
2020-02-29 21:30:44 +00:00
if (enteryName.length < 0 || enteryName.length > 54 || enteryName.search(regexp) == -1) {
messageWindow("danger", message2, "modal-gutter");
// prePprocessor(obj, "", message2);
return ;
}
try {
console.log("Importing session...");
JSON.parse(data); // See if parsing fails and throw error
browser.storage.local.set({[enteryName]: data});
messageWindow("success", "Imported file successfully.");
hideModal();
} catch (e) {
hideModal();
messageWindow("error", "Failed to import data. Not a JSON parsable file.");
return ;
}
};
2020-02-29 21:30:44 +00:00
// Get files after being chosen
document.getElementById("inputId").onchange = (e) => {
let size = e.target.files.length;
let fileArry = e.target.files;
2020-02-29 21:30:44 +00:00
// Loop throughg the chosen files...
for (var i = 0; i < size; i++) {
let reader = new FileReader();
let name = fileArry[i].name;
name = name.split(".")[0];
2020-02-29 21:30:44 +00:00
reader.onloadend = (obj) => {
prePprocessor(obj, name);
showModal();
};
if (fileArry[i].type == "application/json")
reader.readAsText(fileArry[i], {encoding: "string"});
}
};
2020-02-29 21:30:44 +00:00
// Bring up file selector...
document.addEventListener("click", (e) => {
if (e.button == 0) { // Left click
2020-02-29 21:30:44 +00:00
const target = e.target;
const action = target.name;
if (target.className.includes("container")) {
document.getElementById("inputId").click();
2020-02-29 21:30:44 +00:00
} else if (action == "importSave") {
processor();
}
}
});
2020-02-29 21:30:44 +00:00
const showModal = async (modalID = "saveModal") => {
tween(1600, "up", modalID); // in miliseconds
}
const hideModal = (modalID = "saveModal") => {
tween(1600, "down", modalID); // in miliseconds
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const tween = async (miliseconds, direction, modalID) => {
const elm = document.getElementById(modalID);
const timeStep = 1000 / miliseconds;
const steps = timeStep * 100
if (direction == "up") { // Go up
elm.style.display = "";
// elm.style.opacity = "1";
for (var i = 1; i <= steps; i++) {
await sleep(timeStep);
elm.style.opacity = i/steps;
}
} else { // Go down
for (var i = steps; i > 1; i--) {
await sleep(timeStep);
elm.style.opacity = i/steps;
}
// elm.style.opacity = "0";
elm.style.display = "none";
}
}
const messageWindow = (type = "warning", message = "No message passed in...", target = "") => {
let pTag = document.createElement("P");
let text = document.createTextNode(message);
let gutter = document.getElementById("message-gutter");
if (target !== "") {
gutter = document.getElementById(target);
}
pTag.className = "alert alert-" + type;
pTag.appendChild(text);
gutter.prepend(pTag);
setTimeout(function () {
clearChildNodes(gutter);
}, 3200);
}
const clearChildNodes = (parent) => {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}