/
FeatureDetection.js
187 lines (163 loc) · 5.6 KB
/
FeatureDetection.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
/*global define*/
define([
'./defaultValue',
'./defined',
'./Fullscreen'
], function(
defaultValue,
defined,
Fullscreen) {
"use strict";
function extractVersion(versionString) {
var parts = versionString.split('.');
for (var i = 0, len = parts.length; i < len; ++i) {
parts[i] = parseInt(parts[i], 10);
}
return parts;
}
var isChromeResult;
var chromeVersionResult;
function isChrome() {
if (!defined(isChromeResult)) {
isChromeResult = false;
var fields = (/ Chrome\/([\.0-9]+)/).exec(navigator.userAgent);
if (fields !== null) {
isChromeResult = true;
chromeVersionResult = extractVersion(fields[1]);
}
}
return isChromeResult;
}
function chromeVersion() {
return isChrome() && chromeVersionResult;
}
var isSafariResult;
var safariVersionResult;
function isSafari() {
if (!defined(isSafariResult)) {
isSafariResult = false;
// Chrome contains Safari in the user agent too
if (!isChrome() && (/ Safari\/[\.0-9]+/).test(navigator.userAgent)) {
var fields = (/ Version\/([\.0-9]+)/).exec(navigator.userAgent);
if (fields !== null) {
isSafariResult = true;
safariVersionResult = extractVersion(fields[1]);
}
}
}
return isSafariResult;
}
function safariVersion() {
return isSafari() && safariVersionResult;
}
var isWebkitResult;
var webkitVersionResult;
function isWebkit() {
if (!defined(isWebkitResult)) {
isWebkitResult = false;
var fields = (/ AppleWebKit\/([\.0-9]+)(\+?)/).exec(navigator.userAgent);
if (fields !== null) {
isWebkitResult = true;
webkitVersionResult = extractVersion(fields[1]);
webkitVersionResult.isNightly = !!fields[2];
}
}
return isWebkitResult;
}
function webkitVersion() {
return isWebkit() && webkitVersionResult;
}
var isInternetExplorerResult;
var internetExplorerVersionResult;
function isInternetExplorer() {
if (!defined(isInternetExplorerResult)) {
isInternetExplorerResult = false;
var fields;
if (navigator.appName === 'Microsoft Internet Explorer') {
fields = /MSIE ([0-9]{1,}[\.0-9]{0,})/.exec(navigator.userAgent);
if (fields !== null) {
isInternetExplorerResult = true;
internetExplorerVersionResult = extractVersion(fields[1]);
}
} else if (navigator.appName === 'Netscape') {
fields = /Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.exec(navigator.userAgent);
if (fields !== null) {
isInternetExplorerResult = true;
internetExplorerVersionResult = extractVersion(fields[1]);
}
}
}
return isInternetExplorerResult;
}
function internetExplorerVersion() {
return isInternetExplorer() && internetExplorerVersionResult;
}
var isFirefoxResult;
var firefoxVersionResult;
function isFirefox() {
if (!defined(isFirefoxResult)) {
isFirefoxResult = false;
var fields = /Firefox\/([\.0-9]+)/.exec(navigator.userAgent);
if (fields !== null) {
isFirefoxResult = true;
firefoxVersionResult = extractVersion(fields[1]);
}
}
return isFirefoxResult;
}
function firefoxVersion() {
return isFirefox() && firefoxVersionResult;
}
/**
* A set of functions to detect whether the current browser supports
* various features.
*
* @namespace
* @alias FeatureDetection
*/
var FeatureDetection = {
isChrome : isChrome,
chromeVersion : chromeVersion,
isSafari : isSafari,
safariVersion : safariVersion,
isWebkit : isWebkit,
webkitVersion : webkitVersion,
isInternetExplorer : isInternetExplorer,
internetExplorerVersion : internetExplorerVersion,
isFirefox : isFirefox,
firefoxVersion : firefoxVersion,
hardwareConcurrency : defaultValue(navigator.hardwareConcurrency, 3)
};
/**
* Detects whether the current browser supports the full screen standard.
*
* @returns true if the browser supports the full screen standard, false if not.
*
* @see Fullscreen
* @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
*/
FeatureDetection.supportsFullscreen = function() {
return Fullscreen.supportsFullscreen();
};
/**
* Detects whether the current browser supports typed arrays.
*
* @returns true if the browser supports typed arrays, false if not.
*
* @see {@link http://www.khronos.org/registry/typedarray/specs/latest/|Typed Array Specification}
*/
FeatureDetection.supportsTypedArrays = function() {
return typeof ArrayBuffer !== 'undefined';
};
/**
* Detects whether the current browser supports Web Workers.
*
* @returns true if the browsers supports Web Workers, false if not.
*
* @see {@link http://www.w3.org/TR/workers/}
*/
FeatureDetection.supportsWebWorkers = function() {
return typeof Worker !== 'undefined';
};
return FeatureDetection;
});