我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。
如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。
我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?
当前回答
AdBlock似乎阻止加载AdSense(等)JavaScript文件。所以,如果你使用异步版本的AdSense广告,你可以检查adsbygoogle是否是一个数组。这必须在几秒钟后检查,因为异步脚本是…异步的。以下是一个大致的大纲:
window.setTimeout(function(){
if(adsbygoogle instanceof Array) {
// adsbygoogle.js did not execute; probably blocked by an ad blocker
} else {
// adsbygoogle.js executed
}
}, 2000);
为了澄清,这里有一个AdSense异步广告代码的示例:
<!-- this can go anywhere -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- this is where the ads display -->
<ins class="adsbygoogle" ...></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
注意adsbygoogle被初始化为一个数组。adsbygoogle.js库将此数组更改为Object {push:…}当它执行时。在一段时间后检查变量的类型可以告诉您脚本是否已加载。
其他回答
我知道已经有足够多的答案,但由于这个问题出现在谷歌搜索“检测广告拦截”的主题上,我想提供一些见解,以防你不使用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还没有启动。我没有费心去进一步调查。
你不需要一个额外的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>
不需要超时和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';
});
我还没有看到任何好的,简单的答案来解决这两种类型的广告拦截在今天普遍使用,所以我将提供我自己的答案。
广告拦截器类型1:根据脚本名称(AdBlock, uBlock来源等)拦截网络广告脚本。
广告拦截器类型2:基于服务主机名的广告数据库(FireFox内容拦截器,各种网关插件等)拦截网络广告。
这个解决方案对两者都有效。它会弹出一个巨大的粉色“呼吁框”,要求用户禁用广告拦截器。我们喜欢把它放在菜单下面,内容上面。我们实际上并没有阻止对网站的访问-这只是把页面上的内容往下推了一点,但人们会发现这很烦人,几乎所有人都会遵守并禁用他们在你的网站上的广告拦截器。
这就是解决方案:
A)创建一个名为advertising .js的文件,并将其放置在你的web服务器的根目录下,由以下代码行组成:
document.write('<div id="tester">an advertisement</div>');
B)在你的网页中注入以下内容(你甚至可以使用你的广告服务器代码来做到这一点!)建议位置在菜单下方,内容上方。
<script src="advertisement.js"></script>
<table id="tbl_ab_appeal" style="width: 900px; margin-left:auto; margin-right: auto; padding: 25px; background: #FCC; border: 1px solid #F66; visibility:collapse; border-collapse: collapse;">
<tr>
<td>We've detected that you're using an <strong>ad content blocking</strong> browser plug-in or feature. Ads provide a critical source of revenue to the continued operation of [This website name]. We ask that you disable ad blocking while on [This
website name] in the best interests of our community.</td>
</tr>
</table>
<script>
if (document.getElementById("tester") == undefined) adsBlocked();
function adsBlocked() {
document.getElementById("tbl_ab_appeal").style.visibility = "visible";
document.getElementById("tbl_ab_appeal").style.borderCollapse = "separate";
document.getElementById("tbl_ab_appeal").style.marginTop = "10px"
document.getElementById("tbl_ab_appeal").style.marginBottom = "10px"
}
</script>
<script onerror="adsBlocked()" src="//www.googletagservices.com/tag/js/gpt.js"></script>
它是如何工作的?消息被加载,但被设为零高度且不可见。如果本地脚本advertising .js运行失败,或者远程AdSense脚本www.googletagservices.com/tag/js/gpt.js加载失败,则该方框可见。
我已经在浏览器中实现了许多方法来检测广告块,所有的解决方案都失败了,除了下面一个在javascript:
window.onload = function() {
setTimeout(function() {
var ad = document.querySelector("ins.adsbygoogle");
if (ad && ad.innerHTML.replace(/\s/g, "").length == 0) {
console.log('You seem to blocking Google AdSense ads in your browser.');
}
}, 2000);
};
我希望这个javascript解决方案将帮助你。谢谢你的提问。