我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用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'}`) });
其他回答
我理解你的紧张,你可以检查元素是否已由脚本创建或元素被隐藏。如果我们谈论广告拦截,你只能依靠元素可见性,而不是元素存在性。
使用第三方脚本创建的元素永远不会出现,如果脚本目前无法访问(DNS错误,远程web服务器错误,离线web页面预加载等),你总是会得到假阳性。
其他的答案都是正确的,但是记住这个。
如果你有问题的adblock阻止新标签在浏览器中,你可以这样做:
$('a').click(function(e){ // change $('a') into more specific selector
const openedWindow = window.open(this.href, '_blank');
// Check if browser tab was closed within 0.3 second (user can't, adblock does).
setTimeout(() => {
if (openedWindow.closed) {
alert('Adblock detected!');
}
}, 300);
e.preventDefault(); // return false if you like
});
这段代码是有用的,如果你不想阻止整个网站,只是告诉用户为什么他们的浏览器标签是关闭的;)
这为我工作,文件必须准备好之前,你寻找广告块 希望这能帮助到一些人!
jQuery(document).ready(function() {
var adBlockEnabled = false;
var testAd = document.createElement('div');
testAd.innerHTML = ' ';
testAd.className = 'adsbox';
document.body.appendChild(testAd);
window.setTimeout(function() {
if (testAd.offsetHeight === 0) {
adBlockEnabled = true;
}
testAd.remove();
console.log('AdBlock Enabled? ', adBlockEnabled)
}, 100);
});
大多数adblocker取消对ads.js的HTTP请求,并为元素制作0px,但有时adblocker删除了DOM,上面的一些答案将失败,因为没有检查元素的存在。
使用setTimeout()是一个很好的实践,因为没有它,将使脚本与广告拦截器竞争。
下面的脚本将检查dom是否存在/已删除,如果存在则检查元素的offsetHeight。
setTimeout(函数(){ var a = document.querySelector('.showads'), B = a ?(a.offsetHeight ?False:真):真; console.log(“广告了吗?”,b) }, 200);//不要太快,否则会使结果出错。 <div class="ads showads"> 我爱你,我爱你,我爱你。 < / div >
我用jquery最简单的解决方案是:
$.ajax({
url: "/scripts/advertisement.js", // this is just an empty js file
dataType: "script"
}).fail(function () {
// redirect or display message here
});
advertising .js什么都不包含。当有人使用adblock时,它失败了,函数被调用。