Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 1 | // @Author: avm99963 (https://www.avm99963.com) |
| 2 | /* @License: The MIT License (MIT) |
| 3 | |
| 4 | Copyright (c) 2021 Adrià Vilanova Martínez |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in all |
| 14 | copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. */ |
| 23 | |
| 24 | var actualversion; |
| 25 | |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame^] | 26 | function detectCrVersion() { |
| 27 | if (!navigator?.userAgentData) |
| 28 | return Promise.resolve( |
| 29 | window.navigator.appVersion.match(/Chrome\/(.*?) /)?.[1]); |
| 30 | |
| 31 | return navigator.userAgentData.getHighEntropyValues(['fullVersionList']) |
| 32 | .then(res => { |
| 33 | let brands = res?.fullVersionList ?? []; |
| 34 | for (const b of brands) |
| 35 | if (b?.brand == 'Chromium' || b?.brand == 'Google Chrome') |
| 36 | return b?.version; |
| 37 | |
| 38 | return undefined; |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | function convert2OmahaOS(os) { |
| 43 | if (os == 'linux' || os == 'mac' || os == 'cros') return os; |
| 44 | if (os == 'win_7_or_less' || os == 'win_latest') return 'win64'; |
| 45 | return undefined; |
| 46 | } |
| 47 | |
| 48 | function prettifyOS(os) { |
| 49 | if (os == 'linux') return 'Linux'; |
| 50 | if (os == 'mac') return 'Mac'; |
| 51 | if (os == 'cros') return 'Chrome OS'; |
| 52 | if (os == 'win_7_or_less' || os == 'win_latest') return 'Windows'; |
| 53 | return undefined; |
| 54 | } |
| 55 | |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 56 | var app = { |
| 57 | init: function() { |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame^] | 58 | let promises = []; |
| 59 | promises.push(fetch('https://omahaproxy.appspot.com/all.json') |
| 60 | .then(res => res.json())); |
| 61 | promises.push(detectOS()); |
| 62 | promises.push(detectCrVersion()); |
| 63 | |
| 64 | Promise.all(promises) |
| 65 | .then(responses => { |
| 66 | const [chromeVersions, os, version] = responses; |
| 67 | if (!version) { |
| 68 | document.getElementById('version').textContent = |
| 69 | 'No hemos podido determinar tu versión.'; |
| 70 | document.getElementById('updated').textContent = |
| 71 | 'Es posible que no estés usando Chrome o que la página no funcione correctamente.'; |
| 72 | return; |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 73 | } |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame^] | 74 | |
| 75 | let majorVersion = version?.split?.('.')?.[0]; |
| 76 | let omahaOS = convert2OmahaOS(os); |
| 77 | let matches = chromeVersions.filter(function(val, index, array) { |
| 78 | return val.os === omahaOS; |
| 79 | }); |
| 80 | var actualChrome = |
| 81 | matches?.[0]?.versions?.filter?.(function(val, index, array) { |
| 82 | return val.channel === 'stable'; |
| 83 | }); |
| 84 | actualVersion = actualChrome?.[0]?.version?.split?.('.')?.[0]; |
| 85 | if (majorVersion < actualVersion) { |
| 86 | document.getElementById('updated').innerHTML = |
| 87 | '¡Oh, no! Google Chrome ' + |
| 88 | '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.'; |
| 89 | } else if (majorVersion > actualVersion) { |
| 90 | document.getElementById('updated').innerHTML = |
| 91 | 'No estás usando el canal estable de Chrome. Ve con cuidado ;-)'; |
| 92 | } else if (version != actualChrome[0].version) { |
| 93 | document.getElementById('updated').innerHTML = |
| 94 | 'Oops, Google Chrome ' + |
| 95 | '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.'; |
| 96 | } else { |
| 97 | document.getElementById('updated').innerHTML = |
| 98 | 'Chrome está actualizado'; |
| 99 | } |
| 100 | let prettyOS = prettifyOS(os); |
| 101 | document.getElementById('version').textContent = 'Usas la versión ' + |
| 102 | version + ' de Chrome' + (prettyOS ? ' en ' + prettyOS : ''); |
| 103 | }) |
| 104 | .catch(err => { |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 105 | document.getElementById('version').textContent = |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame^] | 106 | 'Ha ocurrido un error inesperado.'; |
| 107 | document.getElementById('updated').textContent = |
| 108 | 'Error: ' + err + '.'; |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 109 | }); |
| 110 | } |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | window.addEventListener('load', _ => { |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 114 | app.init(); |
| 115 | }); |