24
votes

I have been messing around with the Brave browser (https://www.brave.com/), an I cannot figure out how to determine how if a user is using Brave. I used a simple document to output the user agent:

<script>document.write(navigator.userAgent);</script>

and I get:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.108 Safari/537.36

which doesn't really help me in my situation. Does anyone know how to determine anyone using Brave in PHP or JavaScript? Thanks!

9
I'm unable to reproduce this on a mac. navigator.userAgent in the console of freshly downloaded Brave for mac from brave.com gives me: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) brave/0.9.0 Chrome/49.0.2623.108 Brave/0.37.3 Safari/537.36" (see the brave/0.9.0 part identifying that the browser is brave)lxe
I too am unable to reproduce this getting essentially the same UA string back.Ohgodwhy
udger.com/resources/ua-list - no user agent string given for brave, weird.Jack Hales
just pretend it's chrome; why would you need to know it's brave anyway? afaik, brave supports the same techs that chrome does. use feature detection if needed, not browser sniffing.dandavis
one use case for why you would want to know if its brave was if you had a page that autodetects which browser is visiting in order to display information regarding specific browser extensions for that browser. Although brave is built on chrome technology, brave only has a handful of officially sanctioned extensions at present, and installing non sanctioned extensions is a bit of a palaver at present.user280109

9 Answers

8
votes

Kohjah Breese's solution isn't working to detect Brave on Android (e.g. Chrome is detected as Brave). But as he said "If you search DuckDuckGo for [what's my user agent] they will return Brave." Then you can use the API of Duckduckgo to know if they browser is Brave :

var request = new XMLHttpRequest()

request.open('GET', 'https://api.duckduckgo.com/?q=useragent&format=json', true)

request.onload = function () {
  var data = JSON.parse(this.response)
  var isBrave = data['Answer'].includes('Brave');
  if(isBrave){
    console.log( isBrave );
  }
}

request.send()
17
votes

As of April 2020, you can use this detection method to get a boolean answer to whether the user is using Brave or not:

(navigator.brave && await navigator.brave.isBrave() || false)
15
votes

The "Brave" in the user agent was removed in the 0.9 version.

From the changelog:

Removed Brave from the User Agent HTTP header to reduce fingerprinting.

8
votes

There is a way to check if the user is using brave without any api calls. I created the below function that returns false or True if its Brave

function isBrave() {
  if (window.navigator.brave != undefined) {
    if (window.navigator.brave.isBrave.name == "isBrave") {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

document.getElementById("ans").innerHTML = isBrave();
Is this Brave? <span id="ans"></span>
7
votes

Brave appears has some different objects in the window object. I'm not sure how contiguous these are across Brave versions, but I noted two in the window.navigator object that are blanked out: plugins and mimeTypes. Since Brave is meant to be a privacy browser I think it's a good chance these will remain blanked. So my check is to check the length of those.

Please note that you also need to check for the browser being desktop first; it doesn't seem you can detect the Brave Mobile browser; and the below code will pick up many mobile browsers

var agent = navigator.userAgent.toLowerCase();
var isChrome = /chrome|crios/.test(agent) && ! /edge|opr\//.test(agent);
var isBrave = isChrome && window.navigator.plugins.length === 0 && window.navigator.mimeTypes.length === 0;
if(isBrave)
    console.log( isBrave );

If you search DuckDuckGo for [what's my user agent] they will return Brave. If you open the attached JS files you will find an elaborate browser detection that can detect Brave.

5
votes

Brave has the same user agent as Chrome. But Chrome itself add a lot (1768 as for now) of chrome-specific properties to window object. One of them is window.google. So detecting Brave is pretty simple (as for now):

const ua = window.navigator.userAgent.toLowerCase();
const isChrome = /chrome|crios/.test(ua) && ! /edge|opr\//.test(ua)
const isBrave = isChrome && ! window.google;

So brave, lol.

3
votes

Brave doesn’t have its own User-Agent, as pointed out in other answers. However, you can quite easily fingerprint it to differentiate it from Google Chrome. The current release, version 0.23.19, has at least 40 unique characteristics that can tell it apart from other browsers. I go into more detail on that in this article. However, this is a riddiculous sulution. Please just ask Brave to restore their own User-Agent string.

2
votes

2020. Updated solution:

Chrome now adds a new key to the window object: googletag, as opposed to the old one: google

This is the working code now:

const ua = window.navigator.userAgent.toLowerCase();
const isChrome = /chrome|crios/.test(ua) && ! /edge|opr\//.test(ua)
const isBrave = isChrome && ! window.googletag;
1
votes

Brave has a class Brave, which is created and added to navigator as navigator.brave upon page load. It has one method which returns a promise: Brave.prototype.isBrave() Using these three you can create a check for brave browser (something like this):

var isBrave = false;
if(window.Brave&&navigator.brave&&navigator.brave.isBrave){
    isBrave = 'probable';//or some other value, as you wish
    navigator.brave.isBrave().then(function(r){
        if(r)isBrave = true;
    });
}