diff --git a/src/js/background/assignManager.js b/src/js/background/assignManager.js index 6d34cfa..1d48609 100644 --- a/src/js/background/assignManager.js +++ b/src/js/background/assignManager.js @@ -121,8 +121,8 @@ const assignManager = { if(requestInfo.tabId === -1) return {type: "direct"}; - var tab = await browser.tabs.get(requestInfo.tabId); - var proxy = await window.proxifiedContainers.retrieveFromBackground(tab.cookieStoreId); + const tab = await browser.tabs.get(requestInfo.tabId); + const proxy = await window.proxifiedContainers.retrieveFromBackground(tab.cookieStoreId); return proxy; }, @@ -235,7 +235,6 @@ const assignManager = { }); // Before anything happens we decide if the request should be proxified - browser.extension.getBackgroundPage().console.log('[SAMUEL CODE] Adding proxy.onRequest listener'); browser.proxy.onRequest.addListener(this.handleProxifiedRequest, {urls: [""]}); // Before a request is handled by the browser we decide if we should route through a different container diff --git a/src/js/popup.js b/src/js/popup.js index 8507cce..2d63395 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -1069,26 +1069,32 @@ Logic.registerPanel(P_CONTAINER_EDIT, { iconInput.checked = iconInput.value === identity.icon; }); - var edit_proxy_dom = function(result) { - if(result.type == "http") - document.querySelector('#edit-container-panel-proxy').value = result.host.toString() + ":" + result.port.toString(); - else if(result.type == "direct") - 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(); + else if(result.type === "direct") + document.querySelector("#edit-container-panel-proxy").value = ""; + }; + + window.proxifiedContainers.retrieve(identity.cookieStoreId).then((result) => { edit_proxy_dom(result.proxy); }, (error) => { - if(error.error == "uninitialized" || error.error == "doesnotexist") { - window.proxifiedContainers.set(identity.cookieStoreId, DEFAULT_PROXY, error.error == "uninitialized").then((result) => { + if(error.error === "uninitialized" || error.error === "doesnotexist") { + window.proxifiedContainers.set(identity.cookieStoreId, DEFAULT_PROXY, error.error === "uninitialized").then((result) => { edit_proxy_dom(result.proxy); }, (error) => { - browser.extension.getBackgroundPage().console.log(error); + window.proxifiedContainers.report_proxy_error(error); + }).catch((error) => { + window.proxifiedContainers.report_proxy_error(error); }); } else { - browser.extension.getBackgroundPage().console.log(error); + window.proxifiedContainers.report_proxy_error(error); } + }).catch((error) => { + window.proxifiedContainers.report_proxy_error(error); }); return Promise.resolve(null); diff --git a/src/js/proxified-containers.js b/src/js/proxified-containers.js index e56589e..0d2fbe9 100644 --- a/src/js/proxified-containers.js +++ b/src/js/proxified-containers.js @@ -6,90 +6,111 @@ window.proxifiedContainers = { return new Promise((resolve, reject) => { window.proxifiedContainers.retrieve(cookieStoreId).then((success) => { resolve(success.proxy); - }, (error) => { - resolve({type: "direct"}); + }, function() { + resolve({ + type: "direct" + }); + }).catch((error) => { + reject(error); }); }); }, + report_proxy_error: function(error) { + //Currently I print to console but this is inefficient + browser.extension.getBackgroundPage().console.log("proxifiedContainers error occured: " + JSON.Stringify(error)); + }, + //Resolves to a proxy object which can be used in the return of the listener required for browser.proxy.onRequest.addListener retrieve: function(cookieStoreId = null) { return new Promise((resolve, reject) => { browser.storage.local.get("proxifiedContainersKey").then((results) => { - //Steps to test: - //1. Is result empty? If so we must inform the caller to intialize proxifiedContainersStore with some initial info. - //2. Is cookieStoreId null? This means the caller probably wants everything currently in the proxifiedContainersStore object store - //3. If there doesn't exist an entry for the associated cookieStoreId, inform the caller of this - //4. Normal operation - if the cookieStoreId exists in the map, we can simply resolve with the correct proxy value + //Steps to test: + //1. Is result empty? If so we must inform the caller to intialize proxifiedContainersStore with some initial info. + //2. Is cookieStoreId null? This means the caller probably wants everything currently in the proxifiedContainersStore object store + //3. If there doesn't exist an entry for the associated cookieStoreId, inform the caller of this + //4. Normal operation - if the cookieStoreId exists in the map, we can simply resolve with the correct proxy value - var results_array = results["proxifiedContainersKey"]; + const results_array = results["proxifiedContainersKey"]; - if (Object.getOwnPropertyNames(results).length == 0) { - reject({error: "uninitialized", message: ""}); + if (Object.getOwnPropertyNames(results).length === 0) { + reject({ + error: "uninitialized", + message: "" + }); + } else if (cookieStoreId === null) { + resolve(results_array); + } else { + const val = results_array.find(o => o.cookieStoreId === cookieStoreId); + if (val === null) { + reject({ + error: "doesnotexist", + message: "" + }); + } else { + resolve(val); } - - else if(cookieStoreId == null) { - resolve(results_array); - } - - - else { - var val = results_array.find(o => o.cookieStoreId === cookieStoreId); - if(val == null) { - reject({error: "doesnotexist", message: ""}); - } - else { - resolve(val); - } - } - - }, (error) => { - reject({error: "internal", message: error}); } - ); + + }, (error) => { + reject({ + error: "internal", + message: error + }); + }).catch((error) => { + window.proxifiedContainers.report_proxy_error(error); + }); }); }, set: function(cookieStoreId, proxy, initialize = false) { return new Promise((resolve, reject) => { - if(initialize === true) { - var proxifiedContainersStore = []; - browser.storage.local.set({proxifiedContainersKey: proxifiedContainersStore}); + if (initialize === true) { + const proxifiedContainersStore = []; + browser.storage.local.set({ + proxifiedContainersKey: proxifiedContainersStore + }); } //Assumes proxy is a properly formatted object window.proxifiedContainers.retrieve().then((proxifiedContainersStore) => { - var index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId); - if(index === -1) - { - proxifiedContainersStore.push({cookieStoreId: cookieStoreId, proxy: proxy}); + let index = proxifiedContainersStore.findIndex(i => i.cookieStoreId === cookieStoreId); + if (index === -1) { + proxifiedContainersStore.push({ + cookieStoreId: cookieStoreId, + proxy: proxy + }); index = proxifiedContainersStore.length - 1; - } - else - { - proxifiedContainersStore[index] = {cookieStoreId: cookieStoreId, proxy: proxy}; + } else { + proxifiedContainersStore[index] = { + cookieStoreId: cookieStoreId, + proxy: proxy + }; } - browser.storage.local.set({proxifiedContainersKey: proxifiedContainersStore}); + browser.storage.local.set({ + proxifiedContainersKey: proxifiedContainersStore + }); resolve(proxifiedContainersStore[index]); }, (errorObj) => { reject(errorObj); + }).catch((error) => { + throw error; }); }); }, parseProxy: function(proxy_str) { - var regexp = /(\b(\w+):(\w+)@)?(((?:\d{1,3}\.){3}\d{1,3}\b)|(\b(\w+)(\.(\w+))+))(:(\d+))?/; - if(regexp.test(proxy_str) !== true) + const regexp = /(\b(\w+):(\w+)@)?(((?:\d{1,3}\.){3}\d{1,3}\b)|(\b(\w+)(\.(\w+))+))(:(\d+))?/; + if (regexp.test(proxy_str) !== true) return false; - else - { - var matches = regexp.exec(proxy_str); + else { + const matches = regexp.exec(proxy_str); - var result = { + const result = { type: "http", host: matches[4], - port: parseInt(matches[11]) || 8080, + port: parseInt(matches[11], 10) || 8080, username: matches[2] || "", password: matches[3] || "" };