js 判断当前页面处于什么设备终端和浏览器环境
const getBrowser = () => { const { userAgent, browserLanguage, language } = navigator const outputDevice = { isIos: !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios 终端 isMobile: !!userAgent.match(/AppleWebKit.*Mobile.*/), // 是否移动终端 isAndroid: userAgent.indexOf('Android') > -1 || userAgent.indexOf('Linux') > -1, // android 终端端或者uc 浏览器 isIphone: !!userAgent.indexOf('iPhone') > -1, // 是否为iPhone或则QQHD浏览器 iPad: userAgent.indexOf('iPad') > -1, // 是否是iPad isWebApp: userAgent.indexOf('Safari') === -1, // 是否为Webapp,没有头部和底部 isTrident: userAgent.indexOf('Trident') > -1, //IE内核 isPresto: userAgent.indexOf('Presto') > -1, // opera 内核 isWebKit: userAgent.indexOf('AppleWebkit') > -1, // 苹果谷歌内核 isGecko: userAgent.indexOf('Gecko') > -1 && userAgent.indexOf('KHTML') === -1, //火狐内核 isWechat: !!userAgent.match(/micromessenger/gi), isWeiBo: !!userAgent.match(/weibo/gi), isQQ: !!userAgent.match(/qq/gi), } const outputLang = (browserLanguage || language).toLowerCase(); return { outputDevice, outputLang } }
navigator对象:
appCodeName: "Mozilla" appName: "Netscape" appVersion: "5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" bluetooth: Bluetooth {} clipboard: Clipboard {} connection: NetworkInformation {onchange: null, effectiveType: '4g', rtt: 50, downlink: 10, saveData: false} cookieEnabled: true credentials: CredentialsContainer {} deviceMemory: 8 doNotTrack: null geolocation: Geolocation {} hardwareConcurrency: 12 hid: HID {onconnect: null, ondisconnect: null} ink: Ink {} keyboard: Keyboard {} language: "zh-CN" languages: (2) ['zh-CN', 'zh'] locks: LockManager {} managed: NavigatorManagedData {onmanagedconfigurationchange: null} maxTouchPoints: 0 mediaCapabilities: MediaCapabilities {} mediaDevices: MediaDevices {ondevicechange: null} mediaSession: MediaSession {metadata: null, playbackState: 'none'} mimeTypes: MimeTypeArray {0: MimeType, 1: MimeType, application/pdf: MimeType, text/pdf: MimeType, length: 2} onLine: true pdfViewerEnabled: true permissions: Permissions {} platform: "Win32" plugins: PluginArray {0: Plugin, 1: Plugin, 2: Plugin, 3: Plugin, 4: Plugin, PDF Viewer: Plugin, Chrome PDF Viewer: Plugin, Chromium PDF Viewer: Plugin, Microsoft Edge PDF Viewer: Plugin, WebKit built-in PDF: Plugin, …} presentation: Presentation {defaultRequest: null, receiver: null} product: "Gecko" productSub: "20030107" scheduling: Scheduling {} serial: Serial {onconnect: null, ondisconnect: null} serviceWorker: ServiceWorkerContainer {controller: null, ready: Promise, oncontrollerchange: null, onmessage: null, onmessageerror: null} storage: StorageManager {} usb: USB {onconnect: null, ondisconnect: null} userActivation: UserActivation {hasBeenActive: true, isActive: true} userAgent: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" userAgentData: NavigatorUAData brands: Array(3) 0: {brand: ' Not A;Brand', version: '99'} 1: {brand: 'Chromium', version: '98'} 2: {brand: 'Google Chrome', version: '98'} length: 3 [[Prototype]]: Array(0) mobile: false platform: "Windows" [[Prototype]]: NavigatorUAData vendor: "Google Inc." vendorSub: ""
记录进步!!!