Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Safe, reliable Browser sniffing
Mahmoud Elmahdi
Mahmoud Elmahdi

Posted on • Edited on

     

Safe, reliable Browser sniffing

We've recently built a WebExtension for our Web Application as Add-on and extra feature to sell 💰💸 (b/c why not). We decided to serve 4 different browser types:Chrome,Safari,Opera, andFirefox. So we had to figure out which browser is rendering our page to display a browser-specific instructions to the users.


You're lying to me. Aren't You?

Browser detection using the user agent is just sucks!! because it's trivial to spoof thisvalue. For example the snippet below:

navigator.userAgent.indexOf('Chrome')!==-1
Enter fullscreen modeExit fullscreen mode

returnstrue for bothGoogle Chrome andOpera (since Opera replaces its engine with Blink + V8 used by Chromium) because its UA string looks like Chrome. Thats is not what am looking for. And if we're trying to detect a specific browser, the point of feature-checking is kind of lost.

Check outMDN web docs: Browser detection using the user agent for futher details.

Top-level object FWT

window is the top-level object in theBrowser Object Model (BOM) hierarchy. Every single browser has its own properties such asApplePayError in Safari for instance, in addtion to the standard ones (e.g.window.location,window.console, ...etc).

Solution

/* * Browser detection * @return {String}  */constbrowserDetection=()=>{constbrowsers={firefox:!!window.InstallTrigger,safari:!!window.ApplePaySession,opera:window.opr&&!!window.opr.addons,chrome:window.chrome&&!!window.chrome.webstore};returnObject.keys(browsers).find(key=>browsers[key]===true);};console.log(browserDetection())// browser name expected
Enter fullscreen modeExit fullscreen mode
  • Firefox: The InstallTrigger interface is an interesting outlier in the Apps API.
  • Safari:ApplePaySession belongs to the Apple Pay JS API. A session object for managing the payment process on the web.
  • Opera:opr self explanatory..opr.addons represent interface in the Add-ons API
  • Chrome:chrome.webstore API to initiate app and extension installations "inline" from web page. Notice: this will bedeprecated, thanks toMadison Dickson. Any recommendations are welcome!

Tested in the following "Desktop Browsers":

Firefox Quantum Version ~60

Google Chrome Version ~67

Opera Version ~53

Safari Version ~11

Please keep in mind, that solution worked just perfectly in my case, andmight not fit yours.

Demo on Codepen


Feedback are welcome. If you have any suggestions or corrections to make, please do not hesitate to drop me a note/comment.

Top comments(6)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
mheiduk profile image
Marc Heiduk
  • Joined
• Edited on• Edited

For Chrome detection, try:

var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);

CollapseExpand
 
mix3d profile image
Madison Dickson
  • Joined

Keep in mind that the chrome.webstore check will be depreciated in about a month's time:blog.chromium.org/2018/06/improvin...

CollapseExpand
 
mahmoudelmahdi profile image
Mahmoud Elmahdi
Lead Software Engineer w/ a Designer's State of Mind
  • Location
    Vilnius, Lithuania
  • Joined

UPD. Thank you!

CollapseExpand
 
thejosemiller profile image
Joseph Miller
  • Joined

Hello Mahmoud :) Any update? regarding detecting chrome agent?

CollapseExpand
 
kirill_kolombet profile image
kolombet
  • Joined

var isChrome = !!window.chrome;
Probably you want to exclude chrome-based browsers, like opera
var isOpera = window["opr"] && !!window["opr"].addons;
var isChrome = !!window.chrome && !isOpera;

CollapseExpand
 
offirmo profile image
Offirmo
👨‍💻 Senior Fullstack Developer💛 all things JS and 💙 TS🆕 Creator of ⚔️ RPG browser games: 🔗 bit.ly/theboringrpg🆓 Open-source writer & contributor
  • Location
    Australia
  • Work
    Team Lead
  • Joined

Thanks, just what I needed.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Lead Software Engineer w/ a Designer's State of Mind
  • Location
    Vilnius, Lithuania
  • Joined

More fromMahmoud Elmahdi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp