FuckAdBlock

repository·master·Indexed 23 days ago

https://github.com/sitexw/fuckadblock

A JavaScript library version 3.2.1 used to detect if a user has an ad blocker enabled in their browser. It works by attempting to load 'bait' elements and checking if they are hidden or blocked. Compatible with Google Chrome, Mozilla Firefox, Internet Explorer (8+), Safari, and Opera.

Tokens
1.6K
Snippets
3
Records
9
Agent score
34%

What's inside fuckadblock

  1. Create custom FuckAdBlock instances

    master

    By default, FuckAdBlock instantiates itself automatically. To prevent this, declare var fuckAdBlock = false; before importing the script. You can then manually create your own instances with specific configurations.

    <!-- Prevent automatic instantiation -->
    <script>var fuckAdBlock = false;</script>
    <script src="./fuckadblock.js"></script>
    // Create a default instance
    fuckAdBlock = new FuckAdBlock;
    
    // Create a custom instance with specific options
    myFuckAdBlock = new FuckAdBlock({
    	checkOnLoad: true,
    	resetOnEnd: true
    });
  2. Implement AdBlock detection

    master

    To use FuckAdBlock, it is recommended to place the implementation at the end of the <body>. The script can be loaded dynamically to prevent detection evasion. If the script fails to load (e.g., due to being blocked by an extension), you should treat it as an AdBlock detection event.

    // 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);
    }
  3. Configure FuckAdBlock options

    master
    FuckAdBlock uses several configuration options to control its behavior. You can set these via fuckAdBlock.setOption(options, value) or by passing an object to the constructor when creating a new instance.
  4. Run an adblock detection check

    master

    The check(loop) method initiates the detection process. It creates a 'bait' element (an element with common ad-related CSS classes) and checks if it is hidden or removed from the DOM by an adblocker.

    • If loop is true (default), the detector will perform multiple checks over a period defined by loopCheckTime and loopMaxNumber to ensure accuracy.
    • If loop is false, it performs a single check.

    Returns true if a check is successfully started, or false if a check is already in progress.

  5. Initialize FuckAdBlock

    master

    You can use the FuckAdBlock constructor to create a new instance. By default, the library attaches a pre-configured instance to window.fuckAdBlock. You can pass an options object to customize behavior such as automatic checking on page load or event resetting.

    Common options include:

    • checkOnLoad: (boolean) If true, triggers a check automatically when the window load event fires.
    • resetOnEnd: (boolean) If true, clears all registered event listeners after an event is emitted.
    • debug: (boolean) Enables verbose logging to the console.
    • loopMaxNumber: (number) The maximum number of times to loop the check.
    • loopCheckTime: (number) The interval in milliseconds between loop checks.
  6. Handle detection events with onDetected and onNotDetected

    master

    Register callback functions that execute when an adblocker is either detected or not detected.

    • onDetected(fn): Executes fn when an adblocker is found.
    • onNotDetected(fn): Executes fn when no adblocker is found.
    • on(detected, fn): Generic method where detected is a boolean.

    If the resetOnEnd option is set to true, all registered listeners are cleared after the event is triggered.

  7. Reference: FuckAdBlock Configuration Options

    master

    The following options are available when initializing or configuring a FuckAdBlock instance:

    {
      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
    }