BEST-WEB-TOOLS Blog

My own blog posts about development, tech, finance and other (interesting) stuff.

❮❮ back

2021-06-21, Dev, Browser, Firefox, Tampermonkey, Scripting

Block Ads When Ad Blocking is Blocked

Nowadays many blogs, news sites and online tools try to prevent you from entering with an ad-blocker enabled. But many of this sites don't do a very good job in detecting ad-blockers. Usually they just use a script that looks for common strings, objects, behaviours of the most popular ad-blockers.

So I just wrote a tiny Tampermonkey script that hides ads on specific websites that I am using often and who are using ad-block-detectors. You can easily add new configs to that script and make your favorite sites ad-free. For all other sites i still use one of the big browser ad-blockers so i don't need to write configs for the whole internet.

What is Tampermonkey?

Tampermonkey is a browser extension that let you run custom user-scripts on specific websites. There is a big community with over 10 mio users who is providing scripts, but you could also easily write them yourself in Javascript. Tampermonkey is available for Chrome and Firefox and other browsers.

How it works

After you have added Tampermonkey to your browser you create a new user-script and copy/paste the code below into the editor-window. Then you change the sites-config param at the top of the script.

You provide a configuration for the blocker script. This config contains css-selectors of website-areas you want to hide. You can also provide a config for clicking on things to remove annoying cookie banners or sign-up popups.

All Config Options:

  • remove: ['.selector','.selector']
    A list of css selectors that will be set to hidden.

  • timeout: 1000
    A timeout in milliseconds when the script should run at website loading. Set it to something between 0 and 5000 if the ads are loaded dynamically after user interaction.

  • interval: 5000
    An interval in milliseconds when the script should run again. That's helpful if the website is constantly loading new ads on scroll or after a specific time.

  • interaction: true / false
    Simulate a mouse move to force websites to load on-user-interaction scripts. Many sites load ads or cookie banners only after a user-interaction happend.

  • background: '#fffff'
    Override the background color to body on sites with full page brandings that changes the whole look of the site.

  • click: ['.selector']
    Click on elements to remove cookie consent banners or sign-up popups.

The user-script


// ==UserScript==
// @name         mf-adblock
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  a basic anti-adblock workaround that can remove or click elements on a website
// @author       You
// @match        https://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let interval = null;

    let hostname = document.location.hostname;

    let sites = {
        'www.website.com': { // change this to the website you want to clean from ads
            remove: ['.ad-container','.site-sitebar','.js-native-ad'], // change here the selectors
            click: ['button.cookie-consent'], // change here the selectors
            timeout: 1000,
            interval: 5000,
            interaction: true,
            background: '#ffffff'
        },
        // add more sites here
    }

    function cleanup() {

        if(sites[hostname].interaction) {
            document.body.dispatchEvent(new MouseEvent('mousemove'));
        }

        if(sites[hostname].remove) {
            let selectors = sites[hostname].remove;

            selectors.forEach(function(selector) {
                let elements = document.querySelectorAll(selector);

                elements.forEach(function(elem) {
                    elem.style.visibility = 'hidden';
                    elem.style.width = '1px';
                    elem.style.height = '1px';
                    elem.style.overflow = 'hidden';
                    elem.style.opacity = 0;
                });
            });
        }

        if(sites[hostname].background) {
            document.body.style.background = sites[hostname].background;
            document.body.style.overflow = 'scroll';
            document.body.style.position = 'static';
        }

        if(sites[hostname].click) {
            let selectors = sites[hostname].click;

            selectors.forEach(function(selector) {
                let element = document.querySelector(selector);

                if(element !== null) {
                    element.click();
                }
            });
        }
    }

    if(Object.keys(sites).indexOf(hostname) >= 0) {

        let timeout = 0;
        if(sites[hostname].timeout) {
            timeout = sites[hostname].timeout;
        }

        window.setTimeout(function(){
            cleanup();
        }, timeout);

        if(sites[hostname].interval) {
            if(interval === null) {
                interval = window.setInterval(function(){
                    cleanup();
                }, sites[hostname].interval);
            }
        }
    }

})();

Happy ad-free browsing.


❮❮ back