Small fixes plus adding initial tab count code logic

This commit is contained in:
Maxim Stewart 2020-01-31 22:25:41 -06:00
parent 9fc3062fde
commit 9bc91db106
4 changed files with 57 additions and 60 deletions

View File

@ -4,9 +4,9 @@ const messageWindow = (type = "warning", message = "No message passed in...") =>
const getSavedSessionIDs = () => { const getSavedSessionIDs = () => {
storageApi.get(null).then((results) => { storageApi.get(null).then((results) => {
let keys = Object.keys(results); const sessions = Object.keys(results);
for (let key of keys) { for (let session of sessions) {
appendToSavedSessionsList(key); appendToSavedSessionsList(session);
} }
}); });
} }

View File

@ -2,42 +2,46 @@ getSavedSessionIDs();
document.addEventListener("click", (e) => { document.addEventListener("click", (e) => {
if (e.button == 0) { // Left click if (e.button == 0) { // Left click
let name = e.target.name; const target = e.target;
const action = target.name;
if (/(download|delete|edit)/.test(name)) { // Set selection first before doing any actions...
if (target.tagName == "LI" && target.className.includes("sessionLI")) {
if (selectedItem) { if (selectedItem) {
if (name == "download") if (selectedItem == target && selectedItem.className == "sessionLI selected") {
downloadSession(selectedItem);
else if (name == "delete")
deleteFromStorage(selectedItem);
else if (name == "edit")
editSession(selectedItem);
} else {
messageWindow("warning", "Select a session first...");
}
} else if (name == "save") {
saveSession(selectedItem);
} else if (name == "import") {
importSession();
} else if (name == "donate") {
doUrlAction("https://www.paypal.me/ITDominator",);
}
if (e.target.tagName == "LI" && e.target.className.includes("sessionLI")) {
if (selectedItem) {
if (selectedItem == e.target && selectedItem.className == "sessionLI selected") {
selectedItem.setAttribute("class", "sessionLI"); selectedItem.setAttribute("class", "sessionLI");
selectedItem = null; selectedItem = null;
} else { } else {
selectedItem.setAttribute("class", "sessionLI"); selectedItem.setAttribute("class", "sessionLI");
selectedItem = e.target; selectedItem = target;
selectedItem.setAttribute("class", "sessionLI selected"); selectedItem.setAttribute("class", "sessionLI selected");
} }
} else { } else {
selectedItem = e.target; selectedItem = target;
selectedItem.setAttribute("class", "sessionLI selected"); selectedItem.setAttribute("class", "sessionLI selected");
} }
} }
// If elm has certain action do it.
const selectedItemName = (selectedItem !== null) ? selectedItem.getAttribute("name") : "";
if (/(download|delete|edit)/.test(action)) {
if (selectedItem) {
if (action == "download")
downloadSession(selectedItemName);
else if (action == "delete")
deleteFromStorage(selectedItem);
else if (action == "edit")
editSession(selectedItem, selectedItemName);
} else {
messageWindow("warning", "Select a session first...");
}
} else if (action == "save") {
saveSession(selectedItem, selectedItemName);
} else if (action == "import") {
importSession();
} else if (action == "donate") {
doUrlAction("https://www.paypal.me/ITDominator",);
}
} }
}); });
@ -45,7 +49,7 @@ document.addEventListener("dblclick", (e) => {
if (e.button == 0) { // Left click if (e.button == 0) { // Left click
if (e.target.tagName == "LI" && e.target.className.includes("sessionLI")) { if (e.target.tagName == "LI" && e.target.className.includes("sessionLI")) {
selectedItem = e.target; selectedItem = e.target;
let id = selectedItem.innerText.trim(); const id = selectedItem.innerText.trim();
selectedItem.setAttribute("class", "sessionLI selected"); selectedItem.setAttribute("class", "sessionLI selected");
preLoadSession(id); preLoadSession(id);
} }

View File

@ -1,4 +1,4 @@
const message1 = "[ Session Name ] Allowed: a-z, A-Z, 0-9, -, _"; const message1 = "[ Session Name ] Allowed: a-z, A-Z, 0-9, -, _";
const message2 = "Allowed: a-z, A-Z, 0-9, -, _ Please try again...\nName too long or none provided; or, unacceptable character used."; const message2 = "Allowed: a-z, A-Z, 0-9, -, _ Please try again...\nName too long or none provided; or, unacceptable character used.";
const storageApi = browser.storage.local; const storageApi = browser.storage.local;
const tabsApi = browser.tabs; const tabsApi = browser.tabs;
@ -6,13 +6,13 @@ const windowApi = browser.windows;
const regexp = /^[a-zA-Z0-9-_]+$/; // Alphanumeric, dash, underscore const regexp = /^[a-zA-Z0-9-_]+$/; // Alphanumeric, dash, underscore
const saveSession = (elm = null, message = message1) => { const saveSession = (elm = null, name = null, message = message1) => {
let inputTag = document.createElement("INPUT"); let inputTag = document.createElement("INPUT");
let willReplace = false; let willReplace = false;
inputTag.value = new Date().toLocaleString().split(',')[0].replace(/\//g, '-'); inputTag.value = new Date().toLocaleString().split(',')[0].replace(/\//g, '-');
if (elm !== null) { if (elm !== null) {
inputTag.value = elm.innerText; inputTag.value = name;
willReplace = true; willReplace = true;
} }
@ -46,19 +46,19 @@ const saveSession = (elm = null, message = message1) => {
}); });
} }
const editSession = (elm = null, message = message1) => { const editSession = (elm = null, name = null, message = message1) => {
let id = elm.innerText; let id = name;
let inputTag = document.createElement("INPUT"); let inputTag = document.createElement("INPUT");
let newSessionTag = document.createElement("INPUT"); let newSessionTag = document.createElement("INPUT");
let labelTag = document.createElement("LABEL"); let labelTag = document.createElement("LABEL");
let brTag = document.createElement("BR"); let brTag = document.createElement("BR");
inputTag.value = id; inputTag.value = id;
newSessionTag.type = "checkbox"; newSessionTag.type = "checkbox";
newSessionTag.id = "newSession"; newSessionTag.id = "newSession";
newSessionTag.checked = false; newSessionTag.checked = false;
labelTag.innerText = "Create New Session"; labelTag.innerText = "Create New Session";
labelTag.htmlFor = "newSession"; labelTag.htmlFor = "newSession";
storageApi.get(id).then((results) => { storageApi.get(id).then((results) => {
let json = JSON.parse(results[id]); let json = JSON.parse(results[id]);
@ -85,28 +85,21 @@ const editSession = (elm = null, message = message1) => {
} }
json = getSelectionData(container, keys, keysLength); json = getSelectionData(container, keys, keysLength);
if (newSessionTag.checked) { // If creating new session
// If creating new session
if (newSessionTag.checked) {
newName = checkSessionListForDuplicate(newName); newName = checkSessionListForDuplicate(newName);
saveToStorage(newName, JSON.stringify(json), "save"); saveToStorage(newName, JSON.stringify(json), "save");
} else { } else {
// If not creating new session and are the same name if (newName == name) { // If not creating new session and are the same name
if (newName == id) {
storageApi.get(id).then((results) => { storageApi.get(id).then((results) => {
storageApi.remove(id); storageApi.remove(id);
saveToStorage(newName, JSON.stringify(json), "edit", true); saveToStorage(newName, JSON.stringify(json), "edit", true);
}).then(() => { }).then(() => { elm.innerText = newName; });
elm.innerText = newName;
});
} else { // If not creating new session and names are not the same rename } else { // If not creating new session and names are not the same rename
storageApi.get(id).then((results) => { storageApi.get(id).then((results) => {
storageApi.remove(id); storageApi.remove(id);
newName = checkSessionListForDuplicate(newName); newName = checkSessionListForDuplicate(newName);
saveToStorage(newName, JSON.stringify(json), "edit"); saveToStorage(newName, JSON.stringify(json), "edit");
}).then(() => { }).then(() => { elm.innerText = newName; });
elm.innerText = newName;
});
} }
} }
} else { } else {
@ -116,7 +109,7 @@ const editSession = (elm = null, message = message1) => {
}); });
} }
const downloadSession = (elm = null) => { const downloadSession = (session = null) => {
let pTag = document.createElement("P"); let pTag = document.createElement("P");
let inputTag = document.createElement("INPUT"); let inputTag = document.createElement("INPUT");
let chkBoxTag = document.createElement("INPUT"); let chkBoxTag = document.createElement("INPUT");
@ -124,9 +117,8 @@ const downloadSession = (elm = null) => {
let brTag = document.createElement("BR"); let brTag = document.createElement("BR");
let aTagElm = document.getElementById('downloadAnchorElem'); let aTagElm = document.getElementById('downloadAnchorElem');
let text = document.createTextNode("Append Date?"); let text = document.createTextNode("Append Date?");
let id = elm.innerText; let fileName = "session:" + session + ".json";
let fileName = "session:" + id + ".json"; let id = session;
chkBoxTag.type = "checkbox"; chkBoxTag.type = "checkbox";
inputTag.value = fileName; inputTag.value = fileName;
chkBoxTag.id = "chkbx"; chkBoxTag.id = "chkbx";
@ -144,14 +136,15 @@ const downloadSession = (elm = null) => {
}).then((willDl) => { }).then((willDl) => {
if (willDl) { if (willDl) {
if (chkBoxTag.checked) { if (chkBoxTag.checked) {
fileName = "session:" + id + ":" + fileName = "session:" + id + ":" + new Date().toLocaleString()
new Date().toLocaleString().split(',')[0].replace(/\//g, "-") + ".json"; .split(',')[0]
.replace(/\//g, "-") + ".json";
} }
storageApi.get(id).then((results) => { storageApi.get(id).then((results) => {
let json = JSON.parse(results[id]); let json = JSON.parse(results[id]);
console.log("Downloading: " + id);
let dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json)); let dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(json));
console.log("Downloading: " + id);
doUrlAction(dataStr, fileName, true); doUrlAction(dataStr, fileName, true);
}); });
} }

View File

@ -59,12 +59,12 @@ const checkSessionListForDuplicate = (newName) => {
return newName; return newName;
} }
const appendToSavedSessionsList = (enteryName) => { const appendToSavedSessionsList = (enteryName, storeSize) => {
const text = document.createTextNode(storeSize + " | " + enteryName);
let liTag = document.createElement("LI"); let liTag = document.createElement("LI");
let text = document.createTextNode(enteryName.trim());
liTag.className = "sessionLI"; liTag.className = "sessionLI";
liTag.setAttribute("name", enteryName.trim()); liTag.setAttribute("name", enteryName);
liTag.append(text); liTag.append(text);
document.getElementById("savedSessions").append(liTag); document.getElementById("savedSessions").append(liTag);
} }