-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
112 lines (112 loc) · 3.09 KB
/
background.js
File metadata and controls
112 lines (112 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
let tabRequests={}
let tabMainFrameDomain={}
function getBaseDomainBG(hostname)
{
if(!hostname)return null
const parts=hostname.split('.').reverse()
if(parts.length>=2)
{
if(['com','net','org','gov','edu','co','uk','ca','de','fr','app','io','tech','ai','info','biz'].includes(parts[1])&&parts.length>2)
{
return parts[2]+'.'+parts[1]+'.'+parts[0]
}
return parts[1]+'.'+parts[0]
}
return hostname
}
chrome.tabs.onActivated.addListener(activeInfo=>
{
chrome.tabs.get(activeInfo.tabId,(tab)=>
{
if(chrome.runtime.lastError||!tab||!tab.url||tab.url.startsWith('chrome://')||tab.url.startsWith('about:'))
{
delete tabMainFrameDomain[activeInfo.tabId]
return
}
try
{
const url=new URL(tab.url)
tabMainFrameDomain[activeInfo.tabId]=url.hostname.replace(/^www\./,'')
}
catch(e)
{
delete tabMainFrameDomain[activeInfo.tabId]
}
})
})
chrome.webRequest.onBeforeRequest.addListener(
(details)=>
{
if(details.tabId>0&&details.url)
{
if(!tabRequests[details.tabId])
{
tabRequests[details.tabId]=new Set()
}
try
{
const requestUrl=new URL(details.url)
const requestHostname=requestUrl.hostname.replace(/^www\./,'')
tabRequests[details.tabId].add(requestHostname)
if(details.type==="main_frame")
{
tabMainFrameDomain[details.tabId]=requestHostname
if(tabRequests[details.tabId])tabRequests[details.tabId].clear()
tabRequests[details.tabId].add(requestHostname)
}
}
catch(e){}
}
},
{urls:["<all_urls>"]}
)
chrome.tabs.onRemoved.addListener((tabId)=>
{
delete tabRequests[tabId]
delete tabMainFrameDomain[tabId]
})
chrome.tabs.onUpdated.addListener((tabId,changeInfo,tab)=>
{
if(changeInfo.url&&tab&&tab.url)
{
if(tab.url.startsWith('chrome://')||tab.url.startsWith('about:'))
{
delete tabMainFrameDomain[tabId]
if(tabRequests[tabId])tabRequests[tabId].clear()
return
}
try
{
const newUrl=new URL(changeInfo.url)
const newMainHostname=newUrl.hostname.replace(/^www\./,'')
if(getBaseDomainBG(tabMainFrameDomain[tabId])!==getBaseDomainBG(newMainHostname))
{
tabMainFrameDomain[tabId]=newMainHostname
if(tabRequests[tabId])
{
tabRequests[tabId].clear()
}
else
{
tabRequests[tabId]=new Set()
}
if(newMainHostname)tabRequests[tabId].add(newMainHostname)
}
}
catch(e){}
}
})
chrome.runtime.onMessage.addListener((request,sender,sendResponse)=>
{
if(request.action==="getTabInfo_DT")
{
const tabId=request.tabId
const domains=tabRequests[tabId]?Array.from(tabRequests[tabId]):[]
const mainDomain=tabMainFrameDomain[tabId]||null
sendResponse({
domains:domains,
mainFrameDomain:mainDomain
})
return true
}
})