在被阻止以检测Adblocker(Ghostery)的URL上运行Ajax请求 [英] Run an Ajax request on a URL blocked for detecting Adblocker (Ghostery)

查看:120
本文介绍了在被阻止以检测Adblocker(Ghostery)的URL上运行Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些简单的代码来检测被阻止的网址.

I need some simple code for detecting a blocked url.

SethWhite 说:您还可以尝试在被adblocker阻止的URL上运行ajax请求.如果成功,则没有adblocker;如果失败,则是adblocker.

我可以通过以下方式使用 microajax 来做到这一点:

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

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

如果请求未成功,如何调用 window.open ?

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

推荐答案

对于microAjax,请查找其文档.我想在响应中您可以找到响应代码.如果代码是 200 ,则表示成功,您可以运行 window.open().否则,该请求可能会被阻止.

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();

这使用本地URL;最初,我认为使用外部URL是个好主意,但是如果所有者将其设为无效,则会出现误报.

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.

我使用Adblock Plus进行了测试,它可以正常工作.如果这是被Ghostery阻止的URL,它也应该可以正常工作;否则,您可以尝试使用其他URL来查看被阻止的内容.

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.

您也可以使用jQuery的 $.ajax 函数执行此操作,但是我倾向于使用普通JavaScript.

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

这篇关于在被阻止以检测Adblocker(Ghostery)的URL上运行Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