Skip to content

Instantly share code, notes, and snippets.

@FelixWolf
Last active January 14, 2021 16:21
Show Gist options
  • Save FelixWolf/76876f0faadd17297c64ddc1d1dd3ffd to your computer and use it in GitHub Desktop.
Save FelixWolf/76876f0faadd17297c64ddc1d1dd3ffd to your computer and use it in GitHub Desktop.
Get navigator info, backwards compatible with browsers that do not support promises.
function getNavigatorInfo(callback){
if(!callback){
callback = function(result){
let myblob = new Blob([JSON.stringify(result, undefined, 4)], {type: 'text/plain'});
let bloburl = URL.createObjectURL(myblob);
let win = window.open(bloburl, '_blank');
win.focus();
setTimeout(function(){URL.revokeObjectURL(bloburl);},1000);
};
}
var result = {};
function syncronousData(){
//Simple
for(var key of ["appCodeName", "appName", "appVersion", "buildID", "cookieEnabled", "doNotTrack", "hardwareConcurrency", "language", "languages", "maxTouchPoints", "onLine", "oscpu", "platform", "product", "productSub", "userAgent", "vendor", "vendorSub", "webdriver"]){
result[key] = navigator[key];
}
result.location = {};
for(var key of ["hash","host","hostname","href","origin","pathname","port","protocol","search"]){
result.location[key] = document.location[key];
}
//Media Capabilities
navigator.mediaCapabilities = [];
if(navigator.mediaCapabilities){
//TODO: This is an asyncronous request, it needs to be handled else where.
}
//Plugins
result.plugins = [];
var pluginArray = []; //Used to have guarenteed enumerated plugins
for(var plugin of navigator.plugins){
pluginArray.push(plugin);
var pluginResult = {};
for(var pluginValue of ["description", "filename", "name", "version"]){
pluginResult[pluginValue] = plugin[pluginValue];
}
result.plugins.push(pluginResult);
}
//Mime types
result.mimeTypes = [];
if(navigator.mimeTypes){
for(var mimeType of navigator.mimeTypes){
var mimeTypeResult = {};
for(var mimeTypeValue of ["description", "suffixes", "type"]){
mimeTypeResult[mimeTypeValue] = mimeType[mimeTypeValue];
}
if(mimeType.enabledPlugin){
mimeTypeResult.enabledPlugin = -1;
for(var i=0;i<pluginArray.length;i++){
if(mimeType.enabledPlugin == pluginArray[i]){
mimeTypeResult.enabledPlugin = i;
break;
}
}
}else{
mimeTypeResult.enabledPlugin = null;
}
result.mimeTypes.push(mimeTypeResult);
}
}
return result;
}
//Media Devices
result.mediaDevices = {"devices": [], "constraints": []};
if(navigator.mediaDevices){
var constraints = navigator.mediaDevices.getSupportedConstraints();
for(var constraint in constraints){
if(constraints[constraint]){
result.mediaDevices.constraints.push(constraint);
}
}
navigator.mediaDevices.enumerateDevices().then(function(devices){
for(var device of devices){
var deviceInfo = {};
for(var deviceValue of ["deviceId", "groupId", "kind", "label"]){
deviceInfo[deviceValue] = device[deviceValue];
}
result.mediaDevices.devices.push(deviceInfo);
}
callback(syncronousData());
}).catch(function(err){
console.log(err.name + ": " + err.message);
callback(syncronousData());
});
}else{
callback(syncronousData());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment