简体   繁体   English

在我的网站上检测 Adblock 然后显示横幅?

[英]Detect Adblock on my website then show banner?

If your browser has some kind of adblock software enabled, then the site instead of showing the actual ads shows a little banner telling the users that the ad revenue is used for hosting the project and they should consider turning Adblock off.如果您的浏览器启用了某种 adblock 软件,那么该网站会显示一个小横幅,而不是显示实际广告,告诉用户广告收入用于托管项目,他们应该考虑关闭 Adblock。 i searched the web for a while and have found nothing but the code below and it still isn't working.我在网上搜索了一段时间,除了下面的代码之外什么也没找到,但它仍然无法正常工作。 How could I show ads and if its blocked show a banner telling the users to turn it off?我怎样才能显示广告,如果它被阻止显示一个横幅,告诉用户将其关闭?

Like this website: http://www.gamesbox.com/search-results/1802436/drunk-spiderman赞这个网站: http : //www.gamesbox.com/search-results/1802436/drunk-spiderman

<div class="myTestAd" style=" text-align:center;margin:10px">

    <iframe src="http://cdn1.adexprt.com/exo_na/sky1.html" width="120" height="600" frameborder="0" scrolling="no"></iframe>

</div>


<script type="text/javascript">

function TestPage() {
    if ($('.myTestAd').height() == 0)
        alert("You are blocking my beautiful adverts, you swine!");
}

$(TestPage);

</script>

Conveniently enough, ad blockers usually alter the CSS instead of the HTML.方便的是,广告拦截器通常会更改 CSS 而不是 HTML。 So, we can use JavaScript (preferably jQuery) to check if the CSS has been modified.因此,我们可以使用 JavaScript(最好是 jQuery)来检查 CSS 是否已被修改。

Here's an example in jQuery.这是 jQuery 中的一个示例。

(function( $ ){
    $.fn.adblocked = function() {
        if($(this).css('display') == 'none' || $(this).css('height') == 0) { // etc
            return true;
        }
        return false;
    }; 
})( jQuery );

Usage:用法:

if( $('#myImage').adblocked() ) {
    alert('wow');
}

First you need to give a chance for your ads to load.首先,您需要为广告加载提供机会。 After some timeout (in my case 10s) query all your components that contain your ads (in my case elements have tag "my-horizontal-ad") and check if any has a height larger than 'not loaded' (my ads are horizontal).一段时间后(在我的情况下为 10 秒)查询包含您的广告的所有组件(在我的情况下元素具有标签“my-horizo​​ntal-ad”)并检查是否有任何高度大于“未加载”(我的广告是水平的) )。 That height might differ due to your site's CSS (in my case not loaded horizontal ads have height 44px).由于您网站的 CSS(在我的情况下未加载的水平广告的高度为 44 像素),该高度可能会有所不同。

setTimeout(() => {
const myAds = document.querySelectorAll('my-horizontal-ad > div');
const areAdsAvailable = Array.from(myAds).some(ad => ad.clientHeight > 50);
console.log('areAdsAvailable',areAdsAvailable);
}, 10000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM