forked from liamstewart23/ChromeExt-WebsiteIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
98 lines (86 loc) · 3.24 KB
/
background.js
File metadata and controls
98 lines (86 loc) · 3.24 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
// Define DoH endpoints
const DOH_ENDPOINTS = [
'https://dns.google/resolve',
'https://cloudflare-dns.com/dns-query'
];
// Function to get IP address from hostname using DoH
async function getIpFromHostname(hostname) {
if (!hostname) return null;
for (const endpoint of DOH_ENDPOINTS) {
try {
const url = endpoint.includes('cloudflare')
? `${endpoint}?name=${encodeURIComponent(hostname)}&type=A`
: `${endpoint}?name=${encodeURIComponent(hostname)}&type=A`;
const headers = endpoint.includes('cloudflare') ? { 'Accept': 'application/dns-json' } : {};
const response = await fetch(url, { headers });
if (!response.ok) {
continue; // Try next endpoint
}
const data = await response.json();
if (data.Answer && data.Answer.length > 0) {
// Return the first A record found
const ips = data.Answer.filter(record => record.type === 1).map(record => record.data);
if (ips.length > 0) {
return ips; // Return all resolved IPs
}
}
} catch (error) {
// Continue to next endpoint on error
}
}
return null;
}
// Function to update badge based on IP match
async function updateBadgeForTab(tabId) {
try {
const tab = await chrome.tabs.get(tabId);
if (!tab || !tab.url || !tab.url.startsWith('http')) {
chrome.action.setBadgeText({ text: '', tabId: tabId });
return; // Ignore if tab is not valid or not http/https
}
const url = new URL(tab.url);
const hostname = url.hostname;
const resolvedIps = await getIpFromHostname(hostname);
if (!resolvedIps || resolvedIps.length === 0) {
chrome.action.setBadgeText({ text: '', tabId: tabId });
return; // Could not resolve IP
}
const data = await chrome.storage.sync.get('ipNames');
const ipNames = data.ipNames || [];
let foundMatch = false;
for (const entry of ipNames) {
if (entry.ip && entry.name && resolvedIps.includes(entry.ip)) {
const badgeText = entry.name.substring(0, 3);
chrome.action.setBadgeText({ text: badgeText, tabId: tabId });
chrome.action.setBadgeBackgroundColor({ color: '#FFA500', tabId: tabId }); // Orange background
foundMatch = true;
break; // Stop after first match
}
}
if (!foundMatch) {
chrome.action.setBadgeText({ text: '', tabId: tabId });
}
} catch (error) {
// Handle potential errors like the tab being closed before we process it
try {
// Attempt to clear the badge anyway if an error occurred
chrome.action.setBadgeText({ text: '', tabId: tabId });
} catch (clearError) {
// Ignore errors trying to clear badge for potentially non-existent tab
}
}
}
// Listen for tab activation changes
chrome.tabs.onActivated.addListener(activeInfo => {
updateBadgeForTab(activeInfo.tabId);
});
// Listen for tab URL updates
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// Update only when the URL changes and the tab is fully loaded
if (changeInfo.url && tab.status === 'complete') {
updateBadgeForTab(tabId);
} else if (changeInfo.status === 'complete' && tab.url) {
// Also update if the tab finishes loading (e.g., after initial load)
updateBadgeForTab(tabId);
}
});