我希望能够检测用户是否正在使用广告拦截软件,当他们访问我的网站。如果他们正在使用它,我想显示一条消息,要求他们关闭它以支持项目,就像这个网站一样。

如果你进入该网站,而你的浏览器启用了某种广告拦截软件,那么该网站就不会显示真正的广告,而是显示一个小横幅,告诉用户广告收入用于托管项目,他们应该考虑关闭广告拦截。

我想在我的网站上做到这一点,我正在使用adsense广告,我怎么能做到呢?


当前回答

这个很好用

如果有广告拦截器,它会提醒你

简单地说,它发送一个头部请求到一个著名的广告公司的所有广告拦截器(谷歌广告),如果请求被阻止,那么adbloker存在。

checkAdBlocker (); 函数checkAdBlocker() { 尝试{ fetch ( new Request("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", { 方法:“头”, 模式:“no-cors” }))。Catch(错误=> { showNotification () }); } catch (e) { //请求失败,可能是由于广告拦截程序 showNotification () } } 函数showNotification() { 警告(“请禁用广告拦截器”) }

其他回答

我有点晚了,这里有一个最简单的解决方案,我知道的老AdSense代码与jQuery:

$ads = $("ins");
if ($ads.length == 0) {
    // Replacement code about you needing ad income
}

在纯JavaScript中:

$ads = document.getElementsByTagName("ins");
if ($ads.length == 0) {
    // Replacement code about you needing ad income
}

对于$ads,您可以使用任何与正在生成的广告一致的选择器。例如,对于新的AdSense代码,您可以使用$("iframe#google_ads_frame1")。

这对我来说很管用:

function isAdBlocked() {
     return (typeof(window.google_jobrunner) === "undefined") ? true : false;
}

$(document).ready(function(){
    if(isAdBlocked()) {
       alert('Y U NO LIKE ADS?');
    }
});

以上所有的答案都是有效的,但大多数将不适用于dns级别的广告拦截。

dns级别的广告拦截器(如π -hole)基本上会返回NXDOMAIN(domain不存在)的广告拦截域列表(例如telemetry.microsoft.com将“不存在”当它存在时)。

有几种方法可以避免这种情况:

方法A:根据ip地址而不是域名请求广告。

这种方法有点烦人,因为您必须跟踪ip地址。如果您的代码没有得到很好的维护或定期更新,这将会产生问题。

方法B:阻塞所有失败的请求——即使客户端报告NXDOMAIN。

如果它是一个“合法的”NXDOMAIN,这会让用户非常恼火。

我注意到之前的评论使用谷歌adsense作为测试对象。有些页面不使用adsense,使用adsense块作为测试并不是一个好主意。因为站长屏蔽可能会损害你的SEO。 以下是我如何通过adblocker检测简单的阻塞类的例子:

Html:

<div class="ad-placement" id="ablockercheck"></div>
<div id="ablockermsg" style="display: none"></div>

Jquery:

$(document).ready(function()
{
   if(!$("#ablockercheck").is(":visible"))
   {
     $("#ablockermsg").text("Please disable adblocker.").show();
   }
});

"ablockercheck"是adblocker阻止的ID。所以检查它,如果它是可见的,你能够检测,如果广告拦截器被打开。

[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'}`) });