From 3255d23ddf73c98488b03a331656b7f9c46cca7f Mon Sep 17 00:00:00 2001 From: Minigugus <43109623+Minigugus@users.noreply.github.com> Date: Mon, 14 Oct 2019 00:25:31 +0200 Subject: [PATCH] #1427 - Added backup/restore buttons in the Options UI --- src/js/background/backgroundLogic.js | 33 ++++++++++++++++++++++++++++ src/js/background/messageHandler.js | 6 +++++ src/js/options.js | 29 ++++++++++++++++++++++++ src/manifest.json | 6 +++++ src/options.html | 24 ++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 src/js/options.js create mode 100644 src/options.html diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index 52d61a2..25640ba 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -215,6 +215,39 @@ const backgroundLogic = { return browser.tabs.remove(tabIds); }, + async backupIdentitiesState() { + const identities = await browser.contextualIdentities.query({}); + return identities.map(({ color, icon, name }) => ({ color, icon, name })); + }, + + async restoreIdentitiesState(identities) { + const backup = await browser.contextualIdentities.query({}); + let allSucceed = true; + const identitiesPromise = identities.map(({ color, icon, name }) => + browser.contextualIdentities.create({ color, icon, name }).catch(() => { + allSucceed = false; + return null; + }) + ); + const created = await Promise.all(identitiesPromise); + if (!allSucceed) { // Importation failed, restore previous state + await Promise.all( + created.map((identityOrNull) => { + if (identityOrNull) { + return browser.contextualIdentities.remove(identityOrNull.cookieStoreId); + } + return Promise.resolve(); + }) + ); + } else { // Importation succeed, remove old identities + await Promise.all( + backup.map((identity) => + browser.contextualIdentities.remove(identity.cookieStoreId) + ) + ); + } + }, + async queryIdentitiesState(windowId) { const identities = await browser.contextualIdentities.query({}); const identitiesOutput = {}; diff --git a/src/js/background/messageHandler.js b/src/js/background/messageHandler.js index 9fbe88e..0314ff7 100644 --- a/src/js/background/messageHandler.js +++ b/src/js/background/messageHandler.js @@ -61,6 +61,12 @@ const messageHandler = { windowId: m.windowId }); break; + case "backupIdentitiesState": + response = backgroundLogic.backupIdentitiesState(); + break; + case "restoreIdentitiesState": + response = backgroundLogic.restoreIdentitiesState(m.identities); + break; case "queryIdentitiesState": response = backgroundLogic.queryIdentitiesState(m.message.windowId); break; diff --git a/src/js/options.js b/src/js/options.js new file mode 100644 index 0000000..ac28700 --- /dev/null +++ b/src/js/options.js @@ -0,0 +1,29 @@ +window.addEventListener("load", () => { + const backupLink = document.getElementById("containers-save-link"); + document.getElementById("containers-save-button").addEventListener("click", async () => { + const content = JSON.stringify( + await browser.runtime.sendMessage({ + method: "backupIdentitiesState" + }) + ); + backupLink.href = `data:application/json;base64,${btoa(content)}`; + backupLink.download = `containers-backup-${(new Date()).toISOString()}.json`; + backupLink.click(); + }, { capture: true, passive: false }); + + const restoreInput = document.getElementById("containers-restore-input"); + restoreInput.addEventListener("change", () => { + if (restoreInput.files.length) { + const reader = new FileReader(); + reader.onloadend = async () => { + const identitiesState = JSON.parse(reader.result); + await browser.runtime.sendMessage({ + method: "restoreIdentitiesState", + identities: identitiesState + }); + }; + reader.readAsText(restoreInput.files.item(0)); + } + restoreInput.reset(); + }); +}); diff --git a/src/manifest.json b/src/manifest.json index bab033b..5f3b12a 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -59,6 +59,12 @@ "page": "js/background/index.html" }, + "options_ui": { + "page": "options.html", + "browser_style": true, + "chrome_style": true + }, + "content_scripts": [ { "matches": [""], diff --git a/src/options.html b/src/options.html new file mode 100644 index 0000000..477dcdc --- /dev/null +++ b/src/options.html @@ -0,0 +1,24 @@ + + + + Multi-Account Containers + + + + +
+ Restore + +

WARNING ! This operation will erase current configuration with the imported one. All cookies will be discarded.

+
+ +
+ Save + + +

NOTE : Backup containers names, icons and colors, but not containers' cookies.

+
+ + + +