我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
当前回答
我使用JavaScript承诺和jQuery来检测用户是否启用了广告拦截器:
Create a small image and rename it to ads.jpg (or whatever extension). Create a JavaScript function to call it when you need: function hasAdBlocker() { return new Promise(function(resolve, reject) { let img = $('<img style="display: none;" src="ads.jpg" />'); img.on("load", reject); img.on("error", resolve); $("body").append(img); }); } Call it like this: hasAdBlocker().then(function(){ console.log('Has adBlock') }, function() { console.log('No adBlock dedected') })
其他回答
我使用JavaScript承诺和jQuery来检测用户是否启用了广告拦截器:
Create a small image and rename it to ads.jpg (or whatever extension). Create a JavaScript function to call it when you need: function hasAdBlocker() { return new Promise(function(resolve, reject) { let img = $('<img style="display: none;" src="ads.jpg" />'); img.on("load", reject); img.on("error", resolve); $("body").append(img); }); } Call it like this: hasAdBlocker().then(function(){ console.log('Has adBlock') }, function() { console.log('No adBlock dedected') })
async function hasAdBlock() {
try {
await fetch("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", {
method: "HEAD",
mode: "no-cors",
})
return false;
} catch(e) {
return true;
}
}
我已经在浏览器中实现了许多方法来检测广告块,所有的解决方案都失败了,除了下面一个在javascript:
window.onload = function() {
setTimeout(function() {
var ad = document.querySelector("ins.adsbygoogle");
if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
console.log('You seem to blocking Google AdSense ads in your browser.');
}
}, 2000);
};
我希望这个javascript解决方案将帮助你。谢谢你的提问。
尽管这个问题已经过时了,但我最近发现它非常有用,因此只能假设还有其他人在看它。在看了这里和其他地方之后,我猜测,间接检测广告拦截器的主要三种客户端检查是检查被阻止的div/img,被阻止的iframes和被阻止的资源(javascript文件)。
也许它有点夸张或偏执,但它涵盖了广告拦截系统,这些系统只会从选择中屏蔽一到两个,因此如果你只做了一次检查,可能就不会被覆盖。
在页面上,你正在运行检查添加:(我使用jQuery)
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="advertisement.js"></script>
<script type="text/javascript" src="abds.js"></script>
并在页面的其他地方添加以下内容:
<div id="myTestAd"><img src="http://placehold.it/300x250/000000/ffffff.png&text=Advert" /></div>
我使用了一个带有诱饵名称的div,以及一个带有文本“Advert”的外部托管图像,并在AdSense使用的尺寸中使用(感谢placehold.it!)
在advertising .js中,您应该在文档中附加一些内容,以便稍后检查。虽然看起来和前面做的一样,但实际上是在检查文件(advertising .js)本身是否被加载,而不是输出。
$(document).ready(
{
$("body").append("<div id=\"myTestAd2\">check</div>");
});
然后是广告拦截器检测脚本,它结合了一切
$(document).ready(function()
{
var ifr = '<iframe id="adServer" src="http://ads.google.com/adserver/adlogger_tracker.php" width="300" height="300"></iframe>';
$("body").append(ifr);
});
$(window).on("load",function()
{
var atb = $("#myTestAd");
var atb2= $("#myTestAd2");
var ifr = $("#adServer");
setTimeout(function()
{
if( (atb.height()==0) ||
(atb.filter(":visible").length==0) ||
(atb.filter(":hidden").length>0) ||
(atb.is("hidden")) ||
(atb.css("visibility")=="hidden") ||
(atb.css("display")=="none") ||
(atb2.html()!="check") ||
(ifr.height()!=300) ||
(ifr.width()!=300) )
{
alert("You're using ad blocker you normal person, you!");
}
},500);
});
当文档准备好时,即标记被加载,我们也将iframe添加到文档中。然后,当窗口加载时,即内容include . images等加载,我们检查:
第一个测试div的尺寸和可见性。 第二个测试div的内容是“check”,就像如果advertimsent.js没有被阻止一样。 iframe的尺寸(我猜是能见度,因为隐藏对象没有高度或宽度?
以及款式:
div#myTestAd, iframe#adServer
{
display: block;
position: absolute;
left: -9999px;
top: -9999px;
}
div#myTestAd2
{
display: none;
}
希望这能有所帮助
我知道这个问题已经有答案了,但我看了一下建议的示例站点,我看到他们是这样做的:
<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 #666666; color: #FFF; background-color: #666; text-align:center; font-family: Maven Pro, century gothic, arial, helvetica, sans-serif; padding-left: 5px; padding-right: 5px; border-radius: 7px; font-size: 18px;">Advertising seems to be blocked by your browser.<br><br><span style="font-size: 12px;">Please notice that advertising helps us to host the project.<br><br>If you find these ads intrusive or inappropriate, please contact me.</span><br><img src="http://www.playonlinux.com/images/abp.png" alt="Adblock Plus"></div>");
};
</script>