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

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

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


当前回答

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

其他回答

异步函数detectAdBlock() { let adBlockEnabled = false const googleAdUrl = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' 尝试{ 等待取回(新的请求(googleAdUrl))。catch(_ => adBlockEnabled = true) } catch (e) { adBlockEnabled = true }最后{ console.log(' adBlockEnabled: ${adBlockEnabled} ') } } detectAdBlock ()

如果你使用jQuery和谷歌Adsense:

jQuery.getScript(
    "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", 
    function() {
     // Load your ad now    
}).fail(function() {
    // Google failed to load main script, do something now
});

这更容易理解:如果谷歌广告主JavaScript文件加载失败,AdSense将无法工作,所以您使用jQuery的fail函数做一些事情。

“loading your add now”是当我添加“ins”对象时,比如:

jQuery(".my_ad_div").append('<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-xxx"
data-ad-slot="xxx"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>');

在“//谷歌加载主脚本失败,现在做点什么”中,我通常把图像放在广告的地方。

他们利用谷歌的广告代码创建了一个id为“iframe”的iframe。因此,只要您的页面上没有该ID的内容,这也适用于您。

<p id="ads">
<script type="text/javascript"><!--
google_ad_client = "their-ad-code-here";
/* 160x600, droite */
google_ad_slot = "their-ad-code-here";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</p>

<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 #000000; text-align:center; font-family:century gothic, arial, helvetica, sans serif;padding-left:5px;padding-right:5px;'>Advertising seems to be blocked by your browser.<br /><br /><span style='font-size:10px'>Please notice that advertising helps us to host the project.<br /><br />If you find these ads intrusive or inappropriate, please contact me.</span><img src='http://www.playonlinux.com/images/abp.jpg' alt='Adblock Plus' /></div>");
}
--></script>

要检测用户是否屏蔽广告,你所要做的就是在广告javascript中找到一个函数,并尝试测试它。他们用什么方法屏蔽广告并不重要。下面是谷歌Adsense广告的情况:

if(!window.hasOwnProperty('google_render_ad') || window.google_render_ad === undefined) { 
    //They're blocking ads, display your banner
}

该方法概述在这里:http://www.metamorphosite.com/detect-web-popup-blocker-software-adblock-spam

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