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

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

我想在我的网站上做到这一点,我正在使用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>

其他回答

你不需要一个额外的HTTP请求,你可以简单地计算一个假添加的高度。

顺便说一下,这里有一个完整的列表,与广告拦截器避免渲染的元素相匹配。

窗口。adBlockRunning = function() { return (getComputedStyle(document.getElementById("detect"))["display"] == "none") ?真:假; }() console.log (window.adBlockRunning); #{检测 身高:1 px; 宽度:1 px; 位置:绝对的; 左:-999 em; 上图:-999年新兴市场 } <div id="detect" class="ads AD adsbox doubleclick AD -placement carbon-ads"></div>

AdBlock似乎阻止加载AdSense(等)JavaScript文件。所以,如果你使用异步版本的AdSense广告,你可以检查adsbygoogle是否是一个数组。这必须在几秒钟后检查,因为异步脚本是…异步的。以下是一个大致的大纲:

window.setTimeout(function(){
    if(adsbygoogle instanceof Array) {
        // adsbygoogle.js did not execute; probably blocked by an ad blocker
    } else {
        // adsbygoogle.js executed
    }
}, 2000);

为了澄清,这里有一个AdSense异步广告代码的示例:

<!-- this can go anywhere -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

<!-- this is where the ads display -->
<ins class="adsbygoogle" ...></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

注意adsbygoogle被初始化为一个数组。adsbygoogle.js库将此数组更改为Object {push:…}当它执行时。在一段时间后检查变量的类型可以告诉您脚本是否已加载。

这个很好用

如果有广告拦截器,它会提醒你

简单地说,它发送一个头部请求到一个著名的广告公司的所有广告拦截器(谷歌广告),如果请求被阻止,那么adbloker存在。

checkAdBlocker (); 函数checkAdBlocker() { 尝试{ fetch ( new Request("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", { 方法:“头”, 模式:“no-cors” }))。Catch(错误=> { showNotification () }); } catch (e) { //请求失败,可能是由于广告拦截程序 showNotification () } } 函数showNotification() { 警告(“请禁用广告拦截器”) }

尽管这个问题已经过时了,但我最近发现它非常有用,因此只能假设还有其他人在看它。在看了这里和其他地方之后,我猜测,间接检测广告拦截器的主要三种客户端检查是检查被阻止的div/img,被阻止的iframes和被阻止的资源(javascript文件)。

也许它有点夸张或偏执,但它涵盖了广告拦截系统,这些系统只会从选择中屏蔽一到两个,因此如果你只做了一次检查,可能就不会被覆盖。

在页面上,你正在运行检查添加:(我使用jQuery)

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="advertisement.js"></script>
<script type="text/javascript" src="abds.js"></script>

并在页面的其他地方添加以下内容:

<div id="myTestAd"><img src="http://placehold.it/300x250/000000/ffffff.png&text=Advert" /></div>

我使用了一个带有诱饵名称的div,以及一个带有文本“Advert”的外部托管图像,并在AdSense使用的尺寸中使用(感谢placehold.it!)

在advertising .js中,您应该在文档中附加一些内容,以便稍后检查。虽然看起来和前面做的一样,但实际上是在检查文件(advertising .js)本身是否被加载,而不是输出。

$(document).ready(
{

    $("body").append("<div id=\"myTestAd2\">check</div>");

});

然后是广告拦截器检测脚本,它结合了一切

$(document).ready(function()
{
    var ifr = '<iframe id="adServer" src="http://ads.google.com/adserver/adlogger_tracker.php" width="300" height="300"></iframe>';
    $("body").append(ifr);
});

$(window).on("load",function()
{

    var atb = $("#myTestAd");
    var atb2= $("#myTestAd2");
    var ifr = $("#adServer");

    setTimeout(function()
    {

        if( (atb.height()==0) ||
            (atb.filter(":visible").length==0) ||
            (atb.filter(":hidden").length>0) ||
            (atb.is("hidden")) ||
            (atb.css("visibility")=="hidden") ||
            (atb.css("display")=="none") ||
            (atb2.html()!="check") ||
            (ifr.height()!=300) ||
            (ifr.width()!=300) )
        {
            alert("You're using ad blocker you normal person, you!");
        }

    },500);

});

当文档准备好时,即标记被加载,我们也将iframe添加到文档中。然后,当窗口加载时,即内容include . images等加载,我们检查:

第一个测试div的尺寸和可见性。 第二个测试div的内容是“check”,就像如果advertimsent.js没有被阻止一样。 iframe的尺寸(我猜是能见度,因为隐藏对象没有高度或宽度?

以及款式:

div#myTestAd, iframe#adServer
{
    display: block;
    position: absolute;
    left: -9999px;
    top: -9999px;
}

div#myTestAd2
{
    display: none;
}

希望这能有所帮助

只需在你的网站上添加小脚本:

var isAdsDisplayed = true;

名称为adsbygoogle.js

然后做以下事情:

<script src="/js/adsbygoogle.js"></script>
<script>
if(window.isAdsDisplayed === undefined ) {
  // AdBlock is enabled. Show message or track custom data here
}
</script>

找到了这个解