如何使用Javascript处理err_blocked_by_client? [英] How to handle the err_blocked_by_client using Javascript?

查看:164
本文介绍了如何使用Javascript处理err_blocked_by_client?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是检测广告拦截器。我找到了几个很好的例子,比如 FuckAdBlock

My aim is to detect the ad-blocker. I found couple of good examples like FuckAdBlock.

广告服务调用被广告拦截阻止后,我们会收到错误err_blocked_by_client。

When the ad service call is blocked by the ad-blocked we get the error "err_blocked_by_client".

我想通过以下方式处理此错误:

I want to handle this error, in the following way :

var xhr = new XMLHttpRequest();
try
{
xhr.open("GET","http://static.adzerk.net/ados.js", false);
xhr.onreadystatechange = function (oEvent) {  
    if (xhr.readyState === 4) {  
        if (xhr.status === 200) {  
          console.log(xhr.responseText)  
        } else {  
           console.log("Error", xhr.statusText);  
        }  
    }
};
xhr.send();
}
catch(error)
{
    console.log("ConsoleLog \n " + JSON.stringify(xhr));
    console.log("Error Catched" + error);
}

但在这种情况下,我无法识别catch块的错误原因。

But in this case i am not able to identify the error reason on catch block.

请告诉我更好的方法来处理此错误或我在此代码中的错误。

Please let me know the better option to handle this error or my mistake in this code.

谢谢

推荐答案

您需要在尝试之外使用 xhr 变量。它在 JSON.stringify(xhr)上失败 - 因为 xhr 超出范围。您需要使用onerror错误处理程序来处理异步XMLHttpRequest。您还需要从传递给onreadystatechange的函数中删除oEvent参数。见下文:

You need to have the xhr variable outside of your try. It fails on JSON.stringify(xhr) - because xhr is out of scope. You need to use the onerror error handler to handle the async XMLHttpRequest. You also need to remove the oEvent parameter from the function passed to onreadystatechange. See below:

var xhr = new XMLHttpRequest();

try
{    
    xhr.open("GET","http://static.adzerk.net/ados.js", true);

    xhr.onreadystatechange = function () {  
        if (xhr.readyState === 4) {  
            if (xhr.status === 200) {  
              console.log(xhr.responseText)  
            } else {  
               console.log("Error", xhr.statusText);  
            }  
        }            
    };    
    xhr.onerror = function(e) {
        console.log("Error Catched" + e.error);
    };    

    xhr.send();        
}
catch(error)
{
    console.log("ConsoleLog \n " + JSON.stringify(xhr));
    console.log("Error Catched" + error);
}

这篇关于如何使用Javascript处理err_blocked_by_client?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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