#1427 - Added backup/restore buttons in the Options UI
This commit is contained in:
parent
a4c578adde
commit
3255d23ddf
5 changed files with 98 additions and 0 deletions
|
@ -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 = {};
|
||||
|
|
|
@ -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;
|
||||
|
|
29
src/js/options.js
Normal file
29
src/js/options.js
Normal file
|
@ -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();
|
||||
});
|
||||
});
|
|
@ -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>"],
|
||||
|
|
24
src/options.html
Normal file
24
src/options.html
Normal file
|
@ -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>
|
Loading…
Add table
Reference in a new issue