restore show/hide functionality in WebExtension
This commit is contained in:
parent
3e6b6f14b2
commit
dcdd2a3c72
2 changed files with 26 additions and 65 deletions
50
index.js
50
index.js
|
@ -2,7 +2,6 @@
|
||||||
const {ContextualIdentityService} = require('resource://gre/modules/ContextualIdentityService.jsm');
|
const {ContextualIdentityService} = require('resource://gre/modules/ContextualIdentityService.jsm');
|
||||||
|
|
||||||
const tabs = require('sdk/tabs');
|
const tabs = require('sdk/tabs');
|
||||||
const windows = require('sdk/windows');
|
|
||||||
const webExtension = require('sdk/webextension');
|
const webExtension = require('sdk/webextension');
|
||||||
|
|
||||||
const CONTAINER_STORE = 'firefox-container-';
|
const CONTAINER_STORE = 'firefox-container-';
|
||||||
|
@ -22,49 +21,6 @@ function convert(identity) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function query(queryInfo) {
|
|
||||||
|
|
||||||
function matches(window, tab) {
|
|
||||||
if (queryInfo.cookieStoreId !== null &&
|
|
||||||
tab.cookieStoreId !== queryInfo.cookieStoreId) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = [];
|
|
||||||
|
|
||||||
for (const window of windows.browserWindows) {
|
|
||||||
for (const tab of tabs) {
|
|
||||||
if (matches(window, tab)) {
|
|
||||||
result.push(tab);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
function hideContainerTabs(cookieStoreId) {
|
|
||||||
query({cookieStoreId}).then(containerTabs=> {
|
|
||||||
containerTabs.forEach(tab=> {
|
|
||||||
gBrowser.hideTab(tab);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(e=> {
|
|
||||||
throw e;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function showContainterTabs(cookieStoreId) {
|
|
||||||
query({cookieStoreId}).then(containerTabs=> {
|
|
||||||
containerTabs.forEach(tab=> {
|
|
||||||
gBrowser.showTab(tab);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function isContainerCookieStoreId(storeId) {
|
function isContainerCookieStoreId(storeId) {
|
||||||
return storeId !== null && storeId.startsWith(CONTAINER_STORE);
|
return storeId !== null && storeId.startsWith(CONTAINER_STORE);
|
||||||
}
|
}
|
||||||
|
@ -96,7 +52,6 @@ function getContainer(cookieStoreId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryContainers(details) {
|
function queryContainers(details) {
|
||||||
console.log('queryContainers, details: ', details);
|
|
||||||
const identities = [];
|
const identities = [];
|
||||||
|
|
||||||
ContextualIdentityService.getIdentities().forEach(identity=> {
|
ContextualIdentityService.getIdentities().forEach(identity=> {
|
||||||
|
@ -112,7 +67,6 @@ function queryContainers(details) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function createContainer(details) {
|
function createContainer(details) {
|
||||||
console.log('createContainer, details: ', details);
|
|
||||||
const identity = ContextualIdentityService.create(details.name,
|
const identity = ContextualIdentityService.create(details.name,
|
||||||
details.icon,
|
details.icon,
|
||||||
details.color);
|
details.color);
|
||||||
|
@ -121,7 +75,6 @@ function createContainer(details) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateContainer(cookieStoreId, details) {
|
function updateContainer(cookieStoreId, details) {
|
||||||
console.log(`updateContainer, cookieStoreId: ${cookieStoreId}, details: ${details}`);
|
|
||||||
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
||||||
|
|
||||||
if (!containerId) {
|
if (!containerId) {
|
||||||
|
@ -156,7 +109,6 @@ function updateContainer(cookieStoreId, details) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeContainer(cookieStoreId) {
|
function removeContainer(cookieStoreId) {
|
||||||
console.log(`removeContainer, cookieStoreId: ${cookieStoreId}`);
|
|
||||||
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
const containerId = getContainerForCookieStoreId(cookieStoreId);
|
||||||
|
|
||||||
if (!containerId) {
|
if (!containerId) {
|
||||||
|
@ -180,8 +132,6 @@ function removeContainer(cookieStoreId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const contextualIdentities = {
|
const contextualIdentities = {
|
||||||
hide: hideContainerTabs,
|
|
||||||
show: showContainterTabs,
|
|
||||||
get: getContainer,
|
get: getContainer,
|
||||||
query: queryContainers,
|
query: queryContainers,
|
||||||
create: createContainer,
|
create: createContainer,
|
||||||
|
|
|
@ -1,23 +1,36 @@
|
||||||
/* global browser, window, document */
|
/* global browser, window, document */
|
||||||
const identityState = {
|
const identitiesState = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function hideContainer(containerId) {
|
function hideContainerTabs(containerId) {
|
||||||
|
const tabIdsToRemove = [];
|
||||||
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
||||||
|
|
||||||
hideorshowIcon.src = '/img/container-unhide.svg';
|
browser.tabs.query({cookieStoreId: containerId}).then(tabs=> {
|
||||||
browser.runtime.sendMessage({method: 'hide', arguments: containerId});
|
tabs.forEach(tab=> {
|
||||||
|
tabIdsToRemove.push(tab.id);
|
||||||
|
identitiesState[containerId].hiddenTabUrls.push(tab.url);
|
||||||
|
});
|
||||||
|
browser.tabs.remove(tabIdsToRemove);
|
||||||
|
hideorshowIcon.src = '/img/container-unhide.svg';
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function showContainer(containerId) {
|
function showContainerTabs(containerId) {
|
||||||
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
const hideorshowIcon = document.querySelector(`#${containerId}-hideorshow-icon`);
|
||||||
|
|
||||||
|
identitiesState[containerId].hiddenTabUrls.forEach(url=> {
|
||||||
|
// Have to use SDK to open tabs in case they are about:* pages
|
||||||
|
browser.tabs.create({
|
||||||
|
url: url,
|
||||||
|
cookieStoreId: containerId
|
||||||
|
});
|
||||||
|
});
|
||||||
|
identitiesState[containerId].hiddenTabUrls = [];
|
||||||
hideorshowIcon.src = '/img/container-hide.svg';
|
hideorshowIcon.src = '/img/container-hide.svg';
|
||||||
browser.runtime.sendMessage({method: 'show', arguments: containerId});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
||||||
console.log('query identities: ', identities);
|
|
||||||
const identitiesListElement = document.querySelector('.identities-list');
|
const identitiesListElement = document.querySelector('.identities-list');
|
||||||
|
|
||||||
identities.forEach(identity=> {
|
identities.forEach(identity=> {
|
||||||
|
@ -41,6 +54,9 @@ browser.runtime.sendMessage({method: 'query'}).then(identities=> {
|
||||||
|
|
||||||
identitiesListElement.innerHTML += identityRow;
|
identitiesListElement.innerHTML += identityRow;
|
||||||
|
|
||||||
|
if (!(identity in identitiesState)) {
|
||||||
|
identitiesState[identity.cookieStoreId] = {hiddenTabUrls: []};
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const rows = identitiesListElement.querySelectorAll('tr');
|
const rows = identitiesListElement.querySelectorAll('tr');
|
||||||
|
@ -50,15 +66,10 @@ 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 (!(containerId in identityState)) {
|
if (identitiesState[containerId].hiddenTabUrls.length) {
|
||||||
identityState[containerId] = true;
|
showContainerTabs(containerId);
|
||||||
}
|
|
||||||
if (identityState[containerId]) {
|
|
||||||
hideContainer(containerId);
|
|
||||||
identityState[containerId] = false;
|
|
||||||
} else {
|
} else {
|
||||||
showContainer(containerId);
|
hideContainerTabs(containerId);
|
||||||
identityState[containerId] = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue