-
Notifications
You must be signed in to change notification settings - Fork 892
/
mobile-detect.template.js
executable file
·680 lines (606 loc) · 30.8 KB
/
mobile-detect.template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*global module:false, define:false*/
/*jshint latedef:false*/
/*!@license Copyright 2013, Heinrich Goebl, License: MIT, see https://github.com/hgoebl/mobile-detect.js*/
(function (define, undefined) {
define(function () {
'use strict';
var impl = {};
impl.mobileDetectRules = {/*rules*/};
// following patterns come from http://detectmobilebrowsers.com/
impl.detectMobileBrowsers = {
fullPattern: /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i,
shortPattern: /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i,
tabletPattern: /android|ipad|playbook|silk/i
};
var hasOwnProp = Object.prototype.hasOwnProperty,
isArray;
impl.FALLBACK_PHONE = 'UnknownPhone';
impl.FALLBACK_TABLET = 'UnknownTablet';
impl.FALLBACK_MOBILE = 'UnknownMobile';
isArray = ('isArray' in Array) ?
Array.isArray : function (value) { return Object.prototype.toString.call(value) === '[object Array]'; };
function equalIC(a, b) {
return a != null && b != null && a.toLowerCase() === b.toLowerCase();
}
function containsIC(array, value) {
var valueLC, i, len = array.length;
if (!len || !value) {
return false;
}
valueLC = value.toLowerCase();
for (i = 0; i < len; ++i) {
if (valueLC === array[i].toLowerCase()) {
return true;
}
}
return false;
}
function convertPropsToRegExp(object) {
for (var key in object) {
if (hasOwnProp.call(object, key)) {
object[key] = new RegExp(object[key], 'i');
}
}
}
function prepareUserAgent(userAgent) {
return (userAgent || '').substr(0, 500); // mitigate vulnerable to ReDoS
}
(function init() {
var key, values, value, i, len, verPos, mobileDetectRules = impl.mobileDetectRules;
for (key in mobileDetectRules.props) {
if (hasOwnProp.call(mobileDetectRules.props, key)) {
values = mobileDetectRules.props[key];
if (!isArray(values)) {
values = [values];
}
len = values.length;
for (i = 0; i < len; ++i) {
value = values[i];
verPos = value.indexOf('[VER]');
if (verPos >= 0) {
value = value.substring(0, verPos) + '([\\w._\\+]+)' + value.substring(verPos + 5);
}
values[i] = new RegExp(value, 'i');
}
mobileDetectRules.props[key] = values;
}
}
convertPropsToRegExp(mobileDetectRules.oss);
convertPropsToRegExp(mobileDetectRules.phones);
convertPropsToRegExp(mobileDetectRules.tablets);
convertPropsToRegExp(mobileDetectRules.uas);
convertPropsToRegExp(mobileDetectRules.utils);
// copy some patterns to oss0 which are tested first (see issue#15)
mobileDetectRules.oss0 = {
WindowsPhoneOS: mobileDetectRules.oss.WindowsPhoneOS,
WindowsMobileOS: mobileDetectRules.oss.WindowsMobileOS
};
}());
/**
* Test userAgent string against a set of rules and find the first matched key.
* @param {Object} rules (key is String, value is RegExp)
* @param {String} userAgent the navigator.userAgent (or HTTP-Header 'User-Agent').
* @returns {String|null} the matched key if found, otherwise <tt>null</tt>
* @private
*/
impl.findMatch = function(rules, userAgent) {
for (var key in rules) {
if (hasOwnProp.call(rules, key)) {
if (rules[key].test(userAgent)) {
return key;
}
}
}
return null;
};
/**
* Test userAgent string against a set of rules and return an array of matched keys.
* @param {Object} rules (key is String, value is RegExp)
* @param {String} userAgent the navigator.userAgent (or HTTP-Header 'User-Agent').
* @returns {Array} an array of matched keys, may be empty when there is no match, but not <tt>null</tt>
* @private
*/
impl.findMatches = function(rules, userAgent) {
var result = [];
for (var key in rules) {
if (hasOwnProp.call(rules, key)) {
if (rules[key].test(userAgent)) {
result.push(key);
}
}
}
return result;
};
/**
* Check the version of the given property in the User-Agent.
*
* @param {String} propertyName
* @param {String} userAgent
* @return {String} version or <tt>null</tt> if version not found
* @private
*/
impl.getVersionStr = function (propertyName, userAgent) {
var props = impl.mobileDetectRules.props, patterns, i, len, match;
if (hasOwnProp.call(props, propertyName)) {
patterns = props[propertyName];
len = patterns.length;
for (i = 0; i < len; ++i) {
match = patterns[i].exec(userAgent);
if (match !== null) {
return match[1];
}
}
}
return null;
};
/**
* Check the version of the given property in the User-Agent.
* Will return a float number. (eg. 2_0 will return 2.0, 4.3.1 will return 4.31)
*
* @param {String} propertyName
* @param {String} userAgent
* @return {Number} version or <tt>NaN</tt> if version not found
* @private
*/
impl.getVersion = function (propertyName, userAgent) {
var version = impl.getVersionStr(propertyName, userAgent);
return version ? impl.prepareVersionNo(version) : NaN;
};
/**
* Prepare the version number.
*
* @param {String} version
* @return {Number} the version number as a floating number
* @private
*/
impl.prepareVersionNo = function (version) {
var numbers;
numbers = version.split(/[a-z._ \/\-]/i);
if (numbers.length === 1) {
version = numbers[0];
}
if (numbers.length > 1) {
version = numbers[0] + '.';
numbers.shift();
version += numbers.join('');
}
return Number(version);
};
impl.isMobileFallback = function (userAgent) {
return impl.detectMobileBrowsers.fullPattern.test(userAgent) ||
impl.detectMobileBrowsers.shortPattern.test(userAgent.substr(0,4));
};
impl.isTabletFallback = function (userAgent) {
return impl.detectMobileBrowsers.tabletPattern.test(userAgent);
};
impl.prepareDetectionCache = function (cache, userAgent, maxPhoneWidth) {
if (cache.mobile !== undefined) {
return;
}
var phone, tablet, phoneSized;
// first check for stronger tablet rules, then phone (see issue#5)
tablet = impl.findMatch(impl.mobileDetectRules.tablets, userAgent);
if (tablet) {
cache.mobile = cache.tablet = tablet;
cache.phone = null;
return; // unambiguously identified as tablet
}
phone = impl.findMatch(impl.mobileDetectRules.phones, userAgent);
if (phone) {
cache.mobile = cache.phone = phone;
cache.tablet = null;
return; // unambiguously identified as phone
}
// our rules haven't found a match -> try more general fallback rules
if (impl.isMobileFallback(userAgent)) {
phoneSized = MobileDetect.isPhoneSized(maxPhoneWidth);
if (phoneSized === undefined) {
cache.mobile = impl.FALLBACK_MOBILE;
cache.tablet = cache.phone = null;
} else if (phoneSized) {
cache.mobile = cache.phone = impl.FALLBACK_PHONE;
cache.tablet = null;
} else {
cache.mobile = cache.tablet = impl.FALLBACK_TABLET;
cache.phone = null;
}
} else if (impl.isTabletFallback(userAgent)) {
cache.mobile = cache.tablet = impl.FALLBACK_TABLET;
cache.phone = null;
} else {
// not mobile at all!
cache.mobile = cache.tablet = cache.phone = null;
}
};
// t is a reference to a MobileDetect instance
impl.mobileGrade = function (t) {
// impl note:
// To keep in sync w/ Mobile_Detect.php easily, the following code is tightly aligned to the PHP version.
// When changes are made in Mobile_Detect.php, copy this method and replace:
// $this-> / t.
// self::MOBILE_GRADE_(.) / '$1'
// , self::VERSION_TYPE_FLOAT / (nothing)
// isIOS() / os('iOS')
// [reg] / (nothing) <-- jsdelivr complaining about unescaped unicode character U+00AE
var $isMobile = t.mobile() !== null;
if (
// Apple iOS 3.2-5.1 - Tested on the original iPad (4.3 / 5.0), iPad 2 (4.3), iPad 3 (5.1), original iPhone (3.1), iPhone 3 (3.2), 3GS (4.3), 4 (4.3 / 5.0), and 4S (5.1)
t.os('iOS') && t.version('iPad')>=4.3 ||
t.os('iOS') && t.version('iPhone')>=3.1 ||
t.os('iOS') && t.version('iPod')>=3.1 ||
// Android 2.1-2.3 - Tested on the HTC Incredible (2.2), original Droid (2.2), HTC Aria (2.1), Google Nexus S (2.3). Functional on 1.5 & 1.6 but performance may be sluggish, tested on Google G1 (1.5)
// Android 3.1 (Honeycomb) - Tested on the Samsung Galaxy Tab 10.1 and Motorola XOOM
// Android 4.0 (ICS) - Tested on a Galaxy Nexus. Note: transition performance can be poor on upgraded devices
// Android 4.1 (Jelly Bean) - Tested on a Galaxy Nexus and Galaxy 7
( t.version('Android')>2.1 && t.is('Webkit') ) ||
// Windows Phone 7-7.5 - Tested on the HTC Surround (7.0) HTC Trophy (7.5), LG-E900 (7.5), Nokia Lumia 800
t.version('Windows Phone OS')>=7.0 ||
// Blackberry 7 - Tested on BlackBerry Torch 9810
// Blackberry 6.0 - Tested on the Torch 9800 and Style 9670
t.is('BlackBerry') && t.version('BlackBerry')>=6.0 ||
// Blackberry Playbook (1.0-2.0) - Tested on PlayBook
t.match('Playbook.*Tablet') ||
// Palm WebOS (1.4-2.0) - Tested on the Palm Pixi (1.4), Pre (1.4), Pre 2 (2.0)
( t.version('webOS')>=1.4 && t.match('Palm|Pre|Pixi') ) ||
// Palm WebOS 3.0 - Tested on HP TouchPad
t.match('hp.*TouchPad') ||
// Firefox Mobile (12 Beta) - Tested on Android 2.3 device
( t.is('Firefox') && t.version('Firefox')>=12 ) ||
// Chrome for Android - Tested on Android 4.0, 4.1 device
( t.is('Chrome') && t.is('AndroidOS') && t.version('Android')>=4.0 ) ||
// Skyfire 4.1 - Tested on Android 2.3 device
( t.is('Skyfire') && t.version('Skyfire')>=4.1 && t.is('AndroidOS') && t.version('Android')>=2.3 ) ||
// Opera Mobile 11.5-12: Tested on Android 2.3
( t.is('Opera') && t.version('Opera Mobi')>11 && t.is('AndroidOS') ) ||
// Meego 1.2 - Tested on Nokia 950 and N9
t.is('MeeGoOS') ||
// Tizen (pre-release) - Tested on early hardware
t.is('Tizen') ||
// Samsung Bada 2.0 - Tested on a Samsung Wave 3, Dolphin browser
// @todo: more tests here!
t.is('Dolfin') && t.version('Bada')>=2.0 ||
// UC Browser - Tested on Android 2.3 device
( (t.is('UC Browser') || t.is('Dolfin')) && t.version('Android')>=2.3 ) ||
// Kindle 3 and Fire - Tested on the built-in WebKit browser for each
( t.match('Kindle Fire') ||
t.is('Kindle') && t.version('Kindle')>=3.0 ) ||
// Nook Color 1.4.1 - Tested on original Nook Color, not Nook Tablet
t.is('AndroidOS') && t.is('NookTablet') ||
// Chrome Desktop 11-21 - Tested on OS X 10.7 and Windows 7
t.version('Chrome')>=11 && !$isMobile ||
// Safari Desktop 4-5 - Tested on OS X 10.7 and Windows 7
t.version('Safari')>=5.0 && !$isMobile ||
// Firefox Desktop 4-13 - Tested on OS X 10.7 and Windows 7
t.version('Firefox')>=4.0 && !$isMobile ||
// Internet Explorer 7-9 - Tested on Windows XP, Vista and 7
t.version('MSIE')>=7.0 && !$isMobile ||
// Opera Desktop 10-12 - Tested on OS X 10.7 and Windows 7
// @reference: http://my.opera.com/community/openweb/idopera/
t.version('Opera')>=10 && !$isMobile
){
return 'A';
}
if (
t.os('iOS') && t.version('iPad')<4.3 ||
t.os('iOS') && t.version('iPhone')<3.1 ||
t.os('iOS') && t.version('iPod')<3.1 ||
// Blackberry 5.0: Tested on the Storm 2 9550, Bold 9770
t.is('Blackberry') && t.version('BlackBerry')>=5 && t.version('BlackBerry')<6 ||
//Opera Mini (5.0-6.5) - Tested on iOS 3.2/4.3 and Android 2.3
( t.version('Opera Mini')>=5.0 && t.version('Opera Mini')<=6.5 &&
(t.version('Android')>=2.3 || t.is('iOS')) ) ||
// Nokia Symbian^3 - Tested on Nokia N8 (Symbian^3), C7 (Symbian^3), also works on N97 (Symbian^1)
t.match('NokiaN8|NokiaC7|N97.*Series60|Symbian/3') ||
// @todo: report this (tested on Nokia N71)
t.version('Opera Mobi')>=11 && t.is('SymbianOS')
){
return 'B';
}
if (
// Blackberry 4.x - Tested on the Curve 8330
t.version('BlackBerry')<5.0 ||
// Windows Mobile - Tested on the HTC Leo (WinMo 5.2)
t.match('MSIEMobile|Windows CE.*Mobile') || t.version('Windows Mobile')<=5.2
){
return 'C';
}
//All older smartphone platforms and featurephones - Any device that doesn't support media queries
//will receive the basic, C grade experience.
return 'C';
};
impl.detectOS = function (ua) {
return impl.findMatch(impl.mobileDetectRules.oss0, ua) ||
impl.findMatch(impl.mobileDetectRules.oss, ua);
};
impl.getDeviceSmallerSide = function () {
return window.screen.width < window.screen.height ?
window.screen.width :
window.screen.height;
};
/**
* Constructor for MobileDetect object.
* <br>
* Such an object will keep a reference to the given user-agent string and cache most of the detect queries.<br>
* <div style="background-color: #d9edf7; border: 1px solid #bce8f1; color: #3a87ad; padding: 14px; border-radius: 2px; margin-top: 20px">
* <strong>Find information how to download and install:</strong>
* <a href="https://github.com/hgoebl/mobile-detect.js/">github.com/hgoebl/mobile-detect.js/</a>
* </div>
*
* @example <pre>
* var md = new MobileDetect(window.navigator.userAgent);
* if (md.mobile()) {
* location.href = (md.mobileGrade() === 'A') ? '/mobile/' : '/lynx/';
* }
* </pre>
*
* @param {string} userAgent typically taken from window.navigator.userAgent or http_header['User-Agent']
* @param {number} [maxPhoneWidth=600] <strong>only for browsers</strong> specify a value for the maximum
* width of smallest device side (in logical "CSS" pixels) until a device detected as mobile will be handled
* as phone.
* This is only used in cases where the device cannot be classified as phone or tablet.<br>
* See <a href="http://developer.android.com/guide/practices/screens_support.html">Declaring Tablet Layouts
* for Android</a>.<br>
* If you provide a value < 0, then this "fuzzy" check is disabled.
* @constructor
* @global
*/
function MobileDetect(userAgent, maxPhoneWidth) {
this.ua = prepareUserAgent(userAgent);
this._cache = {};
//600dp is typical 7" tablet minimum width
this.maxPhoneWidth = maxPhoneWidth || 600;
}
MobileDetect.prototype = {
constructor: MobileDetect,
/**
* Returns the detected phone or tablet type or <tt>null</tt> if it is not a mobile device.
* <br>
* For a list of possible return values see {@link MobileDetect#phone} and {@link MobileDetect#tablet}.<br>
* <br>
* If the device is not detected by the regular expressions from Mobile-Detect, a test is made against
* the patterns of <a href="http://detectmobilebrowsers.com/">detectmobilebrowsers.com</a>. If this test
* is positive, a value of <code>UnknownPhone</code>, <code>UnknownTablet</code> or
* <code>UnknownMobile</code> is returned.<br>
* When used in browser, the decision whether phone or tablet is made based on <code>screen.width/height</code>.<br>
* <br>
* When used server-side (node.js), there is no way to tell the difference between <code>UnknownTablet</code>
* and <code>UnknownMobile</code>, so you will get <code>UnknownMobile</code> here.<br>
* Be aware that since v1.0.0 in this special case you will get <code>UnknownMobile</code> only for:
* {@link MobileDetect#mobile}, not for {@link MobileDetect#phone} and {@link MobileDetect#tablet}.
* In versions before v1.0.0 all 3 methods returned <code>UnknownMobile</code> which was tedious to use.
* <br>
* In most cases you will use the return value just as a boolean.
*
* @returns {String} the key for the phone family or tablet family, e.g. "Nexus".
* @function MobileDetect#mobile
*/
mobile: function () {
impl.prepareDetectionCache(this._cache, this.ua, this.maxPhoneWidth);
return this._cache.mobile;
},
/**
* Returns the detected phone type/family string or <tt>null</tt>.
* <br>
* The returned tablet (family or producer) is one of following keys:<br>
* <br><tt>{{{keys.phones}}}</tt><br>
* <br>
* If the device is not detected by the regular expressions from Mobile-Detect, a test is made against
* the patterns of <a href="http://detectmobilebrowsers.com/">detectmobilebrowsers.com</a>. If this test
* is positive, a value of <code>UnknownPhone</code> or <code>UnknownMobile</code> is returned.<br>
* When used in browser, the decision whether phone or tablet is made based on <code>screen.width/height</code>.<br>
* <br>
* When used server-side (node.js), there is no way to tell the difference between <code>UnknownTablet</code>
* and <code>UnknownMobile</code>, so you will get <code>null</code> here, while {@link MobileDetect#mobile}
* will return <code>UnknownMobile</code>.<br>
* Be aware that since v1.0.0 in this special case you will get <code>UnknownMobile</code> only for:
* {@link MobileDetect#mobile}, not for {@link MobileDetect#phone} and {@link MobileDetect#tablet}.
* In versions before v1.0.0 all 3 methods returned <code>UnknownMobile</code> which was tedious to use.
* <br>
* In most cases you will use the return value just as a boolean.
*
* @returns {String} the key of the phone family or producer, e.g. "iPhone"
* @function MobileDetect#phone
*/
phone: function () {
impl.prepareDetectionCache(this._cache, this.ua, this.maxPhoneWidth);
return this._cache.phone;
},
/**
* Returns the detected tablet type/family string or <tt>null</tt>.
* <br>
* The returned tablet (family or producer) is one of following keys:<br>
* <br><tt>{{{keys.tablets}}}</tt><br>
* <br>
* If the device is not detected by the regular expressions from Mobile-Detect, a test is made against
* the patterns of <a href="http://detectmobilebrowsers.com/">detectmobilebrowsers.com</a>. If this test
* is positive, a value of <code>UnknownTablet</code> or <code>UnknownMobile</code> is returned.<br>
* When used in browser, the decision whether phone or tablet is made based on <code>screen.width/height</code>.<br>
* <br>
* When used server-side (node.js), there is no way to tell the difference between <code>UnknownTablet</code>
* and <code>UnknownMobile</code>, so you will get <code>null</code> here, while {@link MobileDetect#mobile}
* will return <code>UnknownMobile</code>.<br>
* Be aware that since v1.0.0 in this special case you will get <code>UnknownMobile</code> only for:
* {@link MobileDetect#mobile}, not for {@link MobileDetect#phone} and {@link MobileDetect#tablet}.
* In versions before v1.0.0 all 3 methods returned <code>UnknownMobile</code> which was tedious to use.
* <br>
* In most cases you will use the return value just as a boolean.
*
* @returns {String} the key of the tablet family or producer, e.g. "SamsungTablet"
* @function MobileDetect#tablet
*/
tablet: function () {
impl.prepareDetectionCache(this._cache, this.ua, this.maxPhoneWidth);
return this._cache.tablet;
},
/**
* Returns the (first) detected user-agent string or <tt>null</tt>.
* <br>
* The returned user-agent is one of following keys:<br>
* <br><tt>{{{keys.uas}}}</tt><br>
* <br>
* In most cases calling {@link MobileDetect#userAgent} will be sufficient. But there are rare
* cases where a mobile device pretends to be more than one particular browser. You can get the
* list of all matches with {@link MobileDetect#userAgents} or check for a particular value by
* providing one of the defined keys as first argument to {@link MobileDetect#is}.
*
* @returns {String} the key for the detected user-agent or <tt>null</tt>
* @function MobileDetect#userAgent
*/
userAgent: function () {
if (this._cache.userAgent === undefined) {
this._cache.userAgent = impl.findMatch(impl.mobileDetectRules.uas, this.ua);
}
return this._cache.userAgent;
},
/**
* Returns all detected user-agent strings.
* <br>
* The array is empty or contains one or more of following keys:<br>
* <br><tt>{{{keys.uas}}}</tt><br>
* <br>
* In most cases calling {@link MobileDetect#userAgent} will be sufficient. But there are rare
* cases where a mobile device pretends to be more than one particular browser. You can get the
* list of all matches with {@link MobileDetect#userAgents} or check for a particular value by
* providing one of the defined keys as first argument to {@link MobileDetect#is}.
*
* @returns {Array} the array of detected user-agent keys or <tt>[]</tt>
* @function MobileDetect#userAgents
*/
userAgents: function () {
if (this._cache.userAgents === undefined) {
this._cache.userAgents = impl.findMatches(impl.mobileDetectRules.uas, this.ua);
}
return this._cache.userAgents;
},
/**
* Returns the detected operating system string or <tt>null</tt>.
* <br>
* The operating system is one of following keys:<br>
* <br><tt>{{{keys.oss}}}</tt><br>
*
* @returns {String} the key for the detected operating system.
* @function MobileDetect#os
*/
os: function () {
if (this._cache.os === undefined) {
this._cache.os = impl.detectOS(this.ua);
}
return this._cache.os;
},
/**
* Get the version (as Number) of the given property in the User-Agent.
* <br>
* Will return a float number. (eg. 2_0 will return 2.0, 4.3.1 will return 4.31)
*
* @param {String} key a key defining a thing which has a version.<br>
* You can use one of following keys:<br>
* <br><tt>{{{keys.props}}}</tt><br>
*
* @returns {Number} the version as float or <tt>NaN</tt> if User-Agent doesn't contain this version.
* Be careful when comparing this value with '==' operator!
* @function MobileDetect#version
*/
version: function (key) {
return impl.getVersion(key, this.ua);
},
/**
* Get the version (as String) of the given property in the User-Agent.
* <br>
*
* @param {String} key a key defining a thing which has a version.<br>
* You can use one of following keys:<br>
* <br><tt>{{{keys.props}}}</tt><br>
*
* @returns {String} the "raw" version as String or <tt>null</tt> if User-Agent doesn't contain this version.
*
* @function MobileDetect#versionStr
*/
versionStr: function (key) {
return impl.getVersionStr(key, this.ua);
},
/**
* Global test key against userAgent, os, phone, tablet and some other properties of userAgent string.
*
* @param {String} key the key (case-insensitive) of a userAgent, an operating system, phone or
* tablet family.<br>
* For a complete list of possible values, see {@link MobileDetect#userAgent},
* {@link MobileDetect#os}, {@link MobileDetect#phone}, {@link MobileDetect#tablet}.<br>
* Additionally you have following keys:<br>
* <br><tt>{{{keys.utils}}}</tt><br>
*
* @returns {boolean} <tt>true</tt> when the given key is one of the defined keys of userAgent, os, phone,
* tablet or one of the listed additional keys, otherwise <tt>false</tt>
* @function MobileDetect#is
*/
is: function (key) {
return containsIC(this.userAgents(), key) ||
equalIC(key, this.os()) ||
equalIC(key, this.phone()) ||
equalIC(key, this.tablet()) ||
containsIC(impl.findMatches(impl.mobileDetectRules.utils, this.ua), key);
},
/**
* Do a quick test against navigator::userAgent.
*
* @param {String|RegExp} pattern the pattern, either as String or RegExp
* (a string will be converted to a case-insensitive RegExp).
* @returns {boolean} <tt>true</tt> when the pattern matches, otherwise <tt>false</tt>
* @function MobileDetect#match
*/
match: function (pattern) {
if (!(pattern instanceof RegExp)) {
pattern = new RegExp(pattern, 'i');
}
return pattern.test(this.ua);
},
/**
* Checks whether the mobile device can be considered as phone regarding <code>screen.width</code>.
* <br>
* Obviously this method makes sense in browser environments only (not for Node.js)!
* @param {number} [maxPhoneWidth] the maximum logical pixels (aka. CSS-pixels) to be considered as phone.<br>
* The argument is optional and if not present or falsy, the value of the constructor is taken.
* @returns {boolean|undefined} <code>undefined</code> if screen size wasn't detectable, else <code>true</code>
* when screen.width is less or equal to maxPhoneWidth, otherwise <code>false</code>.<br>
* Will always return <code>undefined</code> server-side.
*/
isPhoneSized: function (maxPhoneWidth) {
return MobileDetect.isPhoneSized(maxPhoneWidth || this.maxPhoneWidth);
},
/**
* Returns the mobile grade ('A', 'B', 'C').
*
* @returns {String} one of the mobile grades ('A', 'B', 'C').
* @function MobileDetect#mobileGrade
*/
mobileGrade: function () {
if (this._cache.grade === undefined) {
this._cache.grade = impl.mobileGrade(this);
}
return this._cache.grade;
}
};
// environment-dependent
if (typeof window !== 'undefined' && window.screen) {
MobileDetect.isPhoneSized = function (maxPhoneWidth) {
return maxPhoneWidth < 0 ? undefined : impl.getDeviceSmallerSide() <= maxPhoneWidth;
};
} else {
MobileDetect.isPhoneSized = function () {};
}
// should not be replaced by a completely new object - just overwrite existing methods
MobileDetect._impl = impl;
MobileDetect.version = 'MOBILE_DETECT_VERSION';
return MobileDetect;
}); // end of call of define()
})((function (undefined) {
if (typeof module !== 'undefined' && module.exports) {
return function (factory) { module.exports = factory(); };
} else if (typeof define === 'function' && define.amd) {
return define;
} else if (typeof window !== 'undefined') {
return function (factory) { window.MobileDetect = factory(); };
} else {
// please file a bug if you get this error!
throw new Error('unknown environment');
}
})());