如何使用jQuery检测是否有东西阻止了Adsense

How to detect if something is blocking Adsense with jQuery?

本文关键字:Adsense 何使用 jQuery 检测 是否      更新时间:2023-09-26

很多人使用像Adblock这样的插件来避免网站上的横幅。如果您可以检测到该插件,则可以轻松显示您喜欢的图像或文本。使用jQuery实现这一要求的好方法是什么?

如果您需要

检查插件(例如Adblock(是否阻止了客户端浏览器上的Adsense横幅,只需使用此代码即可。它将检查横幅是否为空,如果是,它将放置一个文本(例如,您可以安全地将其替换为自定义图像(。

建议的 html(始终在div 内(:

<div id="ad1">
    <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-XXXXXXXXXXXXXXXX" data-ad-slot="XXXXXXXXXXXXXXXX"></ins>
    <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</div>

jQuery 脚本:

$(function(){
    setTimeout(function(){
        var ads_list = $('ins.adsbygoogle');
        if(ads_list){
            ads_list.each(function(){
                if($(this).html().replace(/'s/g, '').length != 0) {
                    return false;
                } else {
                    $(this).parent().text('There should be a banner here... please turn Adblock off!');
                }
            });
        }
    }, 1000);
});

该脚本在页面完全加载后 1 秒运行,以便 Adsense 有时间实际尝试检索请求的横幅。

欢迎提出建议!