Testing User Agent Strings are Outdated, Fingerprint instead

Ever need to detect which browser is running?

Usual way is to test the user-agent string but that, is sadly arbitrary and can be overwritten. Nay, one needs to finger print the browser based on the developer extensions.

Each browser adds their own parts to the dom, parsers, and css extensions. Most of the CSS extensions are well documented but below is a four line function that would give a more reliable results.

// use strict
// Copyright (c)2019 Dwight Spencer. All Rights Reserved
// Released under a modified BSD licence. https://opensource.org/licenses/BSD-3-Clause
const writeLn = (buffer) => document.querySelector("#output")
                      					   .innerHTML += buffer + '<br>';
                                    
function checkFor(name) {
  let re = new RegExp(`^${name}.*`),
       x = [];
  
  for (var key in document) if (re.test(key)) x.push(key)

  return (x.length) ? true : false
}

writeLn("Firefox: " +checkFor("moz"));
writeLn("Chrome: " +checkFor("webkit"));
writeLn("IE: " +checkFor("ms"));
writeLn("Opera: "+checkFor("opera"));

In testing both opera and MS Edge return true for webkit which shows a way to fingerprint a browser further.

2 Likes