Skip to content

Instantly share code, notes, and snippets.

@msure
Last active December 4, 2015 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msure/ebbf3f04b137ee6bfd80 to your computer and use it in GitHub Desktop.
Save msure/ebbf3f04b137ee6bfd80 to your computer and use it in GitHub Desktop.
Examine cookie names/values for a website
function getCookies(cooksName) {
var cooks = document.cookie.split(';')
var cooksInTheKitchen = {}
cooks.forEach(function(i) {
var name = unescape(i.match(/^\s*([^=]+)/m)[1])
var value = unescape(i.match(/=(.*$)/m)[1])
if (cooksInTheKitchen[name] !== undefined) {
console.log("Trying to retrieve cookie named(" + name + "). There appears to be another property with this name though.");
}
cooksInTheKitchen[name] = value
});
if (cooksName !== undefined && cooksName !== '') {
var filtered = {}
for(var key in cooksInTheKitchen) {
if(key === cooksName){
filtered[key] = cooksInTheKitchen[key];
}
}
return filtered;
} else {
return cooksInTheKitchen;
}
};
// after entering function into console, call this anytime you need to see updates to the cookies
console.dir(getCookies());
// if you want to get the values of a particular cookie, send the optional parameter as a string
getGaCookie = getCookies('_ga');
// in most modern browsers you can also translate base64 encoded objects to a string or, as shown below, a javascript object
getEncodedCookie = getCookies('the_cookie');
unencodedCookie = JSON.parse(atob(getEncodedCookie['the_cookie']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment