multi-account-containers/webextension/js/popup.js
luke crouch 50419d6bb1 re-integrate ContextualIdentityService into SDK code (#27)
* bring ContextualIdentityService back into SDK code
* restore show/hide functionality in WebExtension
* 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.
2017-01-03 15:06:10 +00:00

126 lines
3.5 KiB
JavaScript

/* global browser, window, document */
const CONTAINER_HIDE_SRC = '/img/container-hide.svg';
const CONTAINER_UNHIDE_SRC = '/img/container-unhide.svg';
function hideContainerTabs(containerId) {
const tabIdsToRemove = [];
const tabUrlsToSave = [];
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
browser.tabs.query({cookieStoreId: containerId}).then(tabs=> {
tabs.forEach(tab=> {
tabIdsToRemove.push(tab.id);
tabUrlsToSave.push(tab.url);
});
browser.runtime.sendMessage({
method: 'hide',
cookieStoreId: containerId,
tabUrlsToSave: tabUrlsToSave
}).then(()=> {
browser.tabs.remove(tabIdsToRemove);
hideorshowIcon.src = CONTAINER_UNHIDE_SRC;
});
});
}
function showContainerTabs(containerId) {
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
browser.runtime.sendMessage({
method: 'show',
cookieStoreId: containerId
}).then(hiddenTabUrls=> {
hiddenTabUrls.forEach(url=> {
browser.tabs.create({
url: url,
cookieStoreId: containerId
});
});
});
hideorshowIcon.src = CONTAINER_HIDE_SRC;
}
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
const identitiesListElement = document.querySelector('.identities-list');
identities.forEach(identity=> {
let hideOrShowIconSrc = CONTAINER_HIDE_SRC;
if (identity.hiddenTabUrls.length) {
hideOrShowIconSrc = CONTAINER_UNHIDE_SRC;
}
const identityRow = `
<tr data-identity-cookie-store-id="${identity.cookieStoreId}" >
<td><div class="userContext-icon"
data-identity-icon="${identity.icon}"
data-identity-color="${identity.color}"
></div></td>
<td>${identity.name}</td>
<td class="hideorshow" >
<img
data-identity-cookie-store-id="${identity.cookieStoreId}"
id="${identity.cookieStoreId}-hideorshow-icon"
class="hideorshow-icon"
src="${hideOrShowIconSrc}"
/>
</td>
<td>&gt;</td>
</tr>`;
identitiesListElement.innerHTML += identityRow;
});
const rows = identitiesListElement.querySelectorAll('tr');
rows.forEach(row=> {
row.addEventListener('click', e=> {
if (e.target.matches('.hideorshow-icon')) {
const containerId = e.target.dataset.identityCookieStoreId;
browser.runtime.sendMessage({method: 'getIdentitiesState'}).then(identitiesState=> {
if (identitiesState[containerId].hiddenTabUrls.length) {
showContainerTabs(containerId);
} else {
hideContainerTabs(containerId);
}
});
}
});
});
});
document.querySelector('#edit-containers-link').addEventListener('click', ()=> {
browser.runtime.sendMessage('open-containers-preferences').then(()=> {
window.close();
});
});
function moveTabs(sortedTabsArray) {
let positionIndex = 0;
sortedTabsArray.forEach(tabID=> {
browser.tabs.move(tabID, {index: positionIndex});
positionIndex++;
});
}
document.querySelector('#sort-containers-link').addEventListener('click', ()=> {
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
identities.unshift({cookieStoreId: 'firefox-default'});
browser.tabs.query({}).then(tabsArray=> {
const sortedTabsArray = [];
identities.forEach(identity=> {
tabsArray.forEach(tab=> {
if (tab.cookieStoreId === identity.cookieStoreId) {
sortedTabsArray.push(tab.id);
}
});
});
moveTabs(sortedTabsArray);
});
});
});