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)

In reply to Filtering cookies of Chromium outside Chromium itself (v2) by jess.nc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.