UNPKG

unified-video-framework

Version:

Cross-platform video player framework supporting iOS, Android, Web, Smart TVs (Samsung/LG), Roku, and more

184 lines 6.38 kB
export class DeviceDetection { static getInstance() { if (!DeviceDetection.instance) { DeviceDetection.instance = new DeviceDetection(); } return DeviceDetection.instance; } detectDevice() { const userAgent = this.getUserAgent(); return { deviceType: this.getDeviceType(userAgent), os: this.getOS(userAgent), osVersion: this.getOSVersion(userAgent), browser: this.getBrowser(userAgent), browserVersion: this.getBrowserVersion(userAgent), screen: this.getScreenInfo(), userAgent, isMobile: this.isMobile(userAgent), isTablet: this.isTablet(userAgent), isDesktop: this.isDesktop(userAgent), isSmartTV: this.isSmartTV(userAgent), isTizen: this.isTizen(userAgent), isWebOS: this.isWebOS(userAgent), isRoku: this.isRoku(userAgent) }; } getDeviceInfo() { const detection = this.detectDevice(); return { deviceType: detection.deviceType, os: detection.os, osVersion: detection.osVersion, browser: detection.browser, browserVersion: detection.browserVersion, screen: detection.screen, userAgent: detection.userAgent, language: this.getLanguage(), timezone: this.getTimezone() }; } getNetworkInfo() { const connection = this.getConnection(); return { connectionType: connection?.type, effectiveType: connection?.effectiveType, downlink: connection?.downlink, rtt: connection?.rtt, online: navigator?.onLine ?? true }; } getUserAgent() { return typeof navigator !== 'undefined' ? navigator.userAgent : ''; } getDeviceType(userAgent) { if (this.isSmartTV(userAgent)) return 'smart_tv'; if (this.isMobile(userAgent)) return 'mobile'; if (this.isTablet(userAgent)) return 'tablet'; return 'desktop'; } getOS(userAgent) { if (/Windows NT/i.test(userAgent)) return 'Windows'; if (/Mac OS X/i.test(userAgent)) return 'macOS'; if (/Linux/i.test(userAgent)) return 'Linux'; if (/Android/i.test(userAgent)) return 'Android'; if (/iPhone|iPad|iPod/i.test(userAgent)) return 'iOS'; if (/Tizen/i.test(userAgent)) return 'Tizen'; if (/webOS/i.test(userAgent)) return 'webOS'; if (/Roku/i.test(userAgent)) return 'Roku'; return 'Unknown'; } getOSVersion(userAgent) { let match; match = userAgent.match(/Windows NT ([\d.]+)/); if (match) return match[1]; match = userAgent.match(/Mac OS X ([\d_]+)/); if (match) return match[1].replace(/_/g, '.'); match = userAgent.match(/OS ([\d_]+)/); if (match) return match[1].replace(/_/g, '.'); match = userAgent.match(/Android ([\d.]+)/); if (match) return match[1]; match = userAgent.match(/Tizen ([\d.]+)/); if (match) return match[1]; return undefined; } getBrowser(userAgent) { if (/Chrome/i.test(userAgent) && !/Edge/i.test(userAgent)) return 'Chrome'; if (/Firefox/i.test(userAgent)) return 'Firefox'; if (/Safari/i.test(userAgent) && !/Chrome/i.test(userAgent)) return 'Safari'; if (/Edge/i.test(userAgent)) return 'Edge'; if (/Opera/i.test(userAgent)) return 'Opera'; if (/Samsung/i.test(userAgent)) return 'Samsung Internet'; return undefined; } getBrowserVersion(userAgent) { let match; match = userAgent.match(/Chrome\/([\d.]+)/); if (match && !/Edge/i.test(userAgent)) return match[1]; match = userAgent.match(/Firefox\/([\d.]+)/); if (match) return match[1]; match = userAgent.match(/Version\/([\d.]+).*Safari/); if (match) return match[1]; match = userAgent.match(/Edge\/([\d.]+)/); if (match) return match[1]; match = userAgent.match(/Opera.*Version\/([\d.]+)/); if (match) return match[1]; return undefined; } getScreenInfo() { if (typeof window === 'undefined' || !window.screen) { return { width: 1920, height: 1080 }; } const orientation = window.screen.width > window.screen.height ? 'landscape' : 'portrait'; return { width: window.screen.width, height: window.screen.height, orientation }; } getLanguage() { if (typeof navigator === 'undefined') return 'en-US'; return navigator.language || 'en-US'; } getTimezone() { if (typeof Intl === 'undefined') return 'UTC'; return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC'; } getConnection() { if (typeof navigator === 'undefined') return null; return navigator.connection || navigator.mozConnection || navigator.webkitConnection; } isMobile(userAgent) { return /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent); } isTablet(userAgent) { return /iPad|Android.*(?!.*Mobile)/i.test(userAgent); } isDesktop(userAgent) { return !this.isMobile(userAgent) && !this.isSmartTV(userAgent); } isSmartTV(userAgent) { return /Smart.*TV|Tizen|webOS|Roku|PlayStation|Xbox/i.test(userAgent); } isTizen(userAgent) { return /Tizen/i.test(userAgent); } isWebOS(userAgent) { return /webOS/i.test(userAgent); } isRoku(userAgent) { return /Roku/i.test(userAgent); } } export const deviceDetection = DeviceDetection.getInstance(); //# sourceMappingURL=DeviceDetection.js.map