I use the following snippet with my browser homepage to detect whether or not I have my VPN connection turned on. If the IP timezone is different from the browser timezone, then it is most likely the case that the VPN connection is turned on.
Yes, this is a very unreliable way, but asking to improve the code "conceptually" would be too much. What I ask here is to review the syntax. For example, I'm not sure whether it is best to use .then(({browserTimezone, ipTimezone}) => ( ...
or replace it with something like .then(function(detectionResult) { ...
. As a part of my self-education and fulfilling my perfectionism, I want to make this code as simple and concise as possible.
function detectVPN() {
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone
return fetch(`https://ipapi.co/json`)
.then(function(response) {
return response.json()
})
.then(function(data) {
const ipTimezone = data.timezone
return {
browserTimezone,
ipTimezone
}
})
}
detectVPN()
.then(({browserTimezone, ipTimezone}) => (
document.getElementById('time-zones').textContent = browserTimezone + '—' + ipTimezone
))
.catch(function(error) { // e.g. if the ip lookup website isn't reachable
document.getElementById('time-zones').textContent = 'error: ' + error.message
});
<p id="time-zones"></p>
privacy.resistFingerprinting
may change the timezone to UTC. \$\endgroup\$