forked from liamstewart23/ChromeExt-WebsiteIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (117 loc) · 4.27 KB
/
app.js
File metadata and controls
139 lines (117 loc) · 4.27 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 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;
}
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.query({
'active': true,
'lastFocusedWindow': true
}, async function(tabs) {
let copyField = document.querySelector('#copyField');
let nameDisplay = document.querySelector('#nameDisplay');
let optionsLink = document.querySelector('#optionsLink');
async function copyToClipboard() {
const textToCopy = copyField.value;
try {
await navigator.clipboard.writeText(textToCopy);
// Add visual feedback when the IP is copied
copyField.value = "Copied!";
copyField.style.color = "#4CAF50";
setTimeout(() => {
copyField.value = textToCopy;
copyField.style.color = "#000";
}, 1000);
} catch (err) {
// Fallback for older browsers or if clipboard API fails
copyField.select();
document.execCommand("copy");
copyField.value = "Copied!";
copyField.style.color = "#4CAF50";
setTimeout(() => {
copyField.value = textToCopy;
copyField.style.color = "#000";
}, 1000);
}
}
// Add event listener for the options link
if (optionsLink) {
optionsLink.addEventListener('click', function() {
chrome.runtime.openOptionsPage();
});
}
try {
let url = tabs[0].url; //Get current url
let parser = document.createElement('a');
parser.href = url;
const hostname = parser.hostname;
// Check if it's a valid HTTP/HTTPS URL
if (!url.startsWith('http')) {
copyField.value = "N/A";
nameDisplay.textContent = "Not a web page";
nameDisplay.style.display = "block";
nameDisplay.style.color = "#666";
return;
}
// Resolve IP using DoH
const resolvedIps = await getIpFromHostname(hostname);
if (resolvedIps && resolvedIps.length > 0) {
const currentIp = resolvedIps[0]; // Use the first IP
copyField.value = currentIp;
// Check if the IP matches any of the saved IPs
chrome.storage.sync.get('ipNames', function(data) {
if (chrome.runtime.lastError) {
return;
}
const ipNames = data.ipNames || [];
const matchingEntry = ipNames.find(entry => entry.ip === currentIp);
if (matchingEntry) {
nameDisplay.textContent = matchingEntry.name;
nameDisplay.style.display = 'block';
} else {
nameDisplay.style.display = 'none';
}
});
// Add event listener only to the copyField instead of the entire document
copyField.addEventListener('click', copyToClipboard, false);
} else {
copyField.value = "N/A";
nameDisplay.textContent = "Unable to resolve IP";
nameDisplay.style.display = "block";
nameDisplay.style.color = "#666";
}
} catch (err) {
copyField.value = "Error";
nameDisplay.textContent = "Failed to retrieve the IP";
nameDisplay.style.display = "block";
nameDisplay.style.color = "#f44336";
}
});
}, false);