-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdeviceType.js
More file actions
60 lines (49 loc) · 1.63 KB
/
Copy pathdeviceType.js
File metadata and controls
60 lines (49 loc) · 1.63 KB
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
'use strict';
/**
* Detects device type from user agent.
* Returns 'android', 'iphone', 'ipad', 'blackberry', 'windows-phone', 'kindle', 'desktop', or 'other'
* @example
* {{deviceType}}
*/
const factory = () => {
return function() {
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Check for BlackBerry devices
if (/BlackBerry|BB10|PlayBook/i.test(userAgent)) {
return 'blackberry';
}
// Check for Windows Phone
if (/Windows Phone|IEMobile|WPDesktop/i.test(userAgent)) {
return 'windows-phone';
}
// Check for Kindle devices
if (/Kindle|Silk/i.test(userAgent)) {
return 'kindle';
}
// Check for Android devices
if (/android/i.test(userAgent)) {
return 'android';
}
// Check for iPad specifically
if (/iPad/.test(userAgent) && !window.MSStream) {
return 'ipad';
}
// Check for iPhone and iPod
if (/iPhone|iPod/.test(userAgent) && !window.MSStream) {
return 'iphone';
}
// Check for iOS devices using newer detection method
if (/Macintosh/.test(userAgent) && navigator.maxTouchPoints > 1) {
return 'ipad';
}
// Check for desktop devices (Windows, Mac, Linux)
if (/Windows NT|Macintosh|Linux/i.test(userAgent) && !/Mobile|Tablet/i.test(userAgent)) {
return 'desktop';
}
return 'other';
};
};
module.exports = [{
name: 'deviceType',
factory: factory,
}];