Skip to content

Instantly share code, notes, and snippets.

@simonwep
Last active September 30, 2023 14:32
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save simonwep/12bb870f1777a6cca9e7874637b93458 to your computer and use it in GitHub Desktop.
Save simonwep/12bb870f1777a6cca9e7874637b93458 to your computer and use it in GitHub Desktop.
Blocks BlockAdBlock scripts
// ==UserScript==
// @name BlockAdblock Blocker
// @version 1.0
// @namespace http://tampermonkey.net/
// @description Blocks block-adblock
// @match *://**/*
// @grant none
// @run-at document-start
// ==/UserScript==
(() => {
const originalEval = window.eval;
const keywords = ['advertising', 'ad', 'blocker', 'disabled', 'understand', 'site', 'income', 'okay', 'http://blockadblock.com', ''];
window.eval = str => {
// Check for keywords
const matches = keywords.filter(v => str.includes(v));
if (matches.length / keywords.length > 0.85) {
console.log(`[ABBB] Probability of being ad-related: ${(matches.length / keywords.length) * 100}%`);
// Check if it contains the base64 charset in a variable
if (str.match(/[A-Za-z]+='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'/)) {
console.log(` >> It contains the base64 charset`);
// Check if it will clear the body element
if (str.includes(`document.body.innerHTML=''`)) {
console.log(` >> It'll clear your dom. Blocked.`);
return;
}
}
}
return originalEval(str);
};
})();
@rohitsharmaj7
Copy link

Does this code blocks the adblockers applied at our page?

@simonwep
Copy link
Author

@rohitsharmaj7 It blocks adblocker-blocker not adblocker. It's against blockadblock (cuz PiHole and normal adblockers don't work there anymore).

@rohitsharmaj7
Copy link

Are you familiar with any script that can block the adblocker if detected, if yes, please share the link to script or script that can be used in html code. Thanks in advance

@simonwep
Copy link
Author

@rohitsharmaj7 Sorry, I'm against ads in general and if I would be on the side of ads I probably wouldn't have published this script to block existing adblocker-blocker 😅 Spending time developing such things is wasted time, use it to improve the quality of the product itself or develop systems where users get extras by actually paying money. Ads are terrible and that won't change.

People will find a way to circumvent any kind of mechanism which would make them to watch ads, you can't win this game. Either bet on the 70% without any kind of adblocker or find another way to raise money.

And last but not least, you can't block an adblocker - ads are shown client-side and a user can do whatever he wants with what you give him which is the reason why things like PiHole or YouTube Vanced got invented (check both out, they're great).

If you really want to force your users into viewing ads you'll have to use methods which will make user leave your platform.

@dumblob
Copy link

dumblob commented Aug 25, 2020

@simonwep thanks for your effort - I've tried on https://www.get-digital-help.com/formula-for-matching-a-date-within-a-date-range-in-excel/ and it seems not enough. This is really whack-a-mole game ;)

@dumblob
Copy link

dumblob commented Aug 25, 2020

Oh, I think I found the culprit - @match *://**/* should rather be @match *://*/* (changed the double start to single star). Thanks!

@jefro108
Copy link

jefro108 commented Oct 2, 2020

Maybe https://blockadblock.com/ have changed they way they work but I've installed the script in Greasemonkey and https://blockadblock.com/ still detects adblockers 😞

@simonwep
Copy link
Author

simonwep commented Oct 3, 2020

@jefro108 - If I had to guess, they probably found this script and a working work-around - at least that's how it works right 😂

@DRSDavidSoft
Copy link

DRSDavidSoft commented Mar 1, 2021

@rohitsharmaj7 Sorry, I'm against ads in general and if I would be on the side of ads I probably wouldn't have published this script to block existing adblocker-blocker 😅 Spending time developing such things is wasted time, use it to improve the quality of the product itself or develop systems where users get extras by actually paying money. Ads are terrible and that won't change.

People will find a way to circumvent any kind of mechanism which would make them to watch ads, you can't win this game. Either bet on the 70% without any kind of adblocker or find another way to raise money.

And last but not least, you can't block an adblocker - ads are shown client-side and a user can do whatever he wants with what you give him which is the reason why things like PiHole or YouTube Vanced got invented (check both out, they're great).

