-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
69 lines (60 loc) · 3.12 KB
/
script.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
// This function is to fetch the latitude and longitude of the user
document.getElementById("fetchLocationBtn").addEventListener("click", () => {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(position => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
document.getElementById("latitude").textContent = latitude;
document.getElementById("longitude").textContent = longitude;
document.getElementById("ipv4AddressInfo").style.display = "none"; // Hide IPv4 address info
document.getElementById("locationInfo").style.display = "block"; // Show location info
}, error => {
console.error('Error getting location:', error.message);
});
} else {
console.error('Geolocation is not supported by this browser.');
}
});
// This function is to fetch the latitude and longitude of the user
// and the IPv4 address of the user using WebRTC
document.getElementById("fetchBothBtn").addEventListener("click", () => {
if ("geolocation" in navigator) {
// Add a unique query parameter to the geolocation API request
const geoOptions = {
timeout: 10000, // Set a timeout value in milliseconds
maximumAge: 0, // Ensure that cached data is not used
enableHighAccuracy: true // Request high accuracy for location data
};
navigator.geolocation.getCurrentPosition(position => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
document.getElementById("latitude").textContent = latitude;
document.getElementById("longitude").textContent = longitude;
document.getElementById("locationInfo").style.display = "block"; // Show location info
// WebRTC to get IPv4 address
const pc = new RTCPeerConnection();
pc.createDataChannel("");
pc.createOffer().then(function (offer) {
pc.setLocalDescription(offer);
});
pc.onicecandidate = function (event) {
if (event.candidate && event.candidate.candidate.includes("typ host")) {
const ipv4Address = event.candidate.candidate.split(" ")[4];
document.getElementById("ipv4Address").textContent = ipv4Address;
document.getElementById("ipv4AddressInfo").style.display = "block"; // Show IPv4 address info
}
};
}, error => {
console.error('Error getting location:', error.message);
}, geoOptions);
} else {
console.error('Geolocation is not supported by this browser.');
}
});
document.getElementById("clearLocationBtn").addEventListener("click", () => {
document.getElementById("latitude").textContent = "";
document.getElementById("longitude").textContent = "";
document.getElementById("ipv4Address").textContent = "";
document.getElementById("ipv4AddressInfo").style.display = "none";
document.getElementById("locationInfo").style.display = "none";
});