One option would be using navigator.userAgentData
along with the .getHighEntropyValues()
method. Then we can utilize the User-Agent Client Hints API and get high entropy values from userAgentData
by requesting a number of hints. Providing a means to detect OS from User-Agent information since navigator.platform
is no longer.
Or instead just access navigator.userAgent
which has full support and provides a user-agent string for the current browser where the OS could be extracted from.
function checkOS(n) {
if (n.userAgentData) {
const hints = ["architecture", "model", "platform", "platformVersion", "uaFullVersion"];
n.userAgentData.getHighEntropyValues(hints)
.then(ua => {
console.log(ua);
});
} else {
console.log(n.userAgent);
return "navigator.userAgentData is not supported!";
}
}
checkOS(navigator);
This is about the only documentation I've found in the DevTools "issues" tab aside from information on update on user agent string reduction.
A page or script is accessing at least one of navigator.userAgent
, navigator.appVersion
, and navigator.platform
. In a future version of Chrome, the amount of information available in the User Agent string will be reduced. - To fix this issue, replace the usage of navigator.userAgent
, navigator.appVersion
, and navigator.platform
with feature detection, progressive enhancement, or migrate to navigator.userAgentData
.
I tried logging navigator.userAgentData
to the console on a Windows and macOS machine using Chrome to see what kind of data it contained, and the object does have some useful information like the getHighEntropyValues()
method in particular.
// NavigatorUAData {brands: Array(3), mobile: true}
{
"brands": [
{
"brand": "Chromium",
"version": "92"
},
{
"brand": " Not A;Brand",
"version": "99"
},
{
"brand": "Google Chrome",
"version": "92"
}
],
"mobile": false,
"getHighEntropyValues": function getHighEntropyValues() { [native code] }
}
The browser support for navigator.userAgentData
is not great (only supported in Chrome, Edge, Opera), but this could be a step in the right direction for detecting OS from userAgentData
since navigator.platform
is no longer. CodePen demo