diff --git a/src/utils/vr-caps-detect.js b/src/utils/vr-caps-detect.js index de3c29143ed33d60344cfce609fb7937e6ed4c41..685fb3ba326cebd1febc8f6d6005c7c846a6d99b 100644 --- a/src/utils/vr-caps-detect.js +++ b/src/utils/vr-caps-detect.js @@ -10,12 +10,12 @@ export const VR_DEVICE_AVAILABILITY = { maybe: "maybe" // Implies this device may support this VR platform, but may not be installed or in a compatible browser }; -function isMaybeGearVRCompatibleDevice() { - return navigator.userAgent.match(/\WAndroid\W/); +function isMaybeGearVRCompatibleDevice(ua) { + return /\WAndroid\W/.test(ua); } -function isMaybeDaydreamCompatibleDevice() { - return navigator.userAgent.match(/\WAndroid\W/); +function isMaybeDaydreamCompatibleDevice(ua) { + return /\WAndroid\W/.test(ua); } // Blacklist of VR device name regex matchers that we do not want to consider as valid VR devices @@ -43,8 +43,9 @@ const GENERIC_ENTRY_TYPE_DEVICE_BLACKLIST = [/cardboard/i]; // // This function also detects if the user is already in a headset, and returns the isInHMD key to be `true` if so. export async function getAvailableVREntryTypes() { - const isSamsungBrowser = browser.name === "chrome" && navigator.userAgent.match(/SamsungBrowser/); - const isOculusBrowser = navigator.userAgent.match(/Oculus/); + const ua = navigator.userAgent; + const isSamsungBrowser = browser.name === "chrome" && /SamsungBrowser/.test(ua); + const isOculusBrowser = /Oculus/.test(ua); const isInHMD = isOculusBrowser; // This needs to be kept up-to-date with the latest browsers that can support VR and Hubs. @@ -73,21 +74,21 @@ export async function getAvailableVREntryTypes() { // // If we are in Oculus Browser (ie, we are literally wearing a GearVR) then return 'yes'. let gearvr = VR_DEVICE_AVAILABILITY.no; - if (isMaybeGearVRCompatibleDevice()) { + if (isMaybeGearVRCompatibleDevice(ua)) { gearvr = isOculusBrowser ? VR_DEVICE_AVAILABILITY.yes : VR_DEVICE_AVAILABILITY.maybe; } // For daydream detection, we first check if they are on an Android compatible device, and assume they // may support daydream *unless* this browser has WebVR capabilities, in which case we can do better. let daydream = - isMaybeDaydreamCompatibleDevice() && !isInHMD ? VR_DEVICE_AVAILABILITY.maybe : VR_DEVICE_AVAILABILITY.no; + isMaybeDaydreamCompatibleDevice(ua) && !isInHMD ? VR_DEVICE_AVAILABILITY.maybe : VR_DEVICE_AVAILABILITY.no; if (isWebVRCapableBrowser) { const displays = await navigator.getVRDisplays(); // Generic is supported for non-blacklisted devices and presentable HMDs. generic = displays.find( - d => d.capabilities.canPresent && !GENERIC_ENTRY_TYPE_DEVICE_BLACKLIST.find(r => d.displayName.match(r)) + d => d.capabilities.canPresent && !GENERIC_ENTRY_TYPE_DEVICE_BLACKLIST.find(r => r.test(d.displayName)) ) ? VR_DEVICE_AVAILABILITY.yes : VR_DEVICE_AVAILABILITY.no; @@ -95,12 +96,12 @@ export async function getAvailableVREntryTypes() { cardboard = !isIDevice && !isFirefoxBrowser && - displays.find(d => d.capabilities.canPresent && d.displayName.match(/cardboard/i)) + displays.find(d => d.capabilities.canPresent && /cardboard/i.test(d.displayName)) ? VR_DEVICE_AVAILABILITY.yes : VR_DEVICE_AVAILABILITY.no; // For daydream detection, in a WebVR browser we can increase confidence in daydream compatibility. - const hasDaydreamWebVRDevice = displays.find(d => d.displayName.match(/daydream/i)); + const hasDaydreamWebVRDevice = displays.find(d => /daydream/i.test(d.displayName)); if (hasDaydreamWebVRDevice) { // If we detected daydream via WebVR