OS detection with userAgentData

by Eric Fortis

Chrome 93 (sync)

function isMac() {
  return navigator.userAgentData && navigator.userAgentData.platform
    ? navigator.userAgentData.platform === 'macOS'
    : /Mac|iP/.test(navigator.platform);
}

In Chrome 93 the platform string was demoted to a low entropy hint, and low ones are now available synchronously. So we check if platform is truthy only for handling Chrome 92.

Chrome 92 (async)

async function isMac() {
  if (navigator.userAgentData) {
    const ua = await navigator.userAgentData.getHighEntropyValues(['platform']);
    return ua.platform === 'macOS';
  }
  return /Mac|iP/.test(navigator.platform);
})

In Chrome 92, platform was considered a high entropy hint, so it needed to be queried asynchronously.

Sponsored by: