Skip to content

Commit

Permalink
mozilla#1427 - Added backup/restore buttons in the Options UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Minigugus committed Oct 13, 2019
1 parent a4c578a commit 3255d23
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/js/background/backgroundLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
6 changes: 6 additions & 0 deletions src/js/background/messageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/js/options.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
6 changes: 6 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
"page": "js/background/index.html"
},

"options_ui": {
"page": "options.html",
"browser_style": true,
"chrome_style": true
},

"content_scripts": [
{
"matches": ["<all_urls>"],
Expand Down
24 changes: 24 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Multi-Account Containers</title>

</head>
<body>

<fieldset>
<legend>Restore</legend>
<input id="containers-restore-input" type="file">
<p><strong>WARNING !</strong> This operation will erase current configuration with the imported one. All cookies will be discarded.</p>
</fieldset>

<fieldset>
<legend>Save</legend>
<a id="containers-save-link" href="#" style="display: none;"></a>
<button id="containers-save-button">Backup</button>
<p>NOTE : Backup containers names, icons and colors, but <em>not</em> containers' cookies.</p>
</fieldset>

<script src="js/options.js"></script>
</body>
</html>

0 comments on commit 3255d23

Please sign in to comment.