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

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

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


当前回答

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

其他回答

我的建议是:不要这样做!

在任何情况下,你把人们视为“做错事的人”都会导致他们反击。

这是我的建议。

在页面顶部放一个不显眼的小消息(不管广告是否被阻止),文字我完全尊重你阻止广告的权利,并链接到另一个页面/弹出标题为阅读更多....

在另一页上,清楚地表明你明白这是他们的电脑,他们可以自由地使用广告拦截。

也要以一种非指责的方式明确表示,使用这些拦截器会让你更难发布优质内容(详细解释原因),虽然你不希望广告拦截器出现在你的网站上,但这完全是他们的决定。专注于关闭阻塞的积极方面。

那些强烈反对广告的人会忽略这一点,但你从来没有机会说服他们。那些漠不关心的人很可能会被你的吸引力所动摇,因为你没有做“让我走我的路,否则我就拿着我的球回家”的事情,老实说,这应该是五岁孩子的专属领域。

记住,没有人拿枪指着你的头强迫你把东西放到网上。尊重你的读者/用户,你可能会发现很多人会回报你。

在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

如果你使用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>');

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

对我来说,这些把戏都没用,可能是我做错了什么。但这是谷歌广告实现的一种非常具体的方式。

window.onload = function() {
   if (document.getElementsByClassName('google-auto-placed').length == 0){
                // Adblock Detected
   }        
}

如果你有其他的广告系统,如亚马逊,通过检查页面寻找他们的通用类名/ id。

如果你打算把这段代码放在单独的.js文件中,请确保文件名中没有“Ad”字。就叫它magic.js吧

如果谷歌决定更改div名称,此方法将失败。但这似乎不太可能。

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>var adb=true;</script>
<script src="./getbanner.cfm?"></script>
<script>
$(document).ready(function(){if(adb)alert('AdBlock!');});
</script>

在getbanner中。cfm文件:

adb = false;

我认为这是检测广告拦截最简单的方法。