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

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

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


当前回答

时间的答案是很好的思考,但不再工作,所以我已经更新了js文件的名称为“adsense”从“广告”,它是工作回来像一个魅力!

下面是代码,也许这将帮助到一些人:

<html>
      <head>
            <script src="/adsense.js"></script>
      </head>
             <body>
                   <script>
                           if( window.adblockDetecter === undefined ){
                           // Do anithing, adblocker detected!
                           alert('Adblocker Detected!');}
                   </script>
            </body>
</html>

在Js文件中只放这一行:var adblockDetecter = true;

其他回答

这个很好用

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

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

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

你可以看看这个,可能会有帮助 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
}

[2022年10月- uBlock Origin, Adblock Plus, Brave浏览器]

Ad blockers are very smart these days, they can even spoof ad server requests with redirects and return fake responses. Below is the only good solution I've found and it works with even the best ad blocker extensions (like uBlock Origin, Adblock Plus) and in-browser ad blockers (like Brave, Opera) that I've tested. It works with those that block access to the ad server, as well as those that spoof it. It works with any ad provider, not just Google! It uses Google ad service exclusively for detection, because it's blocked by all blockers, its availability is always high and it's fast.

最聪明的广告拦截器不会拦截,而是重定向请求,并返回虚假的“成功”回复。到目前为止,谷歌从未重定向请求,因此我们可以检测到重定向,从而检测到阻止程序。

重要的是:

我们只发送一个HEAD请求,它运行速度快,不会给客户端的数据流量带来负担 Adsbygoogle.js必须使用完整的原始路径调用,该路径在每个广告拦截器的黑名单上(不要将js复制到您自己的网站!)

您可以在任何地方(<头部>/<身体>)和任何时间使用此解决方案。在任何浏览器和任何广告拦截器中直接点击运行代码段:

function detectAdblock(callback) { fetch('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', { method: 'HEAD', mode: 'no-cors', }).then((response) => { // If the request is redirected, then the ads are blocked. callback(response.redirected) }).catch(() => { // If the request fails completely, then the ads are blocked. callback(true) }) } detectAdblock((isAdblockerDetected) => { console.log(`ads are ${isAdblockerDetected ? 'blocked' : 'not blocked'}`) });

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

http://thepcspy.com/read/how_to_block_adblock/

jQuery:

function blockAdblockUser() {
    if ($('.myTestAd').height() == 0) {
        window.location = 'http://example.com/AdblockNotice.html';
    }
}

$(document).ready(function(){
    blockAdblockUser();
});

当然,你需要有一个AdblockNotice.html的登陆页,并且. mytestad类需要反映你实际的广告容器。但这应该有用。

EDIT

正如TD_Nijboer所建议的,更好的方法是使用:hidden(或:visible,如下所示)选择器,这样display: none也会被检查:

function blockAdblockUser() {
    if ($('.myTestAd').filter(':visible').length == 0) {
        // All are hidden, or "not visible", so:
        // Redirect, show dialog, do something...
    } else if ($('.myTestAd').filter(':hidden').length > 0) {
        // Maybe a different error if only some are hidden?
        // Redirect, show dialog, do something...
    }
}

当然,如果需要的话,这两个都可以合并成一个if块。

注意可见性:隐藏将不会被捕捉以及(在布局空间保留,但广告不可见)。要检查这一点,可以使用另一个过滤器:

$('.myTestAd').filter(function fi(){
    return $(this).css('visibility') == 'hidden';
})

这将为您提供一个“不可见”的广告元素数组(理论上,任何大于0的都是一个问题)。