Last active
April 24, 2023 09:30
-
-
Save vip3r011/cc8344a5a19eb9896d076546e885fc63 to your computer and use it in GitHub Desktop.
geolocation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GEOLOCATION_ALLOW = true; | |
(() => { | |
try { | |
//geolocate: enable - true, disable - false | |
//get this setting from db | |
const geolocate = localStorage.getItem('geolocate'); | |
if (geolocate !== null) { | |
GEOLOCATION_ALLOW = Boolean(geolocate); | |
} | |
} | |
catch (err) {} | |
console.log({ GEOLOCATION_ALLOW }); | |
})() | |
function geolocation() { | |
return new Promise(function (resolve, reject) { | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(function (position) { | |
const { latitude, longitude, altitude, altitudeAccuracy, heading, accuracy, speed } = position.coords; | |
const geodata = { | |
error: null, | |
timestamp: position.timestamp, | |
data: { latitude, longitude, altitude, altitudeAccuracy, heading, accuracy, speed }, | |
}; | |
resolve(geodata); | |
}, function (error) { | |
switch (error.code) { | |
case error.PERMISSION_DENIED: | |
console.log("User denied the request for Geolocation."); | |
reject('PERMISSION_DENIED'); | |
break; | |
case error.POSITION_UNAVAILABLE: | |
console.log("Location information is unavailable."); | |
reject('POSITION_UNAVAILABLE'); | |
break; | |
case error.TIMEOUT: | |
console.log("The request to get user location timed out."); | |
reject('TIMEOUT'); | |
break; | |
case error.UNKNOWN_ERROR: | |
console.log("An unknown error occurred."); | |
reject('UNKNOWN_ERROR'); | |
break; | |
} | |
}); | |
} else { | |
console.log("Geolocation is not supported by this browser."); | |
reject('UNSUPPORTED_BROWSER'); | |
} | |
}); | |
} | |
function parseCloudflareTraceData(traceText) { | |
// Define a regular expression to match each key-value pair in the trace data | |
const keyValueRegex = /^([A-Za-z_-]+)=(.*)$/; | |
// Split the trace text into lines | |
const lines = traceText.trim().split('\n'); | |
// Loop through the lines and parse each key-value pair | |
const data = {}; | |
lines.forEach(function (line) { | |
const match = keyValueRegex.exec(line); | |
if (match) { | |
const key = match[1]; | |
const value = match[2]; | |
data[key] = value; | |
} | |
}); | |
return data; | |
} | |
function getCloudflareTraceData() { | |
// Make an HTTP request to the Cloudflare trace endpoint | |
return fetch('https://cloudflare.com/cdn-cgi/trace') | |
.then(function (response) { | |
if (response.ok) { | |
return response.text(); | |
} else { | |
throw new Error('Failed to get Cloudflare trace data: ' + response.status + ' ' + response.statusText); | |
} | |
}) | |
.then(function (traceText) { | |
// Parse the trace data text into an object | |
const data = parseCloudflareTraceData(traceText); | |
return data; | |
}); | |
} | |
/* | |
usage | |
var metrics = { | |
geodata: null, | |
traceData: null | |
}; | |
if (GEOLOCATION_ALLOW) { | |
geolocation().then(function (geodata) { | |
metrics.geodata = geodata; | |
}).catch(function (error) { | |
console.log('Error getting geolocation data:', error); | |
}); | |
} | |
getCloudflareTraceData().then(function (traceData) { | |
// Set the resulting traceData to the metrics object | |
metrics.traceData = traceData; | |
}).catch(function (error) { | |
console.log('Error getting Cloudflare trace data:', error); | |
}); | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment