diff --git a/README.md b/README.md index e4598de..f0dba91 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,21 @@ if(typeof blockAdBlock === 'undefined') { blockAdBlock.on(true, adBlockDetected).onNotDetected(adBlockNotDetected); } +// AMD +require(['BlockAdBlock'], function(BlockAdBlock) { + let blockAdBlock = new BlockAdBlock({ + checkOnLoad: true, + resetOnEnd: true + }); +}) + +// CommonJS +let BlockAdBlock = require('blockadblock'); +let blockAdBlock = new BlockAdBlock({ + checkOnLoad: true, + resetOnEnd: true +}); + // Change the options blockAdBlock.setOption('checkOnLoad', false); // and|or diff --git a/blockadblock.js b/blockadblock.js index bca91a9..91d4979 100644 --- a/blockadblock.js +++ b/blockadblock.js @@ -52,11 +52,11 @@ BlockAdBlock.prototype._options = null; BlockAdBlock.prototype._var = null; BlockAdBlock.prototype._bait = null; - + BlockAdBlock.prototype._log = function(method, message) { console.log('[BlockAdBlock]['+method+'] '+message); }; - + BlockAdBlock.prototype.setOption = function(options, value) { if(value !== undefined) { var key = options; @@ -71,13 +71,13 @@ } return this; }; - + BlockAdBlock.prototype._creatBait = function() { var bait = document.createElement('div'); bait.setAttribute('class', this._options.baitClass); bait.setAttribute('style', this._options.baitStyle); this._var.bait = window.document.body.appendChild(bait); - + this._var.bait.offsetParent; this._var.bait.offsetHeight; this._var.bait.offsetLeft; @@ -85,7 +85,7 @@ this._var.bait.offsetWidth; this._var.bait.clientHeight; this._var.bait.clientWidth; - + if(this._options.debug === true) { this._log('_creatBait', 'Bait has been created'); } @@ -93,21 +93,21 @@ BlockAdBlock.prototype._destroyBait = function() { window.document.body.removeChild(this._var.bait); this._var.bait = null; - + if(this._options.debug === true) { this._log('_destroyBait', 'Bait has been removed'); } }; - + BlockAdBlock.prototype.check = function(loop) { if(loop === undefined) { loop = true; } - + if(this._options.debug === true) { this._log('check', 'An audit was requested '+(loop===true?'with a':'without')+' loop'); } - + if(this._var.checking === true) { if(this._options.debug === true) { this._log('check', 'A check was canceled because there is already an ongoing'); @@ -115,11 +115,11 @@ return false; } this._var.checking = true; - + if(this._var.bait === null) { this._creatBait(); } - + var self = this; this._var.loopNumber = 0; if(loop === true) { @@ -133,16 +133,16 @@ if(this._options.debug === true) { this._log('check', 'A check is in progress ...'); } - + return true; }; BlockAdBlock.prototype._checkBait = function(loop) { var detected = false; - + if(this._var.bait === null) { this._creatBait(); } - + if(window.document.body.getAttribute('abp') !== null || this._var.bait.offsetParent === null || this._var.bait.offsetHeight == 0 @@ -159,18 +159,18 @@ detected = true; } } - + if(this._options.debug === true) { this._log('_checkBait', 'A check ('+(this._var.loopNumber+1)+'/'+this._options.loopMaxNumber+' ~'+(1+this._var.loopNumber*this._options.loopCheckTime)+'ms) was conducted and detection is '+(detected===true?'positive':'negative')); } - + if(loop === true) { this._var.loopNumber++; if(this._var.loopNumber >= this._options.loopMaxNumber) { this._stopLoop(); } } - + if(detected === true) { this._stopLoop(); this._destroyBait(); @@ -190,17 +190,17 @@ clearInterval(this._var.loop); this._var.loop = null; this._var.loopNumber = 0; - + if(this._options.debug === true) { this._log('_stopLoop', 'A loop has been stopped'); } }; - + BlockAdBlock.prototype.emitEvent = function(detected) { if(this._options.debug === true) { this._log('emitEvent', 'An event with a '+(detected===true?'positive':'negative')+' detection was called'); } - + var fns = this._var.event[(detected===true?'detected':'notDetected')]; for(var i in fns) { if(this._options.debug === true) { @@ -218,18 +218,18 @@ BlockAdBlock.prototype.clearEvent = function() { this._var.event.detected = []; this._var.event.notDetected = []; - + if(this._options.debug === true) { this._log('clearEvent', 'The event list has been cleared'); } }; - + BlockAdBlock.prototype.on = function(detected, fn) { this._var.event[(detected===true?'detected':'notDetected')].push(fn); if(this._options.debug === true) { this._log('on', 'A type of event "'+(detected===true?'detected':'notDetected')+'" was added'); } - + return this; }; BlockAdBlock.prototype.onDetected = function(fn) { @@ -238,13 +238,21 @@ BlockAdBlock.prototype.onNotDetected = function(fn) { return this.on(false, fn); }; - - window.BlockAdBlock = BlockAdBlock; - - if(window.blockAdBlock === undefined) { - window.blockAdBlock = new BlockAdBlock({ - checkOnLoad: true, - resetOnEnd: true + + if(typeof define === "function" && define.amd) { + define('BlockAdBlock', function () { + return BlockAdBlock; }); + } else if(typeof module === "object" && module.exports) { + module.exports = BlockAdBlock; + } else { + window.BlockAdBlock = BlockAdBlock; + + if(window.blockAdBlock === undefined) { + window.blockAdBlock = new BlockAdBlock({ + checkOnLoad: true, + resetOnEnd: true + }); + } } })(window);