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
|
2019-01-19 03:29:20 +00:00
|
|
|
|
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, "_");
|
2019-03-01 07:20:25 +00:00
|
|
|
inputTag.value = enteryName;
|
2019-01-19 03:29:20 +00:00
|
|
|
|
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 ;
|
|
|
|
}
|
2019-01-19 03:29:20 +00:00
|
|
|
};
|
|
|
|
|
2020-02-29 21:30:44 +00:00
|
|
|
// Get files after being chosen
|
2019-01-19 03:29:20 +00:00
|
|
|
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...
|
2019-01-19 03:29:20 +00:00
|
|
|
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();
|
|
|
|
};
|
2019-01-19 03:29:20 +00:00
|
|
|
|
|
|
|
if (fileArry[i].type == "application/json")
|
|
|
|
reader.readAsText(fileArry[i], {encoding: "string"});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-29 21:30:44 +00:00
|
|
|
// Bring up file selector...
|
2019-01-19 03:29:20 +00:00
|
|
|
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")) {
|
2019-01-19 03:29:20 +00:00
|
|
|
document.getElementById("inputId").click();
|
2020-02-29 21:30:44 +00:00
|
|
|
} else if (action == "importSave") {
|
|
|
|
processor();
|
2019-01-19 03:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
}
|