this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactoring Privacy & Security, prep for 1.4.0

+50 -18
+49 -17
background.js
··· 97 97 action.setIcon({ path: iconName, tabId }) 98 98 } 99 99 100 + // Cache for storing domain DIDs 101 + // We use caching to prevent creating multiple requests 102 + // for a tab/domain that has already returned a check 103 + // The cache is cleared when the tab is closed 104 + const didCache = new Map() 105 + 100 106 // Main function to perform actions, but only if the privacy consent has been accepted 101 107 function performAction(tab) { 102 108 storage.get("privacyConsentAccepted", ({ privacyConsentAccepted }) => { 103 - // If the user has accepted the privacy consent 104 109 if (privacyConsentAccepted) { 105 110 const domain = getDomainName(tab.url) 106 111 if (isValidDomain(domain)) { 107 - checkForDIDDNS(domain).then((domainDID) => { 108 - if (domainDID) { 109 - setIcon(tab.id, "logo48.png") 110 - tabsWithDID.set(tab.id, domainDID) 111 - } else { 112 - checkForDIDHTTPS(domain).then((httpsDID) => { 113 - if (httpsDID) { 114 - setIcon(tab.id, "logo48.png") 115 - tabsWithDID.set(tab.id, httpsDID) 116 - } else { 117 - setIcon(tab.id, "logo48_gray.png") 118 - tabsWithDID.delete(tab.id) 119 - } 120 - }) 121 - } 122 - }) 112 + // Check if we have cached DID for this tab and domain 113 + const cachedDID = didCache.get(`${tab.id}:${domain}`) 114 + if (cachedDID !== undefined) { 115 + // If we have a cached DID, use it 116 + setDID(tab, cachedDID) 117 + } else { 118 + // If not, proceed with the checks 119 + checkForDIDDNS(domain).then((domainDID) => { 120 + if (domainDID) { 121 + setDID(tab, domainDID) 122 + didCache.set(`${tab.id}:${domain}`, domainDID) 123 + } else { 124 + checkForDIDHTTPS(domain).then((httpsDID) => { 125 + if (httpsDID) { 126 + setDID(tab, httpsDID) 127 + didCache.set(`${tab.id}:${domain}`, httpsDID) 128 + } else { 129 + setIcon(tab.id, "logo48_gray.png") 130 + tabsWithDID.delete(tab.id) 131 + } 132 + }) 133 + } 134 + }) 135 + } 123 136 } 124 137 } 125 138 }) 126 139 } 127 140 141 + // Function to set the DID 142 + function setDID(tab, did) { 143 + setIcon(tab.id, "logo48.png") 144 + tabsWithDID.set(tab.id, did) 145 + } 146 + 128 147 // Execute performAction when a tab is updated and the tab is a website. 129 148 tabs.onUpdated.addListener((tabId, changeInfo, tab) => { 130 149 if ( ··· 132 151 tab.active && 133 152 (tab.url.startsWith("http://") || tab.url.startsWith("https://")) 134 153 ) { 154 + // Get the old domain from the cache 155 + const oldDomain = Array.from(didCache.keys()) 156 + .filter((key) => key.startsWith(`${tabId}:`)) 157 + .map((key) => key.split(":")[1])[0] 158 + 159 + // Get the new domain 160 + const newDomain = getDomainName(tab.url) 161 + 162 + // If the domain has changed, clear the DID state for this tab 163 + if (newDomain !== oldDomain) { 164 + didCache.delete(`${tabId}:${oldDomain}`) 165 + } 166 + // Perform the action 135 167 performAction(tab) 136 168 } 137 169 })
+1 -1
manifest-firefox.json
··· 18 18 "permissions": ["tabs", "storage"], 19 19 "content_security_policy": { 20 20 "extension_pages": "script-src 'self'; object-src 'self'" 21 - }, 21 + }, 22 22 "background": { 23 23 "scripts": ["background.js"] 24 24 },