Perl script to convert an Adblock filter to a PAC filter
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.
Since the filter breaks on a number of websites, I've had to add a whitelist. Currently, the whitelist is hardcoded into the script. As a future enhancement, it should be read in from a file.
The blackhole address may not work on all systems. If it fails,
Since the filter breaks on a number of websites, I've had to add a whitelist. Currently, the whitelist is hardcoded into the script. As a future enhancement, it should be read in from a file.
The blackhole address may not work on all systems. If it fails,
PROXY 127.0.0.1:3421 may be used as an alternative.
#!perl -w
use strict;
# Converts Adblock filter to a PAC file.
# Usage: adb2pac.pl < adblock > adfilter.pac
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/";
}
else {
s/\./\\\./g;
s/\//\\\//g;
push @regexps, "/$_/";
}
}
my $vimcmd = "vim:set ft=javascript tw=0:";
print <<EOM;
var regexps = [
@{[ join(",\n", @regexps) ]}
];
var whitelist = [
/geocaching\\.com/,
/groundspeak\\.com/,
/wheresgeorge\\.com/
];
function FindProxyForURL(url, host) {
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__