├── .gitignore
├── .htaccess
├── .travis.yml
├── LICENSE
├── README.md
├── bootstrap.php
├── composer.json
├── detect.php
├── hooks
├── cache.php
└── log.php
├── index.php
└── templates
├── Gruntfile.js
├── dist
└── bundle.js
├── package.json
├── src
├── Browser.js
├── Device.js
├── Engine.js
├── Family.js
├── Os.js
├── Polyfills.js
├── Using.js
├── Version.js
└── WhichBrowser.js
└── test
├── Browser.js
├── Device.js
├── Engine.js
├── Os.js
├── Version.js
└── WhichBrowser.js
/.gitignore:
--------------------------------------------------------------------------------
1 | /package.json
2 | /Gruntfile.js
3 | node_modules/*
4 | templates/node_modules/*
5 |
--------------------------------------------------------------------------------
/.htaccess:
--------------------------------------------------------------------------------
1 | RewriteEngine on
2 | RewriteRule ^detect.js$ detect.php
3 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 |
3 | node_js:
4 | - "4.1"
5 | - "4.0"
6 | - "0.12"
7 |
8 | before_script:
9 | - cd templates
10 | - npm install -g grunt-cli
11 | - npm install
12 |
13 | script:
14 | - grunt test
15 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2010-2017 Niels Leenheer
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | This is an extremely complicated and almost completely useless browser sniffing library. Useless because you shouldn't use browser sniffing. So stop right now and go read something about feature detecting instead. I'm serious. Go away. You'll thank me later.
4 |
5 |
6 | WhichBrowser/Server
7 | ===================
8 |
9 | This project sets up a server using WhichBrowser that exposes an API for browser detection that can be used by JavaScript in the browser. This project uses the [WhichBrowser/Parser-PHP](https://github.com/WhichBrowser/Parser-PHP) library for the actual useragent sniffing.
10 |
11 | [](https://travis-ci.org/WhichBrowser/Server)
12 | [](https://packagist.org/packages/whichbrowser/server)
13 | [](https://packagist.org/packages/whichbrowser/server)
14 |
15 | [](https://twitter.com/whichbrowserlib)
16 |
17 | Also available:
18 |
19 | - [WhichBrowser/Parser-PHP](https://github.com/WhichBrowser/Parser-PHP)
20 | A PHP version of WhichBrowser for use on the server
21 |
22 | - [WhichBrowser/Parser-JavaScript](https://github.com/WhichBrowser/Parser-JavaScript)
23 | A JavaScript version of WhichBrowser for use with Node.js on the server
24 |
25 | ---
26 |
27 |
28 |
29 |
30 | Requirements
31 | -----------------
32 |
33 | The server should be able to handle PHP (5.4 or later) and included is a `.htaccess` file that instructs the server to use `detect.js` as an alias for `detect.php`. This is not required, but if your server does not support `.htaccess` files you may want to find a way to make your server do the same. Alternatively you could use the `detect.php` directly.
34 |
35 |
36 |
37 | How to install it
38 | -----------------
39 |
40 | You can install WhichBrowser by using Composer - the standard package manager for PHP. The package is called `whichbrowser/server`. This sets up all dependancies like the PHP parser library. Go to an empty directory on your webserver, like the `www` or `public_html` directory of the domain where you want to install WhichBrowser.
41 |
42 | composer create-project whichbrowser/server .
43 |
44 | This will install the server in the current directory and install the `whichbrowser/parser` dependancy. You can easily update the parser library by running a simple command.
45 |
46 | composer update
47 |
48 | You should run this command as often as possible. You might even want to consider setting up a cron job for this purpose.
49 |
50 |
51 |
52 | How to use it
53 | -------------
54 |
55 | The first step is to place a snippet of code on you webpage.
56 |
57 |
65 |
66 | Please make sure you change the URL of the detect.js file to point it to your own server.
67 |
68 | To get the exact snippet you need to include visit the directory in which you installed WhichBrowser in your browser.
69 |
70 | The second step is to create a new `WhichBrowser` object. This object will contain all the information the library could find about your browser.
71 |
72 | For example:
73 |
74 | result = new WhichBrowser();
75 |
76 |
77 | The variable `result` now contains an object which you can query for information. There are various ways to access the information.
78 |
79 |
80 | First of all, you can treat the object as a string to get a human readable identification:
81 |
82 | "You are using " + result
83 | // You are using Chrome 27 on Mac OS X 10.8.4
84 |
85 | If you need to, you can also explicitly typecast the object to a string
86 |
87 | String(result)
88 | result.toString()
89 |
90 |
91 | Or you can turn the object into JSON:
92 |
93 | JSON.stringify(result)
94 | // { "browser": {"name":"Chrome","version":{"value":"27"...
95 |
96 |
97 | Another possiblity is to query the object:
98 |
99 | result.isType('desktop')
100 | // true
101 |
102 | result.isType('mobile', 'tablet', 'media')
103 | // false
104 |
105 | result.isBrowser('Maxthon', '<', '4.0.5')
106 | // false
107 |
108 | result.isOs('iOS', '>=', '5')
109 | // false
110 |
111 | result.isEngine('Blink')
112 | // true
113 |
114 |
115 | You can also access these properties directly:
116 |
117 | result.browser
118 | // Chrome 27
119 |
120 | result.engine
121 | // Blink
122 |
123 | result.os
124 | // Mac OS X 10.8.4
125 |
126 |
127 | Or access parts of these properties directly:
128 |
129 | result.browser.name
130 | // Chrome
131 |
132 | result.browser.name + ' ' + String(result.browser.version)
133 | // Chrome 27
134 |
135 | result.browser.version.major
136 | // 27
137 |
138 | result.browser.version.minor
139 | // 0
140 |
141 | result.browser.version.original
142 | // 27.0.1453.110
143 |
144 | result.engine.name
145 | // Blink
146 |
147 |
148 | Finally you can also query versions directly:
149 |
150 | result.browser.version.is('>', 26)
151 | // true
152 |
153 | result.os.version.is('<', '10.7.4')
154 | // false
155 |
156 |
157 | API reference
158 | -------------
159 |
160 | ### The WhichBrowser object
161 |
162 | After a new `WhichBrowser` object is created, it contains a number of properties and functions. All of these properties are guaranteed to be present.
163 |
164 | **Properties:**
165 |
166 | * `browser`
167 | an object that contains information about the browser itself
168 | * `engine`
169 | an object that contains information about the rendering engine
170 | * `os`
171 | an object that contains information about the operating system
172 | * `device`
173 | an object that contains information about the device
174 |
175 | **Functions:**
176 |
177 | `isType(type [,type [,type [,type]]])`
178 | If a single argument is used, the function returns `true` if the argument matches the `type` propery of `device` obejct. It can use multiple arguments in which case the function returns `true` if one of the arguments matches. If none of the arguments matches, it returns `false`
179 |
180 | `isBrowser(name [, comparison, version])`
181 | Is used to query the `name` and `version` property of the `browser` object. The funcion can contain a single argument to a simple comparison based on `name`, or three arguments to compare both `name` and `version`. The first argument always contains the name of the browser. The second arguments is a string that can container either `<`, `<=`, `=`, `=>` or `>`. The third is an integer, float or string that contains the version. You can use versions like `10`, `10.7` or `'10.7.4'`. For more information about how version comparisons are performed, please see the `is()` function of the `Version` object.
182 |
183 | `isEngine(name [, comparison, version])`
184 | Is used to query the `name` and `version` property of the `engine` object. This function works in exactly the same way as `isBrowser`.
185 |
186 | `isOs(name [, comparison, version])`
187 | Is used to query the `name` and `version` property of the `os` object. This function works in exactly the same way as `isBrowser`.
188 |
189 |
190 | ### The browser object
191 |
192 | The `Browser` object is used for the `browser` property of the main `WhichBrowser` object and contains a number of properties. If a property is not applicable in this situation it will be null.
193 |
194 | **Properties:**
195 |
196 | * `name`
197 | a string containing the name of the browser
198 | * `version`
199 | a version object containing information about the version of the browser
200 | * `stock`
201 | a boolean, true if the browser is the default browser of the operating system, false otherwise
202 | * `channel`
203 | a string containing the distribution channel, ie. 'Nightly' or 'Next'.
204 | * `mode`
205 | a string that can contain the operating mode of the browser, ie. 'proxy'.
206 | * `hidden`
207 | a boolean that is true if the browser does not have a name and is the default of the operating system.
208 |
209 |
210 | ### The engine object
211 |
212 | The `Engine` object is used for the `engine` property of the main `WhichBrowser` object and contains a number of properties. If a property is not applicable in this situation it will be null.
213 |
214 | **Properties:**
215 |
216 | * `name`
217 | a string containing the name of the rendering engine
218 | * `version`
219 | a version object containing information about the version of the rendering engine
220 |
221 |
222 | ### The os object
223 |
224 | The `Os` object is used for the `os` property of the main `WhichBrowser` object and contains a number of properties. If a property is not applicable in this situation it will be null.
225 |
226 | **Properties:**
227 |
228 | * `name`
229 | a string containing the name of the operating system
230 | * `version`
231 | a version object containing information about the version of the operating system
232 |
233 |
234 | ### The device object
235 |
236 | The `Device` object is used for the `device` property of the main `WhichBrowser` object and contains a number of properties. If a property is not applicable in this situation it will be null.
237 |
238 | **Properties:**
239 |
240 | * `type`
241 | a string containing the type of the browser.
242 | * `identified`
243 | a boolean that is true if the device has been positively identified.
244 | * `manufacturer`
245 | a string containing the manufacturer of the device, ie. 'Apple' or 'Samsung'.
246 | * `model`
247 | as string containing the model of the device, ie. 'iPhone' or 'Galaxy S4'.
248 |
249 | The `type` property can contain any value from the following list:
250 |
251 | * desktop
252 | * mobile
253 | * tablet
254 | * gaming
255 | * headset
256 | * ereader
257 | * media
258 | * emulator
259 | * television
260 | * monitor
261 | * camera
262 | * signage
263 | * whiteboard
264 | * car
265 | * pos
266 | * bot
267 |
268 |
269 | ### The version object
270 |
271 | The `Version` object is used for the `version` property of the `browser`, `engine` and `os` object and contains a number of properties and functions. If a property is not applicable in this situation it will be null.
272 |
273 | **Properties:**
274 |
275 | * `original`
276 | a string containing the original version number.
277 | * `alias`
278 | a string containing an alias for the version number, ie. 'XP' for Windows '5.1'.
279 | * `details`
280 | an integer containing the number of digits of the version number that should be printed.
281 | * `major`
282 | an integer containing the major version number.
283 | * `minor`
284 | an integer containing the minor version number.
285 | * `type`
286 | a string containing a type indicator, ie. 'beta' or 'alpha'.
287 |
288 | **Functions:**
289 |
290 | `is(version)` or `is(comparison, version)`
291 | Using this function it is easy to compare a version to another version. If you specify only one argument, this function will return if the versions are the same. You can also specify two arguments, in that case the first argument contains the comparison operator, such as `<`, `<=`, `=`, `=>` or `>`. The second argument is the version you want to compare it to. You can use versions like `10`, `10.7` or `'10.7.4'`, but be aware that `10` is not the same as `10.0`. For example if our OS version is `10.7.4`:
292 |
293 | result.os.version.is('10.7.4')
294 | // true
295 |
296 | result.os.version.is('10.7')
297 | // true
298 |
299 | result.os.version.is('10')
300 | // true
301 |
302 | result.os.version.is('10.0')
303 | // false
304 |
305 | result.os.version.is('>', '10')
306 | // false
307 |
308 | result.os.version.is('>', '10.7')
309 | // false
310 |
311 | result.os.version.is('>', '10.7.3')
312 | // true
313 |
314 |
315 |
316 | License
317 | -------
318 |
319 | Copyright (c) 2015 Niels Leenheer
320 |
321 | Permission is hereby granted, free of charge, to any person obtaining
322 | a copy of this software and associated documentation files (the
323 | "Software"), to deal in the Software without restriction, including
324 | without limitation the rights to use, copy, modify, merge, publish,
325 | distribute, sublicense, and/or sell copies of the Software, and to
326 | permit persons to whom the Software is furnished to do so, subject to
327 | the following conditions:
328 |
329 | The above copyright notice and this permission notice shall be
330 | included in all copies or substantial portions of the Software.
331 |
332 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
333 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
334 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
335 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
336 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
337 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
338 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
339 |
--------------------------------------------------------------------------------
/bootstrap.php:
--------------------------------------------------------------------------------
1 | =5.4.0",
18 | "ralouphie/getallheaders": "~0.1.7",
19 | "whichbrowser/parser": "^2.0.20"
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/detect.php:
--------------------------------------------------------------------------------
1 | getallheaders());
12 | if (isset($_REQUEST['ua'])) $options['useragent'] = $_REQUEST['ua'];
13 | if (isset($_REQUEST['e'])) $options['engine'] = intval($_REQUEST['e']);
14 | if (isset($_REQUEST['f'])) $options['features'] = intval($_REQUEST['f']);
15 | if (isset($_REQUEST['w'])) $options['width'] = intval($_REQUEST['w']);
16 | if (isset($_REQUEST['h'])) $options['height'] = intval($_REQUEST['h']);
17 |
18 | $parser = new WhichBrowser\Parser();
19 | include('hooks/cache.php');
20 |
21 | $parser->analyse($options);
22 | include('hooks/log.php');
23 |
24 | $template = file_get_contents(__DIR__ . '/templates/dist/bundle.js');
25 | echo preg_replace('/\_\=[\'"][\'"].*\_\=[\'"][\'"]/s', $parser->toJavaScript(), $template);
26 |
--------------------------------------------------------------------------------
/hooks/cache.php:
--------------------------------------------------------------------------------
1 | addServer('localhost', 11211);
11 | // $pool = new \Cache\Adapter\Memcached\MemcachedCachePool($client);
12 | // $parser->setCache($pool);
13 |
--------------------------------------------------------------------------------
/hooks/log.php:
--------------------------------------------------------------------------------
1 | browser->getName()) . "',
19 | // browserVersion = '" . mysql_real_escape_string($parser->browser->getVersion()) . "',
20 | // engineName = '" . mysql_real_escape_string($parser->engine->getName()) . "',
21 | // engineVersion = '" . mysql_real_escape_string($parser->engine->getVersion()) . "',
22 | // osName = '" . mysql_real_escape_string($parser->os->getName()) . "',
23 | // osVersion = '" . mysql_real_escape_string($parser->os->getVersion()) . "',
24 | // deviceManufacturer = '" . mysql_real_escape_string($detected->device->getManufacturer()) . "',
25 | // deviceModel = '" . mysql_real_escape_string($detected->device->getModel()) . "',
26 | // deviceType = '" . mysql_real_escape_string($detected->getType()) . "'
27 | // ");
28 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | WhichBrowser
7 |
8 |
16 |
17 |
109 |
110 |
111 |
112 |
133 |
134 |
135 | WhichBrowser
136 |
137 |
138 |
139 | Place the following script tag on your site:
140 |
141 |
142 | <script>
143 | (function(){var p=[],w=window,d=document,e=f=0;p.push('ua='+encodeURIComponent(navigator.userAgent));e|=w.ActiveXObject?1:0;e|=w.opera?2:0;e|=w.chrome?4:0;
144 | e|='getBoxObjectFor' in d || 'mozInnerScreenX' in w?8:0;e|=('WebKitCSSMatrix' in w||'WebKitPoint' in w||'webkitStorageInfo' in w||'webkitURL' in w)?16:0;
145 | e|=(e&16&&({}.toString).toString().indexOf("\n")===-1)?32:0;p.push('e='+e);f|='sandbox' in d.createElement('iframe')?1:0;f|='WebSocket' in w?2:0;
146 | f|=w.Worker?4:0;f|=w.applicationCache?8:0;f|=w.history && history.pushState?16:0;f|=d.documentElement.webkitRequestFullScreen?32:0;f|='FileReader' in w?64:0;
147 | p.push('f='+f);p.push('r='+Math.random().toString(36).substring(7));p.push('w='+screen.width);p.push('h='+screen.height);var s=d.createElement('script');
148 | s.src='///detect.?' + p.join('&');d.getElementsByTagName('head')[0].appendChild(s);})();
149 | </script>
150 |
151 |
152 | After the script has been loaded, you need to create a new WhichBrowser object, which you can query for details about the browser that is currently being used:
153 |
154 |
155 |
156 |
157 |
208 |
209 |
210 |
211 | Unfortunately WhichBrowser does not appear to be installed properly
212 |
213 |
214 |
215 |
216 |
217 |
--------------------------------------------------------------------------------
/templates/Gruntfile.js:
--------------------------------------------------------------------------------
1 | module.exports = function(grunt) {
2 | grunt.initConfig({
3 | pkg: grunt.file.readJSON('package.json'),
4 |
5 | browserify: {
6 | bundle: {
7 | src: 'src/WhichBrowser.js',
8 | dest: 'dist/bundle.js',
9 |
10 | options: {
11 | browserifyOptions: {
12 | standalone: 'WhichBrowser'
13 | }
14 | }
15 | }
16 | },
17 |
18 | uglify: {
19 | bundle: {
20 | src: 'dist/bundle.js',
21 | dest: 'dist/bundle.js',
22 |
23 | options: {
24 | mangle: false,
25 | preserveComments: 'some'
26 | }
27 | }
28 | },
29 |
30 | mochaTest: {
31 | test: {
32 | src: ['test/*.js']
33 | }
34 | },
35 |
36 | jshint: {
37 | all: ['src/*.js']
38 | }
39 | });
40 |
41 | grunt.loadNpmTasks('grunt-browserify');
42 | grunt.loadNpmTasks('grunt-contrib-uglify');
43 | grunt.loadNpmTasks('grunt-contrib-jshint');
44 | grunt.loadNpmTasks('grunt-mocha-test');
45 |
46 | grunt.registerTask('build', ['browserify:bundle', 'uglify:bundle']);
47 | grunt.registerTask('test', ['jshint', 'mochaTest']);
48 | };
49 |
--------------------------------------------------------------------------------
/templates/dist/bundle.js:
--------------------------------------------------------------------------------
1 | !function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.WhichBrowser=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o0){var operator="=",compare=null;if(1==arguments.length&&(compare=new Version({value:arguments[0]})),arguments.length>=2&&(operator=arguments[0],compare=new Version({value:arguments[1]})),compare){var v1="",v2="";switch(compare.major&&this.major&&(v1+=this.major,v2+=compare.major,compare.minor&&this.minor&&(v1+="."+("0000"+this.minor).slice(-4),v2+="."+("0000"+compare.minor).slice(-4),compare.revision&&this.revision&&(v1+=("0000"+this.revision).slice(-4),v2+=("0000"+compare.revision).slice(-4)))),v1=parseFloat(v1),v2=parseFloat(v2),operator){case"<":valid=v2>v1;break;case"<=":valid=v2>=v1;break;case"=":valid=v1==v2;break;case">":valid=v1>v2;break;case">=":valid=v1>=v2}}return valid}return!1},valueOf:function(){return parseFloat(""+this.major+"."+("0000"+this.minor).slice(-4)+("0000"+this.revision).slice(-4))},toJSON:function(){var o={value:this.toString(),details:this.details,hidden:this.hidden,original:this.original,major:this.major,minor:this.minor,build:this.build,revision:this.revision};return this.type&&(o.type=this.type),this.alias&&(o.alias=this.alias),this.nickname&&(o.nickname=this.nickname),o},toString:function(){if(this.alias)return this.alias;var version="";if(this.nickname&&(version+=this.nickname+" "),this.major||this.minor){var v=[];if(v.push(this.major),""!==this.minor&&null!==this.minor&&v.push(this.minor),""!==this.revision&&null!==this.revision&&v.push(this.revision),""===this.type&&this.build&&v.push(this.build),this.details<0&&v.splice(this.details,0-this.details),this.details>0&&v.splice(this.details,v.length-this.details),!this.builds)for(var i=0;i999&&(v.splice(i,1),i--);version+=v.join("."),""!==this.type&&(version+=this.type[0]+(this.build?this.build:""))}return version}},module.exports=Version},{}],9:[function(require,module,exports){require("./Polyfills");var Browser=require("./Browser"),Engine=require("./Engine"),Os=require("./Os"),Device=require("./Device"),Family=require("./Family"),Using=require("./Using"),Version=require("./Version"),WhichBrowser=function(){this.initialize.apply(this,arguments)};WhichBrowser.prototype={initialize:function(options){this.options={},_="",this.browser=new Browser({using:new Using({})}),this.engine=new Engine({}),this.os=new Os({family:new Family({}),version:new Version({})}),this.device=new Device({}),this.camouflage=!1,this.features=[],_=""},a:function(s){return(/^[aeiou]/i.test(s)?"an ":"a ")+s},isX:function(){var valid=!0,x=arguments[0];return arguments.length>=2&&(valid=valid&&this[x].name==arguments[1]),arguments.length>=4&&this[x].version&&valid&&(valid=valid&&this[x].version.is(arguments[2],arguments[3])),valid},isBrowser:function(){var a=Array.prototype.slice.call(arguments);return a.unshift("browser"),this.isX.apply(this,a)},isEngine:function(){var a=Array.prototype.slice.call(arguments);return a.unshift("engine"),this.isX.apply(this,a)},isOs:function(){var a=Array.prototype.slice.call(arguments);return a.unshift("os"),this.isX.apply(this,a)},isDevice:function(d){return this.device.series==d||this.device.model==d},isType:function(){for(var valid=!1,a=0;a 0) {
52 | var operator = '=';
53 | var compare = null;
54 |
55 | if (arguments.length == 1) {
56 | compare = new Version({ value: arguments[0] });
57 | }
58 |
59 | if (arguments.length >= 2) {
60 | operator = arguments[0];
61 | compare = new Version({ value: arguments[1] });
62 | }
63 |
64 | if (compare) {
65 | var v1 = '', v2 = '';
66 |
67 | if (compare.major && this.major) {
68 | v1 += this.major;
69 | v2 += compare.major;
70 |
71 | if (compare.minor && this.minor) {
72 | v1 += '.' + ('0000' + this.minor).slice(-4);
73 | v2 += '.' + ('0000' + compare.minor).slice(-4);
74 |
75 | if (compare.revision && this.revision) {
76 | v1 += ('0000' + this.revision).slice(-4);
77 | v2 += ('0000' + compare.revision).slice(-4);
78 | }
79 | }
80 | }
81 |
82 | v1 = parseFloat(v1);
83 | v2 = parseFloat(v2);
84 |
85 | switch (operator) {
86 | case '<': valid = v1 < v2; break;
87 | case '<=': valid = v1 <= v2; break;
88 | case '=': valid = v1 == v2; break;
89 | case '>': valid = v1 > v2; break;
90 | case '>=': valid = v1 >= v2; break;
91 | }
92 | }
93 |
94 | return valid;
95 | }
96 |
97 | return false;
98 | },
99 |
100 | valueOf: function() {
101 | return parseFloat('' + this.major + '.' + ('0000' + this.minor).slice(-4) + ('0000' + this.revision).slice(-4));
102 | },
103 |
104 | toJSON: function() {
105 | var o = {
106 | value: this.toString(),
107 | details: this.details,
108 | hidden: this.hidden,
109 | original: this.original,
110 | major: this.major,
111 | minor: this.minor,
112 | build: this.build,
113 | revision: this.revision
114 | };
115 |
116 | if (this.type) o.type = this.type;
117 | if (this.alias) o.alias = this.alias;
118 | if (this.nickname) o.nickname = this.nickname;
119 |
120 | return o;
121 | },
122 |
123 | toString: function() {
124 | if (this.alias)
125 | return this.alias;
126 |
127 | var version = '';
128 |
129 | if (this.nickname) {
130 | version += this.nickname + ' ';
131 | }
132 |
133 | if (this.major || this.minor) {
134 | var v = [];
135 | v.push(this.major);
136 | if (this.minor !== '' && this.minor !== null) v.push(this.minor);
137 | if (this.revision !== '' && this.revision !== null) v.push(this.revision);
138 | if (this.type === '' && this.build) v.push(this.build);
139 | if (this.details < 0) v.splice(this.details, 0 - this.details);
140 | if (this.details > 0) v.splice(this.details, v.length - this.details);
141 |
142 | if (!this.builds) {
143 | for (var i = 0; i < v.length; i++) {
144 | if (v[i] > 999) {
145 | v.splice(i, 1);
146 | i--;
147 | }
148 | }
149 | }
150 |
151 | version += v.join('.');
152 |
153 | if (this.type !== '') version += this.type[0] + (this.build ? this.build : '');
154 | }
155 |
156 | return version;
157 | }
158 | };
159 |
160 | module.exports = Version;
161 |
--------------------------------------------------------------------------------
/templates/src/WhichBrowser.js:
--------------------------------------------------------------------------------
1 | require('./Polyfills');
2 |
3 | var Browser = require('./Browser');
4 | var Engine = require('./Engine');
5 | var Os = require('./Os');
6 | var Device = require('./Device');
7 | var Family = require('./Family');
8 | var Using = require('./Using');
9 | var Version = require('./Version');
10 |
11 |
12 | var WhichBrowser = function() { this.initialize.apply(this, arguments); };
13 | WhichBrowser.prototype = {
14 | initialize: function(options) {
15 | this.options = {
16 | };
17 |
18 | _='';
19 | this.browser = new Browser({ using: new Using({}) });
20 | this.engine = new Engine({});
21 | this.os = new Os({ family: new Family({}), version: new Version({}) });
22 | this.device = new Device({});
23 | this.camouflage = false;
24 | this.features = [];
25 | _='';
26 | },
27 |
28 | a: function(s) {
29 | return (/^[aeiou]/i.test(s) ? 'an ' : 'a ') + s;
30 | },
31 |
32 | isX: function() {
33 | var valid = true;
34 | var x = arguments[0];
35 |
36 | if (arguments.length >= 2) {
37 | valid = valid && this[x].name == arguments[1];
38 | }
39 |
40 | if (arguments.length >= 4 && this[x].version && valid) {
41 | valid = valid && this[x].version.is(arguments[2], arguments[3]);
42 | }
43 |
44 | return valid;
45 | },
46 |
47 | isBrowser: function() { var a = Array.prototype.slice.call(arguments); a.unshift('browser'); return this.isX.apply(this, a); },
48 | isEngine: function() { var a = Array.prototype.slice.call(arguments); a.unshift('engine'); return this.isX.apply(this, a); },
49 | isOs: function() { var a = Array.prototype.slice.call(arguments); a.unshift('os'); return this.isX.apply(this, a); },
50 |
51 | isDevice: function(d) {
52 | return this.device.series == d || this.device.model == d;
53 | },
54 |
55 | isType: function() {
56 | var valid = false;
57 | for (var a = 0; a < arguments.length; a++) valid = valid || arguments[a] == this.device.type;
58 | return valid;
59 | },
60 |
61 | toJSON: function() {
62 | return {
63 | browser: this.browser.toJSON(),
64 | os: this.os.toJSON(),
65 | engine: this.engine.toJSON(),
66 | device: this.device.toJSON()
67 | };
68 | },
69 |
70 | toString: function() {
71 | var prefix = this.camouflage ? 'an unknown browser that imitates ' : '';
72 | var browser = this.browser.toString();
73 | var os = this.os.toString();
74 | var engine = this.engine.toString();
75 | var device = this.device.toString();
76 |
77 | if (!device && !os && this.device.type == 'television') device = 'television';
78 | if (!device && this.device.type == 'emulator') device = 'emulator';
79 |
80 | if (browser && os && device) return prefix + browser + ' on ' + this.a(device) + ' running ' + os;
81 | if (browser && !os && device) return prefix + browser + ' on ' + this.a(device);
82 | if (browser && os && !device) return prefix + browser + ' on ' + os;
83 | if (!browser && os && device) return prefix + this.a(device) + ' running ' + os;
84 | if (browser && !os && !device) return prefix + browser;
85 | if (!browser && !os && device) return prefix + this.a(device);
86 | if (this.device.type == 'desktop' && os && engine !== '' && !device) return 'an unknown browser based on ' + engine + ' running on ' + os;
87 | if (this.browser.stock && os && !device) return os;
88 | if (this.browser.stock && engine !== '' && !device) return 'an unknown browser based on ' + engine;
89 |
90 | return 'an unknown browser';
91 | }
92 | };
93 |
94 | module.exports = WhichBrowser;
95 |
--------------------------------------------------------------------------------
/templates/test/Browser.js:
--------------------------------------------------------------------------------
1 | var Browser = require ('../src/Browser');
2 | var Family = require ('../src/Family');
3 | var Using = require ('../src/Using');
4 | var Version = require ('../src/Version');
5 | var assert = require('assert');
6 |
7 | describe('Browser', function() {
8 | describe('name = Safari, version = [ value = 9.1 ]', function() {
9 | var result = new Browser({
10 | name: "Safari",
11 | version: new Version({
12 | value: "9.1"
13 | })
14 | });
15 |
16 | describe('#toString()', function () {
17 | it('should be equal to "Safari 9.1"', function () {
18 | assert.equal("Safari 9.1", result.toString());
19 | });
20 | });
21 | });
22 |
23 | describe('name = Opera, version = [ value = 35.0 ], family = []', function() {
24 | var result = new Browser({
25 | name: "Opera",
26 | version: new Version({
27 | value: "35.0.2066.68",
28 | details: 2
29 | }),
30 | family: new Family({
31 | name: "Chrome",
32 | version: new Version({
33 | value: 48
34 | })
35 | })
36 | });
37 |
38 | describe('#toString()', function () {
39 | it('should be equal to "Opera 35.0"', function () {
40 | assert.equal("Opera 35.0", result.toString());
41 | });
42 | });
43 | });
44 |
45 | describe('using = [ name = Electron, version = [ value = 0.33.4 ] ]', function() {
46 | var result = new Browser({
47 | using: new Using({
48 | name: "Electron",
49 | version: new Version({
50 | value: '0.33.4'
51 | })
52 | })
53 | });
54 |
55 | describe('#toString()', function () {
56 | it('should be equal to "Electron 0.33.4"', function () {
57 | assert.equal("Electron 0.33.4", result.toString());
58 | });
59 | });
60 | });
61 |
62 | describe('using = [ name = BlackBerry Browser, hidden = true ]', function() {
63 | var result = new Browser({
64 | name: "BlackBerry Browser",
65 | hidden: true
66 | });
67 |
68 | describe('#toString()', function () {
69 | it('should be equal to ""', function () {
70 | assert.equal("", result.toString());
71 | });
72 | });
73 | });});
74 |
--------------------------------------------------------------------------------
/templates/test/Device.js:
--------------------------------------------------------------------------------
1 | var Device = require ('../src/Device');
2 | var assert = require('assert');
3 |
4 | describe('Device', function() {
5 | describe('model = SM-G930, identified = false', function() {
6 | var result = new Device({
7 | model: "SM-G930",
8 | identified: false
9 | });
10 |
11 | describe('#toString()', function () {
12 | it('should be equal to "unrecognized device (SM-G930)"', function () {
13 | assert.equal("unrecognized device (SM-G930)", result.toString());
14 | });
15 | });
16 | });
17 |
18 | describe('model = SM-G930, identified = true', function() {
19 | var result = new Device({
20 | model: "SM-G930",
21 | identified: true
22 | });
23 |
24 | describe('#toString()', function () {
25 | it('should be equal to "SM-G930"', function () {
26 | assert.equal("SM-G930", result.toString());
27 | });
28 | });
29 | });
30 |
31 | describe('manufacturer = Samsung, model = Galaxy S7, identified = true', function() {
32 | var result = new Device({
33 | manufacturer: "Samsung",
34 | model: "Galaxy S7",
35 | identified: true
36 | });
37 |
38 | describe('#toString()', function () {
39 | it('should be equal to "Samsung Galaxy S7"', function () {
40 | assert.equal("Samsung Galaxy S7", result.toString());
41 | });
42 | });
43 | });
44 |
45 | describe('manufacturer = Apple, model = AppleTV, identified = true', function() {
46 | var result = new Device({
47 | manufacturer: "Apple",
48 | model: "AppleTV",
49 | identified: true
50 | });
51 |
52 | describe('#toString()', function () {
53 | it('should be equal to "AppleTV"', function () {
54 | assert.equal("AppleTV", result.toString());
55 | });
56 | });
57 | });
58 |
59 | describe('manufacturer = Sony, model = Bravia 2015, series = SmartTV identified = true', function() {
60 | var result = new Device({
61 | manufacturer: "Sony",
62 | model: "Bravia 2015",
63 | series: "SmartTV",
64 | identified: true
65 | });
66 |
67 | describe('#toString()', function () {
68 | it('should be equal to "Sony Bravia 2015 SmartTV"', function () {
69 | assert.equal("Sony Bravia 2015 SmartTV", result.toString());
70 | });
71 | });
72 | });
73 |
74 | describe('manufacturer = Apple, model = Macintosh, hidden = true', function() {
75 | var result = new Device({
76 | manufacturer: "Apple",
77 | model: "Macintosh",
78 | hidden: true
79 | });
80 |
81 | describe('#toString()', function () {
82 | it('should be equal to ""', function () {
83 | assert.equal("", result.toString());
84 | });
85 | });
86 | });
87 |
88 | });
89 |
--------------------------------------------------------------------------------
/templates/test/Engine.js:
--------------------------------------------------------------------------------
1 | var Engine = require ('../src/Engine');
2 | var Version = require ('../src/Version');
3 | var assert = require('assert');
4 |
5 | describe('Engine', function() {
6 | describe('name = Blink', function() {
7 | var result = new Engine({
8 | name: "Blink"
9 | });
10 |
11 | describe('#toString()', function () {
12 | it('should be equal to "Blink"', function () {
13 | assert.equal("Blink", result.toString());
14 | });
15 | });
16 | });
17 |
18 | describe('name = Webkit, version = [ value = 601.5.13 ]', function() {
19 | var result = new Engine({
20 | name: "Webkit",
21 | version: new Version({
22 | value: "601.5.13"
23 | })
24 | });
25 |
26 | describe('#toString()', function () {
27 | it('should be equal to "Webkit"', function () {
28 | assert.equal("Webkit", result.toString());
29 | });
30 | });
31 | });
32 | });
33 |
--------------------------------------------------------------------------------
/templates/test/Os.js:
--------------------------------------------------------------------------------
1 | var Os = require ('../src/Os');
2 | var Version = require ('../src/Version');
3 | var assert = require('assert');
4 |
5 | describe('Os', function() {
6 | describe('name = OS X, alias = Mac OS X, version = [ value = 10.5.2, details = 2 ]', function() {
7 | var result = new Os({
8 | name: "OS X",
9 | alias: "Mac OS X",
10 | version: new Version({
11 | value: "10.5.2",
12 | details: 2
13 | })
14 | });
15 |
16 | describe('#toString()', function () {
17 | it('should be equal to "Mac OS X 10.5"', function () {
18 | assert.equal("Mac OS X 10.5", result.toString());
19 | });
20 | });
21 | });
22 |
23 | describe('name = OS X, version = [ value = 10.11.4, nickname = El Capitan, details = 2 ]', function() {
24 | var result = new Os({
25 | name: "OS X",
26 | version: new Version({
27 | value: "10.11.4",
28 | nickname: "El Capitan",
29 | details: 2
30 | })
31 | });
32 |
33 | describe('#toString()', function () {
34 | it('should be equal to "OS X El Capitan 10.11"', function () {
35 | assert.equal("OS X El Capitan 10.11", result.toString());
36 | });
37 | });
38 | });
39 |
40 | describe('name = Windows, version = [ value = 5.1, alias = XP, details = 2 ]', function() {
41 | var result = new Os({
42 | name: "Windows",
43 | version: new Version({
44 | value: "5.1",
45 | alias: "XP",
46 | details: 2
47 | })
48 | });
49 |
50 | describe('#toString()', function () {
51 | it('should be equal to "Windows XP"', function () {
52 | assert.equal("Windows XP", result.toString());
53 | });
54 | });
55 | });
56 |
57 | describe('name = webOS, hidden = true ]', function() {
58 | var result = new Os({
59 | name: "webOS",
60 | hidden: true
61 | });
62 |
63 | describe('#toString()', function () {
64 | it('should be equal to ""', function () {
65 | assert.equal("", result.toString());
66 | });
67 | });
68 | });
69 | });
70 |
--------------------------------------------------------------------------------
/templates/test/Version.js:
--------------------------------------------------------------------------------
1 | var Version = require ('../src/Version');
2 | var assert = require('assert');
3 |
4 | describe('Version', function() {
5 | describe('value = 10.11.4, nickname = El Capitan, details = 2', function() {
6 | var result = new Version({
7 | value: "10.11.4",
8 | nickname: "El Capitan",
9 | details: 2
10 | });
11 |
12 | describe('#is(x)', function () {
13 | it('should be true for 10', function () {
14 | assert.equal(true, result.is(10));
15 | });
16 |
17 | it('should be true for 10.11', function () {
18 | assert.equal(true, result.is('10.11'));
19 | });
20 |
21 | it('should be true for 10.11.4', function () {
22 | assert.equal(true, result.is('10.11.4'));
23 | });
24 |
25 | it('should be false for 10.11.3', function () {
26 | assert.equal(false, result.is('10.11.3'));
27 | });
28 |
29 | it('should be false for 10.10', function () {
30 | assert.equal(false, result.is('10.10'));
31 | });
32 |
33 | it('should be false for 11', function () {
34 | assert.equal(false, result.is(11));
35 | });
36 | });
37 |
38 | describe('#is(">", x)', function () {
39 | it('should be false for 10', function () {
40 | assert.equal(false, result.is('>', 10));
41 | });
42 |
43 | it('should be false for 10.11', function () {
44 | assert.equal(false, result.is('>', '10.11'));
45 | });
46 |
47 | it('should be true for 10.11.0', function () {
48 | assert.equal(true, result.is('>', '10.11.0'));
49 | });
50 |
51 | it('should be true for 10.10', function () {
52 | assert.equal(true, result.is('>', '10.10'));
53 | });
54 | });
55 |
56 | describe('#is("<", x)', function () {
57 | it('should be false for 10', function () {
58 | assert.equal(false, result.is('<', 10));
59 | });
60 |
61 | it('should be false for 10.11', function () {
62 | assert.equal(false, result.is('<', '10.11'));
63 | });
64 |
65 | it('should be true for 10.11.8', function () {
66 | assert.equal(true, result.is('<', '10.11.8'));
67 | });
68 |
69 | it('should be true for 10.12', function () {
70 | assert.equal(true, result.is('<', '10.12'));
71 | });
72 | });
73 |
74 | describe('#valueOf', function () {
75 | it('should be a number', function () {
76 | assert.equal('number', typeof result.valueOf());
77 | });
78 |
79 | it('should be 10.00110004', function () {
80 | assert.equal(10.00110004, result.valueOf());
81 | });
82 | });
83 |
84 | describe('#toString()', function () {
85 | it('should be equal to "El Capitan 10.11"', function () {
86 | assert.equal("El Capitan 10.11", result.toString());
87 | });
88 | });
89 |
90 | describe('#JSON.stringify()', function () {
91 | it('should be equal to {...}', function () {
92 | assert.equal('{"value":"El Capitan 10.11","details":2,"hidden":null,"original":"10.11.4","major":"10","minor":"11","build":null,"revision":"4","nickname":"El Capitan"}', JSON.stringify(result));
93 | });
94 | });
95 | });
96 |
97 | describe('value = 5.1, alias = XP, details = 2', function() {
98 | var result = new Version({
99 | value: "5.1",
100 | alias: "XP",
101 | details: 2
102 | });
103 |
104 | describe('#toString()', function () {
105 | it('should be equal to "XP"', function () {
106 | assert.equal("XP", result.toString());
107 | });
108 | });
109 |
110 | describe('#JSON.stringify()', function () {
111 | it('should be equal to {...}', function () {
112 | assert.equal('{"value":"XP","details":2,"hidden":null,"original":"5.1","major":"5","minor":"1","build":null,"revision":null,"alias":"XP"}', JSON.stringify(result));
113 | });
114 | });
115 | });
116 | });
117 |
--------------------------------------------------------------------------------
/templates/test/WhichBrowser.js:
--------------------------------------------------------------------------------
1 | var WhichBrowser = require ('../src/WhichBrowser');
2 | var assert = require('assert');
3 |
4 | describe('WhichBrowser', function() {
5 | var result = new WhichBrowser();
6 |
7 | describe('#browser', function () {
8 | it('should be defined', function () {
9 | assert.equal(true, typeof result.browser !== 'undefined');
10 | });
11 | });
12 |
13 | describe('#engine', function () {
14 | it('should be defined', function () {
15 | assert.equal(true, typeof result.engine !== 'undefined');
16 | });
17 | });
18 |
19 | describe('#os', function () {
20 | it('should be defined', function () {
21 | assert.equal(true, typeof result.os !== 'undefined');
22 | });
23 | });
24 |
25 | describe('#device', function () {
26 | it('should be defined', function () {
27 | assert.equal(true, typeof result.device !== 'undefined');
28 | });
29 | });
30 |
31 | describe('#camouflage', function () {
32 | it('should be defined', function () {
33 | assert.equal(true, typeof result.camouflage !== 'undefined');
34 | });
35 | });
36 |
37 | describe('#features', function () {
38 | it('should be defined', function () {
39 | assert.equal(true, typeof result.features !== 'undefined');
40 | });
41 | });
42 | });
43 |
--------------------------------------------------------------------------------