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

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

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


当前回答

我使用JavaScript承诺和jQuery来检测用户是否启用了广告拦截器:

Create a small image and rename it to ads.jpg (or whatever extension). Create a JavaScript function to call it when you need: function hasAdBlocker() { return new Promise(function(resolve, reject) { let img = $('<img style="display: none;" src="ads.jpg" />'); img.on("load", reject); img.on("error", resolve); $("body").append(img); }); } Call it like this: hasAdBlocker().then(function(){ console.log('Has adBlock') }, function() { console.log('No adBlock dedected') })

其他回答

对我来说,这些把戏都没用,可能是我做错了什么。但这是谷歌广告实现的一种非常具体的方式。

window.onload = function() {
   if (document.getElementsByClassName('google-auto-placed').length == 0){
                // Adblock Detected
   }        
}

如果你有其他的广告系统,如亚马逊,通过检查页面寻找他们的通用类名/ id。

如果你打算把这段代码放在单独的.js文件中,请确保文件名中没有“Ad”字。就叫它magic.js吧

如果谷歌决定更改div名称,此方法将失败。但这似乎不太可能。

这对我来说很好……

<html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        </head>
        <body>
            <p>If you click on me, I will disappear.</p>
            <p>Click me away!</p>
            <p>Click me too!</p>
            <script>
                var adBlockEnabled = false;
                var adSense = document.createElement('div');
                adSense.innerHTML = '&nbsp;';
                adSense.className = 'adsbox';
                document.body.appendChild(adSense);
                window.setTimeout(function() {
                  if (adSense.offsetHeight === 0) {
                    adBlockEnabled = true;
                  }
                  adSense.remove();
                  if (adBlockEnabled) {
                        alert('Adblock enabled');
                  } else {
                        alert('Adblock disabled or Not installed');
                  }
                }, 100);
            </script>
        </body>
    </html>

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

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>

找到了这个解

我已经在浏览器中实现了许多方法来检测广告块,所有的解决方案都失败了,除了下面一个在javascript:

window.onload = function() {
    setTimeout(function() {
        var ad = document.querySelector("ins.adsbygoogle");
        if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
            console.log('You seem to blocking Google AdSense ads in your browser.');
        }
    }, 2000);
};

我希望这个javascript解决方案将帮助你。谢谢你的提问。

不是一个直接的回答,但我会把信息放在广告后面。而不是试图检测它,它会在广告没有显示时显示出来。