Skip to content

Instantly share code, notes, and snippets.

@azl397985856
Created September 22, 2020 07:01
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 azl397985856/2131ddc38e39eaee69259029e2e4936e to your computer and use it in GitHub Desktop.
Save azl397985856/2131ddc38e39eaee69259029e2e4936e to your computer and use it in GitHub Desktop.
浏览器检测
class BrowserDetector {
constructor() {
// 测试条件编译
// IE6~10支持
this.isIE_Gte6Lte10 = /*@cc_on!@*/false;
// 测试documentMode
// IE7~11支持
this.isIE_Gte7Lte11 = !!document.documentMode;
// 测试StyleMedia构造函数
// Edge 20及以上版本支持
this.isEdge_Gte20 = !!window.StyleMedia;
// 测试Firefox专有扩展安装API
// 所有版本的Firefox都支持
this.isFirefox_Gte1 = typeof InstallTrigger !== 'undefined';
// 测试chrome对象及其webstore属性
// Opera的某些版本有window.chrome,但没有window.chrome.webstore
// 所有版本的Chrome都支持
this.isChrome_Gte1 = !!window.chrome && !!window.chrome.webstore;
// Safari早期版本会给构造函数的标签符追加"Constructor"字样,如:
// window.Element.toString(); // [object ElementConstructor]
// Safari 3~9.1支持
this.isSafari_Gte3Lte9_1 = /constructor/i.test(window.Element);
// 推送通知API暴露在window对象上
// 使用默认参数值以避免对undefined调用toString()
// Safari 7.1及以上版本支持
this.isSafari_Gte7_1 =
(({pushNotification = {}} = {}) =>
pushNotification.toString() == '[object SafariRemoteNotification]'
)(window.safari);
// 测试addons属性
// Opera 20及以上版本支持
this.isOpera_Gte20 = !!window.opr && !!window.opr.addons;
}
isIE() { return this.isIE_Gte6Lte10 || this.isIE_Gte7Lte11; }
isEdge() { return this.isEdge_Gte20 && !this.isIE(); }
isFirefox() { return this.isFirefox_Gte1; }
isChrome() { return this.isChrome_Gte1; }
isSafari() { return this.isSafari_Gte3Lte9_1 || this.isSafari_Gte7_1; }
isOpera() { return this.isOpera_Gte20; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment