Added tab count code logic

This commit is contained in:
Maxim Stewart 2020-02-01 00:59:29 -06:00
parent 9bc91db106
commit 4c368f7c10
5 changed files with 115 additions and 75 deletions

View File

@ -5,7 +5,7 @@ Easy Session Manager allows you to manage your Firefox session by backing up or
https://addons.mozilla.org/en-US/firefox/addon/easy-session-manager/
# Version: 0.2.1.0
Refactored code.
Refactored code and added tab count per session.
# Images
![1 Default interface with no sessions. ](images/pic1.png)

View File

@ -6,7 +6,72 @@ const getSavedSessionIDs = () => {
storageApi.get(null).then((results) => {
const sessions = Object.keys(results);
for (let session of sessions) {
appendToSavedSessionsList(session);
storageApi.get(session).then((results) => {
size = getStoreSize(results[session]);
appendToSavedSessionsList(session, size);
});
}
});
}
const saveToStorage = (name, data, action = "undefined", willReplace = false, sveElm = null) => {
storageApi.get(name).then((results) => {
const size = getStoreSize(data); // Must be outside try block for catch block to see it
try {
const json = JSON.parse(results[name]); // If a session is found
if (!willReplace) {
swal({
title: "Replace?",
text: "Found a session with that name! Do you want to replace it?",
icon: "warning",
buttons: true,
dangerMode: true,
}).then((willReplace) => {
if (willReplace) {
console.log("door 1");
storageApi.set({[name]: data});
sveElm.innerText = size + " | " + name;
sveElm.name = name;
messageWindow("warning", "Overwrote session...");
} else {
messageWindow("warning", "Canceled " + action + "...");
}
});
} else {
sveElm.innerText = size + " | " + name;
sveElm.name = name;
storageApi.set({[name]: data});
messageWindow("warning", "Overwrote session...");
}
} catch (e) {
if (action !== "edit") {
appendToSavedSessionsList(name, size);
messageWindow("success", "Saved session...");
storageApi.set({[name]: data});
} else {
storageApi.set({[name]: data});
messageWindow("warning", "Overwrote session...");
}
}
});
}
const deleteFromStorage = (elm = null, name = null) => {
swal({
title: "Are you sure?",
text: "Do you wish to delete session:\n" + name + "?",
icon: "warning",
buttons: true,
dangerMode: true,
}).then((willDelete) => {
if (willDelete) {
storageApi.remove(name).then(() => {
elm.parentElement.removeChild(elm);
});
selectedItem = null; // reset selectedItem
messageWindow("success", "Deleted session successfully...");
} else {
messageWindow("warning", "Canceled deletion...");
}
});
}
@ -22,60 +87,3 @@ const windowMaker = (i, keysLength, keys, json) => {
windowApi.create({ url: urls });
}
}
const saveToStorage = (name, data, action = "undefined", willReplace = false) => {
storageApi.get(name).then((results) => {
try {
// If save finds a session successfully then check if replacing
const json = JSON.parse(results[name]);
if (!willReplace) {
swal({
title: "Replace?",
text: "Found a session with that name! Do you want to replace it?",
icon: "warning",
buttons: true,
dangerMode: true,
}).then((willReplace) => {
if (willReplace) {
storageApi.set({[name]: data});
messageWindow("warning", "Overwrote session...");
} else {
messageWindow("warning", "Canceled " + action + "...");
}
});
} else {
storageApi.set({[name]: data});
messageWindow("warning", "Overwrote session...");
}
} catch (e) {
if (action !== "edit") {
appendToSavedSessionsList(name);
messageWindow("success", "Saved session...");
storageApi.set({[name]: data});
} else {
messageWindow("warning", "Overwrote session...");
storageApi.set({[name]: data});
}
}
});
}
const deleteFromStorage = (elm = null) => {
swal({
title: "Are you sure?",
text: "Do you wish to delete session:\n" + elm.innerText + "?",
icon: "warning",
buttons: true,
dangerMode: true,
}).then((willDelete) => {
if (willDelete) {
storageApi.remove(elm.innerText).then(() => {
elm.parentElement.removeChild(elm);
});
selectedItem = null; // reset selectedItem
messageWindow("success", "Deleted session successfully...");
} else {
messageWindow("warning", "Canceled deletion...");
}
});
}

View File

@ -29,7 +29,7 @@ document.addEventListener("click", (e) => {
if (action == "download")
downloadSession(selectedItemName);
else if (action == "delete")
deleteFromStorage(selectedItem);
deleteFromStorage(selectedItem, selectedItemName);
else if (action == "edit")
editSession(selectedItem, selectedItemName);
} else {
@ -49,7 +49,7 @@ document.addEventListener("dblclick", (e) => {
if (e.button == 0) { // Left click
if (e.target.tagName == "LI" && e.target.className.includes("sessionLI")) {
selectedItem = e.target;
const id = selectedItem.innerText.trim();
const id = selectedItem.getAttribute("name");
selectedItem.setAttribute("class", "sessionLI selected");
preLoadSession(id);
}

View File

@ -32,13 +32,13 @@ const saveSession = (elm = null, name = null, message = message1) => {
let enteryName = inputTag.value.replace(/ /g, "_");
if (enteryName.length < 0 || enteryName.length > 54 || enteryName.search(regexp) == -1) {
saveSession(elm, message2);
saveSession(elm, name, message2);
return ;
}
console.log("Saving session...");
sessionData = getSelectionData(container, keys, keysLength);
saveToStorage(enteryName, JSON.stringify(sessionData), "save", willReplace);
saveToStorage(enteryName, JSON.stringify(sessionData), "save", willReplace, elm);
} else {
messageWindow("warning", "Canceled save...");
}
@ -61,9 +61,17 @@ const editSession = (elm = null, name = null, message = message1) => {
labelTag.htmlFor = "newSession";
storageApi.get(id).then((results) => {
let json = JSON.parse(results[id]);
let keys = Object.keys(json);
let keysLength = Object.keys(json).length;
let json = null;
let keys = null;
let keysLength = null;
try {
json = JSON.parse(results[id]);
keys = Object.keys(json);
keysLength = Object.keys(json).length;
} catch (e) {
messageWindow("warning", "Canceled edit; couldn't load any data...");
}
let container = generateSelectionWindow(json, keys, keysLength);
container.prepend(labelTag);
container.prepend(newSessionTag);
@ -80,26 +88,35 @@ const editSession = (elm = null, name = null, message = message1) => {
let newName = inputTag.value.replace(/ /g, "_");
if (newName.length < 0 || newName.length > 54 || newName.search(regexp) == -1) {
editSession(elm, message2);
editSession(elm, name, message2);
return ;
}
json = getSelectionData(container, keys, keysLength);
json = getSelectionData(container, keys, keysLength);
const strData = JSON.stringify(json);
if (newSessionTag.checked) { // If creating new session
newName = checkSessionListForDuplicate(newName);
saveToStorage(newName, JSON.stringify(json), "save");
newName = checkSessionListForDuplicate(newName);
saveToStorage(newName, strData, "save", false, elm);
} else {
if (newName == name) { // If not creating new session and are the same name
storageApi.get(id).then((results) => {
storageApi.remove(id);
saveToStorage(newName, JSON.stringify(json), "edit", true);
}).then(() => { elm.innerText = newName; });
saveToStorage(newName, strData, "edit", true, elm);
}).then(() => {
const size = getStoreSize(strData);
elm.innerText = size + " | " + newName;
elm.setAttribute("name", newName);
});
} else { // If not creating new session and names are not the same rename
storageApi.get(id).then((results) => {
storageApi.remove(id);
newName = checkSessionListForDuplicate(newName);
saveToStorage(newName, JSON.stringify(json), "edit");
}).then(() => { elm.innerText = newName; });
storageApi.remove(id);
saveToStorage(newName, strData, "edit", false, elm);
}).then(() => {
const size = getStoreSize(strData);
elm.innerText = size + " | " + newName;
elm.setAttribute("name", newName);
});
}
}
} else {

View File

@ -73,6 +73,21 @@ const appendToSavedSessionsList = (enteryName, storeSize) => {
// Generics
const getStoreSize = (session) => {
let count = 0;
try {
const json = JSON.parse(session);
const keys = Object.keys(json);
keys.forEach(key => {
count += json[key].length;
});
return count;
} catch (e) { }
return count;
}
const getSessionData = (windows) => {
let sessionData = {};
for (let i = 0; i < windows.length; i++) {