First chrome extension…

I’ve hacked together a quick [well, it took me all bloody afternoon, but it’s my first go] extension which does something pretty simple: when the button on the toolbar is pressed, it loops through all of the cookies for the domain [for the current tab], deletes them all and reloads the page.

This pointed me in the right direction, but I couldn’t figure out how [or for that matter why] to use the background.js to implement the delete function, so it’s called directly from the standard popup.js.

There is one part of it which is not very reliable:

var domain = new URL(tab.url).hostname;
// Almost certainly unreliable!
var domainNoDubs = domain.match(/.*?([\w]+.[\w]+)$/)[1];

…which is basically an attempt to convert a domain www.site.com into site.com. There is a bunch of stuff that can go wrong here and my reliance on the ‘1th’ item in the array returned is pretty flaky. But, having tested it exhaustively on 3 different sites [!], it works. The reason I need to do this is because this call..

chrome.cookies.getAll({domain: domainNoDubs}, function(cookies) {...

…requires the passing in of a domain prefixed with a dot. Simply replacing ‘www‘ with nothing isn’t good enough. What this parameter actually needs to be is the dot-prefixed second level domain – or the highest part of it if it has something in front of it. So for www.this.site.com, it would actually need to be .site.com.

As I say, it works. More or less… Source is here.