-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
156 lines (136 loc) · 3.91 KB
/
content.js
File metadata and controls
156 lines (136 loc) · 3.91 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// content.js - Shows the website IP address
let ipDisplay = null;
let ipLink = null;
let leftArrow = null;
let rightArrow = null;
let currentIP = null;
let isLookingUp = false;
// Create the IP display element
function createIPDisplay() {
if (ipDisplay) return;
ipDisplay = document.createElement('div');
ipDisplay.className = 'website-ip-display';
leftArrow = document.createElement('span');
leftArrow.className = 'arrow arrow-left';
leftArrow.textContent = '←';
rightArrow = document.createElement('span');
rightArrow.className = 'arrow arrow-right hidden';
rightArrow.textContent = '→';
ipLink = document.createElement('a');
ipLink.className = 'ip-link';
ipLink.textContent = 'IP ophalen...';
ipLink.href = '#';
ipLink.target = '_blank';
ipLink.rel = 'noopener noreferrer';
leftArrow.addEventListener('click', () => {
ipDisplay.classList.add('moved-left');
updateArrows();
});
rightArrow.addEventListener('click', () => {
ipDisplay.classList.remove('moved-left');
updateArrows();
});
ipDisplay.appendChild(leftArrow);
ipDisplay.appendChild(ipLink);
ipDisplay.appendChild(rightArrow);
document.body.appendChild(ipDisplay);
updateArrows();
}
function updateArrows() {
if (!leftArrow || !rightArrow) return;
if (ipDisplay.classList.contains('moved-left')) {
leftArrow.classList.add('hidden');
rightArrow.classList.remove('hidden');
} else {
leftArrow.classList.remove('hidden');
rightArrow.classList.add('hidden');
}
}
// Retrieve and display the IP
function displayIP(forceRefresh = false) {
const hostname = window.location.hostname;
if (!hostname) {
if (ipLink) {
ipLink.textContent = 'Geen hostname';
ipLink.href = '#';
}
return;
}
// If the hostname is already an IP address, show it
if (hostname.match(/^\d+\.\d+\.\d+\.\d+$/)) {
if (ipLink) {
ipLink.textContent = hostname;
ipLink.title = 'Dit is al een IP-adres';
ipLink.href = `https://ipinfo.io/${hostname}/json?token=63251f89ade4d1`;
}
return;
}
// Show that a lookup is in progress
if (ipLink) {
ipLink.textContent = 'Zoeken...';
ipLink.href = '#';
ipLink.title = '';
isLookingUp = true;
}
// Request the IP from the background script
browser.runtime.sendMessage({
action: 'getIP',
hostname: hostname,
forceRefresh: forceRefresh
}).then(response => {
isLookingUp = false;
if (response && response.ip) {
currentIP = response.ip;
if (ipLink) {
ipLink.textContent = currentIP;
ipLink.title = `${hostname} → ${currentIP}`;
ipLink.href = `https://ipinfo.io/${currentIP}/json?token=63251f89ade4d1`;
}
} else {
// Retry after a short delay if lookup fails
if (!forceRefresh) {
setTimeout(() => displayIP(true), 1000);
} else {
if (ipLink) {
ipLink.textContent = 'IP onbekend';
ipLink.title = hostname;
ipLink.href = '#';
}
}
}
}).catch(error => {
isLookingUp = false;
console.error('Fout bij ophalen IP:', error);
if (ipLink) {
ipLink.textContent = 'Fout';
ipLink.title = hostname;
ipLink.href = '#';
}
});
}
// Initialize when the page is loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
function init() {
// Skip certain protocols
if (window.location.protocol === 'about:' ||
window.location.protocol === 'chrome:' ||
window.location.protocol === 'moz-extension:' ||
window.location.protocol === 'file:') {
return;
}
createIPDisplay();
displayIP();
}
// Update on navigation without a page reload (for SPAs)
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
displayIP();
}
}).observe(document, { subtree: true, childList: true });