diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index a27d199..a2d2120 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -46,6 +46,11 @@ const backgroundLogic = { window.proxifiedContainers.set(this.cookieStoreId(options.userContextId), options.proxy); } else { donePromise = browser.contextualIdentities.create(options.params); + + //We cannot yet access the new cookieStoreId via this.cookieStoreId(...), so we take this from the resolved promise + donePromise.then((identity) => { + window.proxifiedContainers.set(identity.cookieStoreId, options.proxy); + }); } await donePromise; browser.runtime.sendMessage({ diff --git a/src/js/popup.js b/src/js/popup.js index 6e8fa1e..38e80da 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -1069,6 +1069,9 @@ Logic.registerPanel(P_CONTAINER_EDIT, { iconInput.checked = iconInput.value === identity.icon; }); + //Clear the proxy field before doing the retrieval requests below + document.querySelector("#edit-container-panel-proxy").value = ""; + const edit_proxy_dom = function(result) { if(result.type === "http") document.querySelector("#edit-container-panel-proxy").value = result.host.toString() + ":" + result.port.toString(); @@ -1083,18 +1086,18 @@ Logic.registerPanel(P_CONTAINER_EDIT, { }, (error) => { if(error.error === "uninitialized" || error.error === "doesnotexist") { window.proxifiedContainers.set(identity.cookieStoreId, DEFAULT_PROXY, error.error === "uninitialized").then((result) => { - edit_proxy_dom(result.proxy); + edit_proxy_dom(result); }, (error) => { - window.proxifiedContainers.report_proxy_error(error, "popup.js: occurence 1"); + window.proxifiedContainers.report_proxy_error(error, "popup.js: error 1"); }).catch((error) => { - window.proxifiedContainers.report_proxy_error(error, "popup.js: occurence 2"); + window.proxifiedContainers.report_proxy_error(error, "popup.js: error 2"); }); } else { - window.proxifiedContainers.report_proxy_error(error, "popup.js: occurence 3"); + window.proxifiedContainers.report_proxy_error(error, "popup.js: error 3"); } }).catch((err) => { - window.proxifiedContainers.report_proxy_error(err, "popup.js: occurence 4"); + window.proxifiedContainers.report_proxy_error(err, "popup.js: error 4"); }); return Promise.resolve(null); diff --git a/src/js/proxified-containers.js b/src/js/proxified-containers.js index a6bd6b6..4ab2751 100644 --- a/src/js/proxified-containers.js +++ b/src/js/proxified-containers.js @@ -1,3 +1,20 @@ +//Below lets us print errors, huge thanks to Jonothan @ https://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify +if (!('toJSON' in Error.prototype)) +Object.defineProperty(Error.prototype, 'toJSON', { + value: function () { + var alt = {}; + + Object.getOwnPropertyNames(this).forEach(function (key) { + alt[key] = this[key]; + }, this); + + return alt; + }, + configurable: true, + writable: true +}); + + //This object allows other scripts to access the list mapping containers to their proxies window.proxifiedContainers = { @@ -60,7 +77,7 @@ window.proxifiedContainers = { message: error }); }).catch((error) => { - window.proxifiedContainers.report_proxy_error(error, "5"); + window.proxifiedContainers.report_proxy_error(error, "proxified-containers.js: error 1"); }); }); }, @@ -68,15 +85,22 @@ window.proxifiedContainers = { return new Promise((resolve, reject) => { if (initialize === true) { const proxifiedContainersStore = []; + proxifiedContainersStore.push({ + cookieStoreId: cookieStoreId, + proxy: proxy + }); + browser.storage.local.set({ proxifiedContainersKey: proxifiedContainersStore }); + + resolve(proxy); } //Assumes proxy is a properly formatted object window.proxifiedContainers.retrieve().then((proxifiedContainersStore) => { - let index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId); + if (index === -1) { proxifiedContainersStore.push({ cookieStoreId: cookieStoreId, @@ -93,6 +117,7 @@ window.proxifiedContainers = { browser.storage.local.set({ proxifiedContainersKey: proxifiedContainersStore }); + resolve(proxifiedContainersStore[index]); }, (errorObj) => { reject(errorObj); @@ -100,7 +125,7 @@ window.proxifiedContainers = { throw error; }); }); - }, + }, parseProxy: function(proxy_str) { const regexp = /(\b(\w+):(\w+)@)?(((?:\d{1,3}\.){3}\d{1,3}\b)|(\b(\w+)(\.(\w+))+))(:(\d+))?/; if (regexp.test(proxy_str) !== true)