Detect Adblock and display a message

Asked

Viewed 3,143 times

11

What I’m trying to do is detect Adblock Plus and display a message.

  • 2

    I’m going to invent something to detect Adblock detectors and disable, as this is taking away the user’s right not to want to see certain advertisements.

  • 4

    Next question from the site: "How to detect and block Adblock detector"?

  • @Bacco already has the Anti Adblock Killer. I’m using with Tampermonkey, and it really works!

  • 3

    @Wallacemaxters I’m talking about one to detect it there and block :)

  • I know the question is old, but I’m amazed to see answers that don’t add anything from users with a high reputation. The author may have reasons to display a message to those who block ads that are not "take away the user’s right not to see ads".

1 answer

22


There is a plugin for this called Blockadblock (or Fuckadblock), it is supported by the following browsers:

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

Installation

You can manually install by downloading in https://github.com/sitexw/BlockAdBlock/releases or:

  • Bower:

    bower install blockadblock
    
  • Node.js/io.js:

    npm install blockadblock
    

Example of use:

<script src="blockAdBlock.js"></script>
<script>
(function() {
    //Se não detectar o adblock
    function adBlockNotDetected() {
        alert('AdBlock não está ativado');
    }

    //Se detectar o adblock
    function adBlockDetected() {
        alert('AdBlock está ativado');
    }

    if(typeof blockAdBlock=== 'undefined') {
        alert("blockAdBlock não foi carregado");
    } else {
        blockAdBlock.onDetected(adBlockDetected);
        blockAdBlock.onNotDetected(adBlockNotDetected);
        blockAdBlock.on(true, adBlockDetected);
        blockAdBlock.on(false, adBlockNotDetected);
        blockAdBlock.on(true, adBlockDetected).onNotDetected(adBlockNotDetected);
    }

    blockAdBlock.setOption('checkOnLoad', false);

    blockAdBlock.setOption({
        debug: true,
        checkOnLoad: false,
        resetOnEnd: false
    });
})();
</script>

Alternative

However not everything is guaranteed, adblocks evolve and this can make difficult, one simple thing you can do is use the event onerror

<script>
function possivelAdblockDetectado () {
    alert("Possível adblock detectado");
}
</script>

<script onerror="possivelAdblockDetectado()" async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
    style="display:inline-block;width:300px;height:250px"
    data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
    data-ad-slot="6440411535"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

Or else use window.addEventListener (does not work with window.onerror)

<script type="text/javascript">
(function () {
    var
        removeProtocol = /^[a-z]+[:]/i,
        items = [
            "//www.google-analytics.com/analytics.js",
            "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
        ];

    function detectAdBlock(src) {
        if (items.indexOf(src.replace(removeProtocol, "")) !== -1) {
            alert("Possivel adblock");
        }
    }

    window.addEventListener('error', function(e) {
        if (e.target && e.target.src) {
            detectAdBlock(e.target.src);
        }
    }, true);
})();
</script>
  • But it gets to block even, as the name says, or just displays same msg?

  • You configure the function function adBlockNotDetected() {&#xA; alert('AdBlock não está ativado');&#xA; }&#xA; as you wish, just change the alert, can put a modal bootstrap, or redirect to another page. It goes from your imagination and imagination has no limits :)

  • Show, Thank you, rsrsr.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.