From 98674dd562ec3c3ef741bdb7339ff8e821de6aba Mon Sep 17 00:00:00 2001 From: Jan-Felix Date: Thu, 19 Sep 2024 15:04:41 +0200 Subject: [PATCH 1/2] Revert "trial to fix service worker initialization in packed installs" This reverts commit cb9f1aeae91e6e0d2e0addc9de6de92b4d00224b. --- extension/src/background.ts | 107 ++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/extension/src/background.ts b/extension/src/background.ts index cfc45f6d..b9e5cb96 100644 --- a/extension/src/background.ts +++ b/extension/src/background.ts @@ -6,6 +6,16 @@ interface Fork { rpcUrl: string } +chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((details) => { + if (details.rule.ruleId !== HEADERS_RULE_ID) { + console.debug( + 'rule matched on request', + details.request.url, + details.rule.ruleId + ) + } +}) + // Track tabs showing our extension, so we can dynamically adjust the declarativeNetRequest rule. // This rule removes some headers so foreign pages can be loaded in iframes. We don't want to // generally circumvent this security mechanism, so we only apply it to extension tabs. @@ -115,6 +125,7 @@ const toggle = async (tab: chrome.tabs.Tab) => { }) } } +chrome.action.onClicked.addListener(toggle) // Track extension tabs that are actively simulating, meaning that RPC requests are being sent to // a fork network. @@ -235,6 +246,35 @@ const removeRpcRedirectRules = (tabId: number) => { console.log('removed all RPC redirect rules for tab', tabId, ruleIds) } +chrome.runtime.onMessage.addListener((message, sender) => { + if (!sender.tab?.id) return + + if (message.type === 'startSimulating') { + const { networkId, rpcUrl } = message + simulatingExtensionTabs.delete(sender.tab.id) + removeRpcRedirectRules(sender.tab.id) + simulatingExtensionTabs.set(sender.tab.id, { + networkId, + rpcUrl, + }) + updateRpcRedirectRules(sender.tab.id) + + console.debug( + `start intercepting JSON RPC requests for network #${networkId} in tab #${sender.tab.id}`, + rpcUrl + ) + } + + if (message.type === 'stopSimulating') { + simulatingExtensionTabs.delete(sender.tab.id) + removeRpcRedirectRules(sender.tab.id) + + console.debug( + `stop intercepting JSON RPC requests in tab #${sender.tab.id}` + ) + } +}) + // Keep track of the network IDs for all JSON RPC endpoints used from apps in the Pilot frame const networkIdOfRpcUrlPerTab = new Map< number, @@ -321,63 +361,20 @@ const getJsonRpcBody = (details: chrome.webRequest.WebRequestBodyDetails) => { return json } -chrome.runtime.onInstalled.addListener(() => { - chrome.action.onClicked.addListener(toggle) - - chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((details) => { - if (details.rule.ruleId !== HEADERS_RULE_ID) { - console.debug( - 'rule matched on request', - details.request.url, - details.rule.ruleId - ) - } - }) - - chrome.runtime.onMessage.addListener((message, sender) => { - if (!sender.tab?.id) return - - if (message.type === 'startSimulating') { - const { networkId, rpcUrl } = message - simulatingExtensionTabs.delete(sender.tab.id) - removeRpcRedirectRules(sender.tab.id) - simulatingExtensionTabs.set(sender.tab.id, { - networkId, - rpcUrl, - }) - updateRpcRedirectRules(sender.tab.id) +chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { + const isExtensionTab = !!tab.url?.startsWith(PILOT_URL) + const wasExtensionTab = activeExtensionTabs.has(tabId) - console.debug( - `start intercepting JSON RPC requests for network #${networkId} in tab #${sender.tab.id}`, - rpcUrl - ) - } - - if (message.type === 'stopSimulating') { - simulatingExtensionTabs.delete(sender.tab.id) - removeRpcRedirectRules(sender.tab.id) - - console.debug( - `stop intercepting JSON RPC requests in tab #${sender.tab.id}` - ) - } - }) - - chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { - const isExtensionTab = !!tab.url?.startsWith(PILOT_URL) - const wasExtensionTab = activeExtensionTabs.has(tabId) - - if (isExtensionTab && !wasExtensionTab) { - startTrackingTab(tabId) - } - if (!isExtensionTab && wasExtensionTab) { - stopTrackingTab(tabId) - } + if (isExtensionTab && !wasExtensionTab) { + startTrackingTab(tabId) + } + if (!isExtensionTab && wasExtensionTab) { + stopTrackingTab(tabId) + } - if (changeInfo.status === 'complete' && isExtensionTab) { - chrome.tabs.sendMessage(tabId, { type: 'navigationDetected' }) - } - }) + if (changeInfo.status === 'complete' && isExtensionTab) { + chrome.tabs.sendMessage(tabId, { type: 'navigationDetected' }) + } }) export {} From e86b65f7cce68956df80c965affa92afd7fc3a2b Mon Sep 17 00:00:00 2001 From: Jan-Felix Date: Thu, 19 Sep 2024 15:17:12 +0200 Subject: [PATCH 2/2] try to wake up background worker after chrome restarts --- extension/src/background.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/extension/src/background.ts b/extension/src/background.ts index b9e5cb96..a6c2cede 100644 --- a/extension/src/background.ts +++ b/extension/src/background.ts @@ -125,8 +125,15 @@ const toggle = async (tab: chrome.tabs.Tab) => { }) } } + chrome.action.onClicked.addListener(toggle) +// wake up the background script after chrome restarts +// this fixes an issue of the action onClicked listener not being triggered (see: https://stackoverflow.com/a/76344225) +chrome.runtime.onStartup.addListener(() => { + console.debug(`Zodiac Pilot startup`) +}) + // Track extension tabs that are actively simulating, meaning that RPC requests are being sent to // a fork network. const simulatingExtensionTabs = new Map()