UNPKG

1.78 kBJavaScriptView Raw
1var staticStrUndefined = require('./staticStrUndefined')
2var staticDocument = require('./staticDocument')
3var staticWindow = require('./staticWindow')
4
5var assign = require('./assign')
6var arrayEach = require('./arrayEach')
7
8/* eslint-disable valid-typeof */
9function isBrowseStorage (storage) {
10 try {
11 var testKey = '__xe_t'
12 storage.setItem(testKey, 1)
13 storage.removeItem(testKey)
14 return true
15 } catch (e) {
16 return false
17 }
18}
19
20function isBrowseType (type) {
21 return navigator.userAgent.indexOf(type) > -1
22}
23
24/**
25 * 获取浏览器内核
26 * @return Object
27 */
28function browse () {
29 var $body, isChrome, isEdge
30 var isMobile = false
31 var result = {
32 isNode: false,
33 isMobile: isMobile,
34 isPC: false,
35 isDoc: !!staticDocument
36 }
37 if (!staticWindow && typeof process !== staticStrUndefined) {
38 result.isNode = true
39 } else {
40 isEdge = isBrowseType('Edge')
41 isChrome = isBrowseType('Chrome')
42 isMobile = /(Android|webOS|iPhone|iPad|iPod|SymbianOS|BlackBerry|Windows Phone)/.test(navigator.userAgent)
43 if (result.isDoc) {
44 $body = staticDocument.body || staticDocument.documentElement
45 arrayEach(['webkit', 'khtml', 'moz', 'ms', 'o'], function (core) {
46 result['-' + core] = !!$body[core + 'MatchesSelector']
47 })
48 }
49 assign(result, {
50 edge: isEdge,
51 firefox: isBrowseType('Firefox'),
52 msie: !isEdge && result['-ms'],
53 safari: !isChrome && !isEdge && isBrowseType('Safari'),
54 isMobile: isMobile,
55 isPC: !isMobile,
56 isLocalStorage: isBrowseStorage(staticWindow.localStorage),
57 isSessionStorage: isBrowseStorage(staticWindow.sessionStorage)
58 })
59 }
60 return result
61}
62
63module.exports = browse