Skip to content

Instantly share code, notes, and snippets.

@RolandWarburton
Created October 17, 2021 08:17
Show Gist options
  • Save RolandWarburton/090528b684739b86616416ce83e6a919 to your computer and use it in GitHub Desktop.
Save RolandWarburton/090528b684739b86616416ce83e6a919 to your computer and use it in GitHub Desktop.
mobileDetect.ts
// Borrowed from
// https://github.com/juliangruber/is-mobile/blob/master/index.js
const mobileRE = /(android|bb\d+|meego).+mobile|armv7l|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
const tabletRE = /android|ipad|playbook|silk/i
export function IsMobile (opts?: any) : boolean {
if (!opts) opts = {}
let ua = opts.ua
if (!ua && typeof navigator !== 'undefined') ua = navigator.userAgent
if (ua && ua.headers && typeof ua.headers['user-agent'] === 'string') {
ua = ua.headers['user-agent']
}
if (typeof ua !== 'string') return false
let result = mobileRE.test(ua) || (!!opts.tablet && tabletRE.test(ua))
if (
!result &&
opts.tablet &&
opts.featureDetect &&
navigator &&
navigator.maxTouchPoints > 1 &&
ua.indexOf('Macintosh') !== -1 &&
ua.indexOf('Safari') !== -1
) {
result = true
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment