判断浏览器类型
//获取当前的浏览器
export function getBrowser() {
// 获取浏览器 userAgent
const ua = navigator.userAgent;
const ucs = navigator.userAgent.toLowerCase()
// 是否为 Opera
const isOpera = ua.indexOf('Opera') > -1
// 返回结果
if (isOpera) { return 'Opera' }
// 是否为 Edge
const isEdge = ua.indexOf("Edg") > -1
// 返回结果
if (isEdge) { return 'Edge' }
// 是否为 IE
const isIE = (ua.indexOf('compatible') > -1) && (ua.indexOf('MSIE') > -1) && !isOpera
const isIE11 = (ua.indexOf('Trident') > -1) && (ua.indexOf("rv:11.0") > -1)
// 返回结果
if (isIE11) { return 'IE11'
} else if (isIE) {
// 检测是否匹配
const re = new RegExp('MSIE (\\d+\\.\\d+);')
re.test(ua)
// 获取版本
const ver = parseFloat(RegExp["$1"])
// 返回结果
if (ver == 7) { return 'IE7'
} else if (ver == 8) { return 'IE8'
} else if (ver == 9) { return 'IE9'
} else if (ver == 10) { return 'IE10'
} else { return "IE" }
}
// 是否为 Firefox
const isFirefox = ua.indexOf("Firefox") > -1
// 返回结果
if (isFirefox) { return 'Firefox' }
// 是否为 Safari
const isSafari = (ua.indexOf("Safari") > -1) && (ua.indexOf("Chrome") == -1)
// 返回结果
if (isSafari) { return "Safari" }
// 是否为 QQ
const isQQ = ua.indexOf("QQBrowser") > -1
// 返回结果
if (isQQ) { return 'QQBrowser' }
// 是否为企业微信浏览器
const isWxWork = /wxwork/i.test(ucs);
if(isWxWork) {
return 'WxWork'
}
// 是否为微信浏览器
const isWx = ucs.indexOf('micromessenger') !== -1 ;
if(isWx) {
return 'WeiXin'
}
// 是否为钉钉浏览器
const isDD = ucs.indexOf('dingtalk') !== -1 ;
if(isDD) {
return 'DingDing'
}
// 是否为搜狗浏览器
const isSougo = ucs.indexOf("se 2.x") > -1
// 返回结果
if (isSougo) { return 'SouGo' }
// 是否为2345浏览器
const is2345Explorer = ua.includes("2345Explorer");
// 返回结果
if(is2345Explorer){
return '2345Explorer';
}
// 是否为 Chrome
const isChrome = (ua.indexOf("Chrome") > -1) && (ua.indexOf("Edg") == -1) && (ua.indexOf("QQBrowser") == -1) && (ua.indexOf("2345Explorer") == -1) && (ua.indexOf('WOW64') == -1)
// 返回结果
if (isChrome) { return 'Chrome' }
// 是否是360
const is360 = (ua.indexOf("Chrome") > -1) && (ua.indexOf('WOW64') > -1) && ua.indexOf("QQBrowser") == -1
if(is360){return '360'}
// 都不是
return ''
}