Skip to content

Commit

Permalink
Merge pull request #25 from sitexw/v3.x
Browse files Browse the repository at this point in the history
Update to v3.2.0
  • Loading branch information
sitexw committed Aug 30, 2015
2 parents 1a29bed + d1efde9 commit 57c3c49
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 40 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FuckAdBlock (v3.1.1)
FuckAdBlock (v3.2.0)
===========

You can detect nasty ad blockers.
Expand Down Expand Up @@ -59,9 +59,10 @@ if(typeof fuckAdBlock === 'undefined') {
}

// Change the options
fuckAdBlock.setOptions('checkOnLoad', false);
fuckAdBlock.setOption('checkOnLoad', false);
// and|or
fuckAdBlock.setOptions({
fuckAdBlock.setOption({
debug: true,
checkOnLoad: false,
resetOnEnd: false
});
Expand Down Expand Up @@ -89,6 +90,9 @@ baitClass: 'pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads

// 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;'

// Displays the debug in the console (available only from version 3.2 and more)
debug: false
```

Method available
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fuck-adblock",
"version": "3.1.1",
"version": "3.2.0",
"description": "Detects ad blockers (AdBlock, ...)",
"authors": [{
"name" : "Valentin Allaire",
Expand Down
80 changes: 69 additions & 11 deletions fuckadblock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* FuckAdBlock 3.1.1
* FuckAdBlock 3.2.0
* Copyright (c) 2015 Valentin Allaire <[email protected]>
* Released under the MIT license
* https://github.com/sitexw/FuckAdBlock
Expand All @@ -13,10 +13,11 @@
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;'
baitStyle: 'width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;',
debug: false
};
this._var = {
version: '3.1.1',
version: '3.2.0',
bait: null,
checking: false,
loop: null,
Expand All @@ -30,6 +31,9 @@
var eventCallback = function() {
setTimeout(function() {
if(self._options.checkOnLoad === true) {
if(self._options.debug === true) {
self._log('onload->eventCallback', 'A check loading is launched');
}
if(self._var.bait === null) {
self._creatBait();
}
Expand All @@ -49,6 +53,10 @@
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;
Expand All @@ -57,6 +65,9 @@
}
for(var option in options) {
this._options[option] = options[option];
if(this._options.debug === true) {
this._log('setOption', 'The option "'+option+'" he was assigned to "'+options[option]+'"');
}
}
return this;
};
Expand All @@ -74,18 +85,33 @@
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;
Expand All @@ -101,7 +127,12 @@
self._checkBait(loop);
}, this._options.loopCheckTime);
}
this._checkBait(loop);
setTimeout(function() {
self._checkBait(loop);
}, 1);
if(this._options.debug === true) {
this._log('check', 'A check is in progress ...');
}

return true;
};
Expand Down Expand Up @@ -130,33 +161,52 @@
}
}

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) {
clearInterval(this._var.loop);
this._var.loop = null;
this._var.loopNumber = 0;
this._stopLoop();
}
}

if(detected === true) {
if(loop === true) {
this._var.checking = false;
}
this._stopLoop();
this._destroyBait();
this.emitEvent(true);
} else if(this._var.loop === null || loop === false) {
if(loop === true) {
this._var.checking = false;
}
} else if(this._var.loop === null || loop === false) {
this._destroyBait();
this.emitEvent(false);
if(loop === true) {
this._var.checking = false;
}
}
};
FuckAdBlock.prototype._stopLoop = function(detected) {
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) {
this._log('emitEvent', 'Call function '+(parseInt(i)+1)+'/'+fns.length);
}
if(fns.hasOwnProperty(i)) {
fns[i]();
}
Expand All @@ -169,10 +219,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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fuckadblock",
"version": "3.1.1",
"version": "3.2.0",
"description": "Detects ad blockers (AdBlock, ...)",
"author": {
"name" : "Valentin Allaire",
Expand Down
64 changes: 40 additions & 24 deletions test.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<!doctype html>
<html>
<head>
<title>FuckAdBlock 3.1.1</title>
<title>FuckAdBlock 3.2.0</title>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.7/styles/default.min.css" rel="stylesheet">
<style>
header {
margin-bottom: 20px;
Expand All @@ -30,17 +31,20 @@
border: 1px solid #cccccc;
border-radius: 4px;
}
pre {
background-color: #f0f0f0;
}
</style>
</head>
<body>
<div class="container">
<header class="row">
<div class="col-sm-6">
<h1>FuckAdBlock <small>3.1.1</small></h1>
<h1>FuckAdBlock <small>3.2.0</small></h1>
</div>
<div class="col-sm-6 text-right">
<h4>
<a href="https://github.com/sitexw/BlockAdBlock" style="color: #7fbfe8;">BlockAdBlock</a>
<a href="https://github.com/sitexw/BlockAdBlock" style="color: #acc6d7;">BlockAdBlock</a>
&nbsp; &nbsp;
<a href="http://sitexw.fr/fuckadblock/">Online example</a>
&nbsp; &nbsp;
Expand All @@ -56,8 +60,8 @@ <h4>
</div>
<div class="col-sm-7">
<h3 class="text-left">Publicity example<button class="btn btn-primary btn-xs pull-right" onclick="checkAgain();">Check again</button></h3>
<h5 class="bg-success" id="adb-not-enabled" style="display: none;">AdBlock is not enabled</h5>
<h5 class="bg-danger" id="adb-enabled" style="display: none;">AdBlock is enabled</h5>
<h5 class="bg-success" id="fuck-adb-not-enabled" style="display: none;">AdBlock is not enabled</h5>
<h5 class="bg-danger" id="fuck-adb-enabled" style="display: none;">AdBlock is enabled</h5>
</div>
</div>
<div class="row">
Expand All @@ -78,7 +82,7 @@ <h3 class="text-left">Install via</h3>
<p>Node.js/io.js:</p>
<pre>npm install fuckadblock</pre>
<h3 class="text-left">Code example</h3>
<pre>// Function called if AdBlock is not detected
<pre><code class="javascript">// Function called if AdBlock is not detected
function adBlockNotDetected() {
alert('AdBlock is not enabled');
}
Expand All @@ -103,14 +107,15 @@ <h3 class="text-left">Code example</h3>
}

// Change the options
fuckAdBlock.setOptions('checkOnLoad', false);
fuckAdBlock.setOption('checkOnLoad', false);
// and|or
fuckAdBlock.setOptions({
fuckAdBlock.setOption({
debug: true,
checkOnLoad: false,
resetOnEnd: false
});</pre>
});</code></pre>
<h3 class="text-left">Default options</h3>
<pre>// At launch, check if AdBlock is enabled
<pre><code class="javascript">// At launch, check if AdBlock is enabled
// Uses the method fuckAdBlock.check()
checkOnLoad: true

Expand All @@ -129,9 +134,12 @@ <h3 class="text-left">Default options</h3>

// 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;'
</pre>

// Displays the debug in the console (available only from version 3.2 and more)
debug: false
</code></pre>
<h3 class="text-left">Method available</h3>
<pre>// Allows to set options
<pre><code class="javascript">// Allows to set options
// #options: string|object
// #value: string
fuckAdBlock.setOption(options, value);
Expand All @@ -157,46 +165,48 @@ <h3 class="text-left">Method available</h3>

// Similar to fuckAdBlock.on(true|false, fn)
fuckAdBlock.onDetected(fn);
fuckAdBlock.onNotDetected(fn);</pre>
fuckAdBlock.onNotDetected(fn);</code></pre>
<h3 class="text-left">Instance</h3>
<p>
<i>(Available only from version 3.1 and more)</i><br>
By default, FuckAdBlock is instantiated automatically.<br>
To block this automatic instantiation, simply create a variable "fuckAdBlock" with a value (null, false, ...) before importing the script.
</p>
<pre>&lt;script&gt;var fuckAdBlock = false;&lt/script&gt;
&ltscript src="./fuckadblock.js"&gt;&lt/script&gt;</pre>
<pre><code class="html">&lt;script&gt;var fuckAdBlock = false;&lt/script&gt;
&ltscript src="./fuckadblock.js"&gt;&lt/script&gt;</code></pre>
After that, you are free to create your own instances:
<pre>fuckAdBlock = new FuckAdBlock;
<pre><code class="javascript">fuckAdBlock = new FuckAdBlock;
// and|or
myFuckAdBlock = new FuckAdBlock({
checkOnLoad: true,
resetOnEnd: true
});</pre>
});</code></pre>
</div>
</div>
</div>

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="./fuckadblock.js"></script>
<script>
function adBlockDetected() {
document.getElementById('adb-enabled').style.display = 'block';
document.getElementById('adb-not-enabled').style.display = 'none';
$('#fuck-adb-enabled').show();
$('#fuck-adb-not-enabled').hide();
}
function adBlockNotDetected() {
document.getElementById('adb-enabled').style.display = 'none';
document.getElementById('adb-not-enabled').style.display = 'block';
$('#fuck-adb-enabled').hide();
$('#fuck-adb-not-enabled').show();
}

if(typeof fuckAdBlock === 'undefined') {
adBlockDetected();
} else {
fuckAdBlock.setOption({ debug: true });
fuckAdBlock.onDetected(adBlockDetected).onNotDetected(adBlockNotDetected);
}

function checkAgain() {
document.getElementById('adb-enabled').style.display = 'none';
document.getElementById('adb-not-enabled').style.display = 'none';
$('#fuck-adb-enabled').hide();
$('#fuck-adb-not-enabled').hide();
// setTimeout 300ms for the recheck is visible when you click on the button
setTimeout(function() {
if(typeof fuckAdBlock === 'undefined') {
Expand All @@ -208,5 +218,11 @@ <h3 class="text-left">Instance</h3>
}, 300);
}
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.7/highlight.min.js"></script>
<script>
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
</script>
</body>
</html>

0 comments on commit 57c3c49

Please sign in to comment.