Use navigator.userAgentData in Chrome version page

navigator.userAgent will be deprecated in Chrome 101, so this CL makes
the JS code use navigator.userAgentData to keep retrieving the browser
version, while keeping the deprecated way as a fallback.

In order to simplify the logic now the page only shows the version for
Chrome, and doesn't work with other browsers.

Also, due to the complexity of parsing the Chrome iOS user agent string,
this OS isn't supported yet.

Fixed: misc:43
Change-Id: I23e7221318013afb3b1bba738e9c07851ea1692f
diff --git a/overrides/assets/javascripts/ua_utils.js b/overrides/assets/javascripts/ua_utils.js
new file mode 100644
index 0000000..fdfc383
--- /dev/null
+++ b/overrides/assets/javascripts/ua_utils.js
@@ -0,0 +1,56 @@
+// @Author: avm99963 (https://www.avm99963.com)
+/* @License: The MIT License (MIT)
+
+Copyright (c) 2021 Adrià Vilanova Martínez
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE. */
+
+// Detect the current OS
+function detectOS() {
+  var linuxRegex = /Linux/i;
+  var winRegex = /Win/i;
+  var winNT7 = /NT 7/i;
+  var winNT6 = /NT 6/i;
+  var macRegex = /Mac/i;
+  var crosRegex = /CrOS/i;
+
+  var uaPromise = null;
+  if (navigator.userAgentData) {
+    uaPromise = navigator.userAgentData
+                    .getHighEntropyValues(['platform', 'platformVersion'])
+                    .then(ua => {
+                      return ua?.platform + ' ' + ua?.platformVersion;
+                    });
+  } else {
+    uaPromise = new Promise((res, rej) => {
+      res(navigator.userAgent ?? '');
+    });
+  }
+
+  return uaPromise.then(ua => {
+    if (linuxRegex.test(ua)) return 'linux';
+    if (winRegex.test(ua)) {
+      if (winNT7.test(ua) || winNT6.test(ua)) return 'win_7_or_less';
+      return 'win_latest';
+    }
+    if (macRegex.test(ua)) return 'mac';
+    if (crosRegex.test(ua)) return 'cros';
+    return null;
+  });
+}