DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Filterset Updater For Ghostsaw's Adlbock For Opera

11.14.2009
| 2890 views |
  • submit to reddit
        There is no adblock feature implemented in Opera as fully functional as <a href="https://addons.mozilla.org/firefox/addon/1865" target="_blank">Adblock Plus for Firefox</a>, but a guy called <a href="http://my.opera.com/Ghostshaw/" target="_blank">Ghostsaw created a javascript plug-in</a> that does almost the same job, less efficiently though if performance matters. I wrote an updater script which takes <b>aaaa-customlist.js</b> and updates it. It was written in Perl, but if you're using Opera on Windows instead of an UNIX-based operating system it can easily be compiled using <a href="http://www.activestate.com/activeperl/" target="_blank">ActivePerl</a> and <a href="http://www.indigostar.com/perl2exe.htm" target="_blank">Perl2Exe</a>. You can easily add this to cron (or scheduler in Windows) and your adlbock list will always be up-to-date.
Don't forget to create two comments one below the other:

// FSGUPDATE BEGIN
                           <-- leave empty between
// FSGUPDATE START

#!/usr/bin/perl

use strict;
use warnings;

use LWP;
use Getopt::Std;
use vars qw( $ua $res %o );

$ua = new LWP::UserAgent(
	agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
);

$| = 1;
getopts("o:", \%o);

if(defined $o{p}) {
    print "HTTP Proxy: $o{p}\n";
    $o{p} = "http://" . $o{p} unless($o{p} =~ /^http:\/\//);
    $ua->proxy('http', $o{p});
}

my $adblock = shift;
my $filterset = shift;
my $copy = 1;
my (@black, @white);

unless(defined $filterset) {
    print("usage: $0 [-p http-proxy] <adblock-list-url> <filterset>\n");
    exit(0);
}

my $tmp;

print "fetching $adblock... ";
$res = $ua->get($adblock);
print $res->status_line . "\n";
exit(1) unless($res->is_success);

foreach(split(/[\r\n]+/, $res->content)) {
    if(/\[\s*(.*?adblock.*?)\s*\]/i) {
	print "found ABP header: $1\n";
    } elsif(/checksum: (.+)/i) {
	print "found checksum $1 (skipping check)\n";
    } else {
	if(/^\@\@\|?\/?(.+)\/?$/) {
	    $tmp = $1;
	    $tmp =~ s/\*/\.\*/g;
	    $tmp =~ s/\//\\\//g;
	    push(@white,"(/$tmp/)");
	} elsif(/^\|?\/?(.+)\/?$/) {
	    $tmp = $1;
	    $tmp =~ s/\*/\.\*/g;
	    $tmp =~ s/\//\\\//g;
	    push(@black,"(/$tmp/)");
	}
    }
}

print "updating filterset... ";
unless(open(FILTERSET, "<$filterset")) {
    print "failed: $!\n";
    exit(1);
}
my @filterset = <FILTERSET>;
close(FILTERSET);

unless(open(FILTERSET, ">$filterset")) {
    print "failed: $!\n";
    exit(1);
}

for(@filterset) {
    if(/^\/\/ FSGUPDATE BEGIN/) {
	print FILTERSET;
	print FILTERSET "ujs_white_array.push(\"" . join("|", @white) . "\");\n";
	print FILTERSET "ujs_black_array.push(\"" . join("|", @black) . "\");\n";
	$copy = 0;
    } elsif(/^\/\/ FSGUPDATE END/) {
	print FILTERSET;
	$copy = 1;
    } elsif($copy) {
	print FILTERSET;
    }
}