Skip to content

Instantly share code, notes, and snippets.

@mbaler
Last active April 17, 2019 05:05
Show Gist options
  • Save mbaler/34797c4c55cbb970e0b707e1acd9cb50 to your computer and use it in GitHub Desktop.
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)
// 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),
};
});
}
@mbaler
Copy link
Author

mbaler commented Apr 17, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment