简体   繁体   English

用户使用AdBlock时如何显示我自己的图像?

[英]How to display my own images when user uses AdBlock?

I found this snippet of code and it works well to display the same content on each adsense ad unit. 我找到了这段代码,可以很好地在每个adsense广告单元上显示相同的内容。 However, since my knowledge of scripting is limited, I don't know how to make it so that I can display my own images for each different ad size. 但是,由于我对脚本的了解有限,因此我不知道如何制作脚本,因此无法针对每种不同的广告尺寸展示自己的图片。 I need to be able to display my own image for each 728x90, and 160x600, ad units. 我需要能够为每个728x90和160x600广告单元展示自己的图像。

<script>
window.onload = function(){
  setTimeout(showAdblockImage, 3000); 
};
function showAdblockImage(){
    //get all google ad elements
    var adsList = document.querySelectorAll("ins.adsbygoogle");
    if(!adsList){ return;}
    for(var i=0; i<adsList.length;i++){
        if(adsList[i].innerHTML.replace(/\s/g, "").length != 0){
            //AdBlock is not active, hence exit
            break;
        }
        //apply inline css to force display
        adsList[i].style.cssText = 'display:block !important';
        //modify html content of element
        adsList[i].innerHTML='AD BLOCK USED';
    } 
}
</script> 

Your code selects each element ins.adsbygoogle and replaces it with the text "AD BLOCK USED". 您的代码选择ins.adsbygoogle每个元素,并将其替换为文本“ AD BLOCK USED”。

To replace the ad with an image instead, try to use adsList[i].innerHTML="<a href='#'><img src='photo.png'></a>"; 要将广告替换为图片,请尝试使用adsList[i].innerHTML="<a href='#'><img src='photo.png'></a>"; instead of adsList[i].innerHTML="AD BLOCK USED"; 而不是adsList[i].innerHTML="AD BLOCK USED";

I figured it out thanks to Advocat's comment. 我想通了Advocat的评论。 Here's the final code. 这是最终代码。 I needed to add if statements to select only 1 ad, and then replace that ad with my own code. 我需要添加if语句以仅选择1个广告,然后用我自己的代码替换该广告。

Here's what is working for me: 这是为我工作的东西:

<script>
window.onload = function(){
  setTimeout(showAdblockImage, 0000); 
};
function showAdblockImage(){
    //get all google ad elements
    var adsList = document.querySelectorAll("ins.adsbygoogle");
    if(!adsList){ return;}
    for(var i=0; i<adsList.length;i++){
        if(adsList[i].innerHTML.replace(/\s/g, "").length != 0){
            //AdBlock is not active, hence exit
            break;
        }
        //apply inline css to force display
        adsList[i].style.cssText = 'display:block !important';

    // select ad # 1
    if (i==0){
        adsList[i].innerHTML='<a href="#"><img src="image1.png"></a>';
}
   // select ad #2
    if (i==1){
        adsList[i].innerHTML='<a href="#"><img src="image-2.png"></a>';
}

    } 

}
</script>

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

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