isMobile.js 4.0 KB

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