browser.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { UAParser } from 'ua-parser-js'
  3. const parser = new UAParser()
  4. export const browser = parser.getBrowser()
  5. export const device = parser.getDevice()
  6. export const os = parser.getOS()
  7. export const generateFingerprint = () => {
  8. const windowResolution = `${window.screen.availWidth}x${window.screen.availHeight}/${window.screen.pixelDepth}`
  9. const timezone = new Date().toString().match(/\s\(.+?\)$/)?.[0]
  10. const getMajorVersion = (version?: string): string => {
  11. if (!version) return 'unknown'
  12. const versionParts = version.split('.')
  13. return versionParts[0]
  14. }
  15. const hashCode = (string: string) => {
  16. return string.split('').reduce((a, b) => {
  17. // eslint-disable-next-line no-bitwise
  18. a = (a << 5) - a + b.charCodeAt(0)
  19. // eslint-disable-next-line no-bitwise
  20. return a & a
  21. }, 0)
  22. }
  23. return hashCode(
  24. `${browser.name}${getMajorVersion(browser.version)}${
  25. os.name
  26. }${getMajorVersion(os.version)}${windowResolution}${timezone}`,
  27. ).toString()
  28. }