Skip to content

Instantly share code, notes, and snippets.

@benbenbenbenbenben
Last active December 21, 2015 12:05
Show Gist options
  • Save benbenbenbenbenben/0cd9ad98882981e97cf0 to your computer and use it in GitHub Desktop.
Save benbenbenbenbenben/0cd9ad98882981e97cf0 to your computer and use it in GitHub Desktop.
// 1. get browser
// 2. patch js
// 3. ???
// 4. profit
navigator.sayswho = (function(){
var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
M = M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return M.join(' ');
})();
var CHROME = 1, IE = 2, FF = 3, OPERA = 4, OTHER = 5;
var browser = navigator.sayswho.toUpperCase();
browser = !browser.indexOf('chrom') ? 1
: !browser.indexOf('ie') ? 2
: !browser.indexOf('firef') ? 3
: !browser.indexOf('oper') ? 4
: /* unknown or default */ 5;
String.prototype.replaceAll = (function(ptn, sub) {
switch(browser) {
case CHROME:
case IE:
case FF:
case OPERA:
return function(ptn){ return this.split(ptn).join(sub; }
break;
default:
return function(ptn){ return this.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&").replace(new RegExp(ptn, 'gm'), sub); }
break;
}
})(browser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment