-
-
Save Microtribute/4fb148b9487f1faae05c95a0731c4c7d to your computer and use it in GitHub Desktop.
Reliably detect Brave Browser with native JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helper to find Brave in User Agent string | |
function isBraveAgent(userAgentResponse) { | |
var isBraveIndex = ~userAgentResponse.indexOf('Brave') | |
if (isBraveIndex < 0) { | |
return true | |
} | |
return false | |
} | |
// Function from Javarome | |
// https://stackoverflow.com/questions/3760319/how-to-force-a-program-to-wait-until-an-http-request-is-finished-in-javascript | |
function httpRequest(address, reqType, asyncProc) { | |
var req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); | |
if (asyncProc) { | |
req.onreadystatechange = function() { | |
if (this.readyState == 4) { | |
asyncProc(this); | |
} | |
}; | |
} | |
req.open(reqType, address, !(!asyncProc)); | |
req.send(); | |
return req; | |
} | |
// Check reflected User Agent from whitelisted website (duckduckgo.com) | |
function checkIsBrave() { | |
var isBraveBrower = false | |
var req = httpRequest("https://api.duckduckgo.com/?q=whats+my+user+agent&format=json&pretty=1", "GET"); | |
var duckDuckResponse = JSON.parse(req.responseText) | |
var userAgentResponse = duckDuckResponse["Answer"] | |
var foundBraveInUserAgent = isBraveAgent(userAgentResponse) | |
if (foundBraveInUserAgent){ | |
isBraveBrower = true | |
} | |
return isBraveBrower | |
} | |
checkIsBrave() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment