Hi!
I'm new on PerlMonks. I subscribe because I had a problem with a code and I have not found it until... now :-) So this a small script that use sqlite3 command to filter cookies for Chromium.
What's the point? Don't overload the browser with things that can be done only one time (at the end or start of the programs)
This is the code, it only uses base functions of perl (don't forget to make a copy of your ~/.config/chromium/Cookies before using it):
#!/usr/bin/perl use warnings; use strict; die "Usage: $0 <list|apply>\n" unless @ARGV; my @hosts_granted; my @hosts_denied; open HOSTS,"$ENV{HOME}/.config/chromium/Default/cookies_accept.txt" an +d do { chop $_ and push @hosts_granted,$_ while <HOSTS>; close HOSTS; }; open HOSTS,"$ENV{HOME}/.config/chromium/Default/cookies_reject.txt" an +d do { chop $_ and push @hosts_denied,$_ while <HOSTS>; close HOSTS; }; ## Open SQLite open OLD,"sqlite3 $ENV{HOME}/.config/chromium/Default/Cookies .dump|" +or die $!; if( $ARGV[0] eq "apply" ) { @hosts_granted=(".") if @hosts_denied; die "No filter list!" unless @hosts_granted or @hosts_denied; unlink "$ENV{HOME}/.config/chromium/Default/Cookies.new"; open NEW,"|sqlite3 $ENV{HOME}/.config/chromium/Default/Cookies.new +" or die $!; ## Parsing & filtering my $n=0; my $total=0; while( <OLD> ) { if( /^INSERT INTO "cookies" VALUES\(([^)]+)\)/ ) { my @cookie=split(",",$1); my $host=$cookie[1]; $host=~s/^'|'$//g; $total++; next unless grep { $host =~ /$_/ } @hosts_granted; next if grep { $host =~ /$_/ } @hosts_denied; $n++; } print NEW $_; } print NEW "\n.quit\n"; close NEW; rename "$ENV{HOME}/.config/chromium/Default/Cookies","$ENV{HOME}/. +config/chromium/Default/Cookies.bak"; rename "$ENV{HOME}/.config/chromium/Default/Cookies.new","$ENV{HOM +E}/.config/chromium/Default/Cookies"; print "$n/$total cookies kept\n" } elsif( $ARGV[0] eq "list" ) { while( <OLD> ) { if( /^INSERT INTO "cookies" VALUES\(([^)]+)\)/ ) { my @cookie=split(",",$1); my $host=$cookie[1]; $host=~s/^'|'$//g; print "$host\n"; } } } else { warn "Command `$ARGV[0]' unknown. Available commands: list, apply\ +n"; } close OLD;
Nice, isn't it? (".")
(Note: I really appreciate any advice)
_____________________________
It can be more useful... I like to see the weather in my units (metrics). So, the script has to be updated to match different fields of the cookies.
That's my new version with an exemple of config file:
#!/usr/bin/perl use warnings; use strict; die "Usage: $0 <list|apply>\n" unless @ARGV; my @granted; my @denied; open HOSTS,"$ENV{HOME}/.config/chromium/Default/cookies_accept.txt" an +d do { chop $_ and push @granted,$_ while <HOSTS>; close HOSTS; }; open HOSTS,"$ENV{HOME}/.config/chromium/Default/cookies_reject.txt" an +d do { chop $_ and push @denied,$_ while <HOSTS>; close HOSTS; }; ## Open SQLite open OLD,"sqlite3 $ENV{HOME}/.config/chromium/Default/Cookies .dump|" +or die $!; if( $ARGV[0] eq "apply" ) { die "No filter list!" unless @granted or @denied; unlink "$ENV{HOME}/.config/chromium/Default/Cookies.new"; open NEW,"|sqlite3 $ENV{HOME}/.config/chromium/Default/Cookies.new +" or die $!; ## Parsing & filtering my $n=0; my $total=0; my %keys; while( <OLD> ) { if( /^INSERT INTO "cookies" VALUES\((.+)\);$/ ) { my @cookie=split(",",$1); /^'(.+)'$/ and $_=$1 foreach @cookie; my $go_next=(@granted ? 1 : 0); foreach (@granted) { my $r=1; my $expr=$_; $expr=~s/\$(\w+)/\$cookie[\$keys{$1}]/g; if( eval $expr ) { $go_next=0; last; } } foreach (@denied) { my $r=1; my $expr=$_; $expr=~s/\$(\w+)/\$cookie[\$keys{$1}]/g; if( eval $expr ) { $go_next=1; last; } } $total++; next if $go_next; $n++; } elsif( ! %keys and /^CREATE TABLE cookies \((.+)\);/ ) { @_=map {$& if /^\w+/} split(",",$1); foreach my $i(0..$#_) { $keys{$_[$i]}=$i; } } print NEW $_; } print NEW "\n.quit\n"; close NEW; rename "$ENV{HOME}/.config/chromium/Default/Cookies","$ENV{HOME}/. +config/chromium/Default/Cookies.bak"; rename "$ENV{HOME}/.config/chromium/Default/Cookies.new","$ENV{HOM +E}/.config/chromium/Default/Cookies"; print "$n/$total cookies kept\n" } elsif( $ARGV[0] eq "list" ) { while( <OLD> ) { if( /^INSERT INTO "cookies" VALUES\((.+)\);$/ ) { print join("\t", map {m/^'(.+)'$/ and $1 or $_} split(",", +$1))."\n"; } elsif( /^CREATE TABLE cookies \((.+)\);/ ) { print join("\t", map {$& if /^\w+/} split(",",$1))."\n"; } } } else { warn "Command `$ARGV[0]' unknown. Available commands: list, apply\ +n"; } close OLD;
This is an example of ~/.config/chromium/Default/cookies_accept.txt:
$host_key=~/^www.perlmonks.org$/ $host_key=~/^localhost$/ $host_key=~/\.weather\.com$/ and $name eq "UserPreferences" #... #... #... #(each line is evaluated seperately)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Filtering cookies of Chromium outside Chromium itself
by afoken (Chancellor) on Jul 27, 2010 at 12:40 UTC | |
by jess.nc (Initiate) on Jul 27, 2010 at 18:40 UTC |