-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
235 lines (206 loc) · 6.53 KB
/
app.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Get DOM elements
const input = document.getElementById('input');
const searchBtn = document.getElementById('search-btn');
const output = document.getElementById('output');
const mapEl = document.getElementById('map');
const errorContainer = document.getElementById('error-container');
const errorMessage = document.getElementById('error-message');
/**
* Initialize the map using the user's current IP address.
*/
async function initMap() {
try {
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
getGEO(data.ip);
input.value = data.ip;
} catch (error) {
handleFetchError(error, 'Failed to fetch IP information.');
}
}
input.addEventListener('input', function () {
errorContainer.style.display = 'none';
});
/**
* Search for the geolocation information of the provided IP address.
* @param {Event} e - The click event.
*/
function searchIP(e) {
mapEl.style.display = 'block';
output.style.display = 'block';
const IP = input.value;
getGEO(IP);
e.preventDefault();
}
/**
* Fetch geolocation information for the given IP address and display it.
* @param {string} IP - The IP address to search for.
*/
async function getGEO(IP) {
let data;
try {
const response = await fetch(
`https://geo.ipify.org/api/v1?apiKey=at_wuvOQ7NKYsA7ReFthDnzqtIvnKo9C&ipAddress=${IP}`
);
data = await response.json();
const {
ip,
location: { region, timezone, country, lat, lng },
as: { name: provider },
} = data;
showOutput(ip, region, timezone, provider, country);
showMap(lat, lng, ip);
// Add the searched IP to history
addToHistory(input.value);
} catch (error) {
handleFetchError(data, 'Failed to fetch geolocation information.');
}
}
/**
* Display the map with a marker at the specified coordinates.
* @param {number} lat - The latitude.
* @param {number} lng - The longitude.
* @param {string} ip - The IP address for the marker popup.
*/
function showMap(lat, lng, ip) {
mapEl.innerHTML = '<div id="mapid"></div>';
let mymap = L.map('mapid').setView([lat, lng], 13);
let marker = L.marker([lat, lng]).addTo(mymap);
marker.bindPopup(ip).openPopup();
L.tileLayer(
'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}',
{
maxZoom: 18,
id: JSON.parse(localStorage.getItem('darkMode'))
? 'mapbox/dark-v10'
: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken:
'pk.eyJ1IjoibWJlbGx5ZG8iLCJhIjoiY2tqb2E1anFmMGx2djJ2bzhpeDRkdHFyayJ9.DEAvUgh9QjI7vbAWdxHeaw',
}
).addTo(mymap);
}
/**
* Display the geolocation information in the output section.
* @param {string} ip - The IP address.
* @param {string} region - The region.
* @param {string} timezone - The timezone.
* @param {string} provider - The provider.
* @param {string} country - The country code.
*/
function showOutput(ip, region, timezone, provider, country) {
output.innerHTML = `
<ul class="output-card">
<li class="item">
<h2>IP ADDRESS</h2>
<p>${ip}</p>
</li>
<li class="item">
<h2>LOCATION</h2>
<p>${region}</p>
</li>
<li class="item">
<h2>TIMEZONE</h2>
<p>${timezone}</p>
</li>
<li class="item">
<h2>Provider</h2>
<p>${provider}</p>
</li>
<li class="item">
<h2>Country</h2>
<div class="country">
<img src="https://flagcdn.com/w640/${country.toLowerCase()}.png" alt="">
</div>
</li>
</ul>
`;
}
/**
* Handle fetch errors by displaying an error message.
* @param {Error} error - The error object.
* @param {string} message - The error message to display.
*/
function handleFetchError(error, message) {
mapEl.style.display = 'none';
output.style.display = 'none';
errorContainer.style.display = 'block';
errorMessage.textContent = error.messages || `${message}`;
}
// Event listeners
searchBtn.addEventListener('click', searchIP);
// Function to add an IP to local storage
function addToHistory(IP) {
let history = JSON.parse(localStorage.getItem('ipHistory')) || [];
// Check if the IP is not already in history
if (!history.includes(IP)) {
IP && history.push(IP);
localStorage.setItem('ipHistory', JSON.stringify(history));
// Update the history section
updateHistorySection(history);
}
}
// Function to update the history section
function updateHistorySection(history) {
const historyList = document.querySelector('.history-list');
historyList.innerHTML = '';
history.forEach((ip) => {
const listItem = document.createElement('li');
listItem.classList.add('history-list-item');
listItem.textContent = ip;
// Add delete icon
const deleteIcon = document.createElement('i');
deleteIcon.classList.add('fas', 'fa-trash-alt', 'delete-icon');
deleteIcon.addEventListener('click', (e) => {
// Remove the IP from history and stop the event from propagating
e.stopPropagation();
removeFromHistory(ip);
});
listItem.appendChild(deleteIcon);
historyList.appendChild(listItem);
// Add a click event listener only for displaying the IP in the input field
listItem.addEventListener('click', () => {
input.value = ip;
searchIP(new Event('click'));
});
});
}
// Event listener for the form submission
searchBtn.addEventListener('click', function (e) {
e.preventDefault();
searchIP(e);
});
// Initial history section update
updateHistorySection(JSON.parse(localStorage.getItem('ipHistory')) || []);
function removeFromHistory(IP) {
let history = JSON.parse(localStorage.getItem('ipHistory')) || [];
history = history.filter((item) => item !== IP);
localStorage.setItem('ipHistory', JSON.stringify(history));
console.log(1, { history });
// Update the history section
updateHistorySection(history);
}
// Function to clear the entire history
function clearHistory() {
localStorage.removeItem('ipHistory');
updateHistorySection([]);
}
// Event listener for the clear history button
document
.getElementById('clear-history-btn')
.addEventListener('click', clearHistory);
// Function to toggle dark mode
function toggleDarkMode() {
const body = document.body;
body.classList.toggle('dark-mode');
localStorage.setItem('darkMode', body.classList.contains('dark-mode'));
initMap();
}
// Event listener for the dark mode toggle button
document
.getElementById('toggle-dark-mode')
.addEventListener('click', toggleDarkMode);
// Initial map initialization
initMap();
localStorage.setItem('darkMode', false);