From d538509636d80df058da29e266ab23b9acf3d4e3 Mon Sep 17 00:00:00 2001 From: Amoghavarsha Kudaligi Date: Mon, 5 Feb 2024 11:49:33 +0530 Subject: [PATCH 1/5] Fix cannot set properties of newUpdates of undefined --- packages/extension/src/serviceWorker/index.ts | 189 +++++++++++------- .../src/store/synchnorousCookieStore.ts | 18 +- .../stateProviders/syncCookieStore/index.tsx | 23 ++- 3 files changed, 140 insertions(+), 90 deletions(-) diff --git a/packages/extension/src/serviceWorker/index.ts b/packages/extension/src/serviceWorker/index.ts index 62f0870f3..8156ac10b 100644 --- a/packages/extension/src/serviceWorker/index.ts +++ b/packages/extension/src/serviceWorker/index.ts @@ -34,6 +34,7 @@ import { fetchDictionary } from '../utils/fetchCookieDictionary'; import { ALLOWED_NUMBER_OF_TABS } from '../constants'; import SynchnorousCookieStore from '../store/synchnorousCookieStore'; import canProcessCookies from '../utils/canProcessCookies'; +import { getTab } from '../utils/getTab'; let cookieDB: CookieDatabase | null = null; let syncCookieStore: SynchnorousCookieStore | undefined; @@ -547,8 +548,7 @@ chrome.runtime.onMessage.addListener(async (request) => { !syncCookieStore?.tabs[request.payload.tabId] && tabMode === 'unlimited' ) { - const tabs = await chrome.tabs.query({}); - const currentTab = tabs.find((tab) => tab.id === request.payload.tabId); + const currentTab = await getTab(request.payload.tabId); syncCookieStore?.addTabData(request?.payload?.tabId); syncCookieStore?.updateUrl( @@ -642,66 +642,80 @@ chrome.storage.sync.onChanged.addListener( tabMode = changes.allowedNumberOfTabs.newValue; if (changes?.allowedNumberOfTabs?.newValue === 'single') { - const tabs = await chrome.tabs.query({}); - tabToRead = ''; - - chrome.runtime.sendMessage({ - type: 'ServiceWorker::DevTools::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); + try { + const tabs = await chrome.tabs.query({}); + tabToRead = ''; + + chrome.runtime.sendMessage({ + type: 'ServiceWorker::DevTools::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); - chrome.runtime.sendMessage({ - type: 'ServiceWorker::Popup::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); + chrome.runtime.sendMessage({ + type: 'ServiceWorker::Popup::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); - tabs.map((tab) => { - if (!tab?.id) { - return tab; - } + tabs.map((tab) => { + if (!tab?.id) { + return tab; + } - chrome.action.setBadgeText({ - tabId: tab?.id, - text: '', + chrome.action.setBadgeText({ + tabId: tab?.id, + text: '', + }); + + syncCookieStore?.removeTabData(tab.id); + + chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); + return tab; }); - syncCookieStore?.removeTabData(tab.id); - chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); - return tab; - }); + } catch (error) { + // eslint-disable-next-line no-console + console.warn(error); + } } else { - const tabs = await chrome.tabs.query({}); - chrome.runtime.sendMessage({ - type: 'ServiceWorker::Popup::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); - chrome.runtime.sendMessage({ - type: 'ServiceWorker::DevTools::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); + try { + const tabs = await chrome.tabs.query({}); + + chrome.runtime.sendMessage({ + type: 'ServiceWorker::Popup::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); - await Promise.all( - tabs.map(async (tab) => { - if (!tab?.id) { - return; - } - syncCookieStore?.addTabData(tab.id); - syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); - syncCookieStore?.updateDevToolsState(tab.id, true); - await chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); - }) - ); + chrome.runtime.sendMessage({ + type: 'ServiceWorker::DevTools::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); + + await Promise.all( + tabs.map(async (tab) => { + if (!tab?.id) { + return; + } + syncCookieStore?.addTabData(tab.id); + syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); + syncCookieStore?.updateDevToolsState(tab.id, true); + await chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); + }) + ); + } catch (error) { + // eslint-disable-next-line no-console + console.warn(error); + } } } ); @@ -733,27 +747,50 @@ chrome.storage.sync.onChanged.addListener( }); if (!changes?.isUsingCDP?.newValue) { - await Promise.all( - Object.keys(syncCookieStore?.tabsData ?? {}).map(async (key) => { - try { - await chrome.debugger.detach({ tabId: Number(key) }); - syncCookieStore?.removeCookieData(Number(key)); - syncCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key)); - } catch (error) { - // Fail silently - } finally { - await chrome.tabs.reload(Number(key), { bypassCache: true }); - } - }) - ); + try { + const tabs = await chrome.tabs.query({}); + + await Promise.all( + tabs.map(async (tab) => { + if (!tab.id) { + return; + } + + try { + await chrome.debugger.detach({ tabId: tab.id }); + syncCookieStore?.removeCookieData(tab.id); + syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); + } catch (error) { + // eslint-disable-next-line no-console + console.warn(error); + } finally { + await chrome.tabs.reload(tab.id, { bypassCache: true }); + } + }) + ); + } catch (error) { + // eslint-disable-next-line no-console + console.warn(error); + } } else { - await Promise.all( - Object.keys(syncCookieStore?.tabsData ?? {}).map(async (key) => { - syncCookieStore?.removeCookieData(Number(key)); - syncCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key)); - await chrome.tabs.reload(Number(key), { bypassCache: true }); - }) - ); + try { + const tabs = await chrome.tabs.query({}); + + await Promise.all( + tabs.map(async (tab) => { + if (!tab.id) { + return; + } + + syncCookieStore?.removeCookieData(tab.id); + syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); + await chrome.tabs.reload(tab.id, { bypassCache: true }); + }) + ); + } catch (error) { + // eslint-disable-next-line no-console + console.warn(error); + } } } ); diff --git a/packages/extension/src/store/synchnorousCookieStore.ts b/packages/extension/src/store/synchnorousCookieStore.ts index 4d57d2f28..4c1069a23 100644 --- a/packages/extension/src/store/synchnorousCookieStore.ts +++ b/packages/extension/src/store/synchnorousCookieStore.ts @@ -126,11 +126,7 @@ class SynchnorousCookieStore { this.tabsData[tabId][cookieKey] = cookie; } } - //@ts-ignore Since this is for debugging the data to check the data being collected by the storage. - globalThis.PSAT = { - tabsData: this.tabsData, - tabs: this.tabs, - }; + updateCookieBadgeText(this.tabsData[tabId], tabId); } catch (error) { //Fail silently @@ -299,6 +295,10 @@ class SynchnorousCookieStore { * @param {number} tabId The active tab id. */ removeCookieData(tabId: number) { + if (!this.tabs[tabId] || this.tabsData[tabId]) { + return; + } + delete this.tabsData[tabId]; this.tabsData[tabId] = {}; this.tabs[tabId].newUpdates = 0; @@ -313,6 +313,11 @@ class SynchnorousCookieStore { if (this.tabsData[tabId] && this.tabs[tabId]) { return; } + //@ts-ignore Since this is for debugging the data to check the data being collected by the storage. + globalThis.PSAT = { + tabsData: this.tabsData, + tabs: this.tabs, + }; this.tabsData[tabId] = {}; this.tabs[tabId] = { @@ -356,6 +361,9 @@ class SynchnorousCookieStore { tabId: number, overrideForInitialSync = false ) { + if (!this.tabs[tabId] || !this.tabsData[tabId]) { + return; + } let sentMessageAnyWhere = false; try { diff --git a/packages/extension/src/view/devtools/stateProviders/syncCookieStore/index.tsx b/packages/extension/src/view/devtools/stateProviders/syncCookieStore/index.tsx index c74882804..0fa6e8dc9 100644 --- a/packages/extension/src/view/devtools/stateProviders/syncCookieStore/index.tsx +++ b/packages/extension/src/view/devtools/stateProviders/syncCookieStore/index.tsx @@ -369,15 +369,20 @@ export const Provider = ({ children }: PropsWithChildren) => { ); const tabRemovedListener = useCallback(async () => { - const availableTabs = await chrome.tabs.query({}); - - if ( - availableTabs.length === ALLOWED_NUMBER_OF_TABS && - availableTabs.filter( - (processingTab) => processingTab.id?.toString() === tabToRead - ) - ) { - setReturningToSingleTab(true); + try { + const availableTabs = await chrome.tabs.query({}); + + if ( + availableTabs.length === ALLOWED_NUMBER_OF_TABS && + availableTabs.filter( + (processingTab) => processingTab.id?.toString() === tabToRead + ) + ) { + setReturningToSingleTab(true); + } + } catch (error) { + // eslint-disable-next-line no-console + console.warn(error); } }, [tabToRead]); From 97dd0dd3c1a79c25149039852020fb3f4878e376 Mon Sep 17 00:00:00 2001 From: Amoghavarsha Kudaligi Date: Mon, 5 Feb 2024 12:34:48 +0530 Subject: [PATCH 2/5] Fix uncaught in promise tab doesnt exist. --- packages/extension/src/serviceWorker/index.ts | 189 ++++++++---------- .../src/store/synchnorousCookieStore.ts | 8 +- .../src/store/utils/updateCookieBadgeText.ts | 16 +- 3 files changed, 99 insertions(+), 114 deletions(-) diff --git a/packages/extension/src/serviceWorker/index.ts b/packages/extension/src/serviceWorker/index.ts index 8156ac10b..a280deec1 100644 --- a/packages/extension/src/serviceWorker/index.ts +++ b/packages/extension/src/serviceWorker/index.ts @@ -641,81 +641,70 @@ chrome.storage.sync.onChanged.addListener( } tabMode = changes.allowedNumberOfTabs.newValue; - if (changes?.allowedNumberOfTabs?.newValue === 'single') { - try { - const tabs = await chrome.tabs.query({}); - tabToRead = ''; - - chrome.runtime.sendMessage({ - type: 'ServiceWorker::DevTools::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); + const tabs = await chrome.tabs.query({}); - chrome.runtime.sendMessage({ - type: 'ServiceWorker::Popup::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); - - tabs.map((tab) => { - if (!tab?.id) { - return tab; - } + if (changes?.allowedNumberOfTabs?.newValue === 'single') { + tabToRead = ''; - chrome.action.setBadgeText({ - tabId: tab?.id, - text: '', - }); + chrome.runtime.sendMessage({ + type: 'ServiceWorker::DevTools::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); - syncCookieStore?.removeTabData(tab.id); + chrome.runtime.sendMessage({ + type: 'ServiceWorker::Popup::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); - chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); + tabs.map((tab) => { + if (!tab?.id) { return tab; + } + + chrome.action.setBadgeText({ + tabId: tab?.id, + text: '', }); - } catch (error) { - // eslint-disable-next-line no-console - console.warn(error); - } + + syncCookieStore?.removeTabData(tab.id); + + chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); + return tab; + }); } else { - try { - const tabs = await chrome.tabs.query({}); - - chrome.runtime.sendMessage({ - type: 'ServiceWorker::Popup::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); + chrome.runtime.sendMessage({ + type: 'ServiceWorker::Popup::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); - chrome.runtime.sendMessage({ - type: 'ServiceWorker::DevTools::INITIAL_SYNC', - payload: { - tabMode, - tabToRead: tabToRead, - }, - }); + chrome.runtime.sendMessage({ + type: 'ServiceWorker::DevTools::INITIAL_SYNC', + payload: { + tabMode, + tabToRead: tabToRead, + }, + }); - await Promise.all( - tabs.map(async (tab) => { - if (!tab?.id) { - return; - } - syncCookieStore?.addTabData(tab.id); - syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); - syncCookieStore?.updateDevToolsState(tab.id, true); - await chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); - }) - ); - } catch (error) { - // eslint-disable-next-line no-console - console.warn(error); - } + await Promise.all( + tabs.map(async (tab) => { + if (!tab?.id) { + return; + } + syncCookieStore?.addTabData(tab.id); + syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); + syncCookieStore?.updateDevToolsState(tab.id, true); + await chrome.tabs.reload(Number(tab?.id), { bypassCache: true }); + }) + ); } } ); @@ -746,51 +735,39 @@ chrome.storage.sync.onChanged.addListener( }, }); - if (!changes?.isUsingCDP?.newValue) { - try { - const tabs = await chrome.tabs.query({}); - - await Promise.all( - tabs.map(async (tab) => { - if (!tab.id) { - return; - } - - try { - await chrome.debugger.detach({ tabId: tab.id }); - syncCookieStore?.removeCookieData(tab.id); - syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); - } catch (error) { - // eslint-disable-next-line no-console - console.warn(error); - } finally { - await chrome.tabs.reload(tab.id, { bypassCache: true }); - } - }) - ); - } catch (error) { - // eslint-disable-next-line no-console - console.warn(error); - } - } else { - try { - const tabs = await chrome.tabs.query({}); + const tabs = await chrome.tabs.query({}); - await Promise.all( - tabs.map(async (tab) => { - if (!tab.id) { - return; - } + if (!changes?.isUsingCDP?.newValue) { + await Promise.all( + tabs.map(async (tab) => { + if (!tab.id) { + return; + } + try { + await chrome.debugger.detach({ tabId: tab.id }); syncCookieStore?.removeCookieData(tab.id); syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); + } catch (error) { + // eslint-disable-next-line no-console + console.warn(error); + } finally { await chrome.tabs.reload(tab.id, { bypassCache: true }); - }) - ); - } catch (error) { - // eslint-disable-next-line no-console - console.warn(error); - } + } + }) + ); + } else { + await Promise.all( + tabs.map(async (tab) => { + if (!tab.id) { + return; + } + + syncCookieStore?.removeCookieData(tab.id); + syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id); + await chrome.tabs.reload(tab.id, { bypassCache: true }); + }) + ); } } ); diff --git a/packages/extension/src/store/synchnorousCookieStore.ts b/packages/extension/src/store/synchnorousCookieStore.ts index 4c1069a23..98c2f6943 100644 --- a/packages/extension/src/store/synchnorousCookieStore.ts +++ b/packages/extension/src/store/synchnorousCookieStore.ts @@ -395,14 +395,14 @@ class SynchnorousCookieStore { }, }); } + + if (sentMessageAnyWhere) { + this.tabs[tabId].newUpdates = 0; + } } catch (error) { // eslint-disable-next-line no-console console.warn(error); } - - if (sentMessageAnyWhere) { - this.tabs[tabId].newUpdates = 0; - } } } diff --git a/packages/extension/src/store/utils/updateCookieBadgeText.ts b/packages/extension/src/store/utils/updateCookieBadgeText.ts index 5ea9fee88..0ee66b01d 100644 --- a/packages/extension/src/store/utils/updateCookieBadgeText.ts +++ b/packages/extension/src/store/utils/updateCookieBadgeText.ts @@ -38,10 +38,18 @@ export default function updateCookieBadgeText( storage[cookieKey].frameIdList?.length >= 1 ).length; if (numCookies >= 0) { - chrome.action.setBadgeText({ - tabId: tabId, - text: numCookies.toString(), - }); + chrome.action.setBadgeText( + { + tabId: tabId, + text: numCookies.toString(), + }, + () => { + if (chrome.runtime.lastError) { + // eslint-disable-next-line no-console + console.warn(chrome.runtime.lastError); + } + } + ); } } catch (error) { // do nothing From 68498ad99bf88ea8227de47998e91de588722f40 Mon Sep 17 00:00:00 2001 From: Amoghavarsha Kudaligi Date: Mon, 5 Feb 2024 12:49:25 +0530 Subject: [PATCH 3/5] Fix failing tests. --- packages/extension/src/store/synchnorousCookieStore.ts | 2 +- packages/extension/src/store/tests/synchnorousCookieStore.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/extension/src/store/synchnorousCookieStore.ts b/packages/extension/src/store/synchnorousCookieStore.ts index 98c2f6943..ef675efa5 100644 --- a/packages/extension/src/store/synchnorousCookieStore.ts +++ b/packages/extension/src/store/synchnorousCookieStore.ts @@ -295,7 +295,7 @@ class SynchnorousCookieStore { * @param {number} tabId The active tab id. */ removeCookieData(tabId: number) { - if (!this.tabs[tabId] || this.tabsData[tabId]) { + if (!this.tabs[tabId] || !this.tabsData[tabId]) { return; } diff --git a/packages/extension/src/store/tests/synchnorousCookieStore.ts b/packages/extension/src/store/tests/synchnorousCookieStore.ts index 0dd52bf7f..eefb085f7 100644 --- a/packages/extension/src/store/tests/synchnorousCookieStore.ts +++ b/packages/extension/src/store/tests/synchnorousCookieStore.ts @@ -24,16 +24,15 @@ let synchnorousCookieStore: SynchnorousCookieStore; describe('SynchnorousCookieStore:', () => { beforeAll(() => { globalThis.chrome = SinonChrome as unknown as typeof chrome; + synchnorousCookieStore = new SynchnorousCookieStore(); }); beforeEach(() => { - synchnorousCookieStore = new SynchnorousCookieStore(); synchnorousCookieStore.addTabData(123456); synchnorousCookieStore.updateUrl(123456, 'https://bbc.com'); }); afterEach(() => { - synchnorousCookieStore = new SynchnorousCookieStore(); synchnorousCookieStore.clear(); }); From f6aed62a3c7929ac285f6159699f2d22d18392da Mon Sep 17 00:00:00 2001 From: Amoghavarsha Kudaligi Date: Tue, 6 Feb 2024 16:41:26 +0530 Subject: [PATCH 4/5] Remove console.warn. --- packages/extension/src/store/synchnorousCookieStore.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/extension/src/store/synchnorousCookieStore.ts b/packages/extension/src/store/synchnorousCookieStore.ts index ef675efa5..8a44a9eda 100644 --- a/packages/extension/src/store/synchnorousCookieStore.ts +++ b/packages/extension/src/store/synchnorousCookieStore.ts @@ -400,8 +400,7 @@ class SynchnorousCookieStore { this.tabs[tabId].newUpdates = 0; } } catch (error) { - // eslint-disable-next-line no-console - console.warn(error); + //Fail silently. Ignoring the catch here because the only error this will throw is of "Error: Could not establish connection". } } } From f336e0d49a08c084096357bdc4c34beac5089f7f Mon Sep 17 00:00:00 2001 From: Amoghavarsha Kudaligi Date: Tue, 6 Feb 2024 16:44:34 +0530 Subject: [PATCH 5/5] Modify comment. --- packages/extension/src/store/synchnorousCookieStore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/extension/src/store/synchnorousCookieStore.ts b/packages/extension/src/store/synchnorousCookieStore.ts index 8a44a9eda..6565df844 100644 --- a/packages/extension/src/store/synchnorousCookieStore.ts +++ b/packages/extension/src/store/synchnorousCookieStore.ts @@ -400,7 +400,7 @@ class SynchnorousCookieStore { this.tabs[tabId].newUpdates = 0; } } catch (error) { - //Fail silently. Ignoring the catch here because the only error this will throw is of "Error: Could not establish connection". + //Fail silently. Ignoring the console.warn here because the only error this will throw is of "Error: Could not establish connection". } } }