UI Drafter

OS detection with userAgentData

Eric Fortis

Chrome 93 (sync)

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

As of Chrome 93, the platform string was demoted to a low entropy hint, and low ones are now available synchronously. That's why we have to check if platform is truthy.

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: