Giter Club home page Giter Club logo

joncv / ua-parser-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from faisalman/ua-parser-js

0.0 2.0 0.0 1.95 MB

UAParser.js - Lightweight JavaScript-based user-agent string parser library to detect browser, layout engine, operating system, CPU architecture, and device type/model, entirely from user-agent string. Supports browser & node.js environment. Also available as jQuery/Zepto plugin, Bower/Meteor package, & RequireJS/AMD module.

Home Page: http://faisalman.github.io/ua-parser-js

JavaScript 100.00%

ua-parser-js's Introduction

UAParser.js

A not-so-lightweight-anymore JavaScript-based User-Agent string parser. Can be used either in-browser (client-side) or in a node.js (server-side) environment. Also available as a jQuery/Zepto plugin, Bower/Meteor package, & RequireJS/AMD module. This library aims to identify the web browser, layout engine, operating system, cpu architecture, and device type/model, entirely from the user-agent string with a relatively small footprint (~11KB when minified / ~4KB gzipped). Written in vanilla JavaScript, which means it doesn't require any other library and can be used independently.

Build Status NPM Bower CDNJS Gratipay Flattr this

Methods

  • getBrowser()
    • returns { name: '', version: '' }
# Possible 'browser.name':
Amaya, Android Browser, Arora, Avant, Baidu, Blazer, Bolt, Camino, Chimera, Chrome, 
Chromium, Comodo Dragon, Conkeror, Dillo, Dolphin, Doris, Edge, Epiphany, Fennec,
Firebird, Firefox, Flock, GoBrowser, iCab, ICE Browser, IceApe, IceCat, IceDragon, 
Iceweasel, IE [Mobile], Iron, Jasmine, K-Meleon, Konqueror, Kindle, Links, 
Lunascape, Lynx, Maemo, Maxthon, Midori, Minimo, MIUI Browser, [Mobile] Safari, 
Mosaic, Mozilla, Netfront, Netscape, NetSurf, Nokia, OmniWeb, Opera [Mini/Mobi/Tablet], 
PhantomJS, Phoenix, Polaris, QQBrowser, RockMelt, Silk, Skyfire, SeaMonkey, SlimBrowser,
Swiftfox, Tizen, UCBrowser, Vivaldi, w3m, WeChat, Yandex

# 'browser.version' determined dynamically
  • getDevice()
    • returns { model: '', type: '', vendor: '' }
# Possible 'device.type':
console, mobile, tablet, smarttv, wearable, embedded

# Possible 'device.vendor':
Acer, Alcatel, Amazon, Apple, Archos, Asus, BenQ, BlackBerry, Dell, GeeksPhone, 
Google, HP, HTC, Huawei, Jolla, Lenovo, LG, Meizu, Microsoft, Motorola, Nexian, 
Nintendo, Nokia, Nvidia, Ouya, Palm, Panasonic, Polytron, RIM, Samsung, Sharp, 
Siemens, Sony-Ericsson, Sprint, Xbox, ZTE

# 'device.model' determined dynamically
  • getEngine()
    • returns { name: '', version: '' }
# Possible 'engine.name'
Amaya, EdgeHTML, Gecko, iCab, KHTML, Links, Lynx, NetFront, NetSurf, Presto, 
Tasman, Trident, w3m, WebKit

# 'engine.version' determined dynamically
  • getOS()
    • returns { name: '', version: '' }
# Possible 'os.name'
AIX, Amiga OS, Android, Arch, Bada, BeOS, BlackBerry, CentOS, Chromium OS, Contiki,
Fedora, Firefox OS, FreeBSD, Debian, DragonFly, Gentoo, GNU, Haiku, Hurd, iOS, 
Joli, Linpus, Linux, Mac OS, Mageia, Mandriva, MeeGo, Minix, Mint, Morph OS, NetBSD, 
Nintendo, OpenBSD, OpenVMS, OS/2, Palm, PCLinuxOS, Plan9, Playstation, QNX, RedHat, 
RIM Tablet OS, RISC OS, Sailfish, Series40, Slackware, Solaris, SUSE, Symbian, Tizen, 
Ubuntu, UNIX, VectorLinux, WebOS, Windows [Phone/Mobile], Zenwalk

# 'os.version' determined dynamically
  • getCPU()
    • returns { architecture: '' }
# Possible 'cpu.architecture'
68k, amd64, arm, arm64, avr, ia32, ia64, irix, irix64, mips, mips64, pa-risc, 
ppc, sparc, sparc64
  • getResult()

    • returns { ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }
  • getUA()

    • returns UA string of current instance
  • setUA(uastring)

    • set & parse UA string

Example

<!doctype html>
<html>
<head>
<script type="text/javascript" src="ua-parser.min.js"></script>
<script type="text/javascript">

	var parser = new UAParser();

    // by default it takes ua string from current browser's window.navigator.userAgent
    console.log(parser.getResult());
    /*
        /// this will print an object structured like this:
        {
            ua: "",
            browser: {
                name: "",
                version: ""
            },
            engine: {
                name: "",
                version: ""
            },
            os: {
                name: "",
                version: ""
            },
            device: {
                model: "",
                type: "",
                vendor: ""
            },
            cpu: {
                architecture: ""
            }
        }
    */

    // let's test a custom user-agent string as an example
    var uastring = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2";
    parser.setUA(uastring);

    var result = parser.getResult();
    // this will also produce the same result (without instantiation):
    // var result = UAParser(uastring);

    console.log(result.browser);        // {name: "Chromium", version: "15.0.874.106"}
    console.log(result.device);         // {model: undefined, type: undefined, vendor: undefined}
    console.log(result.os);             // {name: "Ubuntu", version: "11.10"}
    console.log(result.os.version);     // "11.10"
    console.log(result.engine.name);    // "WebKit"
    console.log(result.cpu.architecture);   // "amd64"

    // do some other tests
    var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
    console.log(parser.setUA(uastring2).getBrowser().name); // "Konqueror"
    console.log(parser.getOS());                            // {name: "OpenBSD", version: undefined}
    console.log(parser.getEngine());                        // {name: "KHTML", version: "4.1.4"}

    var uastring3 = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';
    console.log(parser.setUA(uastring3).getDevice().model); // "PlayBook"
    console.log(parser.getOS())                             // {name: "RIM Tablet OS", version: "1.0.0"}
    console.log(parser.getBrowser().name);                  // "Safari"

</script>
</head>
<body>
</body>
</html>

Using node.js

$ npm install ua-parser-js
var http = require('http');
var parser = require('ua-parser-js');

http.createServer(function (req, res) {
    // get user-agent header
    var ua = parser(req.headers['user-agent']);
    // write the result as response
    res.end(JSON.stringify(ua, null, '  '));
})
.listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

Using requirejs

requirejs.config({
    baseUrl : 'js/lib', // path to your script directory
    paths   : {
        'ua-parser-js' : 'ua-parser.min'
    }
});

requirejs(['ua-parser-js'], function(UAParser) {
    var parser = new UAParser();
    console.log(parser.getResult());
});

Using bower

$ bower install ua-parser-js

Using meteor

$ meteor add faisalman:ua-parser-js

Using jQuery/Zepto ($.ua)

Although written in vanilla js (which means it doesn't depends on jQuery), this library will automatically detect if jQuery/Zepto is present and create $.ua object based on browser's user-agent (although in case you need, window.UAParser constructor is still present). To get/set user-agent you can use: $.ua.get() / $.ua.set(uastring).

// In browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0':

// Do some tests
console.log($.ua.device);           // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}
console.log($.ua.os);               // {name: "Android", version: "2.3.4"}
console.log($.ua.os.name);          // "Android"
console.log($.ua.get());            // "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"

// reset to custom user-agent
$.ua.set('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13');

// Test again
console.log($.ua.browser.name);     // "Safari"
console.log($.ua.engine.name);      // "Webkit"
console.log($.ua.device);           // {vendor: "Motorola", model: "Xoom", type: "tablet"}
console.log(parseInt($.ua.browser.version.split('.')[0], 10));  // 4

Extending regex patterns

  • UAParser(uastring[, extensions])

Pass your own regexes to extend the limited matching rules.

// Example:
var uaString = 'MyOwnBrowser/1.3';
var myOwnRegex = [[/(myownbrowser)\/([\w\.]+)/i], [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]];
var parser = new UAParser(uaString, { browser: myOwnRegex });
console.log(parser.getBrowser());   // {name: "MyOwnBrowser", version: "1.3"}

Development

Contribute

  • Fork and clone this repository
  • Make some changes as required
  • Write a unit test to showcase your feature
  • Run the test suites to make sure the changes you made didn't break anything
$ npm run test
  • Commit and push to your own repository
  • Submit a pull request to this repository under develop branch
  • Profit?

Build

Build a minified & packed script

$ npm run build

License

Dual licensed under GPLv2 & MIT

Copyright © 2012-2016 Faisal Salman <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

ua-parser-js's People

Contributors

0x7f avatar austinpray avatar boneyao avatar demis-palma avatar dhcmrlchtdj avatar divdavem avatar duzun avatar faisalman avatar hmvs avatar jackpoll avatar kennynaoh avatar leofiore avatar longsleep avatar malash avatar maurermax avatar mhess-connectify avatar mollases avatar nalgeon avatar nicole-ashley avatar otakusid avatar peterdavehello avatar philippsimon avatar piperchester avatar rossnoble avatar sgautreau avatar shanebo avatar simon-lang avatar startswithaj avatar sylvaingi avatar tantalor avatar

Watchers

 avatar  avatar

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.