我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
当前回答
如果你使用jQuery和谷歌Adsense:
jQuery.getScript(
"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js",
function() {
// Load your ad now
}).fail(function() {
// Google failed to load main script, do something now
});
这更容易理解:如果谷歌广告主JavaScript文件加载失败,AdSense将无法工作,所以您使用jQuery的fail函数做一些事情。
“loading your add now”是当我添加“ins”对象时,比如:
jQuery(".my_ad_div").append('<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-xxx"
data-ad-slot="xxx"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>');
在“//谷歌加载主脚本失败,现在做点什么”中,我通常把图像放在广告的地方。
其他回答
我知道已经有足够多的答案,但由于这个问题出现在谷歌搜索“检测广告拦截”的主题上,我想提供一些见解,以防你不使用adsense。
具体来说,通过这个例子,您可以检测到是否使用了Firefox Adblock提供的默认Adblock列表。它利用了这个块列表中有一个CSS id为#bottomAd的元素。如果我在页面中包含这样一个元素,并测试它的高度,我知道广告拦截是否活跃:
<!-- some code before -->
<div id="bottomAd" style="font-size: 2px;"> </div>
<!-- some code after -->
其余的是通过通常的jQuery嫌疑犯完成的:
$(document).ready( function() {
window.setTimeout( function() {
var bottomad = $('#bottomAd');
if (bottomad.length == 1) {
if (bottomad.height() == 0) {
// adblocker active
} else {
// no adblocker
}
}
}, 1);
}
可以看到,我使用的setTimeout的超时时间至少为1毫秒。我在各种浏览器上测试过,大多数时候,直接检查ready中的元素总是返回0;不管广告拦截器是否有效。对此,我有两个想法:要么渲染还没有完成,要么Adblock还没有启动。我没有费心去进一步调查。
他们利用谷歌的广告代码创建了一个id为“iframe”的iframe。因此,只要您的页面上没有该ID的内容,这也适用于您。
<p id="ads">
<script type="text/javascript"><!--
google_ad_client = "their-ad-code-here";
/* 160x600, droite */
google_ad_slot = "their-ad-code-here";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<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 #000000; text-align:center; font-family:century gothic, arial, helvetica, sans serif;padding-left:5px;padding-right:5px;'>Advertising seems to be blocked by your browser.<br /><br /><span style='font-size:10px'>Please notice that advertising helps us to host the project.<br /><br />If you find these ads intrusive or inappropriate, please contact me.</span><img src='http://www.playonlinux.com/images/abp.jpg' alt='Adblock Plus' /></div>");
}
--></script>
这对我来说很好……
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
<script>
var adBlockEnabled = false;
var adSense = document.createElement('div');
adSense.innerHTML = ' ';
adSense.className = 'adsbox';
document.body.appendChild(adSense);
window.setTimeout(function() {
if (adSense.offsetHeight === 0) {
adBlockEnabled = true;
}
adSense.remove();
if (adBlockEnabled) {
alert('Adblock enabled');
} else {
alert('Adblock disabled or Not installed');
}
}, 100);
</script>
</body>
</html>
你不需要一个额外的HTTP请求,你可以简单地计算一个假添加的高度。
顺便说一下,这里有一个完整的列表,与广告拦截器避免渲染的元素相匹配。
窗口。adBlockRunning = function() { return (getComputedStyle(document.getElementById("detect"))["display"] == "none") ?真:假; }() console.log (window.adBlockRunning); #{检测 身高:1 px; 宽度:1 px; 位置:绝对的; 左:-999 em; 上图:-999年新兴市场 } <div id="detect" class="ads AD adsbox doubleclick AD -placement carbon-ads"></div>
检查是否有广告拦截的有效方法: 简单地检查是否有adblock通过尝试触发谷歌广告的URL。如果是,然后运行callback_has_adblock,如果没有,然后运行callback_no_adblock。这个解决方案会多花费一个请求,但至少它总是有效的:
var hasAdBlock = function (callback_has_adblock, callback_no_adblock) {
$.getScript( "https://pagead2.googlesyndication.com/pagead/show_ads.js" )
.done(function( script, textStatus ) {
callback_no_adblock();
})
.fail(function( jqxhr, settings, exception ) {
callback_has_adblock();
});
};
这个解决方案适用于所有类型的广告,而不仅仅是谷歌adsense。