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

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

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


当前回答

这对我来说很好……

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

其他回答

你不需要一个额外的HTTP请求,你可以简单地计算一个假添加的高度。

顺便说一下,这里有一个完整的列表,与广告拦截器避免渲染的元素相匹配。

窗口。adBlockRunning = function() { return (getComputedStyle(document.getElementById("detect"))["display"] == "none") ?真:假; }() console.log (window.adBlockRunning); #{检测 身高:1 px; 宽度:1 px; 位置:绝对的; 左:-999 em; 上图:-999年新兴市场 } <div id="detect" class="ads AD adsbox doubleclick AD -placement carbon-ads"></div>

[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'}`) });

我在我的网站上使用的这种方法,也许你会发现它很有用。在我看来,这是最简单的解决办法。

AdBlocker阻止特定的类和html元素,通过检查这些选择器的任何被阻止的广告在开发控制台(他们都列出),你可以看到哪些元素将总是被阻止。

例如,只要在stackoverflow上检查这个问题页面,你就会看到一堆被阻止的广告。

例如,任何带有bottom-ad类的元素都会被自动阻塞。

我用bottom-ad类创建了一个非空div元素: < span style=" font - family:宋体;"身高:1 px;“>你好< / div > 页面加载后,检查该元素是否被隐藏。我使用的是jQuery,但也可以使用javascript: $ (' .bottom-ad ') . css('显示')= =“没有”或更好的利用美元(.bottom-ad) .(:可见)

如果值为真,则AdBlocker是活动的。

他们利用谷歌的广告代码创建了一个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>

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

jQuery(document).ready(function() {

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


});