Html 广告拦截器检测又名 Adblock Plus

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18180598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:03:40  来源:igfitidea点击:

Ad Blocker detection AKA Adblock Plus

javascripthtmladblock

提问by chillers

After searching Google and Stackoverflow for a few hours I could not find a solution. What I'm trying to do is detect Adblock plus and display a simple message for now.

在 Google 和 Stackoverflow 搜索了几个小时后,我找不到解决方案。我现在要做的是检测 Adblock plus 并显示一条简单的消息。

What I want to do is detect Adblock plus withoutusing a JavaScript file or jQuery. Most of the adblock plus detect scripts they use a file, example "show_ads.js" that is hosted on there own domain with a line it in to set it "adblock = false;"

我想要做的是在使用 JavaScript 文件或 jQuery 的情况下检测 Adblock plus 。大多数 adblock plus 检测脚本都使用一个文件,例如“show_ads.js”,它托管在自己的域上,并在其中设置一行“adblock = false;”

The problem with using a JavaScript file, users can white list that JavaScript file and it will no longer detect it. What I'm looking for is a JavaScript that loads directly into the HTML that would detect if someone is using ad blocker without the use of a file.

使用 JavaScript 文件的问题,用户可以将该 JavaScript 文件列入白名单,并且不再检测到它。我正在寻找的是直接加载到 HTML 中的 JavaScript,它可以检测是否有人在不使用文件的情况下使用广告拦截器。

Example Below:

示例如下:

<script type="text/javascript">
 // line of code that detects if using ad blocker

 if so display message
 </script>

The reason behind doing it this way no ad blocker can white list the JavaScript file on your server. Yes I know there are other methods of getting around this with NoScript addons but I already have a solution for that. I have a great idea that has never been tried and ad blockers cannot block this once I get done with it.

这样做的原因是没有广告拦截器可以将您服务器上的 JavaScript 文件列入白名单。是的,我知道还有其他方法可以使用 NoScript 插件解决这个问题,但我已经有了解决方案。我有一个从未尝试过的好主意,一旦我完成它,广告拦截器就无法阻止它。

Any suggestions and Examples will be greatly appreciated.

任何建议和示例将不胜感激。

采纳答案by chillers

I have found one of the best scripts if you use third party ads.

如果您使用第三方广告,我找到了最好的脚本之一。

antiblock.orgDisclaimer I'm not affiliated with this site in anyway.

antiblock.org免责声明 我无论如何都与本网站无关。

It will work for most sites and if they want to bypass it they will have to add their own filters (complicated for normal users) or contact adblock filters and have one added but they quit doing that cause the list is getting over loaded and slowing down ad block users.

它适用于大多数网站,如果他们想绕过它,他们将不得不添加自己的过滤器(对普通用户来说很复杂)或联系 adblock 过滤器并添加一个,但他们不再这样做,因为列表加载过多并变慢广告屏蔽用户。

回答by undone

You don't need to have a plugin to detect adblock, simply use this:

你不需要有插件来检测广告块,只需使用这个:

<script type="text/javascript">
    var adblock = true;
</script>
<script type="text/javascript" src="adframe.js"></script>
<script type="text/javascript">
    if(adblock) {
          //adblock is installed and enabled on this site :-D
    }
</script>

Content of adframe.js:

adframe.js 的内容:

adblock = false;

Update:Adblock Plus blocks certain requests or hides certain elements based on patterns it already has. One of those patterns is this (in patterns.ini):

更新:Adblock Plus 根据已有的模式阻止某些请求或隐藏某些元素。其中一种模式是这样的(在patterns.ini 中):

[Filter]
text=/adframe.
hitCount=843
lastHit=1456391595626

which blocks any URL that has /adframe.in it.

它会阻止其中包含的任何 URL /adframe.

Update 25th august 2018

2018 年 8 月 25 日更新

Adblock plus has changed the way it finds the list and blocks the ads. It has bunch of lists called subscriptions which are used for blocking. For example this one which is the default one:

Adblock plus 改变了它查找列表和屏蔽广告的方式。它有一堆称为订阅的列表,用于阻塞。例如,这是默认的:

https://easylist-downloads.adblockplus.org/easylist.txt

https://easylist-downloads.adblockplus.org/easylist.txt

You can use the rules on this file to find a file name to use. For example you can use seo-ads.js

您可以使用此文件的规则来查找要使用的文件名。例如,您可以使用seo-ads.js

P.S for developers: For some reason I couldn't get ABP to block these files on local environment.

PS 开发人员:由于某种原因,我无法让 ABP 在本地环境中阻止这些文件。

P.S: ABP is my favorite ad blocker :-D

PS:ABP 是我最喜欢的广告拦截器 :-D

回答by SiteXw

Use my plugin "fworAdBlock", it can very easily detect AdBlock: https://github.com/sitexw/fworAdBlock

使用我的插件“fworAdBlock”,它可以很容易地检测到 AdBlock:https: //github.com/sitexw/fworAdBlock

Example :

例子 :

fworAdBlock.on(true, function() {
    alert('AdBlock detected !');
}).on(false, function() {
    alert('AdBlock is not detected =)');
});

Example online: http://fworadblock.sitexw.fr/

在线示例:http: //fworadblock.sitexw.fr/

回答by Sumurai8

What I've seen in the field is using a background image behind the ad. If adblock isn't active, the ad will be displayed over the background-image (which makes the background-image not viewable). If adblock is active, the ad is blocked, and the user will instead see the background-image.

我在现场看到的是在广告后面使用背景图片。如果 adblock 未激活,则广告将显示在背景图片上(这会使背​​景图片不可见)。如果 adblock 处于活动状态,则广告会被屏蔽,而用户将看到背景图片。

<div id="ad-container">
  <img src="../ad/ad.png" id="ad">
</div>

With CSS:

使用 CSS:

#ad-container {
  background-image: url( http://domain.com/pleasedonotuseadblocker.png );
  height: 200px;
  width: 200px;
}

#ad {
  height: 200px;
  width: 200px;
}

回答by Sumurai8

If you want to ads to be showing, even when AdBlock is active, you'll have to understand what AdBlock is capable to do.

如果您想展示广告,即使 AdBlock 处于活动状态,您也必须了解 AdBlock 的功能。

  1. AdBlock can block resources from loading
  2. AdBlock can hide specific elements in the DOM.
  1. AdBlock 可以阻止资源加载
  2. AdBlock 可以隐藏 DOM 中的特定元素。

Although it is said that AdBlock can also modify CSS, I can't find any documentation on that other than hiding and collapsing elements.

虽然据说 AdBlock 也可以修改 CSS,但除了隐藏和折叠元素之外,我找不到任何关于它的文档。



So what exactly could you do to be 'smarter' than AdBlock?

那么你到底能做什么才能比 AdBlock 更“聪明”呢?

You could disguise your request in a way that it will never be 'matchable' (e.g. http://domain.com/ae9a70e0a.png, where the image name will be random every time and without a common prefix). As far as I am aware, a rule in AdBlock cannot contain a regex. A rule would either match no ads, or too many resources. It would be possible to rewrite such an url on the server to point to your ad.

您可以以永远不会“匹配”的方式伪装您的请求(例如http://domain.com/ae9a70e0a.png,图像名称每次都是随机的并且没有公共前缀)。据我所知,AdBlock 中的规则不能包含正则表达式。规则要么不匹配任何广告,要么匹配太多资源。可以在服务器上重写这样的 url 以指向您的广告。

However, while AdBlock might not be able to block your ad from loading, it might still be able to hide it. There is no real way of going around this. There will always be a smart CSS selector that will -just- select your element. You could however add a background-image with content. This is not useful for an ad (not clickable), but might help you display an other message. Downside is that if someone decides to block that annoying background image, it will hide your content too.

但是,虽然 AdBlock 可能无法阻止加载您的广告,但它仍然可以隐藏它。没有真正的方法可以解决这个问题。总会有一个智能的 CSS 选择器来——只是——选择你的元素。但是,您可以添加带有内容的背景图像。这对广告(不可点击)没有用,但可能会帮助您显示其他消息。缺点是,如果有人决定屏蔽那个烦人的背景图片,它也会隐藏您的内容。



As far as a script goes, you might be able to load the ad with an ajax request. I suppose (but cannot test) that it will give an error if the resource could not be loaded (because it was blocked). ($.ajax( request ).error( function() { ... } );in jQuery or some equivalent in regular javascript). You could use that to do something else. You could include that in the document itself, instead of an external resource, to ensure it will always run (if javascript is enabled). Even then, you cannot be sure that 'whatever else you do' will ever be visibly displayed. As last resort you can make a window.alert( ... ). Assume that within 3 pages, your visitors will never come back if you use that.

就脚本而言,您或许可以使用 ajax 请求加载广告。我想(但无法测试)如果无法加载资源(因为它被阻止),它会出错。($.ajax( request ).error( function() { ... } );在 jQuery 中或在常规 javascript 中的一些等价物)。你可以用它来做其他事情。您可以将其包含在文档本身而不是外部资源中,以确保它始终运行(如果启用了 javascript)。即便如此,您也无法确定“无论您做什么”都会被明显地显示出来。作为最后的手段,您可以制作一个window.alert( ... ). 假设在 3 页内,如果您使用它,您的访问者将永远不会回来。

An other way I can think of, is making a websocket to the server (afaik this cannot be blocked by AdBlock). On the server side you'll need to examine if the ad pages are not loaded when a certain page is loaded. This information can be sent through the socket, which can be used in your script to do 'something'. This, however, sounds crazy complicated and is a significant overhead for 'just' a script that detects AdBlock.

我能想到的另一种方法是为服务器创建一个 websocket(afaik 这不能被 AdBlock 阻止)。在服务器端,您需要检查在加载某个页面时是否未加载广告页面。此信息可以通过套接字发送,可以在您的脚本中使用它来做“某事”。然而,这听起来非常复杂,并且对于“仅”检测 AdBlock 的脚本来说是一个巨大的开销。

回答by Etienne Martin

A simple Ajax call does the job:

一个简单的 Ajax 调用就可以完成这项工作:

var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
  if( xmlhttp.readyState == XMLHttpRequest.DONE ){
    if( xmlhttp.status !== 404 ){
        console.log("Blocking ads")
    }else{
        console.log("Not blocking ads")
    }
  }
}
xmlhttp.open("GET", "/498100ffe815d700cd838d1/ads/showad.js", true)
xmlhttp.send()

Or even better, without an extra HTTP overhead:

或者甚至更好,没有额外的 HTTP 开销:

var adBlockTester = document.createElement('div');
adBlockTester.innerHTML = '&nbsp;';
adBlockTester.className = 'adsbox';
document.body.appendChild(adBlockTester);
window.setTimeout(function() {
  if( adBlockTester.offsetHeight === 0 ){
    console.log("Blocking ads")
  }else{
    console.log("Not blocking ads")
  }
  document.body.removeChild(adBlockTester);
}, 60);

回答by aleemb

The following snippet will pretty much detect all ad blockers. Requires jQuery.

以下代码段几乎可以检测所有广告拦截器。需要jQuery.

(function(){
    var bait = 'http://googleads.g.doubleclick.net/pagead/gen_204?id=wfocus&gqid=advertisment&advert=ads';
      $.ajax({ url: bait, dataType: "script"})
      .fail(function () { alert('ad blocked'); })
      .abort(function () { alert('ad blocked'); });
    })();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

It's wrapped in a self-executing anonymous function so it doesn't interfere with other vars or code on the site.

它封装在一个自动执行的匿名函数中,因此它不会干扰站点上的其他变量或代码。

The baituses the most popular ad serving network (Google's double click) and includes a few other query params used by easylistand others.

bait使用最流行的广告服务网络(谷歌的双击),包括通过使用一些其他的查询参数easylist等。

The fail()and abort()methods are both required, but only one or the other will be invoked.

fail()abort()方法都需要,但只有一个或另一个将被调用。

Don'tput the code in adblocker.jsor similar since those sort of filenames themselves get blocked from loading. Either inline it or include it in an random/arbitrary filename or combine it in your main site JS file.

不要放入代码adblocker.js或类似的代码,因为这些文件名本身会被阻止加载。内联它或将其包含在随机/任意文件名中或将其组合到您的主站点 JS 文件中。

回答by qnimate

Here is the code to detect adblock. You can learn how the code works here

这是检测广告块的代码。您可以在此处了解代码的工作原理

function detect()
{
    //create a iframe. Append the iframe to the body. And then after 100ms check if their offsetHeight, display or visibility is set such a way that user cannot see them.
    //In the URL use the words specific to advertising so that Adblock can do string matching.
    var iframe = document.createElement("iframe");
    iframe.height = "1px";
    iframe.width = "1px";
    iframe.id = "ads-text-iframe";
    iframe.src = "http://domain.com/ads.html";

    document.body.appendChild(iframe);

    setTimeout(function()
               {
                   var iframe = document.getElementById("ads-text-iframe");
                   if(iframe.style.display == "none" || iframe.style.display == "hidden" || iframe.style.visibility == "hidden" || iframe.offsetHeight == 0)
                   {
                        alert("Adblock is blocking ads on this page");
                        iframe.remove();
                   }
                   else
                   {
                        alert("Adblock is not detecting ads on this page");
                        iframe.remove();
                   }
               }, 100);
}

回答by suncat100

Simple javascript/jQuery detection that works nicely:

简单的 javascript/jQuery 检测效果很好:

$('body').append('<div id="ad-container" style="position:absolute;"><img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=" id="ad"></div>');
var ad_container = $('body').children('#ad-container');
if(!ad_container.is(":visible")) {
  // Add your warning and/or adblock detection logic here.
}
ad_container.remove();

回答by Ojas Kale

For me none of the tricks worked, may something I was doing wrong. but this is a very specific way to implement for google ads.

对我来说,这些技巧都没有奏效,可能是我做错了什么。但这是为谷歌广告实施的一种非常具体的方式。

window.onload = function() {
   if (document.getElementsByClassName('google-auto-placed').length == 0){
                // Adblock Detected
   }        
}

If you have other ad system like amazon, look for their generic class name / ids by inspecting page.

如果您有其他广告系统,例如亚马逊,请通过检查页面来查找其通用类名/ID。

If you are planing to put this code in seperate .js file make sure file name does not have "Ad" word in it. just name it magic.js

如果您打算将此代码放在单独的 .js 文件中,请确保文件名中没有“广告”字样。将它命名为magic.js

If Google ever decides to change div name, this method would fail. but that seems unlikely.

如果 Google 决定更改 div 名称,则此方法将失败。但这似乎不太可能。

回答by Arslan Ameer

The Smartestand easiest way I found is:

我发现的最聪明和最简单的方法是:

1)add this htmlcode on somewhere in your markup probably on top.

1)将此html代码添加到标记中的某处,可能位于顶部。

<div id="bait" class="pub_300x250" style="color: #fff">.</div>

Usually ad blockers detect ad sizes of (pub_300x250) as mentioned in Easylistand blocked them, which is triggered by "bait".

一般广告拦截检测如在所提到的(pub_300x250)广告尺寸Easylist和阻止它们,这是由“诱饵”触发。

2)then add this js code into your script file.

2)然后将此js代码添加到您的脚本文件中。

if (document.getElementById("bait").offsetHeight === 0) {
    // function code or alert (whatever) here.
   alert("Ad-Blocker DETECTED");
}

Our Script detects if that piece of markup is existed in present htmlby checking thorugh "bait" id.

我们的脚本html通过检查“诱饵”ID 来检测当前是否存在该标记。

This works for me with Adblock, AdBlock-Plus& uBlock Originon every site on every browser.

这对我来说适用于Adblock, AdBlock-Plus&uBlock Origin在每个浏览器上的每个站点上。