randomfox (randomfox) wrote,
randomfox
randomfox

Perl script to convert an Adblock filter to a PAC filter (updated 3/25/2005)

The following script takes an Adblock filter from the FilterSet.G archive and converts it to a PAC file. The PAC file is for use with K-Meleon, which does not come with an Adblock filter module.

Added a filter toggle based on John's no-Ads PAC script.

To disable the filter, visit this URL: http://no-ads.int/off
To re-enable the filter, visit this URL: http://no-ads.int/on

The whitelist is currently hardcoded into the script. It may be read in from a file in a future enhancement.

The blackhole address may not work on all systems. If it fails, PROXY 127.0.0.1:3421 may work as an alternative.


#!perl -w
use strict;

# Converts Adblock filter to a PAC file.
# Usage: adb2pac.pl < adblock > adfilter.pac

# Filter toggle based on John's no-Ads PAC script
# http://www.schooner.com/~loverso/no-ads/
# To disable, visit this URL:		http://no-ads.int/off
# To re-enable, visit this URL:		http://no-ads.int/on

 
my @regexps;

my $normal = "DIRECT";
my $blackhole = "PROXY 0.0.0.0:3421";

while (<>) {
    chomp;
    next if /^\[Adblock\]/;
    next if /^!/;
    next if /^$/;

    if (/^\/(.*)\/$/) {
	my $regex = $1;
	$regex =~ s/\//\\\//g;
	push @regexps, "/$regex/i";
    }
    else {
	s/\./\\\./g;
	s/\//\\\//g;
	push @regexps, "/$_/i";
    }
}

my $vimcmd = "vim:set ft=javascript tw=0:";

print <<EOM;
var regexps = [
@{[ join(",\n", @regexps) ]}
];

var whitelist = [
/geocaching\\.com/i,
/groundspeak\\.com/i,
/wheresgeorge\\.com/i,
/schooner\\.com/i
];

var isActive = 1;

var adFilterOn = /no-ads.int\\\/on/i;
var adFilterOff = /no-ads.int\\\/off/i;

function FindProxyForURL(url, host) {
    if (adFilterOn.test(url)) {
	isActive = 1;
	return "$blackhole";
    }
    else if (adFilterOff.test(url)) {
	isActive = 0;
	return "$blackhole";
    }

    if (!isActive) {
	return "$normal";
    }

    for (var i = 0; i < whitelist.length; i++) {
	if (whitelist[i].test(url))
	    return "$normal";
    }

    for (var i = 0; i < regexps.length; i++) {
	if (regexps[i].test(url))
	    return "$blackhole";
    }

    return "$normal";
}

// $vimcmd
EOM

__END__

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 6 comments