浏览器环境判断终极版

将常用浏览器环境判断做封装,基本支持所有环境比如微信、QQ、ipad等

/**环境集合 */
let environmentInfo = {
  inited: false,
  trident: false, //IE内核
  presto: false, //opera内核
  webKit: false, //苹果、谷歌内核
  gecko: false, //火狐内核
  mobile: false, //是否为移动终端
  ios: false, //ios终端
  android: false, //android终端
  iPhone: false, //是否为iPhone或者QQHD浏览器
  iPad: false, //是否iPad
  webApp: false, //是否web应该程序,没有头部与底部
  wechat: false, //是否微信
  qq: false, //是否QQ
  gameCenterApp: false //某某App (即自定义UA)
}

/** 环境设置 */
function updateEnvironmentInfo() {
  const u = navigator.userAgent
  environmentInfo.trident = u.indexOf('Trident') > -1
  environmentInfo.presto = u.indexOf('Presto') > -1
  environmentInfo.webKit = u.indexOf('AppleWebKit') > -1
  environmentInfo.gecko = u.indexOf('Gecko') > -1 && u.indexOf('KHTML') === -1
  environmentInfo.mobile = !!u.match(/AppleWebKit.*Mobile.*/)
  environmentInfo.ios = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
  environmentInfo.android = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
  environmentInfo.iPhone = u.indexOf('iPhone') > -1
  environmentInfo.iPad = u.indexOf('iPad') > -1
  environmentInfo.webApp = u.indexOf('Safari') === -1
  environmentInfo.wechat = u.indexOf('MicroMessenger') > -1
  environmentInfo.qq = u.match(/\sQQ/i) === 'qq'
  environmentInfo.gameCenterApp = u.indexOf('GameCenter/') !== -1
}

/** 执行函数 */
export function getEnvironmentInfo() {
  if (environmentInfo.inited) return environmentInfo
  environmentInfo.inited = true
  updateEnvironmentInfo()
  return environmentInfo
}

你可能感兴趣的:(浏览器环境判断终极版)