How do I tell if a user is using Brave as their browser?

后端 未结 9 675
旧巷少年郎
旧巷少年郎 2021-02-04 01:29

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 simp

相关标签:
9条回答
  • 2021-02-04 02:18

    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>

    0 讨论(0)
  • 2021-02-04 02:18

    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;
        });
    }
    
    0 讨论(0)
  • 2021-02-04 02:20

    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.

    0 讨论(0)
提交回复
热议问题