From 96809cabec3ba23b207375f102888ff8d74c531d Mon Sep 17 00:00:00 2001 From: Maxim Stewart Date: Sat, 19 Jan 2019 01:05:01 -0600 Subject: [PATCH] Improved loading of sessions. --- README.md | 4 +-- src/manifest.json | 2 +- src/scripts/sessionManager.js | 67 +++++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index f363cc0..4a371cb 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ Easy Session Manager allows you to manage your Firefox session by backing up or # Download https://addons.mozilla.org/en-US/firefox/addon/easy-session-manager/ -# Version: 0.0.5 -Added proper alert messages and improved security of import file. +# Version: 0.0.6 +Improved loading of sessions. # Images ![1 Default interface with no sessions. ](images/pic1.png) diff --git a/src/manifest.json b/src/manifest.json index 931a4aa..7391ac0 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": " Easy Session Manager", - "version": "0.0.5", + "version": "0.0.6", "description": " Easy Session Manager allows you to manage your Firefox session by backing up or loading your saved sessions.", "applications": { diff --git a/src/scripts/sessionManager.js b/src/scripts/sessionManager.js index b778133..2a6ead1 100644 --- a/src/scripts/sessionManager.js +++ b/src/scripts/sessionManager.js @@ -1,7 +1,6 @@ -const regexp = /^[a-zA-Z0-9-_]+$/; // Alphanumeric, dash, underscore -const storage = browser.storage.local; -const windowSys = browser.windows; - +const regexp = /^[a-zA-Z0-9-_]+$/; // Alphanumeric, dash, underscore +const storage = browser.storage.local; +const windowSys = browser.windows; const alertMessage = (type, message) => { let msgTag = document.getElementById("allertMessage"); @@ -131,36 +130,52 @@ const editSession = () => { const loadSession = (id = null) => { console.log("Loading session..."); try { - storage.get(id).then((storageResults) => { - let json = JSON.parse(storageResults[id]); - let keys = Object.keys(json); + storage.get(id).then(storageResults => { + let json = JSON.parse(storageResults[id]); + let keys = Object.keys(json); + let keysLength = Object.keys(json).length; - browser.windows.getAll().then((windows) => { - windowSys.getCurrent({populate: true}).then((currentWindow) => { - // Clear out windows - for (var i = 0; i < windows.length; i++) { + browser.windows.getAll().then(windows => { + windowSys.getCurrent({populate: true}).then(currentWindow => { + let wasCurrentTabId = null; + + // Clear all non-current windows and then current window's tabs + for (let i = 0; i < windows.length; i++) { if (currentWindow.id != windows[i].id) { windowSys.remove(windows[i].id); + } else { + let ids = []; + currentWindow.tabs.forEach(tab => { + if (!tab.active) { + ids.push(tab.id); + } else { + wasCurrentTabId = tab.id; + } + }); + browser.tabs.remove(ids); } } - }, windows); - }); - // Open windows and populate with proper tabs - keys.forEach((key) => { - let store = json[key]; - let urls = []; + // First load tabs to current window. + let store = json[keys[0]]; + store.forEach(tab => { + browser.tabs.create({ url: tab.link }); + }); - for (var i = 0; i < store.length; i++) { - urls.push(store[i].link); - } + // If more than one window, load tabs to new windows. + browser.tabs.remove(wasCurrentTabId); + if (keysLength > 1) { + for (let i = 1; i < keysLength; i++) { + let store = json[keys[i]]; + let urls = []; - windowSys.create({ url: urls }); - }); - - // Finalize clear out windows - windowSys.getCurrent({populate: true}).then((currentWindow) => { - windowSys.remove(currentWindow.id); + for (let j = 0; j < store.length; j++) { + urls.push(store[j].link); + } + windowSys.create({ url: urls }); + } + } + }); }); }); } catch (e) { console.log(e); }