UI: Implement JavaScript getOS function Windows platform architecture detection #104136

Merged
Márton Lente merged 15 commits from get-os-win-arm into main 2024-11-08 12:03:41 +01:00
Showing only changes of commit 3d284bbd00 - Show all commits

View File

@ -2,7 +2,97 @@
* https://code.videolan.org/VideoLAN.org/websites/-/blob/master/www.videolan.org/include/os-specific.php * https://code.videolan.org/VideoLAN.org/websites/-/blob/master/www.videolan.org/include/os-specific.php
*/ */
function getOS() { function getOS() {
function getOSWinVersionSync() { // Init variable OS default
var OS = "windows";
function getOSPlaformVersion() {
//MacOS, MacOS X, macOS
if (navigator.appVersion.indexOf("Mac") != -1) {
if (navigator.platform.indexOf("MacPPC") != -1 || navigator.platform.indexOf("PowerPC") != -1) {
OS = "macos-PPC";
} else if (navigator.userAgent.indexOf("OS X 10.5") != -1 ||
navigator.userAgent.indexOf("OS X 10.6") != -1) {
OS = "macos-32";
} else {
OS = "macos";
try {
var glcontext = document.createElement("canvas").getContext("webgl");
var debugrenderer = glcontext ? glcontext.getExtension('WEBGL_debug_renderer_info') : null;
var renderername = debugrenderer && glcontext.getParameter(debugrenderer.UNMASKED_RENDERER_WEBGL) || "";
if (renderername.match(/Apple M/) || renderername.match(/Apple GPU/)) {
OS = "macos-apple-silicon";
}
} catch (e) {}
}
}
// Linux
if (navigator.platform.indexOf("Linux") != -1) {
if ((navigator.userAgent.indexOf("Ubuntu") != -1) ||
(navigator.userAgent.indexOf("ubuntu") != -1)) OS = "linux-ubuntu";
else if (navigator.userAgent.indexOf("Debian") != -1) OS = "linux-debian";
else if (navigator.userAgent.indexOf("Android") != -1) OS = "linux-android";
else if (navigator.userAgent.indexOf("Mandriva") != -1) OS = "linux-mandriva";
else if (navigator.userAgent.indexOf("Red Hat") != -1) OS = "linux-redhat";
else if (navigator.userAgent.indexOf("Fedora") != -1) OS = "linux-fedora";
else if (navigator.userAgent.indexOf("SUSE") != -1) OS = "linux-suse";
else if (navigator.userAgent.indexOf("Gentoo") != -1) OS = "linux-gentoo";
else OS = "linux";
}
// iOS
if (navigator.userAgent.indexOf("iPad") != -1 || navigator.userAgent.indexOf("iPhone") != -1 || navigator.userAgent.indexOf("iPod") != -1) {
OS = "ios";
}
// FreeBSD
if (navigator.platform.indexOf("freebsd") != -1) OS = "freebsd";
if (navigator.platform.indexOf("FreeBSD") != -1) OS = "freebsd";
return OS;
}
// Create functions init
function init() {
// Check if the browser method 'navigator.userAgentData' is supported
if (navigator.userAgentData && typeof navigator.userAgentData.getHighEntropyValues == 'function') {
return initAsync();
} else {
return initSync();
}
}
// TODO: fix function async initAsync
// Create function initAsync for browsers that support navigator.userAgentData (e.g. Chromium-based browsers)
function initAsync() {
/* Code modified from Microsoft's website.
* https://learn.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11#sample-code-for-detecting-arm-or-x86
*/
navigator.userAgentData.getHighEntropyValues(["architecture", "bitness"])
.then(ua => {
// Init variable OS default
var OS = "windows";
// Check if the platform is Windows
if (navigator.userAgentData.platform === "Windows") {
if (ua.architecture === 'arm') {
OS = "windows-arm";
} else {
if (ua.bitness === '64') {
OS = "windows-64";
}
}
} else {
// Check other platforms
OS = getOSPlaformVersion();
}
return OS;
});
}
// Create function initSync for browsers that don't support navigator.userAgentData (e.g. Gecko-based browsers)
function initSync() {
if (navigator.appVersion.indexOf("Win") != -1) { if (navigator.appVersion.indexOf("Win") != -1) {
if (navigator.userAgent.indexOf('Windows NT 5.0') == -1 && if (navigator.userAgent.indexOf('Windows NT 5.0') == -1 &&
navigator.userAgent.indexOf('Windows NT 5.1') == -1 && navigator.userAgent.indexOf('Windows NT 5.1') == -1 &&
@ -30,111 +120,12 @@ function getOS() {
} catch (e) {} } catch (e) {}
} }
} }
}
}
function getOSMacVersion() {
//MacOS, MacOS X, macOS
if (navigator.appVersion.indexOf("Mac") != -1) {
if (navigator.platform.indexOf("MacPPC") != -1 || navigator.platform.indexOf("PowerPC") != -1) {
OS = "macos-PPC";
} else if (navigator.userAgent.indexOf("OS X 10.5") != -1 ||
navigator.userAgent.indexOf("OS X 10.6") != -1) {
OS = "macos-32";
} else {
OS = "macos";
try {
var glcontext = document.createElement("canvas").getContext("webgl");
var debugrenderer = glcontext ? glcontext.getExtension('WEBGL_debug_renderer_info') : null;
var renderername = debugrenderer && glcontext.getParameter(debugrenderer.UNMASKED_RENDERER_WEBGL) || "";
if (renderername.match(/Apple M/) || renderername.match(/Apple GPU/)) {
OS = "macos-apple-silicon";
}
} catch (e) {}
}
}
}
function getOSLinuxVersion() {
if (navigator.platform.indexOf("Linux") != -1) {
if ((navigator.userAgent.indexOf("Ubuntu") != -1) ||
(navigator.userAgent.indexOf("ubuntu") != -1)) OS = "linux-ubuntu";
else if (navigator.userAgent.indexOf("Debian") != -1) OS = "linux-debian";
else if (navigator.userAgent.indexOf("Android") != -1) OS = "linux-android";
else if (navigator.userAgent.indexOf("Mandriva") != -1) OS = "linux-mandriva";
else if (navigator.userAgent.indexOf("Red Hat") != -1) OS = "linux-redhat";
else if (navigator.userAgent.indexOf("Fedora") != -1) OS = "linux-fedora";
else if (navigator.userAgent.indexOf("SUSE") != -1) OS = "linux-suse";
else if (navigator.userAgent.indexOf("Gentoo") != -1) OS = "linux-gentoo";
else OS = "linux";
}
}
function getOSiPadVersion() {
if (navigator.userAgent.indexOf("iPad") != -1 || navigator.userAgent.indexOf("iPhone") != -1 || navigator.userAgent.indexOf("iPod") != -1) {
OS = "ios";
}
}
function getOSFreeBSDVersion() {
if (navigator.platform.indexOf("freebsd") != -1) OS = "freebsd";
if (navigator.platform.indexOf("FreeBSD") != -1) OS = "freebsd";
}
// Create functions init
function init() {
// Check if the browser method 'navigator.userAgentData' is supported
if (navigator.userAgentData && typeof navigator.userAgentData.getHighEntropyValues == 'function') {
initAsync();
} else { } else {
initSync(); OS = getOSPlaformVersion();
} }
}
// Create function initAsync for browsers that support navigator.userAgentData (e.g. Chromium-based browsers)
function initAsync() {
/* Code modified from Microsoft's website.
* https://learn.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11#sample-code-for-detecting-arm-or-x86
*/
navigator.userAgentData.getHighEntropyValues(["architecture", "bitness"])
.then(ua => {
// Init variable OS default
var OS = "windows";
// Check if the platform is Windows
if (navigator.userAgentData.platform === "Windows") {
if (ua.architecture === 'arm') {
OS = "windows-arm";
} else {
if (ua.bitness === '64') {
OS = "windows-64";
}
}
} else {
// Check other platforms
getOSMacVersion();
getOSLinuxVersion();
getOSiPadVersion();
getOSFreeBSDVersion();
}
return OS;
});
}
// Create function initSync for browsers that don't support navigator.userAgentData (e.g. Gecko-based browsers)
function initSync() {
// Init variable OS default
var OS = "windows";
getOSWinVersionSync();
getOSMacVersion();
getOSLinuxVersion();
getOSiPadVersion();
getOSFreeBSDVersion();
return OS; return OS;
} }
init(); return init();
} }