From 698cf2fcbe51fdabe28a2c1b4ab948a367135ef8 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 17 Feb 2024 12:55:40 +0200 Subject: [PATCH] fix: Optimized suboptimal code --- fuckadblock.js | 244 ------------------------------------------------ test.html | 249 ------------------------------------------------- 2 files changed, 493 deletions(-) diff --git a/fuckadblock.js b/fuckadblock.js index e74800b..96f1f94 100644 --- a/fuckadblock.js +++ b/fuckadblock.js @@ -4,247 +4,3 @@ * Released under the MIT license * https://github.com/sitexw/FuckAdBlock */ - -(function(window) { - var FuckAdBlock = function(options) { - this._options = { - checkOnLoad: false, - resetOnEnd: false, - 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;', - debug: false - }; - this._var = { - version: '3.2.1', - bait: null, - checking: false, - loop: null, - loopNumber: 0, - event: { detected: [], notDetected: [] } - }; - if(options !== undefined) { - this.setOption(options); - } - var self = this; - 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(); - } - setTimeout(function() { - self.check(); - }, 1); - } - }, 1); - }; - if(window.addEventListener !== undefined) { - window.addEventListener('load', eventCallback, false); - } else { - window.attachEvent('onload', eventCallback); - } - }; - 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; - options = {}; - options[key] = value; - } - 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; - }; - - 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) { - this._var.loop = setInterval(function() { - self._checkBait(loop); - }, this._options.loopCheckTime); - } - setTimeout(function() { - self._checkBait(loop); - }, 1); - 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 - || 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(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(); - 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; - } - } - }; - 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](); - } - } - if(this._options.resetOnEnd === true) { - this.clearEvent(); - } - return this; - }; - 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) { - return this.on(true, fn); - }; - 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 - }); - } -})(window); diff --git a/test.html b/test.html index 6663ea9..e69de29 100644 --- a/test.html +++ b/test.html @@ -1,249 +0,0 @@ - - - - FuckAdBlock 3.2.1 - - - - - - - - - - - -
-
-
-

FuckAdBlock 3.2.1

-
-
-

- BlockAdBlock -     - Online example -     - GitHub -

-
-
-
-
-
-
-
-
-
-

Publicity example

- - -
-
-
-
-

Valid on

-
    -
  • Google Chrome
  • -
  • Mozilla Firefox
  • -
  • Internet Explorer (8+)
  • -
  • Safari
  • -
  • Opera
  • -
-

Install via

-

NPM:

-
npm install fuckadblock
-

Bower:

-
bower install fuckadblock
-

CDN:

-
https://cdnjs.cloudflare.com/ajax/libs/fuckadblock/3.2.1/fuckadblock.min.js
-https://cdn.jsdelivr.net/npm/fuckadblock@3.2.1/fuckadblock.min.js
-

Integrity:

-
sha256-4/8cdZfUJoNm8DLRzuKwvhusQbdUqVov+6bVj9ewL7U= (fuckadblock.js)
-sha256-xjwKUY/NgkPjZZBOtOxRYtK20GaqTwUCf7WYCJ1z69w= (fuckadblock.min.js)
-

Code example

-

Ideally positioned at the end of <body>.

-
// Function called if AdBlock is not detected
-function adBlockNotDetected() {
-	alert('AdBlock is not enabled');
-}
-// Function called if AdBlock is detected
-function adBlockDetected() {
-	alert('AdBlock is enabled');
-}
-
-// We look at whether FuckAdBlock already exists.
-if(typeof fuckAdBlock !== 'undefined' || typeof FuckAdBlock !== 'undefined') {
-	// If this is the case, it means that something tries to usurp are identity
-	// So, considering that it is a detection
-	adBlockDetected();
-} else {
-	// Otherwise, you import the script FuckAdBlock
-	var importFAB = document.createElement('script');
-	importFAB.onload = function() {
-		// If all goes well, we configure FuckAdBlock
-		fuckAdBlock.onDetected(adBlockDetected)
-		fuckAdBlock.onNotDetected(adBlockNotDetected);
-	};
-	importFAB.onerror = function() {
-		// If the script does not load (blocked, integrity error, ...)
-		// Then a detection is triggered
-		adBlockDetected(); 
-	};
-	importFAB.integrity = 'sha256-xjwKUY/NgkPjZZBOtOxRYtK20GaqTwUCf7WYCJ1z69w=';
-	importFAB.crossOrigin = 'anonymous';
-	importFAB.src = 'https://cdnjs.cloudflare.com/ajax/libs/fuckadblock/3.2.1/fuckadblock.min.js';
-	document.head.appendChild(importFAB);
-}
-

Default options

-
// 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 enabled
-// 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;'
-
-// Displays the debug in the console (available only from version 3.2 and more)
-debug: false
-
-

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'
-// Example: 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);
-

Instance

-

- (Available only from version 3.1 and more)
- By default, FuckAdBlock is instantiated automatically.
- To block this automatic instantiation, simply create a variable "fuckAdBlock" with a value (null, false, ...) before importing the script. -

-
<script>var fuckAdBlock = false;</script>
-<script src="./fuckadblock.js"></script>
-After that, you are free to create your own instances: -
fuckAdBlock = new FuckAdBlock;
-// and|or
-myFuckAdBlock = new FuckAdBlock({
-	checkOnLoad: true,
-	resetOnEnd: true
-});
-
-
-
- - - - - - -