xe-utils
Version:
JavaScript 函数库、工具类
65 lines (61 loc) • 2.04 kB
JavaScript
import staticStrUndefined from './staticStrUndefined.js';import staticDocument from './staticDocument.js';import staticWindow from './staticWindow.js';import helperStringIndexOf from './helperStringIndexOf.js';import assign from './assign.js';import arrayEach from './arrayEach.js';/* eslint-disable valid-typeof */
function isBrowseStorage (storage) {
try {
var testKey = '__xe_t'
storage.setItem(testKey, 1)
storage.removeItem(testKey)
return true
} catch (e) {
return false
}
}
function isBrowseType (type) {
return helperStringIndexOf(navigator.userAgent, type) > -1
}
/**
* 获取浏览器内核
* @return Object
*/
function browse () {
var $body, isChrome, isEdge
var isMobile = false
var isLocalStorage = false
var isSessionStorage = false
var result = {
isNode: false,
isMobile: isMobile,
isPC: false,
isDoc: !!staticDocument
}
if (!staticWindow && typeof process !== staticStrUndefined) {
result.isNode = true
} else {
isEdge = isBrowseType('Edge')
isChrome = isBrowseType('Chrome')
isMobile = /(Android|webOS|iPhone|iPad|iPod|SymbianOS|BlackBerry|Windows Phone)/.test(navigator.userAgent)
if (result.isDoc) {
$body = staticDocument.body || staticDocument.documentElement
arrayEach(['webkit', 'khtml', 'moz', 'ms', 'o'], function (core) {
result['-' + core] = !!$body[core + 'MatchesSelector']
})
}
try {
isLocalStorage = isBrowseStorage(staticWindow.localStorage)
} catch (e) {}
try {
isSessionStorage = isBrowseStorage(staticWindow.sessionStorage)
} catch (e) {}
assign(result, {
edge: isEdge,
firefox: isBrowseType('Firefox'),
msie: !isEdge && result['-ms'],
safari: !isChrome && !isEdge && isBrowseType('Safari'),
isMobile: isMobile,
isPC: !isMobile,
isLocalStorage: isLocalStorage,
isSessionStorage: isSessionStorage
})
}
return result
}
export default browse