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/screenshot_page.js b/overrides/assets/javascripts/screenshot_page.js
index 8f443c3..55432f1 100644
--- a/overrides/assets/javascripts/screenshot_page.js
+++ b/overrides/assets/javascripts/screenshot_page.js
@@ -21,40 +21,6 @@
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;
- });
-}
-
// Get the OS from the 'os' GET parameter if set
function getOSGETParam() {
var sp = new URLSearchParams(location.search);
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;
+ });
+}
diff --git a/overrides/assets/javascripts/version_page.js b/overrides/assets/javascripts/version_page.js
index 825a57c..ecc57d1 100644
--- a/overrides/assets/javascripts/version_page.js
+++ b/overrides/assets/javascripts/version_page.js
@@ -23,272 +23,93 @@
var actualversion;
+function detectCrVersion() {
+ if (!navigator?.userAgentData)
+ return Promise.resolve(
+ window.navigator.appVersion.match(/Chrome\/(.*?) /)?.[1]);
+
+ return navigator.userAgentData.getHighEntropyValues(['fullVersionList'])
+ .then(res => {
+ let brands = res?.fullVersionList ?? [];
+ for (const b of brands)
+ if (b?.brand == 'Chromium' || b?.brand == 'Google Chrome')
+ return b?.version;
+
+ return undefined;
+ });
+}
+
+function convert2OmahaOS(os) {
+ if (os == 'linux' || os == 'mac' || os == 'cros') return os;
+ if (os == 'win_7_or_less' || os == 'win_latest') return 'win64';
+ return undefined;
+}
+
+function prettifyOS(os) {
+ if (os == 'linux') return 'Linux';
+ if (os == 'mac') return 'Mac';
+ if (os == 'cros') return 'Chrome OS';
+ if (os == 'win_7_or_less' || os == 'win_latest') return 'Windows';
+ return undefined;
+}
+
var app = {
init: function() {
- fetch('https://omahaproxy.appspot.com/all.json')
- .then(res => res.json())
- .then(chromeversions => {
- var browser = BrowserDetect.browser, version = BrowserDetect.version,
- version2, os3, os2 = System.OS(), os = BrowserDetect.OS;
- if (navigator.appVersion.indexOf('OPR') != -1) browser = 'Opera';
- if (browser == 'Chrome') {
- version2 = window.navigator.appVersion.match(/Chrome\/(.*?) /)[1];
- if (navigator.appVersion.indexOf('CrOS') != -1)
- var os = 'Chrome OS', os2 = 'cros', os3 = 'Chrome OS';
- else
- var os3 = os2,
- os2 = str_replace(
- ['Windows', 'Mac', 'iPhone/iPod', 'Chrome OS', 'Linux'],
- ['win64', 'mac', 'ios', 'cros', 'linux'], os);
- var matches = chromeversions.filter(function(val, index, array) {
- return val.os === os2;
- });
- var actualchrome =
- matches[0].versions.filter(function(val, index, array) {
- return val.channel === 'stable';
- });
- actualversion = actualchrome[0].version.split('.')[0];
- if (version < actualversion) {
- document.getElementById('updated').innerHTML =
- '¡Oh, no! Google Chrome ' +
- '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.';
- } else if (version > actualversion) {
- document.getElementById('updated').innerHTML =
- 'No estás usando el canal estable de Chrome. Ve con cuidado ;-)';
- } else if (version2 != actualchrome[0].version) {
- document.getElementById('updated').innerHTML =
- 'Oops, Google Chrome ' +
- '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.';
- } else {
- document.getElementById('updated').innerHTML =
- 'Chrome está actualizado';
- }
- } else {
- os3 = os2;
- version2 = version;
+ let promises = [];
+ promises.push(fetch('https://omahaproxy.appspot.com/all.json')
+ .then(res => res.json()));
+ promises.push(detectOS());
+ promises.push(detectCrVersion());
+
+ Promise.all(promises)
+ .then(responses => {
+ const [chromeVersions, os, version] = responses;
+ if (!version) {
+ document.getElementById('version').textContent =
+ 'No hemos podido determinar tu versión.';
+ document.getElementById('updated').textContent =
+ 'Es posible que no estés usando Chrome o que la página no funcione correctamente.';
+ return;
}
+
+ let majorVersion = version?.split?.('.')?.[0];
+ let omahaOS = convert2OmahaOS(os);
+ let matches = chromeVersions.filter(function(val, index, array) {
+ return val.os === omahaOS;
+ });
+ var actualChrome =
+ matches?.[0]?.versions?.filter?.(function(val, index, array) {
+ return val.channel === 'stable';
+ });
+ actualVersion = actualChrome?.[0]?.version?.split?.('.')?.[0];
+ if (majorVersion < actualVersion) {
+ document.getElementById('updated').innerHTML =
+ '¡Oh, no! Google Chrome ' +
+ '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.';
+ } else if (majorVersion > actualVersion) {
+ document.getElementById('updated').innerHTML =
+ 'No estás usando el canal estable de Chrome. Ve con cuidado ;-)';
+ } else if (version != actualChrome[0].version) {
+ document.getElementById('updated').innerHTML =
+ 'Oops, Google Chrome ' +
+ '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.';
+ } else {
+ document.getElementById('updated').innerHTML =
+ 'Chrome está actualizado';
+ }
+ let prettyOS = prettifyOS(os);
+ document.getElementById('version').textContent = 'Usas la versión ' +
+ version + ' de Chrome' + (prettyOS ? ' en ' + prettyOS : '');
+ })
+ .catch(err => {
document.getElementById('version').textContent =
- 'Usas la versión ' + version2 + ' de ' + browser + ' en ' + os3;
+ 'Ha ocurrido un error inesperado.';
+ document.getElementById('updated').textContent =
+ 'Error: ' + err + '.';
});
}
-}
-
-// Function to detect navigator (informally browser)
-// Source:
-
-var BrowserDetect = {
- init: function() {
- this.browser = this.searchString(this.dataBrowser) || '?';
- this.version = this.searchVersion(navigator.userAgent) ||
- this.searchVersion(navigator.appVersion) || '?';
- this.OS = this.searchString(this.dataOS) || '?';
- },
- searchString: function(data) {
- for (var i = 0; i < data.length; i++) {
- var dataString = data[i].string;
- var dataProp = data[i].prop;
- this.versionSearchString = data[i].versionSearch || data[i].identity;
- if (dataString) {
- if (dataString.indexOf(data[i].subString) != -1)
- return data[i].identity;
- } else if (dataProp)
- return data[i].identity;
- }
- },
- searchVersion: function(dataString) {
- var index = dataString.indexOf(this.versionSearchString);
- if (index == -1) return;
- return parseFloat(
- dataString.substring(index + this.versionSearchString.length + 1));
- },
- dataBrowser: [
- {string: navigator.userAgent, subString: 'Chrome', identity: 'Chrome'}, {
- string: navigator.userAgent,
- subString: 'OmniWeb',
- versionSearch: 'OmniWeb/',
- identity: 'OmniWeb'
- },
- {
- string: navigator.vendor,
- subString: 'Apple',
- identity: 'Safari',
- versionSearch: 'Version'
- },
- {prop: window.opera, identity: 'Opera', versionSearch: 'Version'},
- {string: navigator.vendor, subString: 'iCab', identity: 'iCab'},
- {string: navigator.vendor, subString: 'KDE', identity: 'Konqueror'},
- {string: navigator.userAgent, subString: 'Firefox', identity: 'Firefox'},
- {string: navigator.vendor, subString: 'Camino', identity: 'Camino'}, {
- // for newer Netscapes (6+)
- string: navigator.userAgent,
- subString: 'Netscape',
- identity: 'Netscape'
- },
- {
- string: navigator.userAgent,
- subString: 'MSIE',
- identity: 'Explorer',
- versionSearch: 'MSIE'
- },
- {
- string: navigator.userAgent,
- subString: 'Gecko',
- identity: 'Mozilla',
- versionSearch: 'rv'
- },
- {
- // for older Netscapes (4-)
- string: navigator.userAgent,
- subString: 'Mozilla',
- identity: 'Netscape',
- versionSearch: 'Mozilla'
- }
- ],
- dataOS: [
- {string: navigator.platform, subString: 'Win', identity: 'Windows'},
- {string: navigator.platform, subString: 'Mac', identity: 'Mac'},
- {string: navigator.userAgent, subString: 'iPhone', identity: 'iPhone/iPod'},
- {string: navigator.platform, subString: 'Linux', identity: 'Linux'}
- ]
-};
-
-function str_replace(search, replace, subject, count) {
- // http://kevin.vanzonneveld.net
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + improved by: Gabriel Paderni
- // + improved by: Philip Peterson
- // + improved by: Simon Willison (http://simonwillison.net)
- // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
- // + bugfixed by: Anton Ongson
- // + input by: Onno Marsman
- // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + tweaked by: Onno Marsman
- // + input by: Brett Zamir (http://brett-zamir.me)
- // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
- // + input by: Oleg Eremeev
- // + improved by: Brett Zamir (http://brett-zamir.me)
- // + bugfixed by: Oleg Eremeev
- // % note 1: The count parameter must be passed as a string in order
- // % note 1: to find a global variable in which the result will be
- // given
- // * example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
- // * returns 1: 'Kevin.van.Zonneveld'
- // * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name},
- // lars');
- // * returns 2: 'hemmo, mars'
- var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0, f = [].concat(search),
- r = [].concat(replace), s = subject,
- ra = Object.prototype.toString.call(r) === '[object Array]',
- sa = Object.prototype.toString.call(s) === '[object Array]';
- s = [].concat(s);
- if (count) {
- this.window[count] = 0;
- }
-
- for (i = 0, sl = s.length; i < sl; i++) {
- if (s[i] === '') {
- continue;
- }
- for (j = 0, fl = f.length; j < fl; j++) {
- temp = s[i] + '';
- repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
- s[i] = (temp).split(f[j]).join(repl);
- if (count && s[i] !== temp) {
- this.window[count] += (temp.length - s[i].length) / f[j].length;
- }
- }
- }
- return sa ? s : s[0];
-}
-
-/**
- * JavaScript Client Detection
- * (C) viazenetti GmbH (Christian Ludwig)
- */
-
-var System = {
- OS: function() {
- var unknown = '?';
-
- // browser
- var nVer = navigator.appVersion;
- var nAgt = navigator.userAgent;
- var browser = navigator.appName;
- var version = '' + parseFloat(navigator.appVersion);
- var majorVersion = parseInt(navigator.appVersion, 10);
- var nameOffset, verOffset, ix;
-
- // system
- var os = unknown;
- var clientStrings = [
- {s: 'Windows 3.11', r: /Win16/},
- {s: 'Windows 95', r: /(Windows 95|Win95|Windows_95)/},
- {s: 'Windows ME', r: /(Win 9x 4.90|Windows ME)/},
- {s: 'Windows 98', r: /(Windows 98|Win98)/},
- {s: 'Windows CE', r: /Windows CE/},
- {s: 'Windows 2000', r: /(Windows NT 5.0|Windows 2000)/},
- {s: 'Windows XP', r: /(Windows NT 5.1|Windows XP)/},
- {s: 'Windows Server 2003', r: /Windows NT 5.2/},
- {s: 'Windows Vista', r: /Windows NT 6.0/},
- {s: 'Windows 7', r: /(Windows 7|Windows NT 6.1)/},
- {s: 'Windows 8.1', r: /(Windows 8.1|Windows NT 6.3)/},
- {s: 'Windows 8', r: /(Windows 8|Windows NT 6.2)/},
- {s: 'Windows NT 4.0', r: /(Windows NT 4.0|WinNT4.0|WinNT|Windows NT)/},
- {s: 'Windows ME', r: /Windows ME/},
- {s: 'Android', r: /Android/},
- {s: 'Open BSD', r: /OpenBSD/},
- {s: 'Sun OS', r: /SunOS/},
- {s: 'Linux', r: /(Linux|X11)/},
- {s: 'iOS', r: /(iPhone|iPad|iPod)/},
- {s: 'Mac OS X', r: /Mac OS X/},
- {s: 'Mac OS', r: /(MacPPC|MacIntel|Mac_PowerPC|Macintosh)/},
- {s: 'QNX', r: /QNX/},
- {s: 'UNIX', r: /UNIX/},
- {s: 'BeOS', r: /BeOS/},
- {s: 'OS/2', r: /OS\/2/},
- {
- s: 'Search Bot',
- r: /(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver)/
- }
- ];
- for (var id in clientStrings) {
- var cs = clientStrings[id];
- if (cs.r.test(nAgt)) {
- os = cs.s;
- break;
- }
- }
-
- var osVersion = 'unknown';
-
- if (/Windows/.test(os)) {
- osVersion = /Windows (.*)/.exec(os)[1];
- os = 'Windows';
- }
-
- switch (os) {
- case 'Mac OS X':
- osVersion = /Mac OS X (10[\.\_\d]+)/.exec(nAgt)[1];
- break;
-
- case 'Android':
- osVersion = /Android ([\.\_\d]+)/.exec(nAgt)[1];
- break;
-
- case 'iOS':
- osVersion = /OS (\d+)_(\d+)_?(\d+)?/.exec(nVer);
- osVersion =
- osVersion[1] + '.' + osVersion[2] + '.' + (osVersion[3] | 0);
- break;
- }
-
- var jscd = {os: os, osVersion: osVersion};
-
- return (jscd.os + ' ' + jscd.osVersion);
- }
};
window.addEventListener('load', _ => {
- BrowserDetect.init();
app.init();
});
diff --git a/overrides/screenshotpage.html b/overrides/screenshotpage.html
index 40d5f5d..a312b42 100644
--- a/overrides/screenshotpage.html
+++ b/overrides/screenshotpage.html
@@ -16,5 +16,6 @@
This is working as intended, since this also makes the page load faster.
#}
{% block scripts %}
+ <script src="{{ 'assets/javascripts/ua_utils.js' | url }}"></script>
<script src="{{ 'assets/javascripts/screenshot_page.js' | url }}"></script>
{% endblock %}
diff --git a/overrides/versionpage.html b/overrides/versionpage.html
index 26dbb0b..40443e5 100644
--- a/overrides/versionpage.html
+++ b/overrides/versionpage.html
@@ -53,5 +53,6 @@
This is working as intended, since this also makes the page load faster.
#}
{% block scripts %}
+ <script src="{{ 'assets/javascripts/ua_utils.js' | url }}"></script>
<script src="{{ 'assets/javascripts/version_page.js' | url }}"></script>
{% endblock %}