index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict'
  2. module.exports = isMobile
  3. module.exports.isMobile = isMobile
  4. module.exports.default = isMobile
  5. var mobileRE = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series[46]0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i
  6. var tabletRE = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series[46]0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i
  7. function isMobile (opts) {
  8. if (!opts) opts = {}
  9. var ua = opts.ua
  10. if (!ua && typeof navigator !== 'undefined') ua = navigator.userAgent
  11. if (ua && ua.headers && typeof ua.headers['user-agent'] === 'string') {
  12. ua = ua.headers['user-agent']
  13. }
  14. if (typeof ua !== 'string') return false
  15. var result = opts.tablet ? tabletRE.test(ua) : mobileRE.test(ua)
  16. if (
  17. !result &&
  18. opts.tablet &&
  19. opts.featureDetect &&
  20. navigator &&
  21. navigator.maxTouchPoints > 1 &&
  22. ua.indexOf('Macintosh') !== -1 &&
  23. ua.indexOf('Safari') !== -1
  24. ) {
  25. result = true
  26. }
  27. return result
  28. }