From 8ef5cbd81bcdfb191f11e734ed114543effd6822 Mon Sep 17 00:00:00 2001 From: Kendall Werts Date: Thu, 27 Feb 2020 08:42:27 -0600 Subject: [PATCH] Fixed Assignement Toast on page. Now opens in the newly opened tab (after it has been reopened in the correct container). It also does not provide a toast when removing sites from the edit assignments panel, becuase user will most likely not be on that site when editing the panel, so it has been throwing an error. Error should go away. --- src/js/background/assignManager.js | 25 ++++++++++++++--------- src/js/background/messageHandler.js | 22 ++++++++++++++++---- src/js/popup.js | 4 ++-- src/js/utils.js | 31 ++++++++++++++++------------- 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/js/background/assignManager.js b/src/js/background/assignManager.js index 9dd6e88d..c5af298f 100644 --- a/src/js/background/assignManager.js +++ b/src/js/background/assignManager.js @@ -500,16 +500,23 @@ window.assignManager = { userContextId, neverAsk: false }, exemptedTabIds); - actionName = "added"; + actionName = "assigned site to always open in this container"; } else { await this.storageArea.remove(pageUrl); - actionName = "removed"; + actionName = "removed from assigned sites list"; + } + + if (tabId) { + const tab = await browser.tabs.get(tabId); + setTimeout(function(){ + browser.tabs.sendMessage(tabId, { + text: `Successfully ${actionName}` + }); + }, 1000); + + + this.calculateContextMenu(tab); } - browser.tabs.sendMessage(tabId, { - text: `Successfully ${actionName} site to always open in this container` - }); - const tab = await browser.tabs.get(tabId); - this.calculateContextMenu(tab); }, async _getAssignment(tab) { @@ -595,7 +602,7 @@ window.assignManager = { // False represents assignment is not permitted // If the user has explicitly checked "Never Ask Again" on the warning page we will send them straight there if (neverAsk) { - browser.tabs.create({url, cookieStoreId, index, active, openerTabId}); + return browser.tabs.create({url, cookieStoreId, index, active, openerTabId}); } else { let confirmUrl = `${loadPage}?url=${this.encodeURLProperty(url)}&cookieStoreId=${cookieStoreId}`; let currentCookieStoreId; @@ -603,7 +610,7 @@ window.assignManager = { currentCookieStoreId = backgroundLogic.cookieStoreId(currentUserContextId); confirmUrl += `¤tCookieStoreId=${currentCookieStoreId}`; } - browser.tabs.create({ + return browser.tabs.create({ url: confirmUrl, cookieStoreId: currentCookieStoreId, openerTabId, diff --git a/src/js/background/messageHandler.js b/src/js/background/messageHandler.js index d7384ea7..d1bc33db 100644 --- a/src/js/background/messageHandler.js +++ b/src/js/background/messageHandler.js @@ -6,8 +6,9 @@ const messageHandler = { init() { // Handles messages from webextension code - browser.runtime.onMessage.addListener((m) => { + browser.runtime.onMessage.addListener(async (m) => { let response; + let tab; switch (m.method) { case "getShortcuts": @@ -43,9 +44,7 @@ const messageHandler = { case "setOrRemoveAssignment": // m.tabId is used for where to place the in content message // m.url is the assignment to be removed/added - response = browser.tabs.get(m.tabId).then((tab) => { - return assignManager._setOrRemoveAssignment(tab.id, m.url, m.userContextId, m.value); - }); + response = assignManager._setOrRemoveAssignment(m.tabId, m.url, m.userContextId, m.value); break; case "sortTabs": backgroundLogic.sortTabs(); @@ -90,6 +89,21 @@ const messageHandler = { true ); break; + case "assignAndReloadInContainer": + tab = await assignManager.reloadPageInContainer( + m.url, + m.currentUserContextId, + m.newUserContextId, + m.tabIndex, + m.active, + true + ); + // m.tabId is used for where to place the in content message + // m.url is the assignment to be removed/added + response = browser.tabs.get(tab.id).then((tab) => { + return assignManager._setOrRemoveAssignment(tab.id, m.url, m.newUserContextId, m.value); + }); + break; } console.log(m.method, "response", response); return response; diff --git a/src/js/popup.js b/src/js/popup.js index 6972073d..fcc91988 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -1180,8 +1180,8 @@ Logic.registerPanel(P_CONTAINER_ASSIGNMENTS, { Utils.addEnterHandler(deleteButton, async () => { const userContextId = Logic.currentUserContextId(); // Lets show the message to the current tab - const currentTab = await Utils.currentTab(); - Utils.setOrRemoveAssignment(currentTab.id, assumedUrl, userContextId, true); + // const currentTab = await Utils.currentTab(); + Utils.setOrRemoveAssignment(false, assumedUrl, userContextId, true); delete assignments[siteKey]; this.showAssignedContainers(assignments); }); diff --git a/src/js/utils.js b/src/js/utils.js index 161e3d09..68c821b2 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -90,8 +90,8 @@ const Utils = { }); }, - reloadInContainer(url, currentUserContextId, newUserContextId, tabIndex, active) { - return browser.runtime.sendMessage({ + async reloadInContainer(url, currentUserContextId, newUserContextId, tabIndex, active) { + return await browser.runtime.sendMessage({ method: "reloadInContainer", url, currentUserContextId, @@ -102,21 +102,24 @@ const Utils = { }, async alwaysOpenInContainer(identity) { - const currentTab = await this.currentTab(); + let currentTab = await this.currentTab(); const assignedUserContextId = this.userContextId(identity.cookieStoreId); - Utils.setOrRemoveAssignment( - currentTab.id, - currentTab.url, - assignedUserContextId, - false - ); if (currentTab.cookieStoreId !== identity.cookieStoreId) { - Utils.reloadInContainer( + return await browser.runtime.sendMessage({ + method: "assignAndReloadInContainer", + url: currentTab.url, + currentUserContextId: false, + newUserContextId: assignedUserContextId, + tabIndex: currentTab.index +1, + active:currentTab.active + }); + } else { + currentTab = await this.currentTab(); + Utils.setOrRemoveAssignment( + currentTab.id, currentTab.url, - false, - assignedUserContextId, - currentTab.index + 1, - currentTab.active + assignedUserContextId, + false ); } }