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 | |
Adrià Vilanova Martínez | 182a82d | 2022-05-03 23:13:41 +0200 | [diff] [blame] | 4 | Copyright (c) 2022 Adrià Vilanova Martínez |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 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) { |
Adrià Vilanova Martínez | 182a82d | 2022-05-03 23:13:41 +0200 | [diff] [blame] | 43 | if (os == 'linux' || os == 'mac' || os == 'cros' || os == 'android') |
| 44 | return os; |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame] | 45 | if (os == 'win_7_or_less' || os == 'win_latest') return 'win64'; |
| 46 | return undefined; |
| 47 | } |
| 48 | |
| 49 | function prettifyOS(os) { |
| 50 | if (os == 'linux') return 'Linux'; |
| 51 | if (os == 'mac') return 'Mac'; |
| 52 | if (os == 'cros') return 'Chrome OS'; |
| 53 | if (os == 'win_7_or_less' || os == 'win_latest') return 'Windows'; |
Adrià Vilanova Martínez | 182a82d | 2022-05-03 23:13:41 +0200 | [diff] [blame] | 54 | if (os == 'android') return 'Android'; |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame] | 55 | return undefined; |
| 56 | } |
| 57 | |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 58 | var app = { |
| 59 | init: function() { |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame] | 60 | let promises = []; |
| 61 | promises.push(fetch('https://omahaproxy.appspot.com/all.json') |
| 62 | .then(res => res.json())); |
| 63 | promises.push(detectOS()); |
| 64 | promises.push(detectCrVersion()); |
| 65 | |
| 66 | Promise.all(promises) |
| 67 | .then(responses => { |
| 68 | const [chromeVersions, os, version] = responses; |
| 69 | if (!version) { |
| 70 | document.getElementById('version').textContent = |
| 71 | 'No hemos podido determinar tu versión.'; |
| 72 | document.getElementById('updated').textContent = |
| 73 | 'Es posible que no estés usando Chrome o que la página no funcione correctamente.'; |
| 74 | return; |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 75 | } |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame] | 76 | |
| 77 | let majorVersion = version?.split?.('.')?.[0]; |
| 78 | let omahaOS = convert2OmahaOS(os); |
| 79 | let matches = chromeVersions.filter(function(val, index, array) { |
| 80 | return val.os === omahaOS; |
| 81 | }); |
| 82 | var actualChrome = |
| 83 | matches?.[0]?.versions?.filter?.(function(val, index, array) { |
| 84 | return val.channel === 'stable'; |
Adrià Vilanova Martínez | 182a82d | 2022-05-03 23:13:41 +0200 | [diff] [blame] | 85 | }) ?? |
| 86 | []; |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame] | 87 | actualVersion = actualChrome?.[0]?.version?.split?.('.')?.[0]; |
Adrià Vilanova Martínez | 182a82d | 2022-05-03 23:13:41 +0200 | [diff] [blame] | 88 | if (actualVersion === undefined) { |
| 89 | document.getElementById('updated').innerHTML = ''; |
| 90 | } else if (majorVersion < actualVersion) { |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame] | 91 | document.getElementById('updated').innerHTML = |
| 92 | '¡Oh, no! Google Chrome ' + |
| 93 | '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.'; |
| 94 | } else if (majorVersion > actualVersion) { |
| 95 | document.getElementById('updated').innerHTML = |
| 96 | 'No estás usando el canal estable de Chrome. Ve con cuidado ;-)'; |
| 97 | } else if (version != actualChrome[0].version) { |
| 98 | document.getElementById('updated').innerHTML = |
| 99 | 'Oops, Google Chrome ' + |
| 100 | '<a href=\'https://support.google.com/chrome/answer/95414\'>no está actualizado</a>.'; |
| 101 | } else { |
| 102 | document.getElementById('updated').innerHTML = |
| 103 | 'Chrome está actualizado'; |
| 104 | } |
| 105 | let prettyOS = prettifyOS(os); |
| 106 | document.getElementById('version').textContent = 'Usas la versión ' + |
| 107 | version + ' de Chrome' + (prettyOS ? ' en ' + prettyOS : ''); |
| 108 | }) |
| 109 | .catch(err => { |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 110 | document.getElementById('version').textContent = |
Adrià Vilanova Martínez | 7fe3cf2 | 2022-04-04 23:43:23 +0200 | [diff] [blame] | 111 | 'Ha ocurrido un error inesperado.'; |
| 112 | document.getElementById('updated').textContent = |
| 113 | 'Error: ' + err + '.'; |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 114 | }); |
| 115 | } |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | window.addEventListener('load', _ => { |
Adrià Vilanova Martínez | 849f9dd | 2021-06-25 20:40:10 +0200 | [diff] [blame] | 119 | app.init(); |
| 120 | }); |