Fix OS detection for cross-browser compatibility (#8784)

* chore: add .python-version to .gitignore

* fix(get_os): ensure compatibility across all browsers

* format

* chore: add ts ignore comment again

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
This commit is contained in:
Michael 2024-07-18 00:51:36 +02:00 committed by GitHub
parent 6073736651
commit 2cc813a287
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
"website": minor
---
feat:Fix OS detection for cross-browser compatibility

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ __tmp/*
*.pyi
py.typed
.ipynb_checkpoints/
.python-version
# JS build
gradio/templates/*

View File

@ -44,16 +44,25 @@
el.focus();
}
function get_os() {
function get_os(): string {
// @ts-ignore - userAgentData is not yet in the TS types as it is currently experimental
return navigator.userAgentData.platform ?? navigator.userAgent;
if (typeof navigator !== "undefined") {
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("win") > -1) return "Windows";
if (userAgent.indexOf("mac") > -1) return "MacOS";
if (userAgent.indexOf("linux") > -1) return "Linux";
if (userAgent.indexOf("android") > -1) return "Android";
if (userAgent.indexOf("iphone") > -1 || userAgent.indexOf("ipad") > -1)
return "iOS";
}
return "Unknown";
}
let meta_key = "⌘";
$: if (browser && navigator) {
let os = get_os();
meta_key = os.includes("Mac") || os.includes("mac") ? "⌘" : "CTRL+";
meta_key = os === "MacOS" || os === "iOS" ? "⌘" : "CTRL+";
}
function handle_key_down(e: KeyboardEvent): void {