Migrate from cookieStoreId to userContextId
This commit is contained in:
parent
404ee3304e
commit
a6f9175224
2 changed files with 41 additions and 77 deletions
82
index.js
82
index.js
|
@ -21,63 +21,32 @@ prefs.forEach((pref) => {
|
||||||
prefService.set(pref[0], pref[1]);
|
prefService.set(pref[0], pref[1]);
|
||||||
});
|
});
|
||||||
|
|
||||||
const CONTAINER_STORE = 'firefox-container-';
|
|
||||||
|
|
||||||
const identitiesState = {
|
const identitiesState = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function getCookieStoreIdForContainer(containerId) {
|
|
||||||
if (containerId == 0) {
|
|
||||||
return 'firefox-default';
|
|
||||||
}
|
|
||||||
|
|
||||||
return CONTAINER_STORE + containerId;
|
|
||||||
}
|
|
||||||
|
|
||||||
function convert(identity) {
|
function convert(identity) {
|
||||||
const cookieStoreId = getCookieStoreIdForContainer(identity.userContextId);
|
|
||||||
let hiddenTabUrls = [];
|
let hiddenTabUrls = [];
|
||||||
|
|
||||||
if (cookieStoreId in identitiesState) {
|
if (identity.userContextId in identitiesState) {
|
||||||
hiddenTabUrls = identitiesState[cookieStoreId].hiddenTabUrls;
|
hiddenTabUrls = identitiesState[identity.userContextId].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: cookieStoreId,
|
userContextId: identity.userContextId,
|
||||||
hiddenTabUrls: hiddenTabUrls
|
hiddenTabUrls: hiddenTabUrls
|
||||||
};
|
};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isContainerCookieStoreId(storeId) {
|
function getContainer(userContextId) {
|
||||||
return storeId !== null && storeId.startsWith(CONTAINER_STORE);
|
if (!userContextId) {
|
||||||
}
|
|
||||||
|
|
||||||
function getContainerForCookieStoreId(storeId) {
|
|
||||||
if (!isContainerCookieStoreId(storeId)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const containerId = storeId.substring(CONTAINER_STORE.length);
|
|
||||||
|
|
||||||
if (ContextualIdentityService.getIdentityFromId(containerId)) {
|
|
||||||
return parseInt(containerId, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getContainer(cookieStoreId) {
|
|
||||||
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
|
||||||
|
|
||||||
if (!containerId) {
|
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
const identity = ContextualIdentityService.getIdentityFromId(containerId);
|
const identity = ContextualIdentityService.getIdentityFromId(userContextId);
|
||||||
|
|
||||||
return Promise.resolve(convert(identity));
|
return Promise.resolve(convert(identity));
|
||||||
}
|
}
|
||||||
|
@ -94,8 +63,8 @@ function queryContainers(details) {
|
||||||
const convertedIdentity = convert(identity);
|
const convertedIdentity = convert(identity);
|
||||||
|
|
||||||
identities.push(convertedIdentity);
|
identities.push(convertedIdentity);
|
||||||
if (!(convertedIdentity.cookieStoreId in identitiesState)) {
|
if (!(convertedIdentity.userContextId in identitiesState)) {
|
||||||
identitiesState[convertedIdentity.cookieStoreId] = {hiddenTabUrls: []};
|
identitiesState[convertedIdentity.userContextId] = {hiddenTabUrls: []};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -110,14 +79,12 @@ function createContainer(details) {
|
||||||
return Promise.resolve(convert(identity));
|
return Promise.resolve(convert(identity));
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateContainer(cookieStoreId, details) {
|
function updateContainer(userContextId, details) {
|
||||||
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
if (!userContextId) {
|
||||||
|
|
||||||
if (!containerId) {
|
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
const identity = ContextualIdentityService.getIdentityFromId(containerId);
|
const identity = ContextualIdentityService.getIdentityFromId(userContextId);
|
||||||
|
|
||||||
if (!identity) {
|
if (!identity) {
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
|
@ -144,14 +111,12 @@ function updateContainer(cookieStoreId, details) {
|
||||||
return Promise.resolve(convert(identity));
|
return Promise.resolve(convert(identity));
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeContainer(cookieStoreId) {
|
function removeContainer(userContextId) {
|
||||||
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
if (!userContextId) {
|
||||||
|
|
||||||
if (!containerId) {
|
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
const identity = ContextualIdentityService.getIdentityFromId(containerId);
|
const identity = ContextualIdentityService.getIdentityFromId(userContextId);
|
||||||
|
|
||||||
if (!identity) {
|
if (!identity) {
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
|
@ -184,8 +149,8 @@ function openTab(args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let userContextId = 0;
|
let userContextId = 0;
|
||||||
if ('cookieStoreId' in args) {
|
if ('userContextId' in args) {
|
||||||
userContextId = getContainerForCookieStoreId(args.cookieStoreId);
|
userContextId = args.userContextId;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tab = browserWin.gBrowser.addTab(args.url || null,
|
let tab = browserWin.gBrowser.addTab(args.url || null,
|
||||||
|
@ -200,17 +165,16 @@ function queryTabs(args) {
|
||||||
|
|
||||||
for (let tab of tabs) {
|
for (let tab of tabs) {
|
||||||
let xulTab = viewFor(tab);
|
let xulTab = viewFor(tab);
|
||||||
let userContextId = xulTab.getAttribute('usercontextid') || 0;
|
let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10);
|
||||||
let cookieStoreId = getCookieStoreIdForContainer(userContextId);
|
|
||||||
|
|
||||||
if ("cookieStoreId" in args && args.cookieStoreId != cookieStoreId) {
|
if ("userContextId" in args && args.userContextId != userContextId) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
tabList.push({
|
tabList.push({
|
||||||
id: tab.id,
|
id: tab.id,
|
||||||
url: tab.url,
|
url: tab.url,
|
||||||
cookieStoreId: cookieStoreId,
|
userContextId: userContextId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,15 +201,15 @@ function handleWebExtensionMessage(message, sender, sendReply) {
|
||||||
sendReply(queryTabs(message));
|
sendReply(queryTabs(message));
|
||||||
break;
|
break;
|
||||||
case 'hideTab':
|
case 'hideTab':
|
||||||
identitiesState[message.cookieStoreId].hiddenTabUrls = message.tabUrlsToSave;
|
identitiesState[message.userContextId].hiddenTabUrls = message.tabUrlsToSave;
|
||||||
break;
|
break;
|
||||||
case 'showTab':
|
case 'showTab':
|
||||||
sendReply(identitiesState[message.cookieStoreId].hiddenTabUrls);
|
sendReply(identitiesState[message.userContextId].hiddenTabUrls);
|
||||||
identitiesState[message.cookieStoreId].hiddenTabUrls = [];
|
identitiesState[message.userContextId].hiddenTabUrls = [];
|
||||||
break;
|
break;
|
||||||
case 'removeTabs':
|
case 'removeTabs':
|
||||||
sendReply(removeTabs(message.tabIds));
|
sendReply(removeTabs(message.tabIds));
|
||||||
identitiesState[message.cookieStoreId].hiddenTabUrls = [];
|
identitiesState[message.userContextId].hiddenTabUrls = [];
|
||||||
break;
|
break;
|
||||||
case 'getTab':
|
case 'getTab':
|
||||||
sendReply(contextualIdentities.get(message.arguments));
|
sendReply(contextualIdentities.get(message.arguments));
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
const CONTAINER_HIDE_SRC = '/img/container-hide.svg';
|
const CONTAINER_HIDE_SRC = '/img/container-hide.svg';
|
||||||
const CONTAINER_UNHIDE_SRC = '/img/container-unhide.svg';
|
const CONTAINER_UNHIDE_SRC = '/img/container-unhide.svg';
|
||||||
|
|
||||||
function hideContainerTabs(containerId) {
|
function hideContainerTabs(userContextId) {
|
||||||
const tabIdsToRemove = [];
|
const tabIdsToRemove = [];
|
||||||
const tabUrlsToSave = [];
|
const tabUrlsToSave = [];
|
||||||
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
const hideorshowIcon = document.querySelector(`#uci-${userContextId}-hideorshow-icon`);
|
||||||
|
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: 'queryTabs',
|
method: 'queryTabs',
|
||||||
cookieStoreId: containerId
|
userContextId: userContextId
|
||||||
}).then(tabs=> {
|
}).then(tabs=> {
|
||||||
tabs.forEach(tab=> {
|
tabs.forEach(tab=> {
|
||||||
tabIdsToRemove.push(tab.id);
|
tabIdsToRemove.push(tab.id);
|
||||||
|
@ -17,7 +17,7 @@ function hideContainerTabs(containerId) {
|
||||||
});
|
});
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: 'hideTab',
|
method: 'hideTab',
|
||||||
cookieStoreId: containerId,
|
userContextId: userContextId,
|
||||||
tabUrlsToSave: tabUrlsToSave
|
tabUrlsToSave: tabUrlsToSave
|
||||||
}).then(()=> {
|
}).then(()=> {
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
|
@ -29,17 +29,17 @@ function hideContainerTabs(containerId) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function showContainerTabs(containerId) {
|
function showContainerTabs(userContextId) {
|
||||||
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
const hideorshowIcon = document.querySelector(`#uci-${userContextId}-hideorshow-icon`);
|
||||||
|
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: 'showTab',
|
method: 'showTab',
|
||||||
cookieStoreId: containerId
|
userContextId: userContextId
|
||||||
}).then(hiddenTabUrls=> {
|
}).then(hiddenTabUrls=> {
|
||||||
hiddenTabUrls.forEach(url=> {
|
hiddenTabUrls.forEach(url=> {
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: 'openTab',
|
method: 'openTab',
|
||||||
cookieStoreId: containerId,
|
userContextId: userContextId,
|
||||||
url: url
|
url: url
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -84,7 +84,7 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
hideOrShowIconSrc = CONTAINER_UNHIDE_SRC;
|
hideOrShowIconSrc = CONTAINER_UNHIDE_SRC;
|
||||||
}
|
}
|
||||||
const identityRow = `
|
const identityRow = `
|
||||||
<tr data-identity-cookie-store-id="${identity.cookieStoreId}" >
|
<tr data-identity-cookie-store-id="${identity.userContextId}" >
|
||||||
<td>
|
<td>
|
||||||
<div class="userContext-icon"
|
<div class="userContext-icon"
|
||||||
data-identity-icon="${identity.icon}"
|
data-identity-icon="${identity.icon}"
|
||||||
|
@ -101,8 +101,8 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
<td class="hideorshow" >
|
<td class="hideorshow" >
|
||||||
<img
|
<img
|
||||||
title="Hide or show ${identity.name} container tabs"
|
title="Hide or show ${identity.name} container tabs"
|
||||||
data-identity-cookie-store-id="${identity.cookieStoreId}"
|
data-identity-cookie-store-id="${identity.userContextId}"
|
||||||
id="${identity.cookieStoreId}-hideorshow-icon"
|
id="uci-${identity.userContextId}-hideorshow-icon"
|
||||||
class="icon hideorshow-icon"
|
class="icon hideorshow-icon"
|
||||||
src="${hideOrShowIconSrc}"
|
src="${hideOrShowIconSrc}"
|
||||||
/>
|
/>
|
||||||
|
@ -117,18 +117,18 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
|
|
||||||
rows.forEach(row=> {
|
rows.forEach(row=> {
|
||||||
row.addEventListener('click', e=> {
|
row.addEventListener('click', e=> {
|
||||||
const containerId = e.target.parentElement.parentElement.dataset.identityCookieStoreId;
|
const userContextId = e.target.parentElement.parentElement.dataset.identityCookieStoreId;
|
||||||
|
|
||||||
if (e.target.matches('.hideorshow-icon')) {
|
if (e.target.matches('.hideorshow-icon')) {
|
||||||
browser.runtime.sendMessage({method: 'getIdentitiesState'}).then(identitiesState=> {
|
browser.runtime.sendMessage({method: 'getIdentitiesState'}).then(identitiesState=> {
|
||||||
if (identitiesState[containerId].hiddenTabUrls.length) {
|
if (identitiesState[userContextId].hiddenTabUrls.length) {
|
||||||
showContainerTabs(containerId);
|
showContainerTabs(userContextId);
|
||||||
} else {
|
} else {
|
||||||
hideContainerTabs(containerId);
|
hideContainerTabs(userContextId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (e.target.matches('.newtab-icon')) {
|
} else if (e.target.matches('.newtab-icon')) {
|
||||||
browser.runtime.sendMessage({method: 'openTab', cookieStoreId: containerId});
|
browser.runtime.sendMessage({method: 'openTab', userContextId: userContextId});
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -152,14 +152,14 @@ function moveTabs(sortedTabsArray) {
|
||||||
|
|
||||||
document.querySelector('#sort-containers-link').addEventListener('click', ()=> {
|
document.querySelector('#sort-containers-link').addEventListener('click', ()=> {
|
||||||
browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
identities.unshift({cookieStoreId: 'firefox-default'});
|
identities.unshift({userContextId: 0});
|
||||||
|
|
||||||
browser.runtime.sendMessage({method: 'queryTabs'}).then(tabsArray=> {
|
browser.runtime.sendMessage({method: 'queryTabs'}).then(tabsArray=> {
|
||||||
const sortedTabsArray = [];
|
const sortedTabsArray = [];
|
||||||
|
|
||||||
identities.forEach(identity=> {
|
identities.forEach(identity=> {
|
||||||
tabsArray.forEach(tab=> {
|
tabsArray.forEach(tab=> {
|
||||||
if (tab.cookieStoreId === identity.cookieStoreId) {
|
if (tab.userContextId === identity.userContextId) {
|
||||||
sortedTabsArray.push(tab.id);
|
sortedTabsArray.push(tab.id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue