我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。

如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。

我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?


当前回答

以上所有的答案都是有效的,但大多数将不适用于dns级别的广告拦截。

dns级别的广告拦截器(如π -hole)基本上会返回NXDOMAIN(domain不存在)的广告拦截域列表(例如telemetry.microsoft.com将“不存在”当它存在时)。

有几种方法可以避免这种情况:

方法A:根据ip地址而不是域名请求广告。

这种方法有点烦人,因为您必须跟踪ip地址。如果您的代码没有得到很好的维护或定期更新,这将会产生问题。

方法B:阻塞所有失败的请求——即使客户端报告NXDOMAIN。

如果它是一个“合法的”NXDOMAIN,这会让用户非常恼火。

其他回答

他们利用谷歌的广告代码创建了一个id为“iframe”的iframe。因此,只要您的页面上没有该ID的内容,这也适用于您。

<p id="ads">
<script type="text/javascript"><!--
google_ad_client = "their-ad-code-here";
/* 160x600, droite */
google_ad_slot = "their-ad-code-here";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</p>

<script type="text/javascript"><!--
if(document.getElementsByTagName("iframe").item(0) == null)
{
    document.write("<div style='width:160px; height:600px; padding-top: 280px; margin-left:5px;border:1px solid #000000; text-align:center; font-family:century gothic, arial, helvetica, sans serif;padding-left:5px;padding-right:5px;'>Advertising seems to be blocked by your browser.<br /><br /><span style='font-size:10px'>Please notice that advertising helps us to host the project.<br /><br />If you find these ads intrusive or inappropriate, please contact me.</span><img src='http://www.playonlinux.com/images/abp.jpg' alt='Adblock Plus' /></div>");
}
--></script>

我知道这个问题已经有答案了,但我看了一下建议的示例站点,我看到他们是这样做的:

<script type="text/javascript">
if(document.getElementsByTagName("iframe").item(0) == null) {
    document.write("<div style="width: 160px; height: 600px; padding-top: 280px; margin-left: 5px; border: 1px solid #666666; color: #FFF; background-color: #666; text-align:center; font-family: Maven Pro, century gothic, arial, helvetica, sans-serif; padding-left: 5px; padding-right: 5px; border-radius: 7px; font-size: 18px;">Advertising seems to be blocked by your browser.<br><br><span style="font-size: 12px;">Please notice that advertising helps us to host the project.<br><br>If you find these ads intrusive or inappropriate, please contact me.</span><br><img src="http://www.playonlinux.com/images/abp.png" alt="Adblock Plus"></div>");
};
</script>

我注意到之前的评论使用谷歌adsense作为测试对象。有些页面不使用adsense,使用adsense块作为测试并不是一个好主意。因为站长屏蔽可能会损害你的SEO。 以下是我如何通过adblocker检测简单的阻塞类的例子:

Html:

<div class="ad-placement" id="ablockercheck"></div>
<div id="ablockermsg" style="display: none"></div>

Jquery:

$(document).ready(function()
{
   if(!$("#ablockercheck").is(":visible"))
   {
     $("#ablockermsg").text("Please disable adblocker.").show();
   }
});

"ablockercheck"是adblocker阻止的ID。所以检查它,如果它是可见的,你能够检测,如果广告拦截器被打开。

不需要超时和DOM嗅探。简单地尝试从流行的广告网络加载一个脚本,看看广告拦截器是否拦截了HTTP请求。

/**
 * Attempt to load a script from a popular ad network. Ad blockers will intercept the HTTP request.
 *
 * @param {string} url
 * @param {Function} cb
 */
function detectAdBlockerAsync(url, cb){
    var script = document.createElement('script');

    script.onerror = function(){
        script.onerror = null;
        document.body.removeChild(script);
        cb();
    }

    script.src = url;
    document.body.appendChild(script);
}

detectAdBlockerAsync('http://ads.pubmatic.com/AdServer/js/gshowad.js', function(){
    document.body.style.background = '#c00';
});

以上所有的答案都是有效的,但大多数将不适用于dns级别的广告拦截。

dns级别的广告拦截器(如π -hole)基本上会返回NXDOMAIN(domain不存在)的广告拦截域列表(例如telemetry.microsoft.com将“不存在”当它存在时)。

有几种方法可以避免这种情况:

方法A:根据ip地址而不是域名请求广告。

这种方法有点烦人,因为您必须跟踪ip地址。如果您的代码没有得到很好的维护或定期更新,这将会产生问题。

方法B:阻塞所有失败的请求——即使客户端报告NXDOMAIN。

如果它是一个“合法的”NXDOMAIN,这会让用户非常恼火。