diff --git a/src/views/login.ejs b/src/views/login.ejs index e7e6a12e5..e3673d791 100644 --- a/src/views/login.ejs +++ b/src/views/login.ejs @@ -47,20 +47,8 @@ // Required for correct loading of scripts in Electron if (typeof module === 'object') {window.module = module; module = undefined;} - let device; - - if (window.location.search === '?desktop') { - device = "desktop"; - } - else if (window.location.search === '?mobile') { - device = "mobile"; - } - else { - device = isMobile() ? "mobile" : "desktop"; - } - + const device = getDeviceType() console.log("Setting device cookie to:", device); - setCookie("trilium-device", device); function setCookie(name, value) { @@ -70,15 +58,20 @@ document.cookie = name + "=" + (value || "") + expires + "; path=/"; } + function getDeviceType() { + if (window.location.search === '?desktop') return "desktop"; + if (window.location.search === '?mobile') return "mobile"; + return isMobile() ? "mobile" : "desktop"; + } + // https://stackoverflow.com/a/73731646/944162 function isMobile() { const mQ = matchMedia?.('(pointer:coarse)'); if (mQ?.media === '(pointer:coarse)') return !!mQ.matches; if ('orientation' in window) return true; - - return /\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(navigator.userAgent) || - /\b(Android|Windows Phone|iPad|iPod)\b/i.test(navigator.userAgent); + const userAgentsRegEx = /\b(Android|iPhone|iPad|iPod|Windows Phone|BlackBerry|webOS|IEMobile)\b/i + return userAgentsRegEx.test(navigator.userAgent) }