isMobile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  3. // MIT License from https://github.com/kaimallea/isMobile
  4. var applePhone = /iPhone/i;
  5. var appleIpod = /iPod/i;
  6. var appleTablet = /iPad/i;
  7. var androidPhone = /\bAndroid(?:.+)Mobile\b/i; // Match 'Android' AND 'Mobile'
  8. var androidTablet = /Android/i;
  9. var amazonPhone = /\bAndroid(?:.+)SD4930UR\b/i;
  10. var amazonTablet = /\bAndroid(?:.+)(?:KF[A-Z]{2,4})\b/i;
  11. var windowsPhone = /Windows Phone/i;
  12. var windowsTablet = /\bWindows(?:.+)ARM\b/i; // Match 'Windows' AND 'ARM'
  13. var otherBlackberry = /BlackBerry/i;
  14. var otherBlackberry10 = /BB10/i;
  15. var otherOpera = /Opera Mini/i;
  16. var otherChrome = /\b(CriOS|Chrome)(?:.+)Mobile/i;
  17. var otherFirefox = /Mobile(?:.+)Firefox\b/i; // Match 'Mobile' AND 'Firefox'
  18. function match(regex, userAgent) {
  19. return regex.test(userAgent);
  20. }
  21. function isMobile(userAgent) {
  22. var ua = userAgent || (typeof navigator !== 'undefined' ? navigator.userAgent : '');
  23. // Facebook mobile app's integrated browser adds a bunch of strings that
  24. // match everything. Strip it out if it exists.
  25. var tmp = ua.split('[FBAN');
  26. if (typeof tmp[1] !== 'undefined') {
  27. var _tmp = tmp;
  28. var _tmp2 = _slicedToArray(_tmp, 1);
  29. ua = _tmp2[0];
  30. }
  31. // Twitter mobile app's integrated browser on iPad adds a "Twitter for
  32. // iPhone" string. Same probably happens on other tablet platforms.
  33. // This will confuse detection so strip it out if it exists.
  34. tmp = ua.split('Twitter');
  35. if (typeof tmp[1] !== 'undefined') {
  36. var _tmp3 = tmp;
  37. var _tmp4 = _slicedToArray(_tmp3, 1);
  38. ua = _tmp4[0];
  39. }
  40. var result = {
  41. apple: {
  42. phone: match(applePhone, ua) && !match(windowsPhone, ua),
  43. ipod: match(appleIpod, ua),
  44. tablet: !match(applePhone, ua) && match(appleTablet, ua) && !match(windowsPhone, ua),
  45. device: (match(applePhone, ua) || match(appleIpod, ua) || match(appleTablet, ua)) && !match(windowsPhone, ua)
  46. },
  47. amazon: {
  48. phone: match(amazonPhone, ua),
  49. tablet: !match(amazonPhone, ua) && match(amazonTablet, ua),
  50. device: match(amazonPhone, ua) || match(amazonTablet, ua)
  51. },
  52. android: {
  53. phone: !match(windowsPhone, ua) && match(amazonPhone, ua) || !match(windowsPhone, ua) && match(androidPhone, ua),
  54. tablet: !match(windowsPhone, ua) && !match(amazonPhone, ua) && !match(androidPhone, ua) && (match(amazonTablet, ua) || match(androidTablet, ua)),
  55. device: !match(windowsPhone, ua) && (match(amazonPhone, ua) || match(amazonTablet, ua) || match(androidPhone, ua) || match(androidTablet, ua)) || match(/\bokhttp\b/i, ua)
  56. },
  57. windows: {
  58. phone: match(windowsPhone, ua),
  59. tablet: match(windowsTablet, ua),
  60. device: match(windowsPhone, ua) || match(windowsTablet, ua)
  61. },
  62. other: {
  63. blackberry: match(otherBlackberry, ua),
  64. blackberry10: match(otherBlackberry10, ua),
  65. opera: match(otherOpera, ua),
  66. firefox: match(otherFirefox, ua),
  67. chrome: match(otherChrome, ua),
  68. device: match(otherBlackberry, ua) || match(otherBlackberry10, ua) || match(otherOpera, ua) || match(otherFirefox, ua) || match(otherChrome, ua)
  69. },
  70. // Additional
  71. any: null,
  72. phone: null,
  73. tablet: null
  74. };
  75. result.any = result.apple.device || result.android.device || result.windows.device || result.other.device;
  76. // excludes 'other' devices and ipods, targeting touchscreen phones
  77. result.phone = result.apple.phone || result.android.phone || result.windows.phone;
  78. result.tablet = result.apple.tablet || result.android.tablet || result.windows.tablet;
  79. return result;
  80. }
  81. var defaultResult = _objectSpread(_objectSpread({}, isMobile()), {}, {
  82. isMobile: isMobile
  83. });
  84. export default defaultResult;