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

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

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


当前回答

我的建议是:不要这样做!

在任何情况下,你把人们视为“做错事的人”都会导致他们反击。

这是我的建议。

在页面顶部放一个不显眼的小消息(不管广告是否被阻止),文字我完全尊重你阻止广告的权利,并链接到另一个页面/弹出标题为阅读更多....

在另一页上,清楚地表明你明白这是他们的电脑,他们可以自由地使用广告拦截。

也要以一种非指责的方式明确表示,使用这些拦截器会让你更难发布优质内容(详细解释原因),虽然你不希望广告拦截器出现在你的网站上,但这完全是他们的决定。专注于关闭阻塞的积极方面。

那些强烈反对广告的人会忽略这一点,但你从来没有机会说服他们。那些漠不关心的人很可能会被你的吸引力所动摇,因为你没有做“让我走我的路,否则我就拿着我的球回家”的事情,老实说,这应该是五岁孩子的专属领域。

记住,没有人拿枪指着你的头强迫你把东西放到网上。尊重你的读者/用户,你可能会发现很多人会回报你。

其他回答

不需要超时和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';
});

这为我工作,文件必须准备好之前,你寻找广告块 希望这能帮助到一些人!

jQuery(document).ready(function() {

var adBlockEnabled = false;
var testAd = document.createElement('div');
testAd.innerHTML = ' ';
testAd.className = 'adsbox';
document.body.appendChild(testAd);
window.setTimeout(function() {
  if (testAd.offsetHeight === 0) {
    adBlockEnabled = true;
  }
  testAd.remove();
  console.log('AdBlock Enabled? ', adBlockEnabled)
}, 100);


});

你可以看看这个,可能会有帮助 detect-adblocker

它是一个定时答案的实现

将此添加到head标签中的任何脚本之前:

<head>
    <title></title>
    <meta/>

    <!--adBlocker detection code - START-->
    <script src="//adblocker.fortiapp.com/ads.js"></script>
    <script>
        (function (i, o, g, r) {
            i[o] = (typeof i[o] == typeof undefined) ? g : r
        })(window, 'adblocker', true, false);
    </script>
    <!--adBlocker detection code - END-->

    // Other scripts

</head>

然后再使用它:

if (adblocker) {
    // the add blocker is enabled
}else{
    // ad blocker is not enabled
}

这对我来说很好……

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

这份文件可能会有所帮助。 这是检测广告拦截软件最有效的方法 完全免费且易于实现 https://www.detectadblock.com/