ua-parser-js icon indicating copy to clipboard operation
ua-parser-js copied to clipboard

don't rely on getHighEntropyValues to detect Apple Silicon

Open kkuepper opened this issue 1 year ago • 8 comments

Is your feature request related to a problem? Please describe. isAppleSilicon() is not returning the expected result on Firefox and Safari.

Describe the solution you'd like Instead of relying on getHighEntropyValues, it's testing if WebGL is working as described on StackOverflow

Describe alternatives you've considered Currently we're using the following code instead of ua-parser-js implementation:

function isAppleSilicon() {
  try {
    // Best guess if the device uses Apple Silicon: https://stackoverflow.com/a/65412357
    const w = document.createElement("canvas").getContext("webgl");
    if (w == null) {
      return false;
    }
    const d = w.getExtension("WEBGL_debug_renderer_info");
    const g = (d && w.getParameter(d.UNMASKED_RENDERER_WEBGL)) || "";
    if (g.match(/Apple/) && !g.match(/Apple GPU/)) {
      return true;
    }

    if (
      // @ts-expect-error - Object is possibly 'null'
      w.getSupportedExtensions().includes("WEBGL_compressed_texture_s3tc_srgb")
    ) {
      return true;
    }
  } catch {
    return false;
  }

  return false;
}

Additional context I tested the above solution on Safari, Firefox and Chrome on M1 Mac. I didn't test on non-Apple Silicon though.

kkuepper avatar Jun 12 '24 13:06 kkuepper