···9797 action.setIcon({ path: iconName, tabId })
9898}
9999100100+// Cache for storing domain DIDs
101101+// We use caching to prevent creating multiple requests
102102+// for a tab/domain that has already returned a check
103103+// The cache is cleared when the tab is closed
104104+const didCache = new Map()
105105+100106// Main function to perform actions, but only if the privacy consent has been accepted
101107function performAction(tab) {
102108 storage.get("privacyConsentAccepted", ({ privacyConsentAccepted }) => {
103103- // If the user has accepted the privacy consent
104109 if (privacyConsentAccepted) {
105110 const domain = getDomainName(tab.url)
106111 if (isValidDomain(domain)) {
107107- checkForDIDDNS(domain).then((domainDID) => {
108108- if (domainDID) {
109109- setIcon(tab.id, "logo48.png")
110110- tabsWithDID.set(tab.id, domainDID)
111111- } else {
112112- checkForDIDHTTPS(domain).then((httpsDID) => {
113113- if (httpsDID) {
114114- setIcon(tab.id, "logo48.png")
115115- tabsWithDID.set(tab.id, httpsDID)
116116- } else {
117117- setIcon(tab.id, "logo48_gray.png")
118118- tabsWithDID.delete(tab.id)
119119- }
120120- })
121121- }
122122- })
112112+ // Check if we have cached DID for this tab and domain
113113+ const cachedDID = didCache.get(`${tab.id}:${domain}`)
114114+ if (cachedDID !== undefined) {
115115+ // If we have a cached DID, use it
116116+ setDID(tab, cachedDID)
117117+ } else {
118118+ // If not, proceed with the checks
119119+ checkForDIDDNS(domain).then((domainDID) => {
120120+ if (domainDID) {
121121+ setDID(tab, domainDID)
122122+ didCache.set(`${tab.id}:${domain}`, domainDID)
123123+ } else {
124124+ checkForDIDHTTPS(domain).then((httpsDID) => {
125125+ if (httpsDID) {
126126+ setDID(tab, httpsDID)
127127+ didCache.set(`${tab.id}:${domain}`, httpsDID)
128128+ } else {
129129+ setIcon(tab.id, "logo48_gray.png")
130130+ tabsWithDID.delete(tab.id)
131131+ }
132132+ })
133133+ }
134134+ })
135135+ }
123136 }
124137 }
125138 })
126139}
127140141141+// Function to set the DID
142142+function setDID(tab, did) {
143143+ setIcon(tab.id, "logo48.png")
144144+ tabsWithDID.set(tab.id, did)
145145+}
146146+128147// Execute performAction when a tab is updated and the tab is a website.
129148tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
130149 if (
···132151 tab.active &&
133152 (tab.url.startsWith("http://") || tab.url.startsWith("https://"))
134153 ) {
154154+ // Get the old domain from the cache
155155+ const oldDomain = Array.from(didCache.keys())
156156+ .filter((key) => key.startsWith(`${tabId}:`))
157157+ .map((key) => key.split(":")[1])[0]
158158+159159+ // Get the new domain
160160+ const newDomain = getDomainName(tab.url)
161161+162162+ // If the domain has changed, clear the DID state for this tab
163163+ if (newDomain !== oldDomain) {
164164+ didCache.delete(`${tabId}:${oldDomain}`)
165165+ }
166166+ // Perform the action
135167 performAction(tab)
136168 }
137169})