Ad Blocker detection AKA Adblock Plus

前端 未结 15 1398
北海茫月
北海茫月 2020-12-02 06:14

After searching Google and Stackoverflow for a few hours I could not find a solution. What I\'m trying to do is detect Adblock plus and display a simple message for now.

15条回答
  •  悲哀的现实
    2020-12-02 06:33

    A simple Ajax call does the job:

    var xmlhttp = new XMLHttpRequest()
    xmlhttp.onreadystatechange = function() {
      if( xmlhttp.readyState == XMLHttpRequest.DONE ){
        if( xmlhttp.status !== 404 ){
            console.log("Blocking ads")
        }else{
            console.log("Not blocking ads")
        }
      }
    }
    xmlhttp.open("GET", "/498100ffe815d700cd838d1/ads/showad.js", true)
    xmlhttp.send()
    

    Or even better, without an extra HTTP overhead:

    var adBlockTester = document.createElement('div');
    adBlockTester.innerHTML = ' ';
    adBlockTester.className = 'adsbox';
    document.body.appendChild(adBlockTester);
    window.setTimeout(function() {
      if( adBlockTester.offsetHeight === 0 ){
        console.log("Blocking ads")
      }else{
        console.log("Not blocking ads")
      }
      document.body.removeChild(adBlockTester);
    }, 60);
    

提交回复
热议问题