-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Load settings when extension has been enabled (#758)
* Load settings when extension has been enabled. * Remvoe header comment. * Use setupTimeout instead of writing same function everytime. * Follow DRY. * Change date in license header. * Rename setupTimeOuts to setupTimeouts * Refactor code --------- Co-authored-by: sayedtaqui <sayedwp@gmail.com>
- Loading branch information
Showing
7 changed files
with
180 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/extension/src/serviceWorker/chromeListeners/onEnabledListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
/** | ||
* 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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/extension/src/serviceWorker/chromeListeners/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
53 changes: 53 additions & 0 deletions
53
packages/extension/src/serviceWorker/chromeListeners/utils/setupIntervals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
64 changes: 64 additions & 0 deletions
64
...ges/extension/src/serviceWorker/chromeListeners/utils/updateGlobalVariableAndAttachCDP.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |