Skip to content

Instantly share code, notes, and snippets.

@quantumwebco
Created September 10, 2022 13:13
Show Gist options
  • Save quantumwebco/12191f073f4b37fc83d1f635cecf3a03 to your computer and use it in GitHub Desktop.
Save quantumwebco/12191f073f4b37fc83d1f635cecf3a03 to your computer and use it in GitHub Desktop.
Get platform and standalone mode
isInStandaloneMode() {
return (window.matchMedia("(display-mode: standalone)").matches) ||
(("standalone" in window.navigator) && (window.navigator.standalone));
}
isMac() {
return /(macintosh|macintel|macppc|mac68k|macos)/i.test(window.navigator.userAgent.toLowerCase());
}
isIos() {
return /iphone|ipad|ipod/i.test(window.navigator.userAgent.toLowerCase());
}
platform() {
let userAgent = window.navigator.userAgent.toLowerCase(),
macosPlatforms = /(macintosh|macintel|macppc|mac68k|macos)/i,
windowsPlatforms = /(win32|win64|windows|wince)/i,
iosPlatforms = /(iphone|ipad|ipod)/i;
if (macosPlatforms.test(userAgent)) {
return "macos";
}
if (iosPlatforms.test(userAgent)) {
return "ios";
}
if (windowsPlatforms.test(userAgent)) {
return "windows";
}
if (/android/.test(userAgent)) {
return "android";
}
if (/linux/.test(userAgent)) {
return "linux";
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment