I try to keep my Firefox cookie file clean. I used to run a script on
cookies.txt to remove most cookies and keep only the ones for sites I visit often and trust. This was simple when the cookie file format was plain text. However, Firefox has been moving files to the sqlite format, and my script no longer works.
Sqlite seems to be pretty nice. The first thing I needed to do was figure out what format
cookies.sqlite used. I ran
select * from sqlite_master using the command line interface and it told me there was a table named
(id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER) as columns. Pretty straightforward. I used this information, plus the Python
sqlite3 module (standard with Python 2.5) to write a script to clean up my Firefox cookies:
#!/usr/bin/python # Change this to where your cookies file is COOKIE_FILE = ('/Users/amitp/Library/Application Support' '/Firefox/Profiles/
foobar/cookies.sqlite') HOSTS = [ # keep only cookies from these hosts u'.blogger.com', u'www.blogger.com', u'.delicious.com', u'.discus.com', u'discus.com', u'blobs.discus.com', u'.github.com', # … other hosts omitted in this example code ] # Connect and then issue commands # through the “cursor” object import sqlite3 connection = sqlite3.connect(COOKIE_FILE) cursor = connection.cursor() # Remove Google Analytics cookies from all sites cursor.execute('DELETE FROM moz_cookies WHERE name IN' ' ("__utma", "__utmb", "__utmc", "__utmz")') # Remove any cookies from non-approved hosts cursor.execute( 'DELETE FROM moz_cookies WHERE host NOT IN (%s)' % (','.join(len(HOSTS) * ['?'])), HOSTS) # Commit changes to disk, # locking the file during the process connection.commit() # Print the cookies we have left print 'After:' rows = list(cursor.execute( 'SELECT host, name FROM moz_cookies')) for row in rows: print '%30s : %s' % row
The script opens the
cookies.sqlite file (unlike
cookies.txt, it appears to be safe to open and edit this file while Firefox is running!), removes most cookies, commits the changes, and prints out the remaining cookies.
Lots of the Firefox profile information has been moved into the sqlite format (instead of the mess we had before), so I should explore some of the other files to see what might be fun to play with.