move identitiesState to SDK index to preserve it
When a user closes the pop-up, preserve the hidden tabs for the containers, so they can still quickly re-open the tabs.
This commit is contained in:
parent
dcdd2a3c72
commit
497edd75f2
2 changed files with 58 additions and 26 deletions
27
index.js
27
index.js
|
@ -6,16 +6,26 @@ const webExtension = require('sdk/webextension');
|
||||||
|
|
||||||
const CONTAINER_STORE = 'firefox-container-';
|
const CONTAINER_STORE = 'firefox-container-';
|
||||||
|
|
||||||
|
const identitiesState = {
|
||||||
|
};
|
||||||
|
|
||||||
function getCookieStoreIdForContainer(containerId) {
|
function getCookieStoreIdForContainer(containerId) {
|
||||||
return CONTAINER_STORE + containerId;
|
return CONTAINER_STORE + containerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
function convert(identity) {
|
function convert(identity) {
|
||||||
|
const cookieStoreId = getCookieStoreIdForContainer(identity.userContextId);
|
||||||
|
let hiddenTabUrls = [];
|
||||||
|
|
||||||
|
if (cookieStoreId in identitiesState) {
|
||||||
|
hiddenTabUrls = identitiesState[cookieStoreId].hiddenTabUrls;
|
||||||
|
}
|
||||||
const result = {
|
const result = {
|
||||||
name: ContextualIdentityService.getUserContextLabel(identity.userContextId),
|
name: ContextualIdentityService.getUserContextLabel(identity.userContextId),
|
||||||
icon: identity.icon,
|
icon: identity.icon,
|
||||||
color: identity.color,
|
color: identity.color,
|
||||||
cookieStoreId: getCookieStoreIdForContainer(identity.userContextId)
|
cookieStoreId: cookieStoreId,
|
||||||
|
hiddenTabUrls: hiddenTabUrls
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -60,7 +70,12 @@ function queryContainers(details) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
identities.push(convert(identity));
|
const convertedIdentity = convert(identity);
|
||||||
|
|
||||||
|
identities.push(convertedIdentity);
|
||||||
|
if (!(convertedIdentity.cookieStoreId in identitiesState)) {
|
||||||
|
identitiesState[convertedIdentity.cookieStoreId] = {hiddenTabUrls: []};
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.resolve(identities);
|
return Promise.resolve(identities);
|
||||||
|
@ -145,10 +160,11 @@ function handleWebExtensionMessage(message, sender, sendReply) {
|
||||||
sendReply(contextualIdentities.query(message.arguments));
|
sendReply(contextualIdentities.query(message.arguments));
|
||||||
break;
|
break;
|
||||||
case 'hide':
|
case 'hide':
|
||||||
sendReply(contextualIdentities.hide(message.arguments));
|
identitiesState[message.cookieStoreId].hiddenTabUrls = message.tabUrlsToSave;
|
||||||
break;
|
break;
|
||||||
case 'show':
|
case 'show':
|
||||||
sendReply(contextualIdentities.show(message.arguments));
|
sendReply(identitiesState[message.cookieStoreId].hiddenTabUrls);
|
||||||
|
identitiesState[message.cookieStoreId].hiddenTabUrls = [];
|
||||||
break;
|
break;
|
||||||
case 'get':
|
case 'get':
|
||||||
sendReply(contextualIdentities.get(message.arguments));
|
sendReply(contextualIdentities.get(message.arguments));
|
||||||
|
@ -162,6 +178,9 @@ function handleWebExtensionMessage(message, sender, sendReply) {
|
||||||
case 'remove':
|
case 'remove':
|
||||||
sendReply(contextualIdentities.remove(message.arguments));
|
sendReply(contextualIdentities.remove(message.arguments));
|
||||||
break;
|
break;
|
||||||
|
case 'getIdentitiesState':
|
||||||
|
sendReply(identitiesState);
|
||||||
|
break;
|
||||||
case 'open-containers-preferences':
|
case 'open-containers-preferences':
|
||||||
tabs.open('about:preferences#containers');
|
tabs.open('about:preferences#containers');
|
||||||
sendReply({content: 'opened'});
|
sendReply({content: 'opened'});
|
||||||
|
|
|
@ -1,39 +1,54 @@
|
||||||
/* global browser, window, document */
|
/* global browser, window, document */
|
||||||
const identitiesState = {
|
const CONTAINER_HIDE_SRC = '/img/container-hide.svg';
|
||||||
};
|
const CONTAINER_UNHIDE_SRC = '/img/container-unhide.svg';
|
||||||
|
|
||||||
function hideContainerTabs(containerId) {
|
function hideContainerTabs(containerId) {
|
||||||
const tabIdsToRemove = [];
|
const tabIdsToRemove = [];
|
||||||
|
const tabUrlsToSave = [];
|
||||||
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
||||||
|
|
||||||
browser.tabs.query({cookieStoreId: containerId}).then(tabs=> {
|
browser.tabs.query({cookieStoreId: containerId}).then(tabs=> {
|
||||||
tabs.forEach(tab=> {
|
tabs.forEach(tab=> {
|
||||||
tabIdsToRemove.push(tab.id);
|
tabIdsToRemove.push(tab.id);
|
||||||
identitiesState[containerId].hiddenTabUrls.push(tab.url);
|
tabUrlsToSave.push(tab.url);
|
||||||
|
});
|
||||||
|
browser.runtime.sendMessage({
|
||||||
|
method: 'hide',
|
||||||
|
cookieStoreId: containerId,
|
||||||
|
tabUrlsToSave: tabUrlsToSave
|
||||||
|
}).then(()=> {
|
||||||
|
browser.tabs.remove(tabIdsToRemove);
|
||||||
|
hideorshowIcon.src = CONTAINER_UNHIDE_SRC;
|
||||||
});
|
});
|
||||||
browser.tabs.remove(tabIdsToRemove);
|
|
||||||
hideorshowIcon.src = '/img/container-unhide.svg';
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function showContainerTabs(containerId) {
|
function showContainerTabs(containerId) {
|
||||||
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
||||||
|
|
||||||
identitiesState[containerId].hiddenTabUrls.forEach(url=> {
|
browser.runtime.sendMessage({
|
||||||
// Have to use SDK to open tabs in case they are about:* pages
|
method: 'show',
|
||||||
browser.tabs.create({
|
cookieStoreId: containerId
|
||||||
url: url,
|
}).then(hiddenTabUrls=> {
|
||||||
cookieStoreId: containerId
|
hiddenTabUrls.forEach(url=> {
|
||||||
|
browser.tabs.create({
|
||||||
|
url: url,
|
||||||
|
cookieStoreId: containerId
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
identitiesState[containerId].hiddenTabUrls = [];
|
hideorshowIcon.src = CONTAINER_HIDE_SRC;
|
||||||
hideorshowIcon.src = '/img/container-hide.svg';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
||||||
const identitiesListElement = document.querySelector('.identities-list');
|
const identitiesListElement = document.querySelector('.identities-list');
|
||||||
|
|
||||||
identities.forEach(identity=> {
|
identities.forEach(identity=> {
|
||||||
|
let hideOrShowIconSrc = CONTAINER_HIDE_SRC;
|
||||||
|
|
||||||
|
if (identity.hiddenTabUrls.length) {
|
||||||
|
hideOrShowIconSrc = CONTAINER_UNHIDE_SRC;
|
||||||
|
}
|
||||||
const identityRow = `
|
const identityRow = `
|
||||||
<tr data-identity-cookie-store-id="${identity.cookieStoreId}" >
|
<tr data-identity-cookie-store-id="${identity.cookieStoreId}" >
|
||||||
<td><div class="userContext-icon"
|
<td><div class="userContext-icon"
|
||||||
|
@ -46,17 +61,13 @@ browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
||||||
data-identity-cookie-store-id="${identity.cookieStoreId}"
|
data-identity-cookie-store-id="${identity.cookieStoreId}"
|
||||||
id="${identity.cookieStoreId}-hideorshow-icon"
|
id="${identity.cookieStoreId}-hideorshow-icon"
|
||||||
class="hideorshow-icon"
|
class="hideorshow-icon"
|
||||||
src="/img/container-hide.svg"
|
src="${hideOrShowIconSrc}"
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td>></td>
|
<td>></td>
|
||||||
</tr>`;
|
</tr>`;
|
||||||
|
|
||||||
identitiesListElement.innerHTML += identityRow;
|
identitiesListElement.innerHTML += identityRow;
|
||||||
|
|
||||||
if (!(identity in identitiesState)) {
|
|
||||||
identitiesState[identity.cookieStoreId] = {hiddenTabUrls: []};
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const rows = identitiesListElement.querySelectorAll('tr');
|
const rows = identitiesListElement.querySelectorAll('tr');
|
||||||
|
@ -66,11 +77,13 @@ browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
||||||
if (e.target.matches('.hideorshow-icon')) {
|
if (e.target.matches('.hideorshow-icon')) {
|
||||||
const containerId = e.target.dataset.identityCookieStoreId;
|
const containerId = e.target.dataset.identityCookieStoreId;
|
||||||
|
|
||||||
if (identitiesState[containerId].hiddenTabUrls.length) {
|
browser.runtime.sendMessage({method: 'getIdentitiesState'}).then(identitiesState=> {
|
||||||
showContainerTabs(containerId);
|
if (identitiesState[containerId].hiddenTabUrls.length) {
|
||||||
} else {
|
showContainerTabs(containerId);
|
||||||
hideContainerTabs(containerId);
|
} else {
|
||||||
}
|
hideContainerTabs(containerId);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue