javascriptajaxurladblock

Run an Ajax request on a URL blocked for detecting Adblocker (Ghostery)


I need some simple code for detecting a blocked url.

SethWhite has said: You could also try to run an ajax request on a URL blocked by an adblocker. If it succeeds, there's no adblocker, if it fails, there's an adblocker.

I can use microajax to do this in the following way:

microAjax("/resource/url", function (res) {
  alert (res);
});

How I can call window.open if the request does not succeed?


Solution

  • For microAjax, look up its documentation. I'd imagine in the response you can find the response code. If the code is 200 it's a success, and you can run window.open(). Otherwise, the request is likely being blocked.

    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if(request.readyState === 4 && request.status === 200 ) {
            console.log('No blocker');
        }
        else if(request.readyState === 4 && request.status === 0){
            console.log('Blocker exists');
        }
    };
    
    request.open("GET","pathTo/ads.html");
    request.send();
    

    This uses a local URL; I initially thought using external URLs was a good idea, but if they're made invalid by their owners you'll get false positives.

    I tested this using Adblock Plus and it works. If this is a URL blocked by Ghostery, it should work as well; otherwise, you can try different URLs to see what's blocked.

    You could also do this with jQuery's $.ajax function, but I gravitate towards vanilla JavaScript.