If you really want to force your users into viewing ads you'll have to use methods which will make user leave your platform.

👍 Nice answer! I always laugh at people that think they have control over what is shown over the client-side. As I'm personally against any type of ads whatsoever, I find this amusing!

@charlie-moomoo
Copy link

doesnt work

@simonwep
Copy link
Author

@charlie-moomoo it's over a year old... did you expect this to work after a single month without any updates? 😅

@misakibeats
Copy link

Works like a charm thank you. I don't mind ads but some websites push their limits... ever gone onto an anime-related webpage? Either overly sexual anime mobile game ads will pop up or straight up p**n

I ain't having none of that. Thanks a bunch for the script!
Oh, to some of you who are having issues try changing:

// @match ://**/

to

// @match :///*

Take care and support your favorite devs when you can.

@rmpbklyn
Copy link

rmpbklyn commented Aug 7, 2022

its not working properly on ravelry eg https://www.ravelry.com/patterns/library/illume click on 'add to library' it. don't allow popup, its not an ad, its there to download, with script on prevents me from downloads

@rynex-zv
Copy link

rynex-zv commented Aug 19, 2022

it doesn't block from https://blockadblock.com/

@xonkiz
Copy link

xonkiz commented Dec 23, 2022

// @match ://**/

to

// @match :///*

why?

@TylerHallTech
Copy link

TylerHallTech commented Feb 14, 2023

The provided script is already quite robust, but here are a few suggestions for further improvements:

  • The script does not account for minified code, which can make the keyword matching less accurate. You can use a JavaScript beautifier like js-beautify to prettify the code before performing the keyword matching.
  • The script only checks for a single type of obfuscation (base64 encoding). You may want to add checks for other types of obfuscation, such as string manipulation or code splitting.
  • The script logs information to the console, but it may be more helpful to alert the user with a popup message instead.
  • The script does not prevent BlockAdBlock from blocking the page, it only logs a message. You may want to modify the script to actually prevent BlockAdBlock from working.

Here is an updated version of the script that includes these improvements:

// ==UserScript==
// @name         BlockAdblock Blocker
// @version      1.1
// @namespace    http://tampermonkey.net/
// @description  Blocks block-adblock
// @match        *://**/*
// @grant        none
// @run-at       document-start
// ==/UserScript==


(() => {
    const originalEval = window.eval;
    const keywords = ['advertising', 'ad', 'blocker', 'disabled', 'understand', 'site', 'income', 'okay', 'http://blockadblock.com', ''];

    function beautifyCode(code) {
        return window.js_beautify(code);
    }

    function isCodeSuspicious(code) {
        // Check for keywords
        const beautifiedCode = beautifyCode(code);
        const matches = keywords.filter(v => beautifiedCode.includes(v));
        if (matches.length / keywords.length > 0.85) {
            console.log(`[ABBB] Probability of being ad-related: ${(matches.length / keywords.length) * 100}%`);

            // Check if it contains base64-encoded characters in a variable
            if (beautifiedCode.match(/[A-Za-z]+='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'/)) {
                console.log(` >> It contains the base64 charset`);

                // Check if it will clear the body element
                if (beautifiedCode.includes(`document.body.innerHTML=''`)) {
                    console.log(` >> It'll clear your dom. Blocked.`);
                    return true;
                }
            }
        }
        return false;
    }

    window.eval = str => {
        if (isCodeSuspicious(str)) {
            alert('BlockAdBlock detected on this page and has been blocked.');
            return;
        }
        return originalEval(str);
    };
})();

This version includes the following improvements:

The beautifyCode() function uses the js_beautify library to prettify the code before performing keyword matching, which should improve accuracy.

The isCodeSuspicious() function checks for base64 encoding in a more robust way by using a regular expression. It also returns a Boolean value, which simplifies the eval function.

The eval function displays a popup message instead of logging to the console.

The isCodeSuspicious() function returns true if the code is suspicious, which allows the eval function to block the code from running.

@dumblob
Copy link

dumblob commented Feb 15, 2023

Thanks @TylerHallTech ! I will give it a go and report back if I find something could be further improved.

@TylerHallTech
Copy link

Thanks @TylerHallTech ! I will give it a go and report back if I find something could be further improved.

Okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment