Merge f29f9d00eb
into 679d8b9fbb
This commit is contained in:
commit
49aeeeb417
2 changed files with 114 additions and 143 deletions
193
index.js
193
index.js
|
@ -1,8 +1,12 @@
|
||||||
/* global require */
|
/* global require */
|
||||||
const {ContextualIdentityService} = require('resource://gre/modules/ContextualIdentityService.jsm');
|
const {ContextualIdentityService} = require('resource://gre/modules/ContextualIdentityService.jsm');
|
||||||
|
const { Cc, Ci, Cu, Cr } = require('chrome');
|
||||||
|
|
||||||
|
Cu.import("resource://gre/modules/Services.jsm");
|
||||||
|
|
||||||
const tabs = require('sdk/tabs');
|
const tabs = require('sdk/tabs');
|
||||||
const webExtension = require('sdk/webextension');
|
const webExtension = require('sdk/webextension');
|
||||||
|
const { viewFor } = require("sdk/view/core");
|
||||||
|
|
||||||
/* Let's start enabling Containers */
|
/* Let's start enabling Containers */
|
||||||
var prefs = [
|
var prefs = [
|
||||||
|
@ -17,63 +21,26 @@ 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) {
|
|
||||||
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) {
|
|
||||||
return storeId !== null && storeId.startsWith(CONTAINER_STORE);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
const identity = ContextualIdentityService.getIdentityFromId(containerId);
|
|
||||||
|
|
||||||
return Promise.resolve(convert(identity));
|
|
||||||
}
|
|
||||||
|
|
||||||
function queryContainers(details) {
|
function queryContainers(details) {
|
||||||
const identities = [];
|
const identities = [];
|
||||||
|
|
||||||
|
@ -86,64 +53,20 @@ 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: []};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.resolve(identities);
|
return Promise.resolve(identities);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createContainer(details) {
|
function removeContainer(userContextId) {
|
||||||
const identity = ContextualIdentityService.create(details.name,
|
if (!userContextId) {
|
||||||
details.icon,
|
|
||||||
details.color);
|
|
||||||
|
|
||||||
return Promise.resolve(convert(identity));
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateContainer(cookieStoreId, details) {
|
|
||||||
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
|
||||||
|
|
||||||
if (!containerId) {
|
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
const identity = ContextualIdentityService.getIdentityFromId(containerId);
|
const identity = ContextualIdentityService.getIdentityFromId(userContextId);
|
||||||
|
|
||||||
if (!identity) {
|
|
||||||
return Promise.resolve(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (details.name !== null) {
|
|
||||||
identity.name = details.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (details.color !== null) {
|
|
||||||
identity.color = details.color;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (details.icon !== null) {
|
|
||||||
identity.icon = details.icon;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ContextualIdentityService.update(identity.userContextId,
|
|
||||||
identity.name, identity.icon,
|
|
||||||
identity.color)) {
|
|
||||||
return Promise.resolve(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve(convert(identity));
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeContainer(cookieStoreId) {
|
|
||||||
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
|
||||||
|
|
||||||
if (!containerId) {
|
|
||||||
return Promise.resolve(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
const identity = ContextualIdentityService.getIdentityFromId(containerId);
|
|
||||||
|
|
||||||
if (!identity) {
|
if (!identity) {
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
|
@ -159,44 +82,82 @@ function removeContainer(cookieStoreId) {
|
||||||
return Promise.resolve(convertedIdentity);
|
return Promise.resolve(convertedIdentity);
|
||||||
}
|
}
|
||||||
|
|
||||||
const contextualIdentities = {
|
function openTab(args) {
|
||||||
get: getContainer,
|
let browserWin = Services.wm.getMostRecentWindow('navigator:browser');
|
||||||
query: queryContainers,
|
|
||||||
create: createContainer,
|
// This should not really happen.
|
||||||
update: updateContainer,
|
if (!browserWin || !browserWin.gBrowser) {
|
||||||
remove: removeContainer
|
return Promise.resolve(false);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
let userContextId = 0;
|
||||||
|
if ('userContextId' in args) {
|
||||||
|
userContextId = args.userContextId;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tab = browserWin.gBrowser.addTab(args.url || null,
|
||||||
|
{ userContextId: userContextId })
|
||||||
|
browserWin.gBrowser.selectedTab = tab;
|
||||||
|
return Promise.resolve(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryTabs(args) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let tabList = [];
|
||||||
|
|
||||||
|
for (let tab of tabs) {
|
||||||
|
let xulTab = viewFor(tab);
|
||||||
|
let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10);
|
||||||
|
|
||||||
|
if ("userContextId" in args && args.userContextId != userContextId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tabList.push({
|
||||||
|
id: tab.id,
|
||||||
|
url: tab.url,
|
||||||
|
userContextId: userContextId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(tabList);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeTabs(ids) {
|
||||||
|
for (let tab of tabs) {
|
||||||
|
if (ids.indexOf(tab.id) != -1) {
|
||||||
|
tab.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(null);
|
||||||
|
}
|
||||||
|
|
||||||
function handleWebExtensionMessage(message, sender, sendReply) {
|
function handleWebExtensionMessage(message, sender, sendReply) {
|
||||||
switch (message.method) {
|
switch (message.method) {
|
||||||
case 'query':
|
case 'queryIdentities':
|
||||||
sendReply(contextualIdentities.query(message.arguments));
|
sendReply(queryContainers(message.arguments));
|
||||||
break;
|
break;
|
||||||
case 'hide':
|
case 'queryTabs':
|
||||||
identitiesState[message.cookieStoreId].hiddenTabUrls = message.tabUrlsToSave;
|
sendReply(queryTabs(message));
|
||||||
break;
|
break;
|
||||||
case 'show':
|
case 'hideTabs':
|
||||||
sendReply(identitiesState[message.cookieStoreId].hiddenTabUrls);
|
identitiesState[message.userContextId].hiddenTabUrls = message.tabUrlsToSave;
|
||||||
identitiesState[message.cookieStoreId].hiddenTabUrls = [];
|
|
||||||
break;
|
break;
|
||||||
case 'get':
|
case 'showTabs':
|
||||||
sendReply(contextualIdentities.get(message.arguments));
|
sendReply(identitiesState[message.userContextId].hiddenTabUrls);
|
||||||
|
identitiesState[message.userContextId].hiddenTabUrls = [];
|
||||||
break;
|
break;
|
||||||
case 'create':
|
case 'removeTabs':
|
||||||
sendReply(contextualIdentities.create(message.arguments));
|
sendReply(removeTabs(message.tabIds));
|
||||||
break;
|
identitiesState[message.userContextId].hiddenTabUrls = [];
|
||||||
case 'update':
|
|
||||||
sendReply(contextualIdentities.update(message.arguments));
|
|
||||||
break;
|
|
||||||
case 'remove':
|
|
||||||
sendReply(contextualIdentities.remove(message.arguments));
|
|
||||||
break;
|
break;
|
||||||
case 'getIdentitiesState':
|
case 'getIdentitiesState':
|
||||||
sendReply(identitiesState);
|
sendReply(identitiesState);
|
||||||
break;
|
break;
|
||||||
case 'open-containers-preferences':
|
case 'openTab':
|
||||||
tabs.open('about:preferences#containers');
|
sendReply(openTab(message));
|
||||||
sendReply({content: 'opened'});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,38 +2,45 @@
|
||||||
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.tabs.query({cookieStoreId: containerId}).then(tabs=> {
|
browser.runtime.sendMessage({
|
||||||
|
method: 'queryTabs',
|
||||||
|
userContextId: userContextId
|
||||||
|
}).then(tabs=> {
|
||||||
tabs.forEach(tab=> {
|
tabs.forEach(tab=> {
|
||||||
tabIdsToRemove.push(tab.id);
|
tabIdsToRemove.push(tab.id);
|
||||||
tabUrlsToSave.push(tab.url);
|
tabUrlsToSave.push(tab.url);
|
||||||
});
|
});
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: 'hide',
|
method: 'hideTabs',
|
||||||
cookieStoreId: containerId,
|
userContextId: userContextId,
|
||||||
tabUrlsToSave: tabUrlsToSave
|
tabUrlsToSave: tabUrlsToSave
|
||||||
}).then(()=> {
|
}).then(()=> {
|
||||||
browser.tabs.remove(tabIdsToRemove);
|
browser.runtime.sendMessage({
|
||||||
|
method: 'removeTabs',
|
||||||
|
tabIds: tabIdsToRemove
|
||||||
|
});
|
||||||
hideorshowIcon.src = CONTAINER_UNHIDE_SRC;
|
hideorshowIcon.src = CONTAINER_UNHIDE_SRC;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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: 'show',
|
method: 'showTabs',
|
||||||
cookieStoreId: containerId
|
userContextId: userContextId
|
||||||
}).then(hiddenTabUrls=> {
|
}).then(hiddenTabUrls=> {
|
||||||
hiddenTabUrls.forEach(url=> {
|
hiddenTabUrls.forEach(url=> {
|
||||||
browser.tabs.create({
|
browser.runtime.sendMessage({
|
||||||
url: url,
|
method: 'openTab',
|
||||||
cookieStoreId: containerId
|
userContextId: userContextId,
|
||||||
|
url: url
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -67,7 +74,7 @@ document.querySelector('#onboarding-done-button').addEventListener('click', ()=>
|
||||||
document.querySelector('#container-panel').classList.remove('hide');
|
document.querySelector('#container-panel').classList.remove('hide');
|
||||||
});
|
});
|
||||||
|
|
||||||
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
const identitiesListElement = document.querySelector('.identities-list');
|
const identitiesListElement = document.querySelector('.identities-list');
|
||||||
|
|
||||||
identities.forEach(identity=> {
|
identities.forEach(identity=> {
|
||||||
|
@ -77,7 +84,7 @@ browser.runtime.sendMessage({method: 'query'}).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}"
|
||||||
|
@ -94,8 +101,8 @@ browser.runtime.sendMessage({method: 'query'}).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}"
|
||||||
/>
|
/>
|
||||||
|
@ -110,18 +117,18 @@ browser.runtime.sendMessage({method: 'query'}).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.tabs.create({cookieStoreId: containerId});
|
browser.runtime.sendMessage({method: 'openTab', userContextId: userContextId});
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -129,7 +136,10 @@ browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
||||||
});
|
});
|
||||||
|
|
||||||
document.querySelector('#edit-containers-link').addEventListener('click', ()=> {
|
document.querySelector('#edit-containers-link').addEventListener('click', ()=> {
|
||||||
browser.runtime.sendMessage({method: 'open-containers-preferences'}).then(()=> {
|
browser.runtime.sendMessage({
|
||||||
|
method: 'openTab',
|
||||||
|
url: "about:preferences#containers"
|
||||||
|
}).then(()=> {
|
||||||
window.close();
|
window.close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -144,15 +154,15 @@ function moveTabs(sortedTabsArray) {
|
||||||
}
|
}
|
||||||
|
|
||||||
document.querySelector('#sort-containers-link').addEventListener('click', ()=> {
|
document.querySelector('#sort-containers-link').addEventListener('click', ()=> {
|
||||||
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
identities.unshift({cookieStoreId: 'firefox-default'});
|
identities.unshift({userContextId: 0});
|
||||||
|
|
||||||
browser.tabs.query({}).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