ua.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // UA can be overridden by about:config in FF or devtools in Chrome
  2. // so we'll test for window.chrome.app which is only defined in Chrome
  3. // and for browser.runtime.getBrowserInfo in Firefox 51+
  4. /** @typedef UA
  5. * @property {false | number} isChrome - Chrome/ium version number or `false`
  6. * @property {Boolean | number} isFirefox - boolean initially, Firefox version number when ready
  7. * @property {chrome.runtime.PlatformInfo.arch} arch
  8. * @property {chrome.runtime.PlatformInfo.os} os
  9. * @property {string} browserName
  10. * @property {string} browserVersion
  11. */
  12. const ua = {};
  13. export default ua;
  14. // using non-enumerable properties that won't be sent to content scripts via GetInjected
  15. Object.defineProperties(ua, {
  16. isChrome: {
  17. value: global.chrome?.app && +navigator.userAgent.match(/Chrom\S+?\/(\d+)|$/)[1] || false,
  18. },
  19. isFirefox: {
  20. // will be replaced with the version number in ready()
  21. value: !!browser.runtime.getBrowserInfo,
  22. configurable: true,
  23. },
  24. ready: {
  25. value: Promise.all([
  26. browser.runtime.getPlatformInfo(),
  27. browser.runtime.getBrowserInfo?.(),
  28. ]).then(([{ os, arch }, { name, version } = {}]) => {
  29. Object.assign(ua, {
  30. arch,
  31. os,
  32. browserName: name?.toLowerCase() || 'chrome',
  33. browserVersion: version || navigator.userAgent.match(/Chrom\S+?\/(\S+)|$/)[1],
  34. });
  35. if (ua.isFirefox) {
  36. Object.defineProperty(ua, 'isFirefox', {
  37. value: parseFloat(version),
  38. });
  39. }
  40. }),
  41. },
  42. });