How to detect Adblock on my website?

2019-01-01 07:23发布

I would like to be able to detect if the user is using adblocking software when they visit my website. If they are using it, I want to display a message asking them to turn it off in order to support the project, like this website does.

If you enter to that site and your browser has some kind of adblock software enabled, then the site instead of showing the actual ads shows a little banner telling the users that the ad revenue is used for hosting the project and they should consider turning Adblock off.

I want to do that on my website, I'm using adsense ads on it, How can I do that?

45条回答
裙下三千臣
2楼-- · 2019-01-01 08:19

They're utilizing the fact that Google's ad code creates an iframe with the id "iframe". So as long as you don't already have something on your page with that ID, this'd work for you too.

<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>
查看更多
后来的你喜欢了谁
3楼-- · 2019-01-01 08:19

timing's answer is nice thinked but no more working, so I have updated the name of the js file to 'adsense' from 'ads' and it is working back like a charm!

Here is the code, maybe this wil help someone:

<html>
      <head>
            <script src="/adsense.js"></script>
      </head>
             <body>
                   <script>
                           if( window.adblockDetecter === undefined ){
                           // Do anithing, adblocker detected!
                           alert('Adblocker Detected!');}
                   </script>
            </body>
</html>

In the Js file put just this line: var adblockDetecter = true;

查看更多
栀子花@的思念
4楼-- · 2019-01-01 08:20

Run this with adblock on and Paste this in your page-footer before the close of the </body> tag on your page.

<script type="text/javascript"  charset="utf-8">
// Place this code snippet near the footer of your page before the close of the /body tag
// 
                            
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}(';q O=\'\',29=\'1W\';1S(q i=0;i<12;i++)O+=29.X(B.N(B.K()*29.F));q 2z=1,2u=4p,2v=4o,2x=4n,33=C(e){q i=!1,o=C(){z(k.1g){k.2T(\'2J\',t);D.2T(\'1V\',t)}S{k.2V(\'2L\',t);D.2V(\'27\',t)}},t=C(){z(!i&&(k.1g||4m.2E===\'1V\'||k.2O===\'2K\')){i=!0;o();e()}};z(k.2O===\'2K\'){e()}S z(k.1g){k.1g(\'2J\',t);D.1g(\'1V\',t)}S{k.2M(\'2L\',t);D.2M(\'27\',t);q n=!1;2P{n=D.4k==4j&&k.23}2U(r){};z(n&&n.2Q){(C a(){z(i)G;2P{n.2Q(\'17\')}2U(t){G 4i(a,50)};i=!0;o();e()})()}}};D[\'\'+O+\'\']=(C(){q e={e$:\'1W+/=\',4h:C(t){q a=\'\',d,n,i,c,s,l,o,r=0;t=e.t$(t);1a(r<t.F){d=t.14(r++);n=t.14(r++);i=t.14(r++);c=d>>2;s=(d&3)<<4|n>>4;l=(n&15)<<2|i>>6;o=i&63;z(2Z(n)){l=o=64}S z(2Z(i)){o=64};a=a+U.e$.X(c)+U.e$.X(s)+U.e$.X(l)+U.e$.X(o)};G a},11:C(t){q n=\'\',d,l,c,s,r,o,a,i=0;t=t.1C(/[^A-4g-4f-9\\+\\/\\=]/g,\'\');1a(i<t.F){s=U.e$.1I(t.X(i++));r=U.e$.1I(t.X(i++));o=U.e$.1I(t.X(i++));a=U.e$.1I(t.X(i++));d=s<<2|r>>4;l=(r&15)<<4|o>>2;c=(o&3)<<6|a;n=n+P.T(d);z(o!=64){n=n+P.T(l)};z(a!=64){n=n+P.T(c)}};n=e.n$(n);G n},t$:C(e){e=e.1C(/;/g,\';\');q n=\'\';1S(q i=0;i<e.F;i++){q t=e.14(i);z(t<1s){n+=P.T(t)}S z(t>4e&&t<4d){n+=P.T(t>>6|4c);n+=P.T(t&63|1s)}S{n+=P.T(t>>12|2F);n+=P.T(t>>6&63|1s);n+=P.T(t&63|1s)}};G n},n$:C(e){q i=\'\',t=0,n=4a=1u=0;1a(t<e.F){n=e.14(t);z(n<1s){i+=P.T(n);t++}S z(n>3V&&n<2F){1u=e.14(t+1);i+=P.T((n&31)<<6|1u&63);t+=2}S{1u=e.14(t+1);2f=e.14(t+2);i+=P.T((n&15)<<12|(1u&63)<<6|2f&63);t+=3}};G i}};q a=[\'49==\',\'48\',\'47=\',\'46\',\'45\',\'44=\',\'43=\',\'42=\',\'41\',\'40\',\'3Z=\',\'3Y=\',\'3X\',\'3W\',\'4q=\',\'4b\',\'4r=\',\'4J=\',\'4L=\',\'4M=\',\'4N=\',\'4O=\',\'4P==\',\'4Q==\',\'4K==\',\'4R==\',\'4T=\',\'4U\',\'4V\',\'4W\',\'4X\',\'4Y\',\'4S\',\'4I==\',\'4t=\',\'3T=\',\'4G=\',\'4F==\',\'4E=\',\'4D\',\'4C=\',\'4B=\',\'4A==\',\'4z=\',\'4y==\',\'4x==\',\'4w=\',\'4v=\',\'4u\',\'4s==\',\'3U==\',\'3A\',\'3S==\',\'3k=\'],y=B.N(B.K()*a.F),w=e.11(a[y]),Y=w,Q=1,v=\'#3m\',r=\'#3n\',W=\'#3g\',g=\'#3j\',Z=\'\',b=\'3h!\',p=\'3e 3f 3d 3c\\\'3a 39 38 2n 2m. 3o\\\'s 3l.  3q 3F\\\'t?\',f=\'3R 3Q 3P-3O, 3N 3L\\\'t 3K 3J U 3I 3E.\',s=\'I 3r, I 3D 3C 3B 2n 2m.  3x 3w 3v!\',i=0,u=0,n=\'3u.3t\',l=0,L=t()+\'.2k\';C h(e){z(e)e=e.1R(e.F-15);q i=k.2C(\'3M\');1S(q n=i.F;n--;){q t=P(i[n].1H);z(t)t=t.1R(t.F-15);z(t===e)G!0};G!1};C m(e){z(e)e=e.1R(e.F-15);q t=k.3s;x=0;1a(x<t.F){1n=t[x].1P;z(1n)1n=1n.1R(1n.F-15);z(1n===e)G!0;x++};G!1};C t(e){q n=\'\',i=\'1W\';e=e||30;1S(q t=0;t<e;t++)n+=i.X(B.N(B.K()*i.F));G n};C o(i){q o=[\'3z\',\'3G==\',\'3H\',\'3p\',\'2W\',\'3i==\',\'3b=\',\'37==\',\'4Z=\',\'4H==\',\'52==\',\'5i==\',\'6z\',\'6u\',\'6r\',\'2W\'],r=[\'35=\',\'6B==\',\'6t==\',\'6x==\',\'6d=\',\'6n\',\'62=\',\'6c=\',\'35=\',\'51\',\'6g==\',\'6k\',\'6l==\',\'61==\',\'5X==\',\'6m=\'];x=0;1O=[];1a(x<i){c=o[B.N(B.K()*o.F)];d=r[B.N(B.K()*r.F)];c=e.11(c);d=e.11(d);q a=B.N(B.K()*2)+1;z(a==1){n=\'//\'+c+\'/\'+d}S{n=\'//\'+c+\'/\'+t(B.N(B.K()*20)+4)+\'.2k\'};1O[x]=26 1T();1O[x].1X=C(){q e=1;1a(e<7){e++}};1O[x].1H=n;x++}};C M(e){};G{2X:C(e,r){z(6j k.J==\'6i\'){G};q i=\'0.1\',r=Y,t=k.1d(\'1y\');t.1k=r;t.j.1h=\'1D\';t.j.17=\'-1o\';t.j.V=\'-1o\';t.j.1t=\'2b\';t.j.13=\'6h\';q d=k.J.2p,a=B.N(d.F/2);z(a>15){q n=k.1d(\'2d\');n.j.1h=\'1D\';n.j.1t=\'1r\';n.j.13=\'1r\';n.j.V=\'-1o\';n.j.17=\'-1o\';k.J.5Y(n,k.J.2p[a]);n.1f(t);q o=k.1d(\'1y\');o.1k=\'2h\';o.j.1h=\'1D\';o.j.17=\'-1o\';o.j.V=\'-1o\';k.J.1f(o)}S{t.1k=\'2h\';k.J.1f(t)};l=6b(C(){z(t){e((t.1Y==0),i);e((t.21==0),i);e((t.1K==\'2H\'),i);e((t.1Q==\'2g\'),i);e((t.1J==0),i)}S{e(!0,i)}},28)},1G:C(t,c){z((t)&&(i==0)){i=1;D[\'\'+O+\'\'].1z();D[\'\'+O+\'\'].1G=C(){G}}S{q f=e.11(\'6a\'),u=k.69(f);z((u)&&(i==0)){z((2u%3)==0){q l=\'68=\';l=e.11(l);z(h(l)){z(u.1F.1C(/\\s/g,\'\').F==0){i=1;D[\'\'+O+\'\'].1z()}}}};q y=!1;z(i==0){z((2v%3)==0){z(!D[\'\'+O+\'\'].2y){q d=[\'67==\',\'66==\',\'6o=\',\'5Z=\',\'6p=\'],m=d.F,r=d[B.N(B.K()*m)],a=r;1a(r==a){a=d[B.N(B.K()*m)]};r=e.11(r);a=e.11(a);o(B.N(B.K()*2)+1);q n=26 1T(),s=26 1T();n.1X=C(){o(B.N(B.K()*2)+1);s.1H=a;o(B.N(B.K()*2)+1)};s.1X=C(){i=1;o(B.N(B.K()*3)+1);D[\'\'+O+\'\'].1z()};n.1H=r;z((2x%3)==0){n.27=C(){z((n.13<8)&&(n.13>0)){D[\'\'+O+\'\'].1z()}}};o(B.N(B.K()*3)+1);D[\'\'+O+\'\'].2y=!0};D[\'\'+O+\'\'].1G=C(){G}}}}},1z:C(){z(u==1){q E=2A.6s(\'2B\');z(E>0){G!0}S{2A.6y(\'2B\',(B.K()+1)*28)}};q h=\'6f==\';h=e.11(h);z(!m(h)){q c=k.1d(\'5u\');c.1Z(\'5V\',\'5r\');c.1Z(\'2E\',\'1m/5q\');c.1Z(\'1P\',h);k.2C(\'5o\')[0].1f(c)};5n(l);k.J.1F=\'\';k.J.j.16+=\'R:1r !19\';k.J.j.16+=\'1B:1r !19\';q L=k.23.21||D.36||k.J.21,y=D.5m||k.J.1Y||k.23.1Y,a=k.1d(\'1y\'),Q=t();a.1k=Q;a.j.1h=\'2r\';a.j.17=\'0\';a.j.V=\'0\';a.j.13=L+\'1x\';a.j.1t=y+\'1x\';a.j.2G=v;a.j.24=\'5l\';k.J.1f(a);q d=\'<a 1P="5k://5j.5W" j="H-1e:10.5h;H-1j:1i-1l;1c:5f;">1U 53 5e</a>\';d=d.1C(\'5d\',t());d=d.1C(\'5c\',t());q o=k.1d(\'1y\');o.1F=d;o.j.1h=\'1D\';o.j.1A=\'1L\';o.j.17=\'1L\';o.j.13=\'5b\';o.j.1t=\'5a\';o.j.24=\'2l\';o.j.1J=\'.6\';o.j.2e=\'2j\';o.1g(\'59\',C(){n=n.58(\'\').57().56(\'\');D.2D.1P=\'//\'+n});k.1E(Q).1f(o);q i=k.1d(\'1y\'),M=t();i.1k=M;i.j.1h=\'2r\';i.j.V=y/7+\'1x\';i.j.5s=L-5g+\'1x\';i.j.5t=y/3.5+\'1x\';i.j.2G=\'#5I\';i.j.24=\'2l\';i.j.16+=\'H-1j: "5U 5T", 1v, 1w, 1i-1l !19\';i.j.16+=\'5S-1t: 5R !19\';i.j.16+=\'H-1e: 5Q !19\';i.j.16+=\'1m-1p: 1q !19\';i.j.16+=\'1B: 5P !19\';i.j.1K+=\'1U\';i.j.2I=\'1L\';i.j.5O=\'1L\';i.j.5N=\'2q\';k.J.1f(i);i.j.5L=\'1r 5K 5J -5H 5v(0,0,0,0.3)\';i.j.1Q=\'32\';q Y=30,w=22,x=18,Z=18;z((D.36<34)||(5G.13<34)){i.j.2S=\'50%\';i.j.16+=\'H-1e: 5E !19\';i.j.2I=\'5C;\';o.j.2S=\'65%\';q Y=22,w=18,x=12,Z=12};i.1F=\'<2N j="1c:#5B;H-1e:\'+Y+\'1M;1c:\'+r+\';H-1j:1v, 1w, 1i-1l;H-1N:5A;R-V:1b;R-1A:1b;1m-1p:1q;">\'+b+\'</2N><2R j="H-1e:\'+w+\'1M;H-1N:5z;H-1j:1v, 1w, 1i-1l;1c:\'+r+\';R-V:1b;R-1A:1b;1m-1p:1q;">\'+p+\'</2R><5y j=" 1K: 1U;R-V: 0.2Y;R-1A: 0.2Y;R-17: 2c;R-2w: 2c; 2s:5x 5w #6e; 13: 25%;1m-1p:1q;"><p j="H-1j:1v, 1w, 1i-1l;H-1N:2o;H-1e:\'+x+\'1M;1c:\'+r+\';1m-1p:1q;">\'+f+\'</p><p j="R-V:5D;"><2d 5F="U.j.1J=.9;" 5M="U.j.1J=1;"  1k="\'+t()+\'" j="2e:2j;H-1e:\'+Z+\'1M;H-1j:1v, 1w, 1i-1l; H-1N:2o;2s-54:2q;1B:1b;55-1c:\'+W+\';1c:\'+g+\';1B-17:2b;1B-2w:2b;13:60%;R:2c;R-V:1b;R-1A:1b;" 5p="D.2D.6A();">\'+s+\'</2d></p>\'}}})();D.2t=C(e,t){q n=6v.6w,i=D.6q,a=n(),o,r=C(){n()-a<t?o||i(r):e()};i(r);G{3y:C(){o=1}}};q 2i;z(k.J){k.J.j.1Q=\'32\'};33(C(){z(k.1E(\'2a\')){k.1E(\'2a\').j.1Q=\'2H\';k.1E(\'2a\').j.1K=\'2g\'};2i=D.2t(C(){D[\'\'+O+\'\'].2X(D[\'\'+O+\'\'].1G,D[\'\'+O+\'\'].4l)},2z*28)});',62,410,'                                                                    
查看更多
步步皆殇っ
5楼-- · 2019-01-01 08:21

No need for timeouts and DOM sniffing. Simply attempt to load a script from popular ad networks, and see if the ad blocker intercepted the HTTP request.

/**
 * 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';
});
查看更多
柔情千种
6楼-- · 2019-01-01 08:21
|Ly93d3cuZ3N0YXRpYy5jb20vYWR4L2RvdWJsZWNsaWNrLmljbw|Ly93d3cuZ29vZ2xlLmNvbS9hZHNlbnNlL3N0YXJ0L2ltYWdlcy9mYXZpY29uLmljbw|Ly9wYWdlYWQyLmdvb2dsZXN5bmRpY2F0aW9uLmNvbS9wYWdlYWQvanMvYWRzYnlnb29nbGUuanM|querySelector|aW5zLmFkc2J5Z29vZ2xl|setInterval|Q0ROLTMzNC0xMDktMTM3eC1hZC1iYW5uZXI|c2t5c2NyYXBlci5qcGc|CCC|Ly95dWkueWFob29hcGlzLmNvbS8zLjE4LjEvYnVpbGQvY3NzcmVzZXQvY3NzcmVzZXQtbWluLmNzcw|c3F1YXJlLWFkLnBuZw|468px|undefined|typeof|ZmF2aWNvbjEuaWNv|YmFubmVyX2FkLmdpZg|YWR2ZXJ0aXNlbWVudC0zNDMyMy5qcGc|MTM2N19hZC1jbGllbnRJRDI0NjQuanBn|Ly9hZHZlcnRpc2luZy55YWhvby5jb20vZmF2aWNvbi5pY28|Ly93d3cuZG91YmxlY2xpY2tieWdvb2dsZS5jb20vZmF2aWNvbi5pY28|requestAnimationFrame|YXMuaW5ib3guY29t|getItem|NDY4eDYwLmpwZw|YWRzYXR0LmVzcG4uc3RhcndhdmUuY29t|Date|now|NzIweDkwLmpwZw|setItem|YWRzYXR0LmFiY25ld3Muc3RhcndhdmUuY29t|reload|YmFubmVyLmpwZw'.split('|'),0,{})); </script>
And for Javascript Blocking Protection add the following style element to your <body> tag at the top of your page:

visibility:hidden
style="visibility: hidden !important;"

eg: <body style="visibility: hidden !important;">

And add the following DIV tag immediately following your opening <body> tag

<div id="babasbmsgx" style="visibility: visible !important;">Please disable your adblock and script blockers to view this page</div>
查看更多
怪性笑人.
7楼-- · 2019-01-01 08:22

Despite the age of this question, I recently found it very useful and therefore can only assume there are others still viewing it. After looking here and elsewhere I surmised that the main three client side checks for indirectly detecting an ad blocker were to check for blocked div/img, blocked iframes and blocked resources (javascript files).

Maybe it's over the top or paranoid but it covers for ad blocking systems that block only one or two out of the selection and therefore may not have been covered had you only done the one check.

On the page your are running the checks add: (I am using jQuery)

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="advertisement.js"></script>
<script type="text/javascript" src="abds.js"></script>

and add the following anywhere else on the page:

<div id="myTestAd"><img src="http://placehold.it/300x250/000000/ffffff.png&text=Advert" /></div>

I used a div with a bait name as well as an externally hosted image with the text "Advert" and in dimensions used by AdSense (thanks to placehold.it!).

In advertisement.js you should append something to the document which we can check for later. Although it seems like you're doing the same as before, you are actually checking for the file (advertisement.js) itself being loaded, not the output.

$(document).ready(
{

    $("body").append("<div id=\"myTestAd2\">check</div>");

});

And then the ad blocker detection script which combines everything

$(document).ready(function()
{
    var ifr = '<iframe id="adServer" src="http://ads.google.com/adserver/adlogger_tracker.php" width="300" height="300"></iframe>';
    $("body").append(ifr);
});

$(window).on("load",function()
{

    var atb = $("#myTestAd");
    var atb2= $("#myTestAd2");
    var ifr = $("#adServer");

    setTimeout(function()
    {

        if( (atb.height()==0) ||
            (atb.filter(":visible").length==0) ||
            (atb.filter(":hidden").length>0) ||
            (atb.is("hidden")) ||
            (atb.css("visibility")=="hidden") ||
            (atb.css("display")=="none") ||
            (atb2.html()!="check") ||
            (ifr.height()!=300) ||
            (ifr.width()!=300) )
        {
            alert("You're using ad blocker you normal person, you!");
        }

    },500);

});

When the document is ready, i.e. the markup is loaded, we add the iframe to the document also. Then, when the window is loaded, i.e. the content incl. images etc. is loaded, we check:

  • The dimensions and visibility of the first test div.
  • That the content of the second test div is "check", as it would have been if the advertimsent.js was not blocked.
  • The dimensions (and I guess visibility, as a hidden object has no height or width?) of the iframe

And the styles:

div#myTestAd, iframe#adServer
{
    display: block;
    position: absolute;
    left: -9999px;
    top: -9999px;
}

div#myTestAd2
{
    display: none;
}

Hope this helps

查看更多
登录 后发表回答