我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
当前回答
这份文件可能会有所帮助。 这是检测广告拦截软件最有效的方法 完全免费且易于实现 https://www.detectadblock.com/
其他回答
在React.js中,你可以做以下事情:
class AdblockDetect extends Component {
constructor (props) {
super(props)
this.state = {
usingAdblock: false
}
}
componentDidMount() {
this.setState({ usingAdblock: this.fakeAdBanner.offsetHeight === 0 });
}
render() {
if (this.state.usingAdblock === true) {
return this.props.children;
}
return (
<div
ref={r => (this.fakeAdBanner = r)}
style={{ height: '1px', width: '1px', visiblity: 'none', pointerEvents: 'none' }}
className="adBanner"
/>
);
}
}
class App extends Component {
render() {
return (
<div className="App">
<AdblockDetect>You are using adblock</AdblockDetect>
</div>
);
}
}
来源:https://stackoverflow.com/a/55635499/5539715
不需要超时和DOM嗅探。简单地尝试从流行的广告网络加载一个脚本,看看广告拦截器是否拦截了HTTP请求。
/**
* Attempt to load a script from a popular ad network. Ad blockers will intercept the HTTP request.
*
* @param {string} url
* @param {Function} cb
*/
function detectAdBlockerAsync(url, cb){
var script = document.createElement('script');
script.onerror = function(){
script.onerror = null;
document.body.removeChild(script);
cb();
}
script.src = url;
document.body.appendChild(script);
}
detectAdBlockerAsync('http://ads.pubmatic.com/AdServer/js/gshowad.js', function(){
document.body.style.background = '#c00';
});
这个很好用
如果有广告拦截器,它会提醒你
简单地说,它发送一个头部请求到一个著名的广告公司的所有广告拦截器(谷歌广告),如果请求被阻止,那么adbloker存在。
checkAdBlocker (); 函数checkAdBlocker() { 尝试{ fetch ( new Request("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", { 方法:“头”, 模式:“no-cors” }))。Catch(错误=> { showNotification () }); } catch (e) { //请求失败,可能是由于广告拦截程序 showNotification () } } 函数showNotification() { 警告(“请禁用广告拦截器”) }
要检测用户是否屏蔽广告,你所要做的就是在广告javascript中找到一个函数,并尝试测试它。他们用什么方法屏蔽广告并不重要。下面是谷歌Adsense广告的情况:
if(!window.hasOwnProperty('google_render_ad') || window.google_render_ad === undefined) {
//They're blocking ads, display your banner
}
该方法概述在这里:http://www.metamorphosite.com/detect-web-popup-blocker-software-adblock-spam
我的解决方案并不针对特定的广告网络,而且是非常轻量级的。我已经在生产环境中运行了几年。AdBlock会屏蔽所有含有“广告”或“预竞价”字样的url。这就是我所做的:
我在webroot中添加了一个名为prebid-ads.js的小js文件
这是文件中唯一的一行代码。将此变量命名为其他变量,参见下文!
var canRunAds = true;
然后在你页面的某处:
<html>
<head>
<script src="/js/prebid-ads.js"></script>
</head>
<body>
<script>
if( window.canRunAds === undefined ){
// adblocker detected, show fallback
showFallbackImage();
}
</script>
</body>
</html>
uBlock Origin加载他们自己的ads-prebid.js,还原了这个答案中描述的技巧(骄傲!),他们的文件包含以下内容:
(function() {
'use strict';
window.canRunAds = true;
window.isAdBlockActive = false;
})();
作为一种解决方案,只需将canRunAds变量重命名为一些有趣的东西,比如window。adsAreWithUs或window.money高于隐私。
Ans de Nijs的发现和解决方法。谢谢!
支持扩展
像ads.js这样的文件在Chrome上至少会被这些广告拦截器屏蔽:
AdBlock Adblock Plus Adblock职业 Ghostery
不适用于:
隐私獾