Added additional edit functionality and added selective save functionality.

This commit is contained in:
Maxim Stewart 2019-04-19 00:41:25 -05:00
parent f72b054ef5
commit f2ada64507
6 changed files with 191 additions and 138 deletions

View File

@ -4,8 +4,9 @@ Easy Session Manager allows you to manage your Firefox session by backing up or
# Download # Download
https://addons.mozilla.org/en-US/firefox/addon/easy-session-manager/ https://addons.mozilla.org/en-US/firefox/addon/easy-session-manager/
# Version: 0.1.8 # Version: 0.2.0
Fixed select all issue. Added additional edit functionality.
Added selective save functionality.
# Images # Images
![1 Default interface with no sessions. ](images/pic1.png) ![1 Default interface with no sessions. ](images/pic1.png)

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": " Easy Session Manager", "name": " Easy Session Manager",
"version": "0.1.8", "version": "0.2.0",
"description": " Easy Session Manager allows you to manage your Firefox session by backing up or loading your saved sessions.", "description": " Easy Session Manager allows you to manage your Firefox session by backing up or loading your saved sessions.",
"applications": { "applications": {

View File

@ -12,9 +12,9 @@
<input type="image" name="delete" src="../images/icons/delete.png" title="Delete..." alt="Delete" /> <input type="image" name="delete" src="../images/icons/delete.png" title="Delete..." alt="Delete" />
<br/><br/> <br/><br/>
<input id="replaceTabs" name="replaceTabs" type="checkbox" checked /> <input id="replaceTabs" name="replaceTabs" type="checkbox" />
<label for="replaceTabs">Replace Current Tabs</label> <label for="replaceTabs">Replace Current Tabs</label>
<input id="selectiveOpen" name="selectiveOpen" type="checkbox" /> <input id="selectiveOpen" name="selectiveOpen" type="checkbox" checked />
<label for="selectiveOpen">Selective Open</label> <label for="selectiveOpen">Selective Open</label>
<div id="savedSessions"> <div id="savedSessions">

View File

@ -1,9 +1,9 @@
let selectedItem = null; let selectedItem = null;
const toggleSelect = (name) => { const toggleSelect = (source, name) => {
let checkboxes = document.getElementsByName(name); let checkboxes = document.getElementsByName(name);
for (var i = 0; i < checkboxes.length; i++) { for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = !checkboxes[i].checked; checkboxes[i].checked = source.checked;
} }
} }
@ -59,7 +59,7 @@ document.addEventListener("dblclick", (e) => {
selectedItem = e.target; selectedItem = e.target;
selectedItem.setAttribute("class", "sessionLI selected"); selectedItem.setAttribute("class", "sessionLI selected");
try { try {
let id = e.target.innerHTML.trim(); let id = e.target.innerText.trim();
storage.get(id).then(storageResults => { storage.get(id).then(storageResults => {
let json = JSON.parse(storageResults[id]); let json = JSON.parse(storageResults[id]);
let keys = Object.keys(json); let keys = Object.keys(json);
@ -70,71 +70,13 @@ document.addEventListener("dblclick", (e) => {
if (!selectiveOpen.checked) { if (!selectiveOpen.checked) {
loadSession(json, replaceTabs.checked); loadSession(json, replaceTabs.checked);
} else { } else {
let container = document.createElement("DIV"); let container = selectionWindow(json, keys, keysLength);
let ulTemplate = document.querySelector('#ulTemplate');
let liTemplate = document.querySelector('#liTemplate');
for (let i = 0; i < keysLength; i++) {
let ulClone = document.importNode(ulTemplate.content, true);
let ulTag = ulClone.querySelector('.collection');
let selAll = ulClone.querySelector('input');
let h2Tag = ulClone.querySelector('.ulHeader');
let ulLblTag = ulClone.querySelector('label');
let h2Txt = document.createTextNode("Window: " + (i + 1));
let store = json[keys[i]];
let j = 0;
container.id = "editSelectionContainer";
selAll.id = "selectAllWin" + i;
ulLblTag.htmlFor = "selectAllWin" + i;
selAll.addEventListener("click", function () {
toggleSelect("Win" + i);
});
h2Tag.appendChild(h2Txt);
store.forEach(tab => {
let liClone = document.importNode(liTemplate.content, true);
let inptTag = liClone.querySelector("input");
let lblTag = liClone.querySelector("label");
let labelTxt = document.createTextNode(tab.link);
inptTag.id = "Win" + i + "Li" + j;
lblTag.htmlFor = "Win" + i + "Li" + j;
lblTag.title = tab.link;
inptTag.setAttribute("name", "Win" + i);
lblTag.appendChild(labelTxt);
ulTag.appendChild(liClone);
j++;
});
container.appendChild(ulClone);
}
swal("Selective Open", { swal("Selective Open", {
content: container, content: container,
buttons: true, buttons: true,
}).then((willOpen) => { }).then((willOpen) => {
if (willOpen) { if (willOpen) {
let sessionData = {}; json = selectionData(container, keys, keysLength);
let ulTags = container.querySelectorAll("ul");
for (let i = 0; i < keysLength; i++) {
let links = [];
for (var ii = 0; ii < ulTags[i].children.length; ii++) {
let li = ulTags[i].children[ii];
if (li.children[0].checked) {
links.push(
{"link" : li.children[1].title.trim()}
);
}
}
if (links.length > 0) {
sessionData[keys[i]] = links;
}
}
json = sessionData;
keysLength = Object.keys(json).length; keysLength = Object.keys(json).length;
if (keysLength > 0) { if (keysLength > 0) {
loadSession(json, replaceTabs.checked); loadSession(json, replaceTabs.checked);

View File

@ -3,15 +3,34 @@ const windowSys = 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 = "What is this session's name? Allowed: a-z, A-Z, -, _") => { const saveSession = (elm = null, message = "[ Session Name ] Allowed: a-z, A-Z, -, _") => {
let inputTag = document.createElement("INPUT"); let inputTag = document.createElement("INPUT");
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.innerHTML; inputTag.value = elm.innerText;
windowSys.getAll({ populate: true, windowTypes: ["normal"] }).then((windows) => {
let sessionData = {};
for (let i = 0; i < windows.length; i++) {
let links = [];
for (var ii = 0; ii < windows[i].tabs.length; ii++) {
if (!windows[i].tabs[ii].url.includes("about:")) {
links.push(
{"link" : windows[i].tabs[ii].url.trim()}
);
}
}
sessionData["WindowID:" + windows[i].id] = links;
}
let keys = Object.keys(sessionData);
let keysLength = Object.keys(sessionData).length;
let container = selectionWindow(sessionData, keys, keysLength);
container.prepend(inputTag);
swal(message, { swal(message, {
content: inputTag, content: container,
buttons: true, buttons: true,
}).then((value) => { }).then((value) => {
if (value) { if (value) {
@ -24,26 +43,8 @@ const saveSession = (elm = null, message = "What is this session's name? Allowed
} }
console.log("Saving session..."); console.log("Saving session...");
windowSys.getAll({ populate: true, windowTypes: ["normal"] }).then((windows) => { sessionData = selectionData(container, keys, keysLength);
let sessionData = {};
for (let i = 0; i < windows.length; i++) {
let links = [];
for (var ii = 0; ii < windows[i].tabs.length; ii++) {
if (!windows[i].tabs[ii].url.includes("about:")) {
links.push(
{"link" : windows[i].tabs[ii].url.trim()}
);
}
}
sessionData["WindowID:" + windows[i].id] = links;
}
saveToStorage(enteryName, JSON.stringify(sessionData)); saveToStorage(enteryName, JSON.stringify(sessionData));
}).then(() => {
if (document.getElementsByName(enteryName).length == 0) {
appendToSavedSessionsList(enteryName);
}
});
} else { } else {
swal("Canceled save...", { swal("Canceled save...", {
icon: "warning", icon: "warning",
@ -55,15 +56,39 @@ const saveSession = (elm = null, message = "What is this session's name? Allowed
}); });
} }
}); });
});
} }
const editSession = (elm = null, message = "Editing selected session... Allowed: a-z, A-Z, -, _") => { const editSession = (elm = null, message = "Editing selected session...\nAllowed: a-z, A-Z, -, _") => {
let id = elm.innerHTML; let id = elm.innerText;
let inputTag = document.createElement("INPUT"); let inputTag = document.createElement("INPUT");
let checkedTag = document.createElement("INPUT");
let labelTag = document.createElement("LABEL");
let brTag = document.createElement("BR");
inputTag.disabled = true;
inputTag.value = id; inputTag.value = id;
checkedTag.type = "checkbox";
checkedTag.id = "newOrOld";
labelTag.innerText = "Create New Session";
labelTag.htmlFor = "newOrOld";
checkedTag.onchange = function () {
inputTag.disabled = !inputTag.disabled;
}
storage.get(id).then((storageResults) => {
let json = JSON.parse(storageResults[id]);
let keys = Object.keys(json);
let keysLength = Object.keys(json).length;
let container = selectionWindow(json, keys, keysLength);
container.prepend(labelTag);
container.prepend(checkedTag);
container.prepend(brTag);
container.prepend(inputTag);
swal(message, { swal(message, {
content: inputTag, content: container,
buttons: true, buttons: true,
}).then((value) => { }).then((value) => {
if (value) { if (value) {
@ -75,13 +100,20 @@ const editSession = (elm = null, message = "Editing selected session... Allowed:
return ; return ;
} }
json = selectionData(container, keys, keysLength);
if (!checkedTag.checked) {
storage.get(id).then((storageResults) => { storage.get(id).then((storageResults) => {
storage.remove(id); storage.remove(id);
json = JSON.parse(storageResults[id]);
saveToStorage(newName, JSON.stringify(json), true); saveToStorage(newName, JSON.stringify(json), true);
}); });
elm.textContent = newName; elm.textContent = newName;
} else {
if (newName !== elm.textContent) {
saveToStorage(newName, JSON.stringify(json), false);
} else {
editSession(elm, "Cannot have the same name as before...\nAllowed: a-z, A-Z, -, _")
}
}
} else { } else {
swal("Canceled edit...", { swal("Canceled edit...", {
icon: "warning", icon: "warning",
@ -93,6 +125,7 @@ const editSession = (elm = null, message = "Editing selected session... Allowed:
}); });
} }
}); });
});
} }
const saveToStorage = (name, data, fromEdit = false) => { const saveToStorage = (name, data, fromEdit = false) => {
@ -109,6 +142,7 @@ const saveToStorage = (name, data, fromEdit = false) => {
icon: "warning", icon: "warning",
}); });
} else { } else {
appendToSavedSessionsList(name);
swal("Saved session...", { swal("Saved session...", {
icon: "success", icon: "success",
}); });
@ -122,13 +156,13 @@ const saveToStorage = (name, data, fromEdit = false) => {
const deleteFromStorage = (elm = null) => { const deleteFromStorage = (elm = null) => {
swal({ swal({
title: "Are you sure?", title: "Are you sure?",
text: "Do you wish to delete session:\n" + elm.innerHTML + "?", text: "Do you wish to delete session:\n" + elm.innerText + "?",
icon: "warning", icon: "warning",
buttons: true, buttons: true,
dangerMode: true, dangerMode: true,
}).then((willDelete) => { }).then((willDelete) => {
if (willDelete) { if (willDelete) {
storage.remove(elm.innerHTML).then(() => { storage.remove(elm.innerText).then(() => {
elm.parentElement.removeChild(elm); elm.parentElement.removeChild(elm);
}); });
swal("Deleted session successfully...", { swal("Deleted session successfully...", {
@ -157,7 +191,7 @@ const downloadSession = (elm = null) => {
let brTag = document.createElement("BR"); let brTag = document.createElement("BR");
var dlAnchorElem = document.getElementById('downloadAnchorElem'); var dlAnchorElem = document.getElementById('downloadAnchorElem');
let text = document.createTextNode("Append Date?"); let text = document.createTextNode("Append Date?");
let id = elm.innerHTML; let id = elm.innerText;
let fileName = "session:" + id + ".json"; let fileName = "session:" + id + ".json";
chkBoxTag.type = "checkbox"; chkBoxTag.type = "checkbox";
inputTag.value = fileName; inputTag.value = fileName;
@ -201,7 +235,6 @@ const loadSession = (json = null, replaceTabs = false) => {
let wasCurrentTabId = null; let wasCurrentTabId = null;
if (replaceTabs) { // Clear all windows but main then load... if (replaceTabs) { // Clear all windows but main then load...
// Clear all non-current windows and then current window's tabs
for (let i = 0; i < windows.length; i++) { for (let i = 0; i < windows.length; i++) {
if (currentWindow.id != windows[i].id) { if (currentWindow.id != windows[i].id) {
windowSys.remove(windows[i].id); windowSys.remove(windows[i].id);
@ -272,3 +305,75 @@ const appendToSavedSessionsList = (enteryName) => {
} }
getSavedSessionIDs(); getSavedSessionIDs();
/* Selection Process */
const selectionWindow = (json = "", keys = null, keysLength = 0) => {
let container = document.createElement("DIV");
let ulTemplate = document.querySelector('#ulTemplate');
let liTemplate = document.querySelector('#liTemplate');
for (let i = 0; i < keysLength; i++) {
let ulClone = document.importNode(ulTemplate.content, true);
let ulTag = ulClone.querySelector('.collection');
let selAll = ulClone.querySelector('input');
let h2Tag = ulClone.querySelector('.ulHeader');
let ulLblTag = ulClone.querySelector('label');
let h2Txt = document.createTextNode("Window: " + (i + 1));
let store = json[keys[i]];
let j = 0;
container.id = "editSelectionContainer";
selAll.id = "selectAllWin" + i;
ulLblTag.htmlFor = "selectAllWin" + i;
selAll.addEventListener("click", function (eve) {
toggleSelect(eve.target, "Win" + i);
});
h2Tag.appendChild(h2Txt);
store.forEach(tab => {
let liClone = document.importNode(liTemplate.content, true);
let inptTag = liClone.querySelector("input");
let lblTag = liClone.querySelector("label");
let labelTxt = document.createTextNode(tab.link);
inptTag.id = "Win" + i + "Li" + j;
lblTag.htmlFor = "Win" + i + "Li" + j;
lblTag.title = tab.link;
inptTag.setAttribute("name", "Win" + i); // Used for toggle select all
lblTag.appendChild(labelTxt);
ulTag.appendChild(liClone);
j++;
});
container.appendChild(ulClone);
}
return container;
}
const selectionData = (container = null, keys = null, keysLength = 0) => {
let sessionData = {};
let ulTags = container.querySelectorAll("ul");
for (let i = 0; i < keysLength; i++) {
let links = [];
for (var ii = 0; ii < ulTags[i].children.length; ii++) {
let li = ulTags[i].children[ii];
if (li.children[0].checked) {
links.push(
{"link" : li.children[1].innerText.trim()}
);
}
}
if (links.length > 0) {
sessionData[keys[i]] = links;
}
}
return sessionData;
}

View File

@ -31,9 +31,9 @@ input[type=image]:hover {
} }
#editSelectionContainer { #editSelectionContainer {
height: 350px; height: 315px;
min-height: 350x; min-height: 315px;
max-height: 350px; max-height: 315px;
white-space: nowrap; white-space: nowrap;
display: block; display: block;
overflow-x: auto; overflow-x: auto;
@ -62,6 +62,11 @@ input[type=image]:hover {
color: rgba(255, 255, 255, 0.5); color: rgba(255, 255, 255, 0.5);
} }
.collection {
padding-top: 1em;
padding-bottom: 1em;
}
.container { .container {
width: 100%; width: 100%;
height: 100%; height: 100%;