How to block Adblock

I totally abhor all the recent ranting on how Adblock is going to destroy the online economy so I’ve written them some code. They can be obnoxious about the way people view their sites and block them or they can get on with things. What I’ve written is technically DRM 2.0. Please don’t hate me.

Published 2007-09-03. Read 60,667 times. Comments. Tagged: drm javascript webdev

I totally abhor all the recent ranting on how Adblock is going to destroy the online economy. People left right and centre have been whining on and on and on about how these freeloaders t’k ouwr jawbs rob the pants off any website owner who stands still long enough.

I’m yet to see one post from somebody that sounds like they know how web pages work, let alone how their adverts work. Perhaps it’s because the things I read are written by rednecks who want to ban Firefox completely. At least he was hacked. Karma’s a bitch, aint it?!

No matter — I’m here and I DO know how web pages work. I even know how advert code works. To top it off, I’ve got a fairly damned good idea on how Adblock works. I’m no expert in each area but I’d like to say I know what I’m on about especially when I say: if content providers really wanted to stop people blocking adverts, they could.

It’s so comically easy to block Adblock users, a four year old chimpanzee could do it.

Before I walk you through the code and how it all works I’ll explain why I’m writing this. I’m a Firefox and Adblock user. The two combined have made my online life joyous. I can read content without being distracted every two seconds by irrelevant adverts for things I wouldn’t buy. If I want to support a site for what it’s doing, I donate money or time to them.

I’m also a content provider. I do stick ads on some of my pages but I’m not out to rape every visitor in their face. I don’t mind if people block my adverts. It’s their connection. Their experience. As long as they get the most out of it, I’m happy. Anyway. Let’s get to some code.

Blocking Adblock

We start off with our advert code. Most designers nest their code in a positioning element like a <div>. Have a butchers at the following code to see what I’m on about:

<div class="myTestAd" style=" text-align:center;margin:10px">
	<!-- advert code goes here -->
</div>

In place of the comment there is advert code. This is usually a JavaScript link off to the advertising agency’s website. In the case of Google AdSense, it’s a link off to http://pagead2.googlesyndication.com/pagead/show_ads.js. This script generates the HTML to be displayed on the page inside an iframe. In layman’s‘ terms, an iframe is effectively a web page nested inside a page. This allows Google, in this case, to display their advert to the end user.

So what ends up being rendered is an iframe inside our div. If Adblock is introduced into the equation, the following rule nukes the advert: #*(src*=http://pagead2.)

This is a CSS-selector-based rule that hides any output whose source points to any address beginning with http://pagead2. so the iframe whose source URL does begin with that, is hidden. Not blocked. The JavaScript code still runs but the output is hidden.

Therefore using another chunk of JavaScript at page-level, we can quickly see if the advert has been blocked with this rule. How? We just query the height of the output iframe. If it equals zero, it has been mushed otherwise everything is fine. To do this check we need to call upon the mystic powers of the Document-Object Model but because I’m lazy like that, I’m using the very excellent jQuery library to do all the leg-work. Here is all the JavaScript we need to detect and alert the user they’re blocking adverts:

function TestPage() {
    if ($('.myTestAd').height() == 0)
        alert("You are blocking my beautiful adverts, you swine!");
}

$(TestPage);

I meant it. A chimp could come up with that. Check out the test page

This version just pops up a message box, calls the user a swine and leaves them be but if people wanted to get really obnoxious about their adverts, they could redirect them, blocking them outright.

Can’t you just block Adblock-blockers?

Of course.

Just as Adblock can detect the advert code, there could easily be code to detect Adblock-blocking code. I fear if we ever get into that cat-and-mouse game, we’re in for a bumpy ride where, ultimately, nobody wins. Agencies will update their code, a million open source people will update the Adblock definitions — ad infinitum.

But it should be important for content providers to recognise now that if their content is on the open internet, it is public. People are and should be allowed to do what they like with the content for personal use. You don’t get to overrule my decision to chose what my eyes see. If you try, I’ll take my eyes elsewhere.

So isn’t this DRM 2.0?

You could argue that placing checks on pages and preventing people who block advertising is managing what the user is allowed to do with the content (rights management). This is not something I believe is healthy for any medium, least of all the web. Restricting use restricts quality and usefulness.

So say it is DRM, and I implement this on a website, redirecting people to a block page, telling them to disable their advert blocker, does that mean my media, my webpage is protected by the infamous Digital Millennium Copyright Act (DMCA)? According to the DMCA it is illegal to provide tools that circumvent DRM — therefore making Adblock-blocker-blockers illegal.

Let’s piss all over that fair-use clause one more time. America truly is a scary place at times…

The moral arguments

Over the two-and-a-half years this post has been live, there has been a range of comments from people. Most of them revolve around an outrage that people believe they should only see what they want to see. When I wrote this post, I was probably more in their camp than I am now.

You have to understand that producing content takes time. I mostly write for myself but the AdSense payouts are a nice bonus. If you’re a company with shareholders (as plenty of the major content distributors are), your drive is somewhat different to mine: you have to turn a profit, or at least attempt to. For them generating content costs real money. They pay staff and their hosting infrastructure does often have to be somewhat more resilient to masses of people.

I do think it probably comes down to a numbers game. Comparing my Google AdSense and Google Analytics statistics, I can see that roughly 10% of people viewing the site use AdBlock (I’m excluded from the numbers). To me, that doesn’t make too much difference. But if I were earning £100k per month and spending £20k/month sustaining that, I might miss that extra £10k revenue. It’s not a small amount of money. Scale it up and you’re soon talking about the difference between hiring people and letting them go.

And if that 10% were to rise to 20% or 30%, even I might start to take issue… And I might even do something about it.

Plenty of commenters have said that it’s their browser and that’s fair enough… But it’s my content. I could argue that your license to use it is dependant on you viewing adverts. As I’ve said, I do write for myself so technically speaking, you need my content more than I need you to read it.

But this amendment doesn’t come without a dose of hypocrisy. I am still an AdBlock user. And no, I don’t have all the answers. Good night.

About Oli: I’m a Django and Python programmer, occasional designer, Ubuntu member, Ask Ubuntu moderator and technical blogger. I occasionally like to rant about subjects I should probably learn more about but I usually mean well.

Stay updated: Subscribe by RSS or Subscribe by Email.