From b44397a6bf0ecc52d06f9dffb5f291b68f0be1c7 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Tue, 7 Jan 2025 08:29:56 +0100 Subject: [PATCH 1/2] refactor(views/login): add getDeviceType function --- src/views/login.ejs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/views/login.ejs b/src/views/login.ejs index a17a58909..8142b1c1c 100644 --- a/src/views/login.ejs +++ b/src/views/login.ejs @@ -43,20 +43,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) { @@ -66,6 +54,12 @@ 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)'); From 15faf161b53b80facb8ddc82e1b8e044d7249007 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Tue, 7 Jan 2025 08:37:37 +0100 Subject: [PATCH 2/2] refactor(views/login): simplify userAgent matching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - there is no need to have the Regexp check done in 2 separate tests – just do it once - I also have ordered the userAgents by order of "popularity", so (in theory) it should match faster for most people this way (although realistically you will not notice this at all) --- src/views/login.ejs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/views/login.ejs b/src/views/login.ejs index 8142b1c1c..af4152df5 100644 --- a/src/views/login.ejs +++ b/src/views/login.ejs @@ -66,9 +66,8 @@ 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) }