From 822431ac14a6f13b65ad5d1eb31659353203bfc6 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 3 Dec 2020 13:53:47 +0100 Subject: [PATCH] guess platform, might need tweaking still --- src/Platform.js | 32 +++++++++++++++++++++++++++----- src/main.js | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Platform.js b/src/Platform.js index 328826c..21de264 100644 --- a/src/Platform.js +++ b/src/Platform.js @@ -26,10 +26,32 @@ export const Platform = createEnum( "Linux" ); -export function guessApplicablePlatforms(userAgent) { - // use https://github.com/faisalman/ua-parser-js to guess, and pass as RootVM options - return [Platform.DesktopWeb, Platform.Linux]; - // return [Platform.MobileWeb, Platform.Android]; +export function guessApplicablePlatforms(userAgent, platform) { + // return [Platform.DesktopWeb, Platform.Linux]; + let nativePlatform; + let webPlatform; + if (/android/i.test(userAgent)) { + nativePlatform = Platform.Android; + webPlatform = Platform.MobileWeb; + } else if ( // https://stackoverflow.com/questions/9038625/detect-if-device-is-ios/9039885 + ( + /iPad|iPhone|iPod/.test(navigator.platform) || + (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) + ) && !window.MSStream + ) { + nativePlatform = Platform.iOS; + webPlatform = Platform.MobileWeb; + } else if (platform.toLowerCase().indexOf("linux") !== -1) { + nativePlatform = Platform.Linux; + webPlatform = Platform.DesktopWeb; + } else if (platform.toLowerCase().indexOf("mac") !== -1) { + nativePlatform = Platform.macOS; + webPlatform = Platform.DesktopWeb; + } else { + nativePlatform = Platform.Windows; + webPlatform = Platform.DesktopWeb; + } + return [nativePlatform, webPlatform]; } export function isWebPlatform(p) { @@ -39,4 +61,4 @@ export function isWebPlatform(p) { export function isDesktopPlatform(p) { return p === Platform.Linux || p === Platform.Windows || p === Platform.macOS; -} \ No newline at end of file +} diff --git a/src/main.js b/src/main.js index 3ffd2f1..16c324a 100644 --- a/src/main.js +++ b/src/main.js @@ -24,7 +24,7 @@ export async function main(container) { const vm = new RootViewModel({ request: xhrRequest, openLink: url => location.href = url, - platforms: guessApplicablePlatforms(navigator.userAgent), + platforms: guessApplicablePlatforms(navigator.userAgent, navigator.platform), preferences: new Preferences(window.localStorage), origin: location.origin, });