我找到了这段代码,它可以很好地在每个adsense广告单元上显示相同的内容。但是,由于我对脚本的了解有限,我不知道如何制作,以便我可以为每种不同的广告尺寸显示自己的图像。我需要能够为每个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>
答案 0 :(得分:1)
您的代码会选择每个元素ins.adsbygoogle
并将其替换为文本&#34; AD BLOCK USED&#34;。
要用图片替换广告,请尝试使用adsList[i].innerHTML="<a href='#'><img src='photo.png'></a>";
代替adsList[i].innerHTML="AD BLOCK USED";
答案 1 :(得分:0)
感谢Advocat的评论,我想通了。这是最终的代码。我需要添加if语句以仅选择1个广告,然后使用我自己的代码替换该广告。
这里有什么对我有用:
<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>