Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Errors appearing on extension page. #472

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions packages/extension/src/serviceWorker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -641,8 +641,9 @@ chrome.storage.sync.onChanged.addListener(
}
tabMode = changes.allowedNumberOfTabs.newValue;

const tabs = await chrome.tabs.query({});

if (changes?.allowedNumberOfTabs?.newValue === 'single') {
const tabs = await chrome.tabs.query({});
tabToRead = '';

chrome.runtime.sendMessage({
Expand Down Expand Up @@ -670,19 +671,21 @@ chrome.storage.sync.onChanged.addListener(
tabId: tab?.id,
text: '',
});

syncCookieStore?.removeTabData(tab.id);

chrome.tabs.reload(Number(tab?.id), { bypassCache: true });
return tab;
});
} 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: {
Expand Down Expand Up @@ -732,26 +735,37 @@ chrome.storage.sync.onChanged.addListener(
},
});

const tabs = await chrome.tabs.query({});

if (!changes?.isUsingCDP?.newValue) {
await Promise.all(
Object.keys(syncCookieStore?.tabsData ?? {}).map(async (key) => {
tabs.map(async (tab) => {
if (!tab.id) {
return;
}

try {
await chrome.debugger.detach({ tabId: Number(key) });
syncCookieStore?.removeCookieData(Number(key));
syncCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key));
await chrome.debugger.detach({ tabId: tab.id });
syncCookieStore?.removeCookieData(tab.id);
syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id);
} catch (error) {
// Fail silently
// eslint-disable-next-line no-console
console.warn(error);
} finally {
await chrome.tabs.reload(Number(key), { bypassCache: true });
await chrome.tabs.reload(tab.id, { bypassCache: true });
}
})
);
} 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 });
tabs.map(async (tab) => {
if (!tab.id) {
return;
}

syncCookieStore?.removeCookieData(tab.id);
syncCookieStore?.sendUpdatedDataToPopupAndDevTools(tab.id);
await chrome.tabs.reload(tab.id, { bypassCache: true });
})
);
}
Expand Down
29 changes: 18 additions & 11 deletions packages/extension/src/store/synchnorousCookieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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] = {
Expand Down Expand Up @@ -356,6 +361,9 @@ class SynchnorousCookieStore {
tabId: number,
overrideForInitialSync = false
) {
if (!this.tabs[tabId] || !this.tabsData[tabId]) {
return;
}
let sentMessageAnyWhere = false;

try {
Expand Down Expand Up @@ -387,13 +395,12 @@ class SynchnorousCookieStore {
},
});
}
} catch (error) {
// eslint-disable-next-line no-console
console.warn(error);
}

if (sentMessageAnyWhere) {
this.tabs[tabId].newUpdates = 0;
if (sentMessageAnyWhere) {
this.tabs[tabId].newUpdates = 0;
}
} catch (error) {
//Fail silently. Ignoring the console.warn here because the only error this will throw is of "Error: Could not establish connection".
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/extension/src/store/tests/synchnorousCookieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
16 changes: 12 additions & 4 deletions packages/extension/src/store/utils/updateCookieBadgeText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
Loading