Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of AMD and CommonJS modules #77

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -75,6 +75,21 @@ if(typeof fuckAdBlock !== 'undefined' || typeof FuckAdBlock !== 'undefined') {
importFAB.src = 'https://cdnjs.cloudflare.com/ajax/libs/fuckadblock/3.2.1/fuckadblock.min.js';
document.head.appendChild(importFAB);
}

// AMD
require(['FuckAdBlock'], function(FuckAdBlock) {
let fuckAdBlock = new FuckAdBlock({
checkOnLoad: true,
resetOnEnd: true
});
})

// CommonJS
let FuckAdBlock = require('fuckadblock');
let fuckAdBlock = new FuckAdBlock({
checkOnLoad: true,
resetOnEnd: true
});
```

Default options
68 changes: 38 additions & 30 deletions fuckadblock.js
Original file line number Diff line number Diff line change
@@ -52,11 +52,11 @@
FuckAdBlock.prototype._options = null;
FuckAdBlock.prototype._var = null;
FuckAdBlock.prototype._bait = null;

FuckAdBlock.prototype._log = function(method, message) {
console.log('[FuckAdBlock]['+method+'] '+message);
};

FuckAdBlock.prototype.setOption = function(options, value) {
if(value !== undefined) {
var key = options;
@@ -71,55 +71,55 @@
}
return this;
};

FuckAdBlock.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;
this._var.bait.offsetTop;
this._var.bait.offsetWidth;
this._var.bait.clientHeight;
this._var.bait.clientWidth;

if(this._options.debug === true) {
this._log('_creatBait', 'Bait has been created');
}
};
FuckAdBlock.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');
}
};

FuckAdBlock.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');
}
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;
};
FuckAdBlock.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');
}
};

FuckAdBlock.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 @@
FuckAdBlock.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');
}
};

FuckAdBlock.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;
};
FuckAdBlock.prototype.onDetected = function(fn) {
@@ -238,13 +238,21 @@
FuckAdBlock.prototype.onNotDetected = function(fn) {
return this.on(false, fn);
};

window.FuckAdBlock = FuckAdBlock;

if(window.fuckAdBlock === undefined) {
window.fuckAdBlock = new FuckAdBlock({
checkOnLoad: true,
resetOnEnd: true

if(typeof define === "function" && define.amd) {
define('FuckAdBlock', function () {
return FuckAdBlock;
});
} else if(typeof module === "object" && module.exports) {
module.exports = FuckAdBlock;
} else {
window.FuckAdBlock = FuckAdBlock;

if(window.fuckAdBlock === undefined) {
window.fuckAdBlock = new FuckAdBlock({
checkOnLoad: true,
resetOnEnd: true
});
}
}
})(window);