我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
当前回答
我知道这个问题已经有答案了,但我看了一下建议的示例站点,我看到他们是这样做的:
<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>
其他回答
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;
}
}
异步函数detectAdBlock() { let adBlockEnabled = false const googleAdUrl = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' 尝试{ 等待取回(新的请求(googleAdUrl))。catch(_ => adBlockEnabled = true) } catch (e) { adBlockEnabled = true }最后{ console.log(' adBlockEnabled: ${adBlockEnabled} ') } } detectAdBlock ()
[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'}`) });
如果你有问题的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
});
这段代码是有用的,如果你不想阻止整个网站,只是告诉用户为什么他们的浏览器标签是关闭的;)
只需在你的网站上添加小脚本:
var isAdsDisplayed = true;
名称为adsbygoogle.js
然后做以下事情:
<script src="/js/adsbygoogle.js"></script>
<script>
if(window.isAdsDisplayed === undefined ) {
// AdBlock is enabled. Show message or track custom data here
}
</script>
找到了这个解