Skip to content

Instantly share code, notes, and snippets.

@bowsersenior
Created August 25, 2012 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bowsersenior/b623791abc0b3e2dca00 to your computer and use it in GitHub Desktop.
Save bowsersenior/b623791abc0b3e2dca00 to your computer and use it in GitHub Desktop.
/*
* adapted from: http://ostermiller.org/bookmarklets/clearcookies.js
*
* Copyright (C) 2004 Stephen Ostermiller
* http://ostermiller.org/contact.pl?regarding=Bookmarklets
*
* This bookmarklet is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This bookmarklet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* Clears all cookies for the current page.
*
* @name Clear Cookies
*
* @substitute cookie_list a
* @substitute sub_domain b
* @substitute location_pathname c
* @substitute document_cookie d
* @substitute cookie_index e
* @substitute cookie_count f
*
* @compatible Firefox
* @compatible Netscape
* @compatible Opera
* @compatible Internet Explorer
* @compatible Mozilla
* @notcompatible Konqueror
* @compatible Safari
*
* @category Cookies
* @category Utilities
*
* @keyword clear cookies bookmarklet
* @keyword delete cookies bookmarklet
* @keyword remove cookies bookmarklet
* @keyword expire cookies bookmarklet
*/
void(
(function(){
var cookie_list,sub_domain,location_pathname,cookie_index,cookie_count;
cookie_count=0;
// Get the semicolon separated list of cookies and make an array out of them
cookie_list=document.cookie.split("; ");
// Go through each cookie and delete it. The &&cooki_list[cookie_index] check is there
// for the case in which there are no cookies and the split returns the empty string.
for(cookie_index=0;cookie_index<cookie_list.length&&cookie_list[cookie_index];cookie_index++){
cookie_count++;
// Each cookie needs to be expired on all possible domains. For example:
// .subdomain.example.com
// subdomain.example.com
// .example.com
// example.com
// .com
// com
for(sub_domain="."+location.host;sub_domain;sub_domain=sub_domain.replace(/^(?:\.|[^\.]+)/,"")){
// Each cookie needs to be expired on all possible paths.
// Take one character at a time off the path.
for(location_pathname=location.pathname;location_pathname;location_pathname=location_pathname.replace(/.$/,"")){
// set an expired cookie to delete the cookie
// The new cookie will have the same name and value as the old cookie,
// but will be deleted immediately by the browser's cookie management
document.cookie=(
cookie_list[cookie_index]+
"; domain="+
sub_domain+
"; path="+
location_pathname+
"; expires="+
// Take a couple years off of today's date
new Date((new Date()).getTime()-1e11).toGMTString()
);
}
}
}
var msg = "Expired "+cookie_count+" cookies";
window.Notification && Notification.requestPermission(function(){new Notification(msg)});
})()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment