Skip to content

Instantly share code, notes, and snippets.

@jasenmichael
Created October 30, 2021 22:26
Show Gist options
  • Save jasenmichael/6021ed318d491ca3424c138b01536508 to your computer and use it in GitHub Desktop.
Save jasenmichael/6021ed318d491ca3424c138b01536508 to your computer and use it in GitHub Desktop.
collect-data.html - ipData, browserInfo, os, date, location
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
;
(async () => {
// get browserInfo
let nVer = navigator.appVersion;
let nAgt = navigator.userAgent;
let browserName = navigator.appName;
let fullVersion = '' + parseFloat(navigator.appVersion);
let majorVersion = parseInt(navigator.appVersion, 10);
let nameOffset, verOffset, ix;
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset = nAgt.indexOf("Opera")) != -1) {
browserName = "Opera";
fullVersion = nAgt.substring(verOffset + 6);
if ((verOffset = nAgt.indexOf("Version")) != -1)
fullVersion = nAgt.substring(verOffset + 8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
browserName = "Microsoft Internet Explorer";
fullVersion = nAgt.substring(verOffset + 5);
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
browserName = "Chrome";
fullVersion = nAgt.substring(verOffset + 7);
}
// In Safari, the true version is after "Safari" or after "Version"
else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
browserName = "Safari";
fullVersion = nAgt.substring(verOffset + 7);
if ((verOffset = nAgt.indexOf("Version")) != -1)
fullVersion = nAgt.substring(verOffset + 8);
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
browserName = "Firefox";
fullVersion = nAgt.substring(verOffset + 8);
}
// In most other browsers, "name/version" is at the end of userAgent
else if ((nameOffset = nAgt.lastIndexOf(' ') + 1) <
(verOffset = nAgt.lastIndexOf('/'))) {
browserName = nAgt.substring(nameOffset, verOffset);
fullVersion = nAgt.substring(verOffset + 1);
if (browserName.toLowerCase() == browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix = fullVersion.indexOf(";")) != -1)
fullVersion = fullVersion.substring(0, ix);
if ((ix = fullVersion.indexOf(" ")) != -1)
fullVersion = fullVersion.substring(0, ix);
majorVersion = parseInt('' + fullVersion, 10);
if (isNaN(majorVersion)) {
fullVersion = '' + parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion, 10);
}
// end - get broswser info
// get os
var os = "Unknown OS";
if (navigator.appVersion.indexOf("Win") != -1) os = "Windows";
if (navigator.appVersion.indexOf("Mac") != -1) os = "MacOS";
if (navigator.appVersion.indexOf("X11") != -1) os = "UNIX";
if (navigator.appVersion.indexOf("Linux") != -1) os = "Linux";
// end - get os
// get location data
let locationData
navigator.geolocation.getCurrentPosition((res) => {
console.log('coords:', res.coords);
locationData = res.coords
}, (error) => {
locationData = `Error: ${error}`
}, {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
// end - get location data
// get ip info
const ipData = await $.get("https://ipinfo.io", response => response, "json")
const info = {
ipData,
browserInfo: {
browserName,
fullVersion,
majorVersion,
appName: navigator.appName,
userAgent: navigator.userAgent
},
os,
date: new Date(),
location: locationData
}
console.log('info', info);
})()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment