UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

1 lines 14.3 kB
{"version":3,"file":"browser.mjs","sources":["../../../src/tools/browser.ts"],"sourcesContent":["import { Type } from './type'\n\nlet UA: string = ''\ntry {\n UA = navigator?.userAgent.toLowerCase()\n} catch {\n UA = '?'\n}\n\n/**\n * Cheap user-agent / platform / capability detector for browser environments.\n *\n * Exported as the `Browser` singleton. All checks are derived from a module-level\n * `UA` string (a lower-cased `navigator.userAgent`, captured once at import time)\n * or from browser globals (`window`, `document`, `navigator`, `localStorage`).\n * Because of this, the methods are only meaningful when running in a browser —\n * in non-browser environments (e.g. Node/SSR) `UA` falls back to `'?'` and any\n * method that touches `window`/`document`/`localStorage` directly may throw.\n *\n * @see bitrix/js/main/core/src/lib/browser.js\n */\nclass BrowserManager {\n /**\n * Checks whether the current browser is Opera.\n *\n * @returns `true` if the user agent string contains `opera`.\n */\n isOpera(): boolean {\n return UA.includes('opera')\n }\n\n /**\n * Checks whether the current browser is Internet Explorer (any version).\n *\n * @returns `true` if `document` exposes the legacy `attachEvent` API and the browser is not Opera.\n */\n isIE(): boolean {\n return 'attachEvent' in document && !this.isOpera()\n }\n\n /**\n * Checks whether the current browser is Internet Explorer 6.\n *\n * @returns `true` if the user agent string contains `msie 6`.\n */\n isIE6(): boolean {\n return UA.includes('msie 6')\n }\n\n /**\n * Checks whether the current browser is Internet Explorer 7.\n *\n * @returns `true` if the user agent string contains `msie 7`.\n */\n isIE7(): boolean {\n return UA.includes('msie 7')\n }\n\n /**\n * Checks whether the current browser is Internet Explorer 8.\n *\n * @returns `true` if the user agent string contains `msie 8`.\n */\n isIE8(): boolean {\n return UA.includes('msie 8')\n }\n\n /**\n * Checks whether the current browser is Internet Explorer 9 or the document is rendered in IE9+ document mode.\n *\n * @returns `true` if `document.documentMode` is defined and `>= 9`.\n */\n isIE9(): boolean {\n return 'documentMode' in document && ((document?.documentMode as number) >= 9)\n }\n\n /**\n * Checks whether the current browser is Internet Explorer 10 or the document is rendered in IE10+ document mode.\n *\n * @returns `true` if `document.documentMode` is defined and `>= 10`.\n */\n isIE10(): boolean {\n return 'documentMode' in document && ((document?.documentMode as number) >= 10)\n }\n\n /**\n * Checks whether the current browser is Safari.\n *\n * @returns `true` if the user agent string contains `safari` and does not contain `chrome`.\n */\n isSafari(): boolean {\n return UA.includes('safari') && !UA.includes('chrome')\n }\n\n /**\n * Checks whether the current browser is Firefox.\n *\n * @returns `true` if the user agent string contains `firefox`.\n */\n isFirefox() {\n return UA.includes('firefox')\n }\n\n /**\n * Checks whether the current browser is Chrome.\n *\n * @returns `true` if the user agent string contains `chrome`.\n */\n isChrome() {\n return UA.includes('chrome')\n }\n\n /**\n * Detects the Internet Explorer version using a chain of user-agent and\n * `document`/`navigator` heuristics (including legacy Trident/MSIE detection).\n *\n * @returns The detected IE version number, or `-1` if the browser is Opera, Safari, Firefox, or Chrome (i.e. not IE).\n */\n detectIEVersion() {\n if (\n this.isOpera()\n || this.isSafari()\n || this.isFirefox()\n || this.isChrome()\n ) {\n return -1\n }\n\n let rv = -1\n\n if (\n // @ts-expect-error we detect IEVersion ////\n !!window.MSStream\n // @ts-expect-error we detect IEVersion ////\n && !window.ActiveXObject\n && 'ActiveXObject' in window\n ) {\n rv = 11\n } else if (this.isIE10()) {\n rv = 10\n } else if (this.isIE9()) {\n rv = 9\n } else if (this.isIE()) {\n rv = 8\n }\n\n if (rv === -1 || rv === 8) {\n if (navigator.appName === 'Microsoft Internet Explorer') {\n const re = /MSIE (\\d[.0-9]*)/\n const res = navigator.userAgent.match(re)\n\n if (Type.isArrayLike(res) && res.length > 0) {\n rv = Number.parseFloat(res[1]!)\n }\n }\n\n if (navigator.appName === 'Netscape') {\n // Alternative check for IE 11\n rv = 11\n const re = /Trident\\/.*rv:(\\d[.0-9]*)/\n\n if (re.exec(navigator.userAgent) != null) {\n const res = navigator.userAgent.match(re)\n\n if (Type.isArrayLike(res) && res.length > 0) {\n rv = Number.parseFloat(res[1]!)\n }\n }\n }\n }\n\n return rv\n }\n\n /**\n * Checks whether the current browser is Internet Explorer 11.\n *\n * @returns `true` if {@link detectIEVersion} resolves to `11` or higher.\n */\n isIE11(): boolean {\n return this.detectIEVersion() >= 11\n }\n\n /**\n * Checks whether the current OS is macOS.\n *\n * @returns `true` if the user agent string contains `macintosh`.\n */\n isMac(): boolean {\n return UA.includes('macintosh')\n }\n\n /**\n * Checks whether the current OS is Windows.\n *\n * @returns `true` if the user agent string contains `windows`.\n */\n isWin(): boolean {\n return UA.includes('windows')\n }\n\n /**\n * Checks whether the current OS is Linux (desktop, not Android).\n *\n * @returns `true` if the user agent string contains `linux` and the platform is not Android.\n */\n isLinux(): boolean {\n return UA.includes('linux') && !this.isAndroid()\n }\n\n /**\n * Checks whether the current OS is Android.\n *\n * @returns `true` if the user agent string contains `android`.\n */\n isAndroid(): boolean {\n return UA.includes('android')\n }\n\n /**\n * Checks whether the current device is an iPad.\n *\n * @returns `true` if the user agent string contains `ipad;`, or the platform is macOS with touch support (modern iPadOS reporting as Mac).\n */\n isIPad(): boolean {\n return UA.includes('ipad;') || (this.isMac() && this.isTouchDevice())\n }\n\n /**\n * Checks whether the current device is an iPhone.\n *\n * @returns `true` if the user agent string contains `iphone;`.\n */\n isIPhone(): boolean {\n return UA.includes('iphone;')\n }\n\n /**\n * Checks whether the current device runs iOS.\n *\n * @returns `true` if {@link isIPad} or {@link isIPhone} is `true`.\n */\n isIOS(): boolean {\n return this.isIPad() || this.isIPhone()\n }\n\n /**\n * Checks whether the current device is a mobile device.\n *\n * @returns `true` if the device is an iPhone, iPad, or Android device, or the user agent string contains `mobile` or `touch`.\n */\n isMobile(): boolean {\n return (\n this.isIPhone()\n || this.isIPad()\n || this.isAndroid()\n || UA.includes('mobile')\n || UA.includes('touch')\n )\n }\n\n /**\n * Checks whether the current display is a high-density (Retina) screen.\n *\n * @returns `true` if `window.devicePixelRatio` is defined and `>= 2`.\n */\n isRetina(): boolean {\n return (window.devicePixelRatio && window.devicePixelRatio >= 2) === true\n }\n\n /**\n * Checks whether the current device supports touch input.\n *\n * @returns `true` if `window` exposes `ontouchstart` or `navigator.maxTouchPoints` is greater than `0`.\n */\n isTouchDevice(): boolean {\n return (\n 'ontouchstart' in window\n || navigator.maxTouchPoints > 0\n )\n }\n\n /**\n * Checks whether a document is rendered in standards mode (as opposed to quirks mode).\n *\n * @param target - The document to inspect. Defaults to the global `document` when omitted/falsy.\n * @returns `true` if `compatMode` is `'CSS1Compat'`, or a truthy fallback based on `documentElement.clientHeight` when `compatMode` is unavailable.\n */\n isDoctype(target: any): boolean {\n const doc = target || document\n\n if (doc.compatMode) {\n return doc.compatMode === 'CSS1Compat'\n }\n\n return doc.documentElement && doc.documentElement.clientHeight\n }\n\n /**\n * Checks whether `localStorage` is available and writable in the current environment.\n *\n * @returns `true` if a test key can be written to and removed from `localStorage` without throwing.\n */\n isLocalStorageSupported(): boolean {\n try {\n localStorage.setItem('test', 'test')\n localStorage.removeItem('test')\n return true\n } catch {\n return false\n }\n }\n\n /**\n * Detects the Android OS version from the user agent string.\n *\n * @returns The parsed Android version number, or `0` if it cannot be detected (e.g. not Android).\n */\n detectAndroidVersion(): number {\n const re = /Android (\\d[.0-9]*)/\n\n if (re.exec(navigator.userAgent) != null) {\n const res = navigator.userAgent.match(re)\n if (Type.isArrayLike(res) && res.length > 0) {\n return Number.parseFloat(res[1]!)\n }\n }\n\n return 0\n }\n}\n\nexport const Browser = new BrowserManager()\n"],"names":[],"mappings":";;;;;;;;;;;;AAEA,IAAI,EAAA,GAAa,EAAA;AACjB,IAAI;AACF,EAAA,EAAA,GAAK,SAAA,EAAW,UAAU,WAAA,EAAY;AACxC,CAAA,CAAA,MAAQ;AACN,EAAA,EAAA,GAAK,GAAA;AACP;AAcA,MAAM,cAAA,CAAe;AAAA,EArBrB;AAqBqB,IAAA,MAAA,CAAA,IAAA,EAAA,gBAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMnB,OAAA,GAAmB;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