Generating cookies.txt in Firefox
The browser team at Mozilla have been working their buts off the last year in particular bringing what I would generally regarded as a better browsing experience. If you haven’t taken a look at Firefox in the last year or so (or you left Firefox because it was slow and Chrome was once upon a time the speedy, new, hotness everyone was after), I strongly recommend you give it another look.
As part of the work in modernizing Firefox, a major rewrite of the entire rendering and parsing system broke all legacy extensions. The replacement (web extensions) is probably a smarter way to go for compatibility and robustness, but a number of smaller unmaintained extensions have been left in the dust. The multitude of extensions to produce a cookies.txt file are what concern me in particular. The cook
When legacy extensions finally bit the dust sometime last year, I quickly wrote up a little bit of python to replicate the functionality on Linux systems. I was willing to let this little nugget of Python live in my personal bucket of tools until I started seeing a few bug reports for tools like youtube-dl where developers were complaining about being unable to generate a cookies.txt file. The particular value for these developers is extracting the Cloudflare DDOS prevention cookies for scripts and tools that may need them.
Basic Overview
The script (available here, and further down the page) is pretty straightforward. We do some arbitrary parsing to search for a particular Firefox profile in the .mozilla/firefox
folder of a user’s home directory. This is where your profiles (for most people only one) reside. A simple wildcard glob selects all profiles that end with the name default, containing a cookies sqlite database. Provided everything checks out, all cookies matching a few particular hosts are extracted and dumped to a cookies.txt in the directory the script is executed in.
While it isn’t extremely pretty, and it could readily be expanded with a proper getopt front end to ease selecting profiles and such, it’s structurally sound enough to tinker for anyone impatiently Googling to figure out “HOW DO I GENERATE COOKIES.TXT” without getting hung up on the details.
Enjoy.