Skip to content

Instantly share code, notes, and snippets.

@m13253
Created August 17, 2016 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m13253/122c275c7ad8c991990605107424d181 to your computer and use it in GitHub Desktop.
Save m13253/122c275c7ad8c991990605107424d181 to your computer and use it in GitHub Desktop.
(function (root, factory) {
var lodash = 'lodash';
if (typeof define === 'function' && define.amd) {
define([lodash], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require(lodash));
} else {
root.BOMDetect = factory(_);
}
}(this, function (_) {
function inWindow(name) {
return name in window;
}
function inChrome(name) {
if (window.chrome) return name in window.chrome;
return false;
}
function inExternal(name) {
if (window.external) return name in window.external;
return false;
}
function inStyle(name) {
return name in window.document.documentElement.style;
}
function inDocument(name) {
return name in window.document;
}
return {
isIE: function () {
return _.every(['documentMode', 'compatMode'], inDocument)
&& inWindow('ActiveXObject');
},
isEdge: function () {
return inWindow('msWriteProfilerMark') && !this.isIE();
},
isOpera: function () {
return (inWindow('opr') && !inWindow('external')) || inWindow('opera');
},
isFirefox: function () {
return inStyle('MozAppearance') && inExternal('addSearchEngine')
&& _.every(['InstallTrigger', 'createImageBitmap', 'sidebar'], inWindow)
&& window['InstallTrigger'].constructor.name === 'InstallTriggerImpl';
},
isSafari: function () {
return inWindow('safari')
&& Element.toString() === '[object ElementConstructor]'
&& window.safari['pushNotification'].toString() === '[object SafariRemoteNotification]';
},
isChrome: function () {
return inStyle('WebkitAppearance')
&& _.isEqual(_.keys(window.chrome), ['loadTimes', 'csi', 'app', 'webstore', 'runtime'])
&& _.isEqual(_.keys(window.external), ['AddSearchProvider', 'IsSearchProviderInstalled'])
&& !this.isOpera() && !this.isSafari();
},
likeChromium: function () {
return inWindow('chrome') || inWindow('external');
},
isQQBrowser: function () {
return _.every(['getVersion', 'getGuid', 'getSupplyId', 'isLogined', 'doLogin', 'doLogout'], inExternal);
},
isBaiduBrowser: function () {
return _.every(['StartRequest', 'GetNextReqID', 'ReportResult', 'GetOriginalUrl'], inExternal);
},
isBaiduSparkBrowser: function () {
return _.every(['StartRequest', 'GetNextReqID', 'GetVersion', 'GetSparkInfo'], inExternal);
},
is360Browser: function () {
return _.every(['AppCmd', 'GetSID', 'GetMID', 'GetRunPath'], inExternal);
},
isCMBrowser: function () {
return _.every(['LiebaoGetVersion', 'LiebaoGetUUID'], inExternal);
},
isUCBrowser: function () {
return inChrome('wowPrivate');
},
is2345ExplorerBrowser: function () {
return inWindow('adblock_2345explorer')
&& _.every(['videofloat2345', 'adblock2345', 'extension2345', 'ntp2345'], inChrome);
},
is2345ChromeBrowser: function () {
return !this.is2345ExplorerBrowser()
&& _.every(['extension2345', 'ntp2345'], inChrome);
},
isSogouExplorerBrowser: function () {
return _.every(['Sogou404', 'downloadWithSogou'], inExternal);
},
isTheWorldChromeBrowser: function () {
return _.every(['searchBox', 'setSuggestResult', 'appNotifications'], inChrome);
},
isMaxthonCloudBrowser: function () {
return _.every(['mxVersion', 'mxProductName', 'mxProductType', 'mxGetRuntime', 'mxCall'], inExternal);
},
is115Browser: function () {
return _.every(['browserInterface', 'uploadInterface', 'downloadInterface'], inWindow);
},
isYandexBrowser: function () {
return inWindow('yandex');
},
getBrowserName: function () {
if (this.isIE()) return 'IE浏览器';
if (this.isEdge()) return 'Edge浏览器';
if (this.isOpera()) return 'Opera浏览器';
if (this.isSafari()) return 'Safari浏览器';
if (this.isFirefox()) return 'Firefox浏览器';
if (this.likeChromium()) {
if (this.is360Browser()) return '360浏览器';
if (this.isQQBrowser()) return 'QQ浏览器';
if (this.isCMBrowser()) return '猎豹安全浏览器';
if (this.isUCBrowser()) return 'UC浏览器';
if (this.isBaiduBrowser()) return '百度浏览器';
if (this.isBaiduSparkBrowser()) return '百度浏览器国际版';
if (this.isMaxthonCloudBrowser()) return '傲游云浏览器';
if (this.isSogouExplorerBrowser()) return '搜狗高速浏览器';
if (this.isTheWorldChromeBrowser()) return '世界之窗浏览器极速版';
if (this.is2345ExplorerBrowser()) return '2345王牌浏览器';
if (this.is2345ChromeBrowser()) return '2345加速浏览器';
if (this.is115Browser()) return '115浏览器';
if (this.isYandexBrowser()) return 'Yandex浏览器';
if (this.isChrome()) return 'Chrome浏览器';
return 'Chromium浏览器';
}
return '[未知浏览器]';
}
};
}));
<!DOCTYPE html>
<html lang="zh-cmn-hans">
<head>
<meta charset="UTF-8">
<title>BOMDetect Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.0.0/lodash.min.js"></script>
<script src="./BOMDetect.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(event) {
document.body.appendChild(document.createTextNode(BOMDetect.getBrowserName()));
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment