Skip to content

Commit

Permalink
Merge pull request #135 from gnosisguild/fix-pilot-open
Browse files Browse the repository at this point in the history
Fix pilot open
  • Loading branch information
jfschwarz authored Sep 19, 2024
2 parents b2843d9 + cb9f1ae commit 2ba60a6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
107 changes: 55 additions & 52 deletions extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ 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.
Expand Down Expand Up @@ -125,7 +115,6 @@ 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.
Expand Down Expand Up @@ -246,35 +235,6 @@ 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,
Expand Down Expand Up @@ -361,20 +321,63 @@ const getJsonRpcBody = (details: chrome.webRequest.WebRequestBodyDetails) => {
return json
}

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
const isExtensionTab = !!tab.url?.startsWith(PILOT_URL)
const wasExtensionTab = activeExtensionTabs.has(tabId)
chrome.runtime.onInstalled.addListener(() => {
chrome.action.onClicked.addListener(toggle)

if (isExtensionTab && !wasExtensionTab) {
startTrackingTab(tabId)
}
if (!isExtensionTab && wasExtensionTab) {
stopTrackingTab(tabId)
}
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((details) => {
if (details.rule.ruleId !== HEADERS_RULE_ID) {
console.debug(
'rule matched on request',
details.request.url,
details.rule.ruleId
)
}
})

if (changeInfo.status === 'complete' && isExtensionTab) {
chrome.tabs.sendMessage(tabId, { type: 'navigationDetected' })
}
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}`
)
}
})

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 (changeInfo.status === 'complete' && isExtensionTab) {
chrome.tabs.sendMessage(tabId, { type: 'navigationDetected' })
}
})
})

export {}
1 change: 1 addition & 0 deletions extension/src/bridge/SafeAppBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const SAFE_APP_WHITELIST = [
'https://curve.fi',
'https://app.spark.fi',
'https://community.safe.global',
'https://app.nexusmutual.io',
]

export default class SafeAppBridge {
Expand Down

0 comments on commit 2ba60a6

Please sign in to comment.