Created
July 8, 2019 01:45
-
-
Save ZachMoreno/5c2f7f189e4bc3d3ba907b3d20bdeb84 to your computer and use it in GitHub Desktop.
The Brave Browser removed "Brave" from its User-Agent in v0.9. DuckDuckGo can detect Brave & this promise utilizes their API to detect Brave.
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
const detectBraveBrowser = () => { | |
return new Promise((resolve, reject) => { | |
if(!navigator.userAgent.includes('Chrome')) { return resolve(false); } | |
const xhr = new XMLHttpRequest(); | |
const onload = () => { | |
if(xhr.status >= 200 && xhr.status < 300) { | |
const response = JSON.parse(xhr.responseText); | |
if(!response) { return resolve(false); } | |
if(!response.Answer) { return resolve(false); } | |
if(!response.Answer.includes('Brave')) { return resolve(false); } | |
return resolve(true); | |
} else { | |
return reject(JSON.parse(xhr.responseText)); | |
} | |
}; | |
xhr.onload = onload; | |
xhr.open('GET', 'https://api.duckduckgo.com/?q=useragent&format=json'); | |
xhr.send(); | |
}); | |
}; | |
detectBraveBrowser().then((isBrave) => { | |
console.log('isBrave', isBrave); | |
}).catch((error) => { console.error(error); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had the same problem. You just have to put everything in an async function, then it should work.