Monday, December 12, 2005

Ad blocking on the cheap

This is an article I wrote a while ago in my SlashDot journal. But since I rarely visit SlashDot anymore, and the article is still just as relevant, I'm posting it here.

One way to block web-ads without buying any expensive software is to use the proxy-autoconfig file that most web browsers support. This file is a JavaScript script that every URL gets filtered through. The script returns the address of a proxy server for handling the URL or "DIRECT" if the URL should not be proxied.

You can use this for ad blocking. Have the script check the domain (or the whole URL, if you feel the need to get that specific) for an ad server and send the hits to a local web server on your LAN. Send the rest directly to the internet.

The web server should not be configured to act as a proxy. When the ads come through, that web server will return "404 not found" errors for them all. The ad ends up displaying as a broken-image icon or some other kind of error. This will block all kinds of ads, including the really annoying Flash ads that are becoming so popular these days.

Name the script something with a ".pac" (for Proxy Auto-Config) extension and configure your web browser to use it as part of its proxy configuration.

My script (including a small subset of the blocked advertising domains) is:

function FindProxyForURL(url, host)
{
    // Blackhole these specific domains by sending
    // them to a web server that's not equipped to
    // proxy anything.
    //
    if (dnsDomainIs(host, ".247media.com") ||
        dnsDomainIs(host, ".accendo.com") ||
        dnsDomainIs(host, ".ad-flow.com") ||
        dnsDomainIs(host, ".adflight.com") ||
        dnsDomainIs(host, ".admonitor.net") ||
            // Insert as many of your own as you like.
            // I've got over 100 in my own script
        dnsDomainIs(host, "ads.x10.com") ||
        dnsDomainIs(host, "ads1.zdnet.com") ||
        dnsDomainIs(host, "ads2.zdnet.com") ||
        dnsDomainIs(host, "ads3.zdnet.com"))
    {
        // Replace the following with the address of
        // your own web server
        //
        return "PROXY 192.168.1.5";
    }

    return "DIRECT";
}

No comments: