UserAgent_DEPRECATED.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Copyright 2004-present Facebook. All Rights Reserved.
  3. *
  4. * @providesModule UserAgent_DEPRECATED
  5. */
  6. /**
  7. * Provides entirely client-side User Agent and OS detection. You should prefer
  8. * the non-deprecated UserAgent module when possible, which exposes our
  9. * authoritative server-side PHP-based detection to the client.
  10. *
  11. * Usage is straightforward:
  12. *
  13. * if (UserAgent_DEPRECATED.ie()) {
  14. * // IE
  15. * }
  16. *
  17. * You can also do version checks:
  18. *
  19. * if (UserAgent_DEPRECATED.ie() >= 7) {
  20. * // IE7 or better
  21. * }
  22. *
  23. * The browser functions will return NaN if the browser does not match, so
  24. * you can also do version compares the other way:
  25. *
  26. * if (UserAgent_DEPRECATED.ie() < 7) {
  27. * // IE6 or worse
  28. * }
  29. *
  30. * Note that the version is a float and may include a minor version number,
  31. * so you should always use range operators to perform comparisons, not
  32. * strict equality.
  33. *
  34. * **Note:** You should **strongly** prefer capability detection to browser
  35. * version detection where it's reasonable:
  36. *
  37. * http://www.quirksmode.org/js/support.html
  38. *
  39. * Further, we have a large number of mature wrapper functions and classes
  40. * which abstract away many browser irregularities. Check the documentation,
  41. * grep for things, or ask on javascript@lists.facebook.com before writing yet
  42. * another copy of "event || window.event".
  43. *
  44. */
  45. var _populated = false;
  46. // Browsers
  47. var _ie, _firefox, _opera, _webkit, _chrome;
  48. // Actual IE browser for compatibility mode
  49. var _ie_real_version;
  50. // Platforms
  51. var _osx, _windows, _linux, _android;
  52. // Architectures
  53. var _win64;
  54. // Devices
  55. var _iphone, _ipad, _native;
  56. var _mobile;
  57. function _populate() {
  58. if (_populated) {
  59. return;
  60. }
  61. _populated = true;
  62. // To work around buggy JS libraries that can't handle multi-digit
  63. // version numbers, Opera 10's user agent string claims it's Opera
  64. // 9, then later includes a Version/X.Y field:
  65. //
  66. // Opera/9.80 (foo) Presto/2.2.15 Version/10.10
  67. var uas = navigator.userAgent;
  68. var agent = /(?:MSIE.(\d+\.\d+))|(?:(?:Firefox|GranParadiso|Iceweasel).(\d+\.\d+))|(?:Opera(?:.+Version.|.)(\d+\.\d+))|(?:AppleWebKit.(\d+(?:\.\d+)?))|(?:Trident\/\d+\.\d+.*rv:(\d+\.\d+))/.exec(uas);
  69. var os = /(Mac OS X)|(Windows)|(Linux)/.exec(uas);
  70. _iphone = /\b(iPhone|iP[ao]d)/.exec(uas);
  71. _ipad = /\b(iP[ao]d)/.exec(uas);
  72. _android = /Android/i.exec(uas);
  73. _native = /FBAN\/\w+;/i.exec(uas);
  74. _mobile = /Mobile/i.exec(uas);
  75. // Note that the IE team blog would have you believe you should be checking
  76. // for 'Win64; x64'. But MSDN then reveals that you can actually be coming
  77. // from either x64 or ia64; so ultimately, you should just check for Win64
  78. // as in indicator of whether you're in 64-bit IE. 32-bit IE on 64-bit
  79. // Windows will send 'WOW64' instead.
  80. _win64 = !!(/Win64/.exec(uas));
  81. if (agent) {
  82. _ie = agent[1] ? parseFloat(agent[1]) : (
  83. agent[5] ? parseFloat(agent[5]) : NaN);
  84. // IE compatibility mode
  85. if (_ie && document && document.documentMode) {
  86. _ie = document.documentMode;
  87. }
  88. // grab the "true" ie version from the trident token if available
  89. var trident = /(?:Trident\/(\d+.\d+))/.exec(uas);
  90. _ie_real_version = trident ? parseFloat(trident[1]) + 4 : _ie;
  91. _firefox = agent[2] ? parseFloat(agent[2]) : NaN;
  92. _opera = agent[3] ? parseFloat(agent[3]) : NaN;
  93. _webkit = agent[4] ? parseFloat(agent[4]) : NaN;
  94. if (_webkit) {
  95. // We do not add the regexp to the above test, because it will always
  96. // match 'safari' only since 'AppleWebKit' appears before 'Chrome' in
  97. // the userAgent string.
  98. agent = /(?:Chrome\/(\d+\.\d+))/.exec(uas);
  99. _chrome = agent && agent[1] ? parseFloat(agent[1]) : NaN;
  100. } else {
  101. _chrome = NaN;
  102. }
  103. } else {
  104. _ie = _firefox = _opera = _chrome = _webkit = NaN;
  105. }
  106. if (os) {
  107. if (os[1]) {
  108. // Detect OS X version. If no version number matches, set _osx to true.
  109. // Version examples: 10, 10_6_1, 10.7
  110. // Parses version number as a float, taking only first two sets of
  111. // digits. If only one set of digits is found, returns just the major
  112. // version number.
  113. var ver = /(?:Mac OS X (\d+(?:[._]\d+)?))/.exec(uas);
  114. _osx = ver ? parseFloat(ver[1].replace('_', '.')) : true;
  115. } else {
  116. _osx = false;
  117. }
  118. _windows = !!os[2];
  119. _linux = !!os[3];
  120. } else {
  121. _osx = _windows = _linux = false;
  122. }
  123. }
  124. var UserAgent_DEPRECATED = {
  125. /**
  126. * Check if the UA is Internet Explorer.
  127. *
  128. *
  129. * @return float|NaN Version number (if match) or NaN.
  130. */
  131. ie: function() {
  132. return _populate() || _ie;
  133. },
  134. /**
  135. * Check if we're in Internet Explorer compatibility mode.
  136. *
  137. * @return bool true if in compatibility mode, false if
  138. * not compatibility mode or not ie
  139. */
  140. ieCompatibilityMode: function() {
  141. return _populate() || (_ie_real_version > _ie);
  142. },
  143. /**
  144. * Whether the browser is 64-bit IE. Really, this is kind of weak sauce; we
  145. * only need this because Skype can't handle 64-bit IE yet. We need to remove
  146. * this when we don't need it -- tracked by #601957.
  147. */
  148. ie64: function() {
  149. return UserAgent_DEPRECATED.ie() && _win64;
  150. },
  151. /**
  152. * Check if the UA is Firefox.
  153. *
  154. *
  155. * @return float|NaN Version number (if match) or NaN.
  156. */
  157. firefox: function() {
  158. return _populate() || _firefox;
  159. },
  160. /**
  161. * Check if the UA is Opera.
  162. *
  163. *
  164. * @return float|NaN Version number (if match) or NaN.
  165. */
  166. opera: function() {
  167. return _populate() || _opera;
  168. },
  169. /**
  170. * Check if the UA is WebKit.
  171. *
  172. *
  173. * @return float|NaN Version number (if match) or NaN.
  174. */
  175. webkit: function() {
  176. return _populate() || _webkit;
  177. },
  178. /**
  179. * For Push
  180. * WILL BE REMOVED VERY SOON. Use UserAgent_DEPRECATED.webkit
  181. */
  182. safari: function() {
  183. return UserAgent_DEPRECATED.webkit();
  184. },
  185. /**
  186. * Check if the UA is a Chrome browser.
  187. *
  188. *
  189. * @return float|NaN Version number (if match) or NaN.
  190. */
  191. chrome : function() {
  192. return _populate() || _chrome;
  193. },
  194. /**
  195. * Check if the user is running Windows.
  196. *
  197. * @return bool `true' if the user's OS is Windows.
  198. */
  199. windows: function() {
  200. return _populate() || _windows;
  201. },
  202. /**
  203. * Check if the user is running Mac OS X.
  204. *
  205. * @return float|bool Returns a float if a version number is detected,
  206. * otherwise true/false.
  207. */
  208. osx: function() {
  209. return _populate() || _osx;
  210. },
  211. /**
  212. * Check if the user is running Linux.
  213. *
  214. * @return bool `true' if the user's OS is some flavor of Linux.
  215. */
  216. linux: function() {
  217. return _populate() || _linux;
  218. },
  219. /**
  220. * Check if the user is running on an iPhone or iPod platform.
  221. *
  222. * @return bool `true' if the user is running some flavor of the
  223. * iPhone OS.
  224. */
  225. iphone: function() {
  226. return _populate() || _iphone;
  227. },
  228. mobile: function() {
  229. return _populate() || (_iphone || _ipad || _android || _mobile);
  230. },
  231. nativeApp: function() {
  232. // webviews inside of the native apps
  233. return _populate() || _native;
  234. },
  235. android: function() {
  236. return _populate() || _android;
  237. },
  238. ipad: function() {
  239. return _populate() || _ipad;
  240. }
  241. };
  242. module.exports = UserAgent_DEPRECATED;