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

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

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


当前回答

如果使用新的AdSense代码,你可以做一个简单的检查,而不是求助于内容或css检查。

把你的广告正常地放在你的标记中:

<ins class="adsbygoogle" style="display: block;"
   data-ad-client="ca-pub-######"
   data-ad-slot="#######"
   data-ad-format="auto"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

然后你在你的页面底部调用adsense代码(注意,在调用adsbygoogle.js脚本时不要使用"async"标志):

<script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

然后在下面添加一小段代码:

<script>
if (!adsbygoogle.loaded) {
   // do something to alert the user
}
</script>

AdSense总是创建/设置标志adsbygoogle。当广告加载时,你可以将检查放在setTimeout函数中,以延迟检查几秒钟。

其他回答

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

这对我来说很管用:

function isAdBlocked() {
     return (typeof(window.google_jobrunner) === "undefined") ? true : false;
}

$(document).ready(function(){
    if(isAdBlocked()) {
       alert('Y U NO LIKE ADS?');
    }
});

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

在React.js中,你可以做以下事情:

class AdblockDetect extends Component {
  constructor (props) {
    super(props)
    this.state = {
      usingAdblock: false
    }
  }

  componentDidMount() {
    this.setState({ usingAdblock: this.fakeAdBanner.offsetHeight === 0 });
  }

  render() {
    if (this.state.usingAdblock === true) {
      return this.props.children;
    }

    return (
      <div
        ref={r => (this.fakeAdBanner = r)}
        style={{ height: '1px', width: '1px', visiblity: 'none', pointerEvents: 'none' }}
        className="adBanner"
      />
    );
  }
}

class App extends Component {
  render() {
    return (
      <div className="App">
        <AdblockDetect>You are using adblock</AdblockDetect>
      </div>
    );
  }
}

来源:https://stackoverflow.com/a/55635499/5539715

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的都是一个问题)。