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

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

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


当前回答

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

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

其他回答

安全的方法是将广告包装在<div>内,并检查高度

<div id="check-ab">
/* your ads code */
</div>

setTimeout(function(){
  if(document.getElementById("check-ab").offsetHeight === 0){
    console.log("ads blocked");
  }
  else{
    console.log("ads running");
  }
}, 100);

它与adblock plus和bluehell防火墙一起工作。

我的解决方案并不针对特定的广告网络,而且是非常轻量级的。我已经在生产环境中运行了几年。AdBlock会屏蔽所有含有“广告”或“预竞价”字样的url。这就是我所做的:

我在webroot中添加了一个名为prebid-ads.js的小js文件

这是文件中唯一的一行代码。将此变量命名为其他变量,参见下文!

var canRunAds = true;

然后在你页面的某处:

<html>
  <head>
    <script src="/js/prebid-ads.js"></script>
  </head>
  <body>
    <script>
      if( window.canRunAds === undefined ){
        // adblocker detected, show fallback
        showFallbackImage();
      }
    </script>
  </body>
</html>

uBlock Origin加载他们自己的ads-prebid.js,还原了这个答案中描述的技巧(骄傲!),他们的文件包含以下内容:

(function() {
    'use strict';
    window.canRunAds = true;
    window.isAdBlockActive = false;
})();

作为一种解决方案,只需将canRunAds变量重命名为一些有趣的东西,比如window。adsAreWithUs或window.money高于隐私。

Ans de Nijs的发现和解决方法。谢谢!

支持扩展

像ads.js这样的文件在Chrome上至少会被这些广告拦截器屏蔽:

AdBlock Adblock Plus Adblock职业 Ghostery

不适用于:

隐私獾

我有点晚了,这里有一个最简单的解决方案,我知道的老AdSense代码与jQuery:

$ads = $("ins");
if ($ads.length == 0) {
    // Replacement code about you needing ad income
}

在纯JavaScript中:

$ads = document.getElementsByTagName("ins");
if ($ads.length == 0) {
    // Replacement code about you needing ad income
}

对于$ads,您可以使用任何与正在生成的广告一致的选择器。例如,对于新的AdSense代码,您可以使用$("iframe#google_ads_frame1")。

我还没有看到任何好的,简单的答案来解决这两种类型的广告拦截在今天普遍使用,所以我将提供我自己的答案。

广告拦截器类型1:根据脚本名称(AdBlock, uBlock来源等)拦截网络广告脚本。

广告拦截器类型2:基于服务主机名的广告数据库(FireFox内容拦截器,各种网关插件等)拦截网络广告。

这个解决方案对两者都有效。它会弹出一个巨大的粉色“呼吁框”,要求用户禁用广告拦截器。我们喜欢把它放在菜单下面,内容上面。我们实际上并没有阻止对网站的访问-这只是把页面上的内容往下推了一点,但人们会发现这很烦人,几乎所有人都会遵守并禁用他们在你的网站上的广告拦截器。

这就是解决方案:

A)创建一个名为advertising .js的文件,并将其放置在你的web服务器的根目录下,由以下代码行组成:

document.write('<div id="tester">an advertisement</div>');

B)在你的网页中注入以下内容(你甚至可以使用你的广告服务器代码来做到这一点!)建议位置在菜单下方,内容上方。

<script src="advertisement.js"></script>
<table id="tbl_ab_appeal" style="width: 900px; margin-left:auto; margin-right: auto; padding: 25px; background: #FCC; border: 1px solid #F66; visibility:collapse; border-collapse: collapse;">
    <tr>
        <td>We've detected that you're using an <strong>ad content blocking</strong> browser plug-in or feature. Ads provide a critical source of revenue to the continued operation of [This website name].&nbsp; We ask that you disable ad blocking while on [This
            website name] in the best interests of our community.</td>
    </tr>
</table>
<script>
    if (document.getElementById("tester") == undefined) adsBlocked();

    function adsBlocked() {
        document.getElementById("tbl_ab_appeal").style.visibility = "visible";
        document.getElementById("tbl_ab_appeal").style.borderCollapse = "separate";
        document.getElementById("tbl_ab_appeal").style.marginTop = "10px"
        document.getElementById("tbl_ab_appeal").style.marginBottom = "10px"
    }
</script>
<script onerror="adsBlocked()" src="//www.googletagservices.com/tag/js/gpt.js"></script>

它是如何工作的?消息被加载,但被设为零高度且不可见。如果本地脚本advertising .js运行失败,或者远程AdSense脚本www.googletagservices.com/tag/js/gpt.js加载失败,则该方框可见。

大多数adblocker取消对ads.js的HTTP请求,并为元素制作0px,但有时adblocker删除了DOM,上面的一些答案将失败,因为没有检查元素的存在。

使用setTimeout()是一个很好的实践,因为没有它,将使脚本与广告拦截器竞争。

下面的脚本将检查dom是否存在/已删除,如果存在则检查元素的offsetHeight。

setTimeout(函数(){ var a = document.querySelector('.showads'), B = a ?(a.offsetHeight ?False:真):真; console.log(“广告了吗?”,b) }, 200);//不要太快,否则会使结果出错。 <div class="ads showads"> 我爱你,我爱你,我爱你。 < / div >