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: Load settings when extension has been enabled #758

Merged
merged 8 commits into from
Jul 18, 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
7 changes: 7 additions & 0 deletions packages/extension/src/serviceWorker/chromeListeners/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { onTabRemovedListener } from './tabOnRemovedListener';
import { onCommittedNavigationListener } from './onCommittedNavigationListener';
import { windowsOnRemovedListener } from './windowsOnRemovedListener';
import { windowsOnCreatedListener } from './windowsOnCreatedListener';
import { onEnabledListener } from './onEnabledListener';

/**
* Fires before sending an HTTP request, once the request headers are available.
Expand All @@ -51,6 +52,12 @@ chrome?.webRequest?.onResponseStarted?.addListener(
['extraHeaders', 'responseHeaders']
);

/**
* Fires when the etension is enabled.
* @see https://developer.chrome.com/docs/extensions/reference/api/management#event-onEnabled
*/
chrome.management.onEnabled.addListener(onEnabledListener);

chrome.tabs.onCreated.addListener(onTabCreatedListener);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
amovar18 marked this conversation as resolved.
Show resolved Hide resolved
*/
/**
* Internal dependencies
*/
import synchnorousCookieStore from '../../store/synchnorousCookieStore';
import { updateGlobalVariableAndAttachCDP, setupIntervals } from './utils';

export const onEnabledListener = async (
details: chrome.management.ExtensionInfo
) => {
if (chrome.runtime.id !== details.id) {
return;
}

synchnorousCookieStore?.clear();

setupIntervals();

await updateGlobalVariableAndAttachCDP();
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2023 Google LLC
*
Expand All @@ -32,41 +17,14 @@
* Internal dependencies
*/
import synchnorousCookieStore from '../../store/synchnorousCookieStore';
import { getAndParseNetworkCookies } from '../../utils/getAndParseNetworkCookies';
import attachCDP from '../attachCDP';
import { updateGlobalVariableAndAttachCDP, setupIntervals } from './utils';

export const runtimeOnInstalledListener = async (
details: chrome.runtime.InstalledDetails
) => {
synchnorousCookieStore?.clear();

// @see https://developer.chrome.com/blog/longer-esw-lifetimes#whats_changed
// Doing this to keep the service worker alive so that we dont loose any data and introduce any bug.
setInterval(async () => {
await chrome.storage.session.get();
}, 20000);

// @todo Send tab data of the active tab only, also if sending only the difference would make it any faster.
setInterval(() => {
if (Object.keys(synchnorousCookieStore?.tabsData ?? {}).length === 0) {
return;
}

Object.keys(synchnorousCookieStore?.tabsData ?? {}).forEach((key) => {
synchnorousCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key));
});
}, 1200);

// @todo Send tab data of the active tab only, also if sending only the difference would make it any faster.
setInterval(() => {
if (Object.keys(synchnorousCookieStore?.tabsData ?? {}).length === 0) {
return;
}

Object.keys(synchnorousCookieStore?.tabsData ?? {}).forEach((key) => {
getAndParseNetworkCookies(key);
});
}, 5000);
setupIntervals();

if (details.reason === 'install') {
await chrome.storage.sync.clear();
Expand All @@ -77,43 +35,9 @@ export const runtimeOnInstalledListener = async (
}

if (details.reason === 'update') {
await chrome.storage.local.clear();
const preSetSettings = await chrome.storage.sync.get();
synchnorousCookieStore.tabMode =
preSetSettings?.allowedNumberOfTabs ?? 'single';
synchnorousCookieStore.globalIsUsingCDP =
preSetSettings?.isUsingCDP ?? false;

if (synchnorousCookieStore.tabMode === 'unlimited') {
const allTabs = await chrome.tabs.query({});
const targets = await chrome.debugger.getTargets();
await Promise.all(
allTabs.map(async (tab) => {
if (!tab.id || tab.url?.startsWith('chrome://')) {
return;
}

synchnorousCookieStore?.addTabData(tab.id);

if (synchnorousCookieStore.globalIsUsingCDP) {
synchnorousCookieStore.initialiseVariablesForNewTab(
tab.id.toString()
);

await attachCDP({ tabId: tab.id });

const currentTab = targets.filter(
({ tabId }) => tabId && tab.id && tabId === tab.id
);
synchnorousCookieStore?.updateParentChildFrameAssociation(
tab.id,
currentTab[0].id,
'0'
);
}
})
);
}
await updateGlobalVariableAndAttachCDP();

if (
preSetSettings?.allowedNumberOfTabs &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,11 @@
* Internal dependencies
*/
import synchnorousCookieStore from '../../store/synchnorousCookieStore';
import { getAndParseNetworkCookies } from '../../utils/getAndParseNetworkCookies';
import { setupIntervals } from './utils';

export const onStartUpListener = async () => {
const storage = await chrome.storage.sync.get();

// @see https://developer.chrome.com/blog/longer-esw-lifetimes#whats_changed
// Doing this to keep the service worker alive so that we dont loose any data and introduce any bug.
setInterval(async () => {
await chrome.storage.session.get();
}, 20000);

// @todo Send tab data of the active tab only, also if sending only the difference would make it any faster.
setInterval(() => {
if (Object.keys(synchnorousCookieStore?.tabsData ?? {}).length === 0) {
return;
}

Object.keys(synchnorousCookieStore?.tabsData ?? {}).forEach((key) => {
synchnorousCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key));
});
}, 1200);

// @todo Send tab data of the active tab only, also if sending only the difference would make it any faster.
setInterval(() => {
if (Object.keys(synchnorousCookieStore?.tabsData ?? {}).length === 0) {
return;
}

Object.keys(synchnorousCookieStore?.tabsData ?? {}).forEach((key) => {
getAndParseNetworkCookies(key);
});
}, 5000);
setupIntervals();

if (storage?.allowedNumberOfTabs) {
synchnorousCookieStore.tabMode = storage.allowedNumberOfTabs;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { default as setupIntervals } from './setupIntervals';
export { default as updateGlobalVariableAndAttachCDP } from './updateGlobalVariableAndAttachCDP';
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Internal dependencies
*/
import synchnorousCookieStore from '../../../store/synchnorousCookieStore';
import { getAndParseNetworkCookies } from '../../../utils/getAndParseNetworkCookies';

const setupIntervals = () => {
// @see https://developer.chrome.com/blog/longer-esw-lifetimes#whats_changed
// Doing this to keep the service worker alive so that we dont loose any data and introduce any bug.
setInterval(async () => {
await chrome.storage.session.get();
}, 20000);

// @todo Send tab data of the active tab only, also if sending only the difference would make it any faster.
setInterval(() => {
if (Object.keys(synchnorousCookieStore?.tabsData ?? {}).length === 0) {
return;
}

Object.keys(synchnorousCookieStore?.tabsData ?? {}).forEach((key) => {
synchnorousCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key));
});
}, 1200);

// @todo Send tab data of the active tab only, also if sending only the difference would make it any faster.
setInterval(() => {
if (Object.keys(synchnorousCookieStore?.tabsData ?? {}).length === 0) {
return;
}

Object.keys(synchnorousCookieStore?.tabsData ?? {}).forEach((key) => {
getAndParseNetworkCookies(key);
});
}, 5000);
};

export default setupIntervals;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Internal dependencies
*/
import synchnorousCookieStore from '../../../store/synchnorousCookieStore';
import attachCDP from '../../attachCDP';

const updateGlobalVariableAndAttachCDP = async () => {
await chrome.storage.local.clear();

const preSetSettings = await chrome.storage.sync.get();

synchnorousCookieStore.tabMode =
preSetSettings?.allowedNumberOfTabs ?? 'single';
synchnorousCookieStore.globalIsUsingCDP = preSetSettings?.isUsingCDP ?? false;

if (synchnorousCookieStore.tabMode === 'unlimited') {
const allTabs = await chrome.tabs.query({});
const targets = await chrome.debugger.getTargets();

await Promise.all(
allTabs.map(async (tab) => {
if (!tab.id || tab.url?.startsWith('chrome://')) {
return;
}

synchnorousCookieStore?.addTabData(tab.id);

if (synchnorousCookieStore.globalIsUsingCDP) {
synchnorousCookieStore.initialiseVariablesForNewTab(
tab.id.toString()
);

await attachCDP({ tabId: tab.id });

const currentTab = targets.filter(
({ tabId }) => tabId && tab.id && tabId === tab.id
);
synchnorousCookieStore?.updateParentChildFrameAssociation(
tab.id,
currentTab[0].id,
'0'
);
}
})
);
}
};

export default updateGlobalVariableAndAttachCDP;
Loading