Coder Social home page Coder Social logo

sniffr's Introduction

sniff-sniff...

Browser, OS and device detection based on the available user agent string. Can be used both in a browser (also as a standalone script) or in a server environment.

it's very rarely a good idea to use user agent sniffing. You can almost always find a better, more broadly compatible way to solve your problem! MDN: Browser detection using the user agent

Note: Sniffr is written in Typescript and includes all the necessary typings, can be used both in JavaScript and Typescript projects

Why use it

In case some browser-specific issue cannot be fixed uniformly across browsers we may need to perform some browser detection. For example, browser X crashes when function Y from library Z is used, so we have to detect when we are dealing with browser X and disable library Z.

What is recognized

Browsers

  • Firefox
  • Internet Explorer
  • Edge
  • Chrome
  • Opera
  • Opera mini
  • Safari
  • Android Browser
  • BlackBerry Browser
  • Yandex Browser
  • SeaMonkey

Operating Systems

  • Windows
  • Linux
  • Mac OS
  • ChromeOS
  • iOS
  • Blackberry OS
  • OpenBSD
  • Android
  • Firefox OS
  • Windows Phone
  • Windows Mobile

Devices

  • iPad
  • iPhone
  • Galaxy
  • HTC
  • Nexus
  • Nokia
  • Lumia
  • Blackberry
  • XBox

Installation

NPM

To install the library use npm:

npm install sniffr

As a standalone script in a browser

Hosted version (by jsDelivr) can be found here (replace the version number) https://cdn.jsdelivr.net/gh/amoilanen/[email protected]/dist/sniffr.standalone.min.js

https://github.com/amoilanen/sniffr/blob/master/dist/sniffr.standalone.min.js is a downloadable minified version of the library to be used as a standalone script in a browser.

How to use in a browser

NPM

The library is can be directly used in a browser, no server-side code is run.

import { RecognizedBrowser } from "sniffr"

//If Windows and Firefox 28 or later
if (RecognizedBrowser.os.name === "windows"
  && RecognizedBrowser.browser.name === "firefox" && RecognizedBrowser.browser.version[0] >= 28) {
  //Apply some workaround
}

For backward compatibility purposes the following more wordy legacy usage pattern is also supported:

import Sniffr from "sniffr"

const sniffr = new Sniffr()
sniffr.sniff()

//If Windows and Firefox 28 or later
if (sniffr.os.name === "windows"
  && sniffr.browser.name === "firefox" && sniffr.browser.version[0] >= 28) {
  //Apply some workaround
}

As a standalone script

When the script is loaded Sniffr object will be initialized and put to the global namespace, it can be accessed directly:

//If Windows and Firefox 28 or later
if (Sniffr.os.name === "windows"
  && Sniffr.browser.name === "firefox" && Sniffr.browser.version[0] >= 28) {
  //Apply some workaround
}

API

  • RecognizedBrowser.os: operating system
  • RecognizedBrowser.browser: browser
  • RecognizedBrowser.device: device

Sniffr.sniff : function that expects a user agent string as an argument, it is called automatically in a browser

How to use on the server side

Sniffr can also be used in a Node.js environment in case you need to do some server-side user agent analysis as well.

First install it

npm install sniffr

Then load the module, provide it the agent string and query the results just like in a browser environment:

var Sniffr = require("sniffr").default;
var s = new Sniffr();

s.sniff("Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");

console.log("Operating System:");
console.log(s.os);
console.log("Browser:");
console.log(s.browser);
console.log("Device:");
console.log(s.device);

Other libraries

Some libraries like jQuery provide only browser information and not the OS information. Some like Detectizr are plugins for other libraries that you may not use. And some require server-side code. A few libraries are usable only on the server or only in a browser.

Sniffr provides simple and symmetric API, does not depend on other libraries, does not require the server part, is tiny, fast and easily extensible. In addition, it can be used both in browser and server environments.

Credits

The original sniffing dog image location is http://publicdomainvectors.org/en/free-clipart/Dog-sniffing-vector-image/11807.html

sniffr's People

Contributors

amoilanen avatar godofbrowser avatar tameraydin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sniffr's Issues

Detect engine

in addition to browser, os and device also detect the rendering engine, such as WebKit

Defining the exports object in the global environment breaks the libs on the client side

Hi.

The latest update in the library brings new lines of code:

if (typeof window !== 'undefined') { window.exports = window.exports || {} }

It's breaking the libraries these are defining the environment and putting itself on the object's dependence on the environment

var globalObject = typeof window === 'object' && window || typeof self === 'object' && self; if(typeof exports !== 'undefined') { factory(exports); } else if(globalObject) { globalObject.hljs = factory({}); }

In this case, all of the client libraries that do not bundling or importers as modules are defending in the exports global object, which is not expected. Because these should be defined in the global window object.

Opera in iOS

Hi Anton,

for Opera in iOS, Sniffr.browser.name returns 'UNKNOWN', as navigator.userAgent does not contain /opera/.

Since for Opera in iOS we have to expect a UA-String like

office lens_20160209_182051_processed

something like

navigator.userAgent.match( /OPiOS/i )

will work.

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.