Skip to content

Commit

Permalink
Implementation of the v3 !
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin committed Jul 31, 2014
1 parent e75e941 commit 1179b6e
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 125 deletions.
99 changes: 93 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,98 @@
FuckAdBlock
FuckAdBlock (v3.0.0)
===========

Allows you to detect those nasty ad blockers
Allows you to detect those nasty ad blockers.
Online exemple: http://sitexw.fr/fuckadblock/


Code exemple
---------------------
```
// Function called if AdBlock is not detected
function adBlockNotDetected() {
alert('AdBlock is not actived');
}
// Function called if AdBlock is detected
function adBlockDetected() {
alert('AdBlock is actived');
}
// Recommended audit because AdBlock lock the file 'fuckadblock.js'
// If the file is not called, the variable does not exist 'fuckAdBlock'
// This means that AdBlock is present
if(fuckAdBlock === undefined) {
adBlockDetected();
} else {
fuckAdBlock.onDetected(adBlockDetected);
fuckAdBlock.onNotDetected(adBlockNotDetected);
// and|or
fuckAdBlock.on(true, adBlockDetected);
fuckAdBlock.on(false, adBlockNotDetected);
// and|or
fuckAdBlock.on(true, adBlockDetected).onNotDetected(adBlockNotDetected);
}
// Change the options
fuckAdBlock.setOptions('checkOnLoad', false);
// and|or
fuckAdBlock.setOptions({
checkOnLoad: false,
resetOnEnd: false
});
```

Default options
---------------------
```
fuckAdBlock.add(true, function() { /* what to do in case of an ad blocker */ });
fuckAdBlock.add(false, function() { /* what to do in case of no ad blocker */ });
fuckAdBlock.check(); // check and trigger callbacks
fuckAdBlock.removeAll(); // detach all the delegates
// At launch, check if AdBlock is enabled
// Uses the method fuckAdBlock.check()
checkOnLoad: true
// At the end of the check, is that it removes all events added ?
resetOnEnd: true
// The number of milliseconds between each check
loopCheckTime: 50
// The number of negative checks after which there is considered that AdBlock is not actived
// Time (ms) = 50*(5-1) = 200ms (per default)
loopMaxNumber: 5
// CSS class used by the bait caught AdBlock
baitClass: 'pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links'
// CSS style used to hide the bait of the users
baitStyle: 'width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;'
```

Method available
---------------------
```
// Allows to set options
// #options: string|object
// #value: string
fuckAdBlock.setOption(options, value);
// Allows to check if AdBlock is enabled
// The parameter 'loop' allows checking without loop several times according to the value of 'loopMaxNumber'
// Exemple: loop=true => time~=200ms (time varies depending on the configuration)
// loop=false => time~=1ms
// #loop: boolean (default: true)
fuckAdBlock.check(loop);
// Allows to manually simulate the presence of AdBlock or not
// #detected: boolean (AdBlock is detected ?)
fuckAdBlock.emitEvent(detected);
// Allows to clear all events added via methods 'on', 'onDetected' and 'onNotDetected'
fuckAdBlock.clearEvent();
// Allows to add an event if AdBlock is detected
// #detected: boolean (true: detected, false: not detected)
// #fn: function
fuckAdBlock.on(detected, fn);
// Similar to fuckAdBlock.on(true|false, fn)
fuckAdBlock.onDetected(fn);
fuckAdBlock.onNotDetected(fn);
```
17 changes: 8 additions & 9 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "FuckAdBlock",
"author": ["Valentin Allaire <[email protected]>",
"Stephan Hesse <[email protected]>"],
"version": "2.2.1",
"main": "fuckadblock.js",
"licenses": [],
"ignore": [
"package.json"
]
"name": "FuckAdBlock",
"author": ["Valentin Allaire <[email protected]>"],
"version": "3.0.0",
"main": "fuckadblock.js",
"licenses": [],
"ignore": [
"package.json"
]
}
228 changes: 150 additions & 78 deletions fuckadblock.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,162 @@
/*
FuckAdBlock 2.2.1
FuckAdBlock 3.0.0
http://github.com/sitexw/FuckAdBlock
*/

function FuckAdBlock() {
this.interval = 50;
this.max = 200;
this.end_reset = true;
this.check_onready = true;
var start = false;
var func_true = [];
var func_false = [];
var div = null;
var loop = null;
var loo_n = null;
var loop_func = function() {
var div_temp = window.getComputedStyle(div,null);
if(div.offsetParent === null
|| div.offsetHeight == 0
|| div.offsetLeft == 0
|| div.offsetTop == 0
|| div.offsetWidth == 0
|| div.clientHeight == 0
|| div.clientWidth == 0
|| div_temp.getPropertyValue('display') == 'none'
|| div_temp.getPropertyValue('visibility') == 'hidden') {
exe(true);
} else if(loo_n >= that.max) {
exe(false);
}
loo_n += that.interval;
(function(window) {
if(window.fuckAdBlock !== undefined) {
return;
}
var exe = function(type) {
if(type == true) {
var array = func_true;
} else {
var array = func_false;

var FuckAdBlock = function(options) {
if(options !== undefined) {
this.setOption(options);
}
clearInterval(loop);
document.body.removeChild(div);
start = false;
for(k in array) {
array[k]();

var self = this;
window.addEventListener('load', function() {
setTimeout(function() {
if(self._options.checkOnLoad === true) {
self.check();
}
}, 1);
}, false);
};
FuckAdBlock.prototype._options = {
checkOnLoad: true,
resetOnEnd: true,
loopCheckTime: 50,
loopMaxNumber: 5,
baitClass: 'pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links',
baitStyle: 'width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;',
};
FuckAdBlock.prototype._var = {
bait: null,
checking: false,
loop: null,
loopNumber: 0,
event: {
detected: [],
notDetected: []
}
};
FuckAdBlock.prototype._bait = null;

FuckAdBlock.prototype.setOption = function(options, value) {
if(value !== undefined) {
var key = options;
options = {};
options[key] = value;
}
if(that.end_reset == true) {
func_true = [];
func_false = [];
}
}
this.add = function(type, func) {
if(type == true) {
func_true[func_true.length] = func;
} else {
func_false[func_false.length] = func;
for(option in options) {
this._options[option] = options[option];
}
return this;
}
this.removeAll = function() {
func_true = [];
func_false = [];
}
this.check = function() {
try {
if(start == true) {
return false;
};

FuckAdBlock.prototype._creatBait = function() {
this._var.bait = document.createElement('div');
this._var.bait.setAttribute('class', this._options.baitClass);
this._var.bait.setAttribute('style', this._options.baitStyle);
window.document.body.appendChild(this._var.bait);
};
FuckAdBlock.prototype._destroyBait = function() {
window.document.body.removeChild(this._var.bait);
this._var.bait = null;
};

FuckAdBlock.prototype.check = function(loop) {
if(loop === undefined) {
loop = true;
}

if(this._var.checking === true) {
return false;
}
this._var.checking = true;

var self = this;
this._var.loopNumber = 0;
if(loop === true) {
this._var.loop = setInterval(function() {
self._checkBait(loop);
}, this._options.loopCheckTime);
}
this._checkBait(loop);

return true;
};
FuckAdBlock.prototype._checkBait = function(loop) {
if(this._var.bait === null) {
this._creatBait();
}

var detected = false;

if(this._var.bait.offsetParent === null
|| this._var.bait.offsetHeight == 0
|| this._var.bait.offsetLeft == 0
|| this._var.bait.offsetTop == 0
|| this._var.bait.offsetWidth == 0
|| this._var.bait.clientHeight == 0
|| this._var.bait.clientWidth == 0) {
detected = true;
}
if(window.getComputedStyle !== undefined) {
var baitTemp = window.getComputedStyle(this._var.bait, null);
if(baitTemp.getPropertyValue('display') == 'none'
|| baitTemp.getPropertyValue('visibility') == 'hidden') {
detected = true;
}
start = true;
div = document.createElement('div');
div.setAttribute('class', 'pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links');
div.setAttribute('style', 'width: 1px !important; height: 1px !important; position: absolute !important; left: -1000px !important; top: -1000px !important;');
document.body.appendChild(div);
loo_n = 0;
loop = setInterval(loop_func, this.interval);
loop_func();
} catch(e) {
console.error(e);
}
}

var that = this;
window.addEventListener('load', function() {
setTimeout(function() {
if(that.check_onready === true) {
fuckAdBlock.check();

if(loop === true) {
this._var.loopNumber++;
if(this._var.loopNumber >= this._options.loopMaxNumber) {
clearInterval(this._var.loop);
this._var.loop = null;
this._var.loopNumber = 0;
}
}

if(detected === true) {
if(loop === true) {
this._var.checking = false;
}
}, 1);
}, false);
}
var fuckAdBlock = new FuckAdBlock();
this.emitEvent(true);
} else if(this._var.loop === null || loop === false) {
if(loop === true) {
this._var.checking = false;
}
this.emitEvent(false);
}
};

FuckAdBlock.prototype.emitEvent = function(detected) {
var fns = this._var.event[(detected===true?'detected':'notDetected')];
for(i in fns) {
fns[i]();
}
if(this._options.resetOnEnd === true) {
this.clearEvent();
}
return this;
};
FuckAdBlock.prototype.clearEvent = function() {
this._var.event.detected = [];
this._var.event.notDetected = [];
};

FuckAdBlock.prototype.on = function(detected, fn) {
this._var.event[(detected===true?'detected':'notDetected')].push(fn);
return this;
};
FuckAdBlock.prototype.onDetected = function(fn) {
return this.on(true, fn);
};
FuckAdBlock.prototype.onNotDetected = function(fn) {
return this.on(false, fn);
};

window.fuckAdBlock = new FuckAdBlock();
})(window);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "FuckAdBlock",
"version": "2.2.1",
"version": "3.0.0",
"description": "Detects ad blockers",
"main": "fuckadblock.js",
"scripts": {
Expand All @@ -15,7 +15,7 @@
"block",
"detection"
],
"author": "Valentin Allaire <[email protected]>, Stephan Hesse <[email protected]>",
"author": "Valentin Allaire <[email protected]>",
"bugs": {
"url": "https://github.com/sitexw/FuckAdBlock/issues"
},
Expand Down
Loading

0 comments on commit 1179b6e

Please sign in to comment.