Skip to content

Instantly share code, notes, and snippets.

@adgad
Created June 2, 2016 12:35
Show Gist options
  • Save adgad/d2d0a51779fcf99df2e42ea8064ae286 to your computer and use it in GitHub Desktop.
Save adgad/d2d0a51779fcf99df2e42ea8064ae286 to your computer and use it in GitHub Desktop.
Adblocking Gist
(function() {
//The below is a slightly modified version of https://github.com/sitexw/BlockAdBlock
var BlockAdBlock = function() {
this._options = {
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;'
};
this._var = {
version: '3.2.1',
bait: null,
checking: false,
loop: null,
loopNumber: 0,
event: {
detected: [],
notDetected: []
}
};
};
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;
this._var.bait.offsetTop;
this._var.bait.offsetWidth;
this._var.bait.clientHeight;
this._var.bait.clientWidth;
};
BlockAdBlock.prototype._destroyBait = function() {
window.document.body.removeChild(this._var.bait);
this._var.bait = null;
};
BlockAdBlock.prototype.check = function(loop) {
if (loop === undefined) {
loop = true;
}
if (this._var.checking === true) {
return false;
}
this._var.checking = true;
if (this._var.bait === null) {
this._creatBait();
}
this._var.loopNumber = 0;
if (loop === true) {
this._var.loop = setInterval(function() {
this._checkBait(loop);
}.bind(this), this._options.loopCheckTime);
}
setTimeout(function() {
this._checkBait(loop);
}.bind(this), 1);
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 || 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 && (baitTemp.getPropertyValue('display') === 'none' || baitTemp.getPropertyValue('visibility') === 'hidden')) {
detected = true;
}
}
if (loop === true) {
this._var.loopNumber++;
if (this._var.loopNumber >= this._options.loopMaxNumber) {
this._stopLoop();
}
}
if (detected === true) {
this._stopLoop();
this._destroyBait();
this.emitEvent(true);
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;
}
}
};
BlockAdBlock.prototype._stopLoop = function() {
clearInterval(this._var.loop);
this._var.loop = null;
this._var.loopNumber = 0;
};
BlockAdBlock.prototype.emitEvent = function(detected) {
var fns = this._var.event[(detected === true ? 'detected' : 'notDetected')];
for (var i in fns) {
if (fns.hasOwnProperty(i)) {
fns[i]();
}
}
if (this._options.resetOnEnd === true) {
this.clearEvent();
}
return this;
};
BlockAdBlock.prototype.clearEvent = function() {
this._var.event.detected = [];
this._var.event.notDetected = [];
};
BlockAdBlock.prototype.on = function(detected, fn) {
this._var.event[(detected === true ? 'detected' : 'notDetected')].push(fn);
return this;
};
BlockAdBlock.prototype.onDetected = function(fn) {
return this.on(true, fn);
};
BlockAdBlock.prototype.onNotDetected = function(fn) {
return this.on(false, fn);
};
//This is where we initialise the check and do our stuff
function localStorageSupported() {
var check = 'adblock-localstoragecheck';
try {
localStorage.setItem(check, check);
localStorage.removeItem(check);
return true;
} catch(e) {
return false;
}
}
//Should probably only run test if localstorage supported, otherwise can't track if they've converted
if(localStorageSupported()) {
var blockAdBlock = new BlockAdBlock();
blockAdBlock.onDetected(function() {
//set a flag to say this person has adblocked
localStorage.setItem('adblockdetected', true);
console.log('adblock detected - run test');
});
blockAdBlock.onNotDetected(function() {
//check if this person had previously adblocked
var hasPreviouslyBlocked = localStorage.getItem('adblockdetected');
//if they have, then this is a conversion
if(hasPreviouslyBlocked) {
localStorage.removeItem('adblockdetected');
console.log('This person has whitelisted - count as conversion');
}
});
//trigger the check
blockAdBlock.check(true);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment