Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

user-agent.ts 949 B

You have to be logged in to leave a comment. Sign In
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
  1. // A tiny user agent checking RegExp for analytics purposes
  2. // The order matters with these
  3. const OS_REGEXPS = [
  4. /(iphone os|ipad os) ([^);]+)/i,
  5. /(mac) os x ([^);]+)/i,
  6. /(windows) ([^);]+)/i,
  7. /(android) ([^);]+)/i,
  8. /(cros) ([^);]+)/i,
  9. /(linux) ([^);]+)/i,
  10. ]
  11. // The order matters with these
  12. const BROWSER_REGEXPS = [
  13. /(firefox)\/([^\s)]+)/i,
  14. /(edge)\/([^\s)]+)/i,
  15. /(chrome)\/([^\s)]+)/i,
  16. /(safari)\/([^\s)]+)/i,
  17. /ms(ie)\/([^\s)]+)/i,
  18. ]
  19. export default function parseUserAgent(ua = navigator.userAgent) {
  20. ua = ua.toLowerCase()
  21. const osRe = OS_REGEXPS.find((re) => re.test(ua))
  22. let [, os = 'other', os_version = '0'] = (osRe && ua.match(osRe)) || []
  23. if (os === 'iphone os' || os === 'ipad os') os = 'ios'
  24. const browserRe = BROWSER_REGEXPS.find((re) => re.test(ua))
  25. const [, browser = 'other', browser_version = '0'] = (browserRe && ua.match(browserRe)) || []
  26. return { os, os_version, browser, browser_version }
  27. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...