UNPKG

jshow-device-detect

Version:
144 lines 4.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const constants_1 = require("../constants"); const brower_1 = require("../utils/brower"); const parser_1 = tslib_1.__importDefault(require("./parser")); class UADetect { constructor(ua) { this._parser = new parser_1.default(ua); } get parser() { return this._parser; } get userAgent() { return this._parser.userAgent; } set userAgent(value) { this._parser.reset(value); } get browser() { return this._parser.browser; } get engine() { return this._parser.engine; } get os() { return this._parser.os; } get device() { return this._parser.device; } get cpu() { return this._parser.cpu; } reset(ua) { this._parser.reset(ua); return this; } isMobile(ua) { const device = ua ? this._parser.getDevice(ua) : this._parser.device; switch (device.type) { case constants_1.DEVICE_TYPES.MOBILE: case constants_1.DEVICE_TYPES.TABLET: return true; default: return false; } } isSafari(ua, onlyMobile = false) { const browser = ua ? this._parser.getBrowser(ua) : this._parser.browser; switch (browser.name) { case constants_1.BROWSER_TYPES.SAFARI: return !onlyMobile; case constants_1.BROWSER_TYPES.MOBILE_SAFARI: return true; default: return false; } } isFirefox(ua) { const browser = ua ? this._parser.getBrowser(ua) : this._parser.browser; return browser.name === constants_1.BROWSER_TYPES.FIREFOX; } isIE(ua) { const browser = ua ? this._parser.getBrowser(ua) : this._parser.browser; switch (browser.name) { case constants_1.BROWSER_TYPES.INTERNET_EXPLORER: case constants_1.BROWSER_TYPES.IE: return true; default: return false; } } isEdge(ua, type) { const isChromium = this.isEdgeChromium(ua); switch (type) { case 'Chromium': if (isChromium) return true; break; case 'Legacy': if (isChromium) return false; break; } const browser = ua ? this._parser.getBrowser(ua) : this._parser.browser; return browser.name === constants_1.BROWSER_TYPES.EDGE; } isWindows(ua, onlyMobile = false) { const os = ua ? this._parser.getOS(ua) : this._parser.os; return os.name === (onlyMobile ? constants_1.OS_TYPES.WINDOWS_PHONE : constants_1.OS_TYPES.WINDOWS); } isAndroid(ua) { const os = ua ? this._parser.getOS(ua) : this._parser.os; return os.name === constants_1.OS_TYPES.ANDROID; } isMac(ua) { const os = ua ? this._parser.getOS(ua) : this._parser.os; return os.name === constants_1.OS_TYPES.MAC_OS; } isIOS(ua) { const os = ua ? this._parser.getOS(ua) : this._parser.os; return os.name === constants_1.OS_TYPES.IOS || this.isIOS13('iPad'); } isIOS13(type) { const nav = (0, brower_1.getNavigator)(); if (!nav) return false; const platform = nav.platform; if (!platform) return false; return ((type ? platform.indexOf(type) !== -1 : /iPad|iPhone|iPod/.test(platform)) || (platform === 'MacIntel' && nav.maxTouchPoints > 1 && !window.MSStream)); } isMIUI(ua) { const browser = ua ? this._parser.getBrowser(ua) : this._parser.browser; return browser.name === constants_1.BROWSER_TYPES.MIUI; } isElectron(ua) { const _ua = (ua || this._parser.userAgent).toLowerCase(); return _ua ? /electron/.test(_ua) : false; } isPad(ua) { const device = ua ? this._parser.getDevice(ua) : this._parser.device; return (device.type === constants_1.DEVICE_TYPES.TABLET) || this.isIOS13('iPad'); } isWechat(ua, type = 'mobile') { const browser = ua ? this._parser.getBrowser(ua) : this._parser.browser; switch (browser.name) { case constants_1.BROWSER_TYPES.WECHAT: return !(type !== 'mobile'); case constants_1.BROWSER_TYPES.WECHAT_DESKTOP: return !(type !== 'desktop'); default: return false; } } isEdgeChromium(ua) { const _ua = ua || this._parser.userAgent; return _ua.indexOf('Edg/') !== -1; } } exports.default = UADetect; //# sourceMappingURL=detect.js.map