1 | var appleIphone = /iPhone/i;
|
2 | var appleIpod = /iPod/i;
|
3 | var appleTablet = /iPad/i;
|
4 | var appleUniversal = /\biOS-universal(?:.+)Mac\b/i;
|
5 | var androidPhone = /\bAndroid(?:.+)Mobile\b/i;
|
6 | var androidTablet = /Android/i;
|
7 | var amazonPhone = /(?:SD4930UR|\bSilk(?:.+)Mobile\b)/i;
|
8 | var amazonTablet = /Silk/i;
|
9 | var windowsPhone = /Windows Phone/i;
|
10 | var windowsTablet = /\bWindows(?:.+)ARM\b/i;
|
11 | var otherBlackBerry = /BlackBerry/i;
|
12 | var otherBlackBerry10 = /BB10/i;
|
13 | var otherOpera = /Opera Mini/i;
|
14 | var otherChrome = /\b(CriOS|Chrome)(?:.+)Mobile/i;
|
15 | var otherFirefox = /Mobile(?:.+)Firefox\b/i;
|
16 | var isAppleTabletOnIos13 = function (navigator) {
|
17 | return (typeof navigator !== 'undefined' &&
|
18 | navigator.platform === 'MacIntel' &&
|
19 | typeof navigator.maxTouchPoints === 'number' &&
|
20 | navigator.maxTouchPoints > 1 &&
|
21 | typeof MSStream === 'undefined');
|
22 | };
|
23 | function createMatch(userAgent) {
|
24 | return function (regex) { return regex.test(userAgent); };
|
25 | }
|
26 | export default function isMobile(param) {
|
27 | var nav = {
|
28 | userAgent: '',
|
29 | platform: '',
|
30 | maxTouchPoints: 0
|
31 | };
|
32 | if (!param && typeof navigator !== 'undefined') {
|
33 | nav = {
|
34 | userAgent: navigator.userAgent,
|
35 | platform: navigator.platform,
|
36 | maxTouchPoints: navigator.maxTouchPoints || 0
|
37 | };
|
38 | }
|
39 | else if (typeof param === 'string') {
|
40 | nav.userAgent = param;
|
41 | }
|
42 | else if (param && param.userAgent) {
|
43 | nav = {
|
44 | userAgent: param.userAgent,
|
45 | platform: param.platform,
|
46 | maxTouchPoints: param.maxTouchPoints || 0
|
47 | };
|
48 | }
|
49 | var userAgent = nav.userAgent;
|
50 | var tmp = userAgent.split('[FBAN');
|
51 | if (typeof tmp[1] !== 'undefined') {
|
52 | userAgent = tmp[0];
|
53 | }
|
54 | tmp = userAgent.split('Twitter');
|
55 | if (typeof tmp[1] !== 'undefined') {
|
56 | userAgent = tmp[0];
|
57 | }
|
58 | var match = createMatch(userAgent);
|
59 | var result = {
|
60 | apple: {
|
61 | phone: match(appleIphone) && !match(windowsPhone),
|
62 | ipod: match(appleIpod),
|
63 | tablet: !match(appleIphone) &&
|
64 | (match(appleTablet) || isAppleTabletOnIos13(nav)) &&
|
65 | !match(windowsPhone),
|
66 | universal: match(appleUniversal),
|
67 | device: (match(appleIphone) ||
|
68 | match(appleIpod) ||
|
69 | match(appleTablet) ||
|
70 | match(appleUniversal) ||
|
71 | isAppleTabletOnIos13(nav)) &&
|
72 | !match(windowsPhone)
|
73 | },
|
74 | amazon: {
|
75 | phone: match(amazonPhone),
|
76 | tablet: !match(amazonPhone) && match(amazonTablet),
|
77 | device: match(amazonPhone) || match(amazonTablet)
|
78 | },
|
79 | android: {
|
80 | phone: (!match(windowsPhone) && match(amazonPhone)) ||
|
81 | (!match(windowsPhone) && match(androidPhone)),
|
82 | tablet: !match(windowsPhone) &&
|
83 | !match(amazonPhone) &&
|
84 | !match(androidPhone) &&
|
85 | (match(amazonTablet) || match(androidTablet)),
|
86 | device: (!match(windowsPhone) &&
|
87 | (match(amazonPhone) ||
|
88 | match(amazonTablet) ||
|
89 | match(androidPhone) ||
|
90 | match(androidTablet))) ||
|
91 | match(/\bokhttp\b/i)
|
92 | },
|
93 | windows: {
|
94 | phone: match(windowsPhone),
|
95 | tablet: match(windowsTablet),
|
96 | device: match(windowsPhone) || match(windowsTablet)
|
97 | },
|
98 | other: {
|
99 | blackberry: match(otherBlackBerry),
|
100 | blackberry10: match(otherBlackBerry10),
|
101 | opera: match(otherOpera),
|
102 | firefox: match(otherFirefox),
|
103 | chrome: match(otherChrome),
|
104 | device: match(otherBlackBerry) ||
|
105 | match(otherBlackBerry10) ||
|
106 | match(otherOpera) ||
|
107 | match(otherFirefox) ||
|
108 | match(otherChrome)
|
109 | },
|
110 | any: false,
|
111 | phone: false,
|
112 | tablet: false
|
113 | };
|
114 | result.any =
|
115 | result.apple.device ||
|
116 | result.android.device ||
|
117 | result.windows.device ||
|
118 | result.other.device;
|
119 | result.phone =
|
120 | result.apple.phone || result.android.phone || result.windows.phone;
|
121 | result.tablet =
|
122 | result.apple.tablet || result.android.tablet || result.windows.tablet;
|
123 | return result;
|
124 | }
|
125 |
|
\ | No newline at end of file |