1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 1× 1× 1× 1× 3× 21× 2× 1× | // 参考 https://github.com/mumuy/browser let u = navigator.userAgent let match = { //内核 'Trident': u.indexOf('Trident') > -1 || u.indexOf('NET CLR') > -1, 'Presto': u.indexOf('Presto') > -1, 'WebKit': u.indexOf('AppleWebKit') > -1, 'Gecko': u.indexOf('Gecko/') > -1, 'KHTML': u.indexOf('KHTML/') > -1, //h5+app 'Html5Plus':u.indexOf('Html5Plus') > -1, //系统或平台 'Windows': u.indexOf('Windows') > -1, 'Linux': u.indexOf('Linux') > -1 || u.indexOf('X11') > -1, 'Mac OS': u.indexOf('Macintosh') > -1, 'Android': u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, 'Ubuntu': u.indexOf('Ubuntu') > -1, 'FreeBSD': u.indexOf('FreeBSD') > -1, 'Debian': u.indexOf('Debian') > -1, 'Windows Phone': u.indexOf('IEMobile') > -1 || u.indexOf('Windows Phone') > -1, 'BlackBerry': u.indexOf('BlackBerry') > -1 || u.indexOf('RIM') > -1, 'MeeGo': u.indexOf('MeeGo') > -1, 'Symbian': u.indexOf('Symbian') > -1, 'iOS': u.indexOf('like Mac OS X') > -1, 'Chrome OS': u.indexOf('CrOS') > -1, 'WebOS': u.indexOf('hpwOS') > -1, //设备 'Mobile': u.indexOf('Mobi') > -1 || u.indexOf('iPh') > -1 || u.indexOf('480') > -1, 'Tablet': u.indexOf('Tablet') > -1 || u.indexOf('Pad') > -1 || u.indexOf('Nexus 7') > -1 } let hash = { engine: ['WebKit', 'Trident', 'Gecko', 'Presto', 'KHTML'], os: ['Windows', 'Linux', 'Mac OS', 'Android', 'Ubuntu', 'FreeBSD', 'Debian', 'iOS', 'Windows Phone', 'BlackBerry', 'MeeGo', 'Symbian', 'Chrome OS', 'WebOS'], device: ['Mobile', 'Tablet'] }; for (let key in hash) { for (let item of hash[key]) { if (match[item]) { hash[key] = item; } } } function getLanguage() { // @ts-ignore return (navigator.browserLanguage || navigator.language).toLowerCase() } function getEngine() { return hash.engine } function getOs() { return hash.os } let osVersion = { 'Windows': function () { let v = u.replace(/^Mozilla\/\d.0 \(Windows NT ([\d.]+);.*$/, '$1'); let hash = { '10': '10', '6.4': '10', '6.3': '8.1', '6.2': '8', '6.1': '7', '6.0': 'Vista', '5.2': 'XP', '5.1': 'XP', '5.0': '2000' }; return hash[v] || v; }, 'Android': function () { return u.replace(/^.*Android ([\d.]+);.*$/, '$1'); }, 'iOS': function () { return u.replace(/^.*OS ([\d_]+) like.*$/, '$1').replace(/_/g, '.'); }, 'Debian': function () { return u.replace(/^.*Debian\/([\d.]+).*$/, '$1'); }, 'Windows Phone': function () { return u.replace(/^.*Windows Phone( OS)? ([\d.]+);.*$/, '$2'); }, 'Mac OS': function () { return u.replace(/^.*Mac OS X ([\d_]+).*$/, '$1').replace(/_/g, '.'); }, 'WebOS': function () { return u.replace(/^.*hpwOS\/([\d.]+);.*$/, '$1'); } }; function getOsVersion() { return osVersion[hash.os](); } function getDevice() { if (typeof hash.device != 'string') { return 'PC' } else { return hash.device } } function isIE() { return u.indexOf('MSIE') > -1 || u.indexOf('Trident') > -1; } function getIEVersion() { return u.replace(/^.*MSIE ([\d.]+).*$/, '$1').replace(/^.*rv:([\d.]+).*$/, '$1'); } function isPC() { return !match.Tablet || !match.Mobile } function isWin(){ return match.Windows } function isMac(){ return match["Mac OS"] } function isTablet() { return match.Tablet } function isMobile() { return match.Mobile } function isIos(){ return match.iOS } function isAndroid(){ return match.Android } function isApp(){ return match.Html5Plus } function isCanvas() { let elem = document.createElement('canvas'); return !!(elem.getContext && elem.getContext('2d')); } function isWebgl() { let elem = document.createElement('canvas'); return !!(window.WebGLRenderingContext && ( elem.getContext( 'webgl' ) || elem.getContext( 'experimental-webgl' ) )); } function isSvg() { return !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGRect; } function isWebsocket() { return 'WebSocket' in window || 'MozWebSocket' in window; } function isWorker() { return !!window.Worker; } function isLocalStorage() { let key = (+new Date()).toString(); try { localStorage.setItem(key, key); localStorage.removeItem(key); return true; } catch (e) { return false; } } function isGeolocation() { return 'geolocation' in navigator; } function isFile() { return !!(window.File && window.FileReader && window.FileList && window.Blob) } export { getLanguage, getEngine, getOs, getOsVersion, getDevice, getIEVersion, isIE, isPC, isMac, isWin, isTablet, isMobile, isAndroid, isIos, isApp, isCanvas, isWebgl, isSvg, isWebsocket, isWorker, isLocalStorage, isGeolocation, isFile } |