我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
当前回答
[2022年10月- uBlock Origin, Adblock Plus, Brave浏览器]
Ad blockers are very smart these days, they can even spoof ad server requests with redirects and return fake responses. Below is the only good solution I've found and it works with even the best ad blocker extensions (like uBlock Origin, Adblock Plus) and in-browser ad blockers (like Brave, Opera) that I've tested. It works with those that block access to the ad server, as well as those that spoof it. It works with any ad provider, not just Google! It uses Google ad service exclusively for detection, because it's blocked by all blockers, its availability is always high and it's fast.
最聪明的广告拦截器不会拦截,而是重定向请求,并返回虚假的“成功”回复。到目前为止,谷歌从未重定向请求,因此我们可以检测到重定向,从而检测到阻止程序。
重要的是:
我们只发送一个HEAD请求,它运行速度快,不会给客户端的数据流量带来负担 Adsbygoogle.js必须使用完整的原始路径调用,该路径在每个广告拦截器的黑名单上(不要将js复制到您自己的网站!)
您可以在任何地方(<头部>/<身体>)和任何时间使用此解决方案。在任何浏览器和任何广告拦截器中直接点击运行代码段:
function detectAdblock(callback) { fetch('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', { method: 'HEAD', mode: 'no-cors', }).then((response) => { // If the request is redirected, then the ads are blocked. callback(response.redirected) }).catch(() => { // If the request fails completely, then the ads are blocked. callback(true) }) } detectAdblock((isAdblockerDetected) => { console.log(`ads are ${isAdblockerDetected ? 'blocked' : 'not blocked'}`) });
其他回答
我有点晚了,这里有一个最简单的解决方案,我知道的老AdSense代码与jQuery:
$ads = $("ins");
if ($ads.length == 0) {
// Replacement code about you needing ad income
}
在纯JavaScript中:
$ads = document.getElementsByTagName("ins");
if ($ads.length == 0) {
// Replacement code about you needing ad income
}
对于$ads,您可以使用任何与正在生成的广告一致的选择器。例如,对于新的AdSense代码,您可以使用$("iframe#google_ads_frame1")。
以上所有的答案都是有效的,但大多数将不适用于dns级别的广告拦截。
dns级别的广告拦截器(如π -hole)基本上会返回NXDOMAIN(domain不存在)的广告拦截域列表(例如telemetry.microsoft.com将“不存在”当它存在时)。
有几种方法可以避免这种情况:
方法A:根据ip地址而不是域名请求广告。
这种方法有点烦人,因为您必须跟踪ip地址。如果您的代码没有得到很好的维护或定期更新,这将会产生问题。
方法B:阻塞所有失败的请求——即使客户端报告NXDOMAIN。
如果它是一个“合法的”NXDOMAIN,这会让用户非常恼火。
你可以看看这个,可能会有帮助 detect-adblocker
它是一个定时答案的实现
将此添加到head标签中的任何脚本之前:
<head>
<title></title>
<meta/>
<!--adBlocker detection code - START-->
<script src="//adblocker.fortiapp.com/ads.js"></script>
<script>
(function (i, o, g, r) {
i[o] = (typeof i[o] == typeof undefined) ? g : r
})(window, 'adblocker', true, false);
</script>
<!--adBlocker detection code - END-->
// Other scripts
</head>
然后再使用它:
if (adblocker) {
// the add blocker is enabled
}else{
// ad blocker is not enabled
}
http://thepcspy.com/read/how_to_block_adblock/
jQuery:
function blockAdblockUser() {
if ($('.myTestAd').height() == 0) {
window.location = 'http://example.com/AdblockNotice.html';
}
}
$(document).ready(function(){
blockAdblockUser();
});
当然,你需要有一个AdblockNotice.html的登陆页,并且. mytestad类需要反映你实际的广告容器。但这应该有用。
EDIT
正如TD_Nijboer所建议的,更好的方法是使用:hidden(或:visible,如下所示)选择器,这样display: none也会被检查:
function blockAdblockUser() {
if ($('.myTestAd').filter(':visible').length == 0) {
// All are hidden, or "not visible", so:
// Redirect, show dialog, do something...
} else if ($('.myTestAd').filter(':hidden').length > 0) {
// Maybe a different error if only some are hidden?
// Redirect, show dialog, do something...
}
}
当然,如果需要的话,这两个都可以合并成一个if块。
注意可见性:隐藏将不会被捕捉以及(在布局空间保留,但广告不可见)。要检查这一点,可以使用另一个过滤器:
$('.myTestAd').filter(function fi(){
return $(this).css('visibility') == 'hidden';
})
这将为您提供一个“不可见”的广告元素数组(理论上,任何大于0的都是一个问题)。
时间的答案是很好的思考,但不再工作,所以我已经更新了js文件的名称为“adsense”从“广告”,它是工作回来像一个魅力!
下面是代码,也许这将帮助到一些人:
<html>
<head>
<script src="/adsense.js"></script>
</head>
<body>
<script>
if( window.adblockDetecter === undefined ){
// Do anithing, adblocker detected!
alert('Adblocker Detected!');}
</script>
</body>
</html>
在Js文件中只放这一行:var adblockDetecter = true;