Last active
April 17, 2019 05:05
-
-
Save mbaler/34797c4c55cbb970e0b707e1acd9cb50 to your computer and use it in GitHub Desktop.
Chrome client latest (stable) version checker (https://jsbin.com/bihejimoko/edit?js,console)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check whether client has latest (stable) version of Chrome | |
if (isChrome()) { | |
getVersionInfo() | |
.then((result) => { | |
const output = | |
`Stable Version Info: Full (Major)\n` + | |
`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n` + | |
`Client: ${result.clientVer} (${result.clientVerMajor})\n` + | |
`Latest: ${result.latestVer} (${result.latestVerMajor})\n` + | |
`Is client latest? - ${result.isLatest} (${result.isLatestMajor})`; | |
console.log(output); | |
}); | |
} else { | |
console.log("This ain't even Chrome!"); | |
} | |
/** | |
* Is current browser Chrome? | |
* @returns {boolean} | |
*/ | |
function isChrome() { | |
const isChromium = window.chrome; | |
const winNav = window.navigator; | |
const vendorName = winNav.vendor; | |
const isOpera = winNav.userAgent.indexOf("OPR") > -1; | |
const isIEedge = winNav.userAgent.indexOf("Edge") > -1; | |
return ( | |
isChromium !== null && | |
typeof isChromium !== "undefined" && | |
vendorName === "Google Inc." && | |
isOpera === false && | |
isIEedge === false | |
); | |
} | |
/** | |
* Get Chrome version info | |
* @returns {Promise<Object>} | |
*/ | |
function getVersionInfo() { | |
const userAgent = window.navigator.userAgent; | |
const clientVer = userAgent.match(/Chrom(e|ium)\/(([0-9]|\.)+)/)[2]; | |
const clientVerMajor = clientVer.slice(0, 2); | |
let opSys = ""; | |
if (userAgent.indexOf("Linux") !== -1) opSys = "linux"; | |
if (userAgent.indexOf("Mac") !== -1) opSys = "mac"; | |
if (userAgent.indexOf("Windows") !== -1) opSys = "win"; | |
const fetchUrl = `https://omahaproxy.appspot.com/all.json?os=${opSys}&channel=stable`; | |
return fetch(fetchUrl) | |
.then((res) => res.json()) | |
.then((jsonResponse) => { | |
const latestVer = jsonResponse[0].versions[0].current_version; | |
const latestVerMajor = latestVer.slice(0, 2); | |
return { | |
clientVer, | |
clientVerMajor, | |
latestVer, | |
latestVerMajor, | |
isLatest: (clientVer === latestVer), | |
isLatestMajor: (clientVerMajor === latestVerMajor), | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo: https://jsbin.com/bihejimoko/edit?js,console