问题
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