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

情到浓时终转凉″ 提交于 2019-12-11 12:40:57

问题


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?


回答1:


Edit: 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.



来源:https://stackoverflow.com/questions/33812704/run-an-ajax-request-on-a-url-blocked-for-detecting-adblocker-ghostery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!