From 9695c93f7822ee29156c276b921594c18d278e68 Mon Sep 17 00:00:00 2001 From: Maykonn Date: Tue, 19 Jun 2018 11:41:29 -0300 Subject: [PATCH] Minor bug fixes and editorconfig --- .editorconfig | 10 + BrowsingModeDetector.js | 449 +++++++++++++++++++--------------------- example.html | 80 +++---- 3 files changed, 268 insertions(+), 271 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f027308 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +trim_trailing_whitespace = true +insert_final_newline = true +max_line_length = 120 +indent_style = space +indent_size = 2 diff --git a/BrowsingModeDetector.js b/BrowsingModeDetector.js index 3b0d3fd..94f6908 100644 --- a/BrowsingModeDetector.js +++ b/BrowsingModeDetector.js @@ -6,132 +6,111 @@ * @author Maykonn Welington Candido */ var BrowsingModeDetector = function () { - var _ignoringBots = true; - var _browsingInIncognitoMode; - var _callbackForNormalMode; - var _callbackForIncognitoOrPrivateMode; - - this.BROWSING_NORMAL_MODE = 'NORMAL_MODE'; - this.BROWSING_INCOGNITO_PRIVATE_MODE = 'INCOGNITO_PRIVATE_MODE'; - - /** - * @returns {BrowsingModeDetector} - */ - this.ignoringBots = function () { - _ignoringBots = true; - return this; - }; - - /** - * @returns {BrowsingModeDetector} - */ - this.notIgnoringBots = function () { - _ignoringBots = false; - return this; - }; - - this.getBrowsingMode = function () { - return (_browsingInIncognitoMode ? this.BROWSING_INCOGNITO_PRIVATE_MODE : this.BROWSING_NORMAL_MODE); - }; - - this.setBrowsingInIncognitoMode = function () { - _browsingInIncognitoMode = true; - }; - - this.setBrowsingInNormalMode = function () { - _browsingInIncognitoMode = false; - }; - - /** - * @param callback optional - */ - this.isBotBrowsing = function (callback) { - var userAgentToTest = window.navigator.userAgent; - var isBot = /googlebot|Googlebot-Mobile|Googlebot-Image|Google favicon|Mediapartners-Google|bingbot|slurp|java|wget|curl|Commons-HttpClient|Python-urllib|libwww|httpunit|nutch|phpcrawl|msnbot|jyxobot|FAST-WebCrawler|FAST Enterprise Crawler|biglotron|teoma|convera|seekbot|gigablast|exabot|ngbot|ia_archiver|GingerCrawler|webmon |httrack|webcrawler|grub.org|UsineNouvelleCrawler|antibot|netresearchserver|speedy|fluffy|bibnum.bnf|findlink|msrbot|panscient|yacybot|AISearchBot|IOI|ips-agent|tagoobot|MJ12bot|dotbot|woriobot|yanga|buzzbot|mlbot|yandexbot|purebot|Linguee Bot|Voyager|CyberPatrol|voilabot|baiduspider|citeseerxbot|spbot|twengabot|postrank|turnitinbot|scribdbot|page2rss|sitebot|linkdex|Adidxbot|blekkobot|ezooms|dotbot|Mail.RU_Bot|discobot|heritrix|findthatfile|europarchive.org|NerdByNature.Bot|sistrix crawler|ahrefsbot|Aboundex|domaincrawler|wbsearchbot|summify|ccbot|edisterbot|seznambot|ec2linkfinder|gslfbot|aihitbot|intelium_bot|facebookexternalhit|yeti|RetrevoPageAnalyzer|lb-spider|sogou|lssbot|careerbot|wotbox|wocbot|ichiro|DuckDuckBot|lssrocketcrawler|drupact|webcompanycrawler|acoonbot|openindexspider|gnam gnam spider|web-archive-net.com.bot|backlinkcrawler|coccoc|integromedb|content crawler spider|toplistbot|seokicks-robot|it2media-domain-crawler|ip-web-crawler.com|siteexplorer.info|elisabot|proximic|changedetection|blexbot|arabot|WeSEE:Search|niki-bot|CrystalSemanticsBot|rogerbot|360Spider|psbot|InterfaxScanBot|Lipperhey SEO Service|CC Metadata Scaper|g00g1e.net|GrapeshotCrawler|urlappendbot|brainobot|fr-crawler|binlar|SimpleCrawler|Livelapbot|Twitterbot|cXensebot|smtbot|bnf.fr_bot|A6-Indexer|ADmantX|Facebot|Twitterbot|OrangeBot|memorybot|AdvBot|MegaIndex|SemanticScholarBot|ltx71|nerdybot|xovibot|BUbiNG|Qwantify|archive.org_bot|Applebot|TweetmemeBot|crawler4j|findxbot|SemrushBot|yoozBot|lipperhey|y!j-asr|Domain Re-Animator Bot|AddThis/i.test(userAgentToTest); - return (typeof callback === 'undefined' ? isBot : callback(isBot)); - }; - - /** - * @param callback - * @returns {BrowsingModeDetector} - */ - this.setCallbackForNormalMode = function (callback) { - _callbackForNormalMode = callback; - return this; - }; - - /** - * @param callback - * @returns {BrowsingModeDetector} - */ - this.setCallbackForIncognitoOrPrivateMode = function (callback) { - _callbackForIncognitoOrPrivateMode = callback; - return this; - }; - - /** - * @param defaultCallback optional - */ - this.do = function (defaultCallback) { - if ( - typeof defaultCallback !== 'function' && - (typeof _callbackForNormalMode !== 'function' || typeof _callbackForIncognitoOrPrivateMode !== 'function') - ) { - throw 'Default callback or specific callbacks are required'; - } - - var _callback = function () { - if (_browsingInIncognitoMode && typeof _callbackForIncognitoOrPrivateMode === 'function') { - _callbackForIncognitoOrPrivateMode(); - } else if (!_browsingInIncognitoMode && typeof _callbackForNormalMode === 'function') { - _callbackForNormalMode(); - } - - if (typeof defaultCallback !== 'undefined') { - defaultCallback(_browsingInIncognitoMode) - } - }; - - if (_ignoringBots && this.isBotBrowsing()) { - _browsingInIncognitoMode = false; - _callback(); - } - - (new BrowserFactory()) - .browser(this) - .detectBrowsingMode(); - - this.retry( - function () { - return typeof _browsingInIncognitoMode !== 'undefined'; - }, - _callback - ); - }; - - this.retry = function (ready, callback) { - var iteration = 0; - var maxRetry = 50; - var interval = 10; - var isTimeout = false; - - var id = window.setInterval( - function () { - if (ready()) { - window.clearInterval(id); - callback(isTimeout); - } - if (iteration++ > maxRetry) { - window.clearInterval(id); - isTimeout = true; - callback(isTimeout); - } - }, - interval - ); - }; + var _ignoringBots = true; + var _browsingInIncognitoMode; + var _callbackForNormalMode; + var _callbackForIncognitoOrPrivateMode; + var _callbackDefault; // if given, will be executed even _callbackForNormalMode and _callbackForIncognitoOrPrivateMode are given + + this.BROWSING_NORMAL_MODE = 'NORMAL_MODE'; + this.BROWSING_INCOGNITO_PRIVATE_MODE = 'INCOGNITO_PRIVATE_MODE'; + + /** + * @returns {BrowsingModeDetector} + */ + this.ignoringBots = function () { + _ignoringBots = true; + return this; + }; + /** + * @returns {BrowsingModeDetector} + */ + this.notIgnoringBots = function () { + _ignoringBots = false; + return this; + }; + + this.getBrowsingMode = function () { + return (_browsingInIncognitoMode ? this.BROWSING_INCOGNITO_PRIVATE_MODE : this.BROWSING_NORMAL_MODE); + }; + + this.setBrowsingInIncognitoMode = function () { + _browsingInIncognitoMode = true; + }; + + this.setBrowsingInNormalMode = function () { + _browsingInIncognitoMode = false; + }; + + /** + * @param callback optional + */ + this.isBotBrowsing = function (callback) { + var userAgentToTest = window.navigator.userAgent; + var isBot = /googlebot|Googlebot-Mobile|Googlebot-Image|Google favicon|Mediapartners-Google|bingbot|slurp|java|wget|curl|Commons-HttpClient|Python-urllib|libwww|httpunit|nutch|phpcrawl|msnbot|jyxobot|FAST-WebCrawler|FAST Enterprise Crawler|biglotron|teoma|convera|seekbot|gigablast|exabot|ngbot|ia_archiver|GingerCrawler|webmon |httrack|webcrawler|grub.org|UsineNouvelleCrawler|antibot|netresearchserver|speedy|fluffy|bibnum.bnf|findlink|msrbot|panscient|yacybot|AISearchBot|IOI|ips-agent|tagoobot|MJ12bot|dotbot|woriobot|yanga|buzzbot|mlbot|yandexbot|purebot|Linguee Bot|Voyager|CyberPatrol|voilabot|baiduspider|citeseerxbot|spbot|twengabot|postrank|turnitinbot|scribdbot|page2rss|sitebot|linkdex|Adidxbot|blekkobot|ezooms|dotbot|Mail.RU_Bot|discobot|heritrix|findthatfile|europarchive.org|NerdByNature.Bot|sistrix crawler|ahrefsbot|Aboundex|domaincrawler|wbsearchbot|summify|ccbot|edisterbot|seznambot|ec2linkfinder|gslfbot|aihitbot|intelium_bot|facebookexternalhit|yeti|RetrevoPageAnalyzer|lb-spider|sogou|lssbot|careerbot|wotbox|wocbot|ichiro|DuckDuckBot|lssrocketcrawler|drupact|webcompanycrawler|acoonbot|openindexspider|gnam gnam spider|web-archive-net.com.bot|backlinkcrawler|coccoc|integromedb|content crawler spider|toplistbot|seokicks-robot|it2media-domain-crawler|ip-web-crawler.com|siteexplorer.info|elisabot|proximic|changedetection|blexbot|arabot|WeSEE:Search|niki-bot|CrystalSemanticsBot|rogerbot|360Spider|psbot|InterfaxScanBot|Lipperhey SEO Service|CC Metadata Scaper|g00g1e.net|GrapeshotCrawler|urlappendbot|brainobot|fr-crawler|binlar|SimpleCrawler|Livelapbot|Twitterbot|cXensebot|smtbot|bnf.fr_bot|A6-Indexer|ADmantX|Facebot|Twitterbot|OrangeBot|memorybot|AdvBot|MegaIndex|SemanticScholarBot|ltx71|nerdybot|xovibot|BUbiNG|Qwantify|archive.org_bot|Applebot|TweetmemeBot|crawler4j|findxbot|SemrushBot|yoozBot|lipperhey|y!j-asr|Domain Re-Animator Bot|AddThis/i.test(userAgentToTest); + return (typeof callback === 'undefined' ? isBot : callback(isBot)); + }; + + /** + * @param callback + * @returns {BrowsingModeDetector} + */ + this.setCallbackForNormalMode = function (callback) { + _callbackForNormalMode = callback; return this; + }; + + /** + * @param callback + * @returns {BrowsingModeDetector} + */ + this.setCallbackForIncognitoOrPrivateMode = function (callback) { + _callbackForIncognitoOrPrivateMode = callback; + return this; + }; + + /** + * @param defaultCallback optional + */ + this.do = function (defaultCallback) { + _validateUserCallback(defaultCallback); + + if (_ignoringBots && this.isBotBrowsing()) { + _browsingInIncognitoMode = false; + _executeUserCallback(); + return; + } + + (new BrowserFactory()) + .browser(this) + .detectBrowsingMode(_executeUserCallback); + }; + + var _validateUserCallback = function (defaultCallback) { + _callbackDefault = defaultCallback; + + if ( + typeof _callbackDefault !== 'function' && + (typeof _callbackForNormalMode !== 'function' || typeof _callbackForIncognitoOrPrivateMode !== 'function') + ) { + throw 'Default callback or specific callbacks are required'; + } + }; + + var _executeUserCallback = function () { + if (_browsingInIncognitoMode && typeof _callbackForIncognitoOrPrivateMode === 'function') { + _callbackForIncognitoOrPrivateMode(this); + } else if (!_browsingInIncognitoMode && typeof _callbackForNormalMode === 'function') { + _callbackForNormalMode(this); + } + + if (typeof _callbackDefault !== 'undefined') { + _callbackDefault(_browsingInIncognitoMode, this) + } + }; + + return this; }; @@ -140,36 +119,36 @@ var BrowsingModeDetector = function () { * @constructor */ var BrowserFactory = function () { - var _browser; - - this.browser = function (BrowsingModeDetector) { - if (typeof _browser === 'object') { - return _browser; - } - - return _browser = _resolve(BrowsingModeDetector); - }; - - /** - * @param {BrowsingModeDetector} BrowsingModeDetector - * @returns {*} - * @private - */ - var _resolve = function (BrowsingModeDetector) { - if (window.webkitRequestFileSystem) { - return new WebkitBrowser(BrowsingModeDetector); - } else if ('MozAppearance' in document.documentElement.style) { - return new MozillaBrowser(BrowsingModeDetector); - } else if (/constructor/i.test(window.HTMLElement)) { - return new SafariBrowser(BrowsingModeDetector); - } else if (window.PointerEvent || window.MSPointerEvent) { - return new IE10EdgeBrowser(BrowsingModeDetector); - } else { - return new OtherBrowser(BrowsingModeDetector); - } - }; - - return this; + var _browser; + + this.browser = function (BrowsingModeDetector) { + if (typeof _browser === 'object') { + return _browser; + } + + return _browser = _resolve(BrowsingModeDetector); + }; + + /** + * @param {BrowsingModeDetector} BrowsingModeDetector + * @returns {*} + * @private + */ + var _resolve = function (BrowsingModeDetector) { + if (window.webkitRequestFileSystem) { + return new WebkitBrowser(BrowsingModeDetector); + } else if ('MozAppearance' in document.documentElement.style) { + return new MozillaBrowser(BrowsingModeDetector); + } else if (/constructor/i.test(window.HTMLElement)) { + return new SafariBrowser(BrowsingModeDetector); + } else if (window.PointerEvent || window.MSPointerEvent) { + return new IE10EdgeBrowser(BrowsingModeDetector); + } else { + return new OtherBrowser(BrowsingModeDetector); + } + }; + + return this; }; @@ -179,28 +158,30 @@ var BrowserFactory = function () { * @constructor */ var WebkitBrowser = function (BrowsingModeDetector) { - this.BrowsingModeDetector = BrowsingModeDetector; - - this.detectBrowsingMode = function () { - var self = this; + this.BrowsingModeDetector = BrowsingModeDetector; - var callbackWhenWebkitRequestFileSystemIsON = function () { - self.BrowsingModeDetector.setBrowsingInNormalMode(); - }; + this.detectBrowsingMode = function (_executeUserCallback) { + var self = this; - var callbackWhenWebkitRequestFileSystemIsOFF = function (e) { - self.BrowsingModeDetector.setBrowsingInIncognitoMode(); - }; + var callbackWhenWebkitRequestFileSystemIsON = function () { + self.BrowsingModeDetector.setBrowsingInNormalMode(); + _executeUserCallback(); + }; - window.webkitRequestFileSystem( - window.TEMPORARY, - 1, - callbackWhenWebkitRequestFileSystemIsON, - callbackWhenWebkitRequestFileSystemIsOFF - ); + var callbackWhenWebkitRequestFileSystemIsOFF = function () { + self.BrowsingModeDetector.setBrowsingInIncognitoMode(); + _executeUserCallback(); }; - return this; + window.webkitRequestFileSystem( + window.TEMPORARY, + 1, + callbackWhenWebkitRequestFileSystemIsON, + callbackWhenWebkitRequestFileSystemIsOFF + ); + }; + + return this; }; /** @@ -209,17 +190,18 @@ var WebkitBrowser = function (BrowsingModeDetector) { * @constructor */ var MozillaBrowser = function (BrowsingModeDetector) { - this.BrowsingModeDetector = BrowsingModeDetector; - - this.detectBrowsingMode = function () { - if (navigator.serviceWorker) { - this.BrowsingModeDetector.setBrowsingInNormalMode(); - } else { - this.BrowsingModeDetector.setBrowsingInIncognitoMode(); - } - }; - - return this; + this.BrowsingModeDetector = BrowsingModeDetector; + + this.detectBrowsingMode = function (_executeUserCallback) { + if (navigator.serviceWorker) { + this.BrowsingModeDetector.setBrowsingInNormalMode(); + } else { + this.BrowsingModeDetector.setBrowsingInIncognitoMode(); + } + _executeUserCallback(); + }; + + return this; }; /** @@ -228,41 +210,42 @@ var MozillaBrowser = function (BrowsingModeDetector) { * @constructor */ var SafariBrowser = function (BrowsingModeDetector) { - this.BrowsingModeDetector = BrowsingModeDetector; - - this.detectBrowsingMode = function () { - // iOS 11 - // From gist discussion: https://gist.github.com/cou929/7973956#gistcomment-2272103 - try { - window.openDatabase(null, null, null, null); - } catch (e) { - this.BrowsingModeDetector.setBrowsingInIncognitoMode(); - } - - // Older Safari - try { - if (localStorage.length) { - this.BrowsingModeDetector.setBrowsingInNormalMode(); - } else { - localStorage.i = 1; - localStorage.removeItem('i'); - this.BrowsingModeDetector.setBrowsingInNormalMode(); - } - } catch (e) { - // From: https://gist.github.com/jherax/a81c8c132d09cc354a0e2cb911841ff1 - // Safari only enables cookie in private mode - // if cookie is disabled then all client side storage is disabled - // if all client side storage is disabled, then there is no point - // in using private mode - if (navigator.cookieEnabled) { - this.BrowsingModeDetector.setBrowsingInIncognitoMode(); - } else { - this.BrowsingModeDetector.setBrowsingInNormalMode(); - } - } - }; + this.BrowsingModeDetector = BrowsingModeDetector; + + this.detectBrowsingMode = function (_executeUserCallback) { + // iOS 11 + // From gist discussion: https://gist.github.com/cou929/7973956#gistcomment-2272103 + try { + window.openDatabase(null, null, null, null); + } catch (e) { + this.BrowsingModeDetector.setBrowsingInIncognitoMode(); + _executeUserCallback(); + } + + // Older Safari + try { + if (!localStorage.length) { + localStorage.i = 1; + localStorage.removeItem('i'); + } + this.BrowsingModeDetector.setBrowsingInNormalMode(); + } catch (e) { + // From: https://gist.github.com/jherax/a81c8c132d09cc354a0e2cb911841ff1 + // Safari only enables cookie in private mode + // if cookie is disabled then all client side storage is disabled + // if all client side storage is disabled, then there is no point + // in using private mode + if (navigator.cookieEnabled) { + this.BrowsingModeDetector.setBrowsingInIncognitoMode(); + } else { + this.BrowsingModeDetector.setBrowsingInNormalMode(); + } + } - return this; + _executeUserCallback(); + }; + + return this; }; /** @@ -271,15 +254,18 @@ var SafariBrowser = function (BrowsingModeDetector) { * @constructor */ var IE10EdgeBrowser = function (BrowsingModeDetector) { - this.BrowsingModeDetector = BrowsingModeDetector; - - this.detectBrowsingMode = function () { - if (!window.indexedDB) { - this.BrowsingModeDetector.setBrowsingInIncognitoMode(); - } - }; - - return this; + this.BrowsingModeDetector = BrowsingModeDetector; + + this.detectBrowsingMode = function (_executeUserCallback) { + if (window.indexedDB) { + this.BrowsingModeDetector.setBrowsingInNormalMode(); + } else { + this.BrowsingModeDetector.setBrowsingInIncognitoMode(); + } + _executeUserCallback(); + }; + + return this; }; /** @@ -288,11 +274,12 @@ var IE10EdgeBrowser = function (BrowsingModeDetector) { * @constructor */ var OtherBrowser = function (BrowsingModeDetector) { - this.BrowsingModeDetector = BrowsingModeDetector; + this.BrowsingModeDetector = BrowsingModeDetector; - this.detectBrowsingMode = function () { - this.BrowsingModeDetector.setBrowsingInNormalMode(); - }; + this.detectBrowsingMode = function (_executeUserCallback) { + this.BrowsingModeDetector.setBrowsingInNormalMode(); + _executeUserCallback(); + }; - return this; -}; \ No newline at end of file + return this; +}; diff --git a/example.html b/example.html index 3f7a271..9f8b290 100644 --- a/example.html +++ b/example.html @@ -1,62 +1,62 @@ - - Test incognito or private browsing + + Test incognito or private browsing - +
Testing...
- \ No newline at end of file +