Instantly share code, notes, and snippets.
Created
January 16, 2016 05:39
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save billthompson/be3eccdca87012ac9ec0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make working with cookies in the console better. | |
var Cookie = {}; | |
// Create a new Cookie | |
Cookie.create = function (name, value, days) { | |
var expires, | |
domainPieces, | |
cookieDomain = '', | |
date = new Date(); | |
days = days || 7300; | |
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
expires = "; expires=" + date.toUTCString(); | |
// Try to set the cookie at '.domain.tld'. If unable to parse the and build this correctly, will default to | |
// setting on the current subdomain/domain. | |
try { | |
// cookieDomain for headless/localhost should be '', NULL or FALSE | |
if(document.domain !== '' && document.domain !== 'localhost') { | |
domainPieces = document.domain.split('.'); | |
// Pretty sure we aren't going to error out now so we change cookieDomain so we can push to it. | |
cookieDomain = []; | |
// Push the last 2 pieces of domainPieces to the cookieDomain | |
for(var i = domainPieces.length - 1; i >= domainPieces.length - 2; i--) { | |
cookieDomain.push(domainPieces[i]); | |
} | |
// Makes it so we can just join on '.' after reversing | |
cookieDomain.push(''); | |
cookieDomain = [';', 'domain=', cookieDomain.reverse().join('.')].join(''); | |
} | |
} catch(e) { } | |
// Set the cookie | |
document.cookie = name + "=" + value + expires + cookieDomain + "; path=/"; | |
return true; | |
}; | |
// Append to the tail of an existing cookie | |
Cookie.append = function (existingCookieName, additionalValue) { | |
var currentValue = this.get(existingCookieName); | |
if (currentValue){ | |
// Append the current value | |
return this.create(existingCookieName, currentValue + encodeURIComponent('|') + additionalValue); | |
} | |
else { | |
// No current value | |
return null; | |
} | |
}; | |
// Update/replace a cookie | |
Cookie.update = function (existingCookieName, newValue) { | |
return this.create(existingCookieName, newValue); | |
}; | |
// Get the decoded value of a specific cookie | |
Cookie.getPretty = function(name) { | |
return decodeURIComponent(this.get(name)); | |
}; | |
// Get a specific value from a large cookie compromised of multiple pairs | |
Cookie.getSpecificValueFromCookie = function (existingCookie, key, delimiter) { | |
var cookieAsObject; | |
delimiter = delimiter || '|'; | |
try { | |
cookieAsObject = this.cookieValueToObject(existingCookie, delimiter); | |
if (cookieAsObject.hasOwnProperty(key)) { | |
return cookieAsObject[key]; | |
} | |
else { | |
return null; | |
} | |
} | |
catch (err) { | |
return null; | |
} | |
}; | |
Cookie.expire = function(name) { | |
document.cookie = name + '=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | |
}; | |
// Get a specific cookie's entire raw value (as it's stored in the browser) | |
Cookie.get = function (name) { | |
var nameEQ = name + "=", | |
allCookies = document.cookie.split(';'), | |
currentCookie, | |
i; | |
for (i = 0; i < allCookies.length; i += 1) { | |
currentCookie = allCookies[i]; | |
while (currentCookie.charAt(0) === ' ') { | |
currentCookie = currentCookie.substring(1, currentCookie.length); | |
} | |
if (currentCookie.indexOf(nameEQ) === 0) { | |
// Get the cookie value | |
return currentCookie.substring(nameEQ.length, currentCookie.length); | |
} | |
} | |
// Cookie not found. Return false so this function can be called in a control structure | |
return false; | |
}; | |
// Convert a cookie value containing multiple pairs to an object | |
Cookie.cookieValueToObject = function (cookieName, delimiter) { | |
var obj = {}, | |
cookieValues, | |
i, | |
key, | |
value; | |
delimiter = delimiter || '|'; | |
cookieValues = this.getPretty(cookieName).split(delimiter); | |
for (i = cookieValues.length -1; i >= 0; i -= 1 ) { | |
key = cookieValues[i].split('=')[0]; | |
value = cookieValues[i].split('=')[1]; | |
obj[key] = value; | |
} | |
return obj; | |
}; | |
//Convert a cookie value containing multiple elements of information into an Array | |
Cookie.cookieValueToArray = function (cookieName, delimiter) { | |
delimiter = delimiter || '|'; | |
return [].concat(this.getPretty(cookieName).split(delimiter)); | |
}; | |
// Helper for the cookie/object functions above as we don't always know what the property name is | |
Cookie.findPropertyByRegex = function (object, pattern) { | |
var key; | |
for (key in object) { | |
if (object.hasOwnProperty(key)) { | |
if (key.match(pattern)) { | |
// Return the actual key | |
return key; | |
} | |
} | |
} | |
// Could not be found | |
return null; | |
}; | |
/** | |
* Add a CookieManager to make storing/saving multiple cookies easier. | |
* | |
* @constructor | |
* @property {Object} list | |
*/ | |
Cookie.Manager = function() { | |
this.list = {}; | |
}; | |
Cookie.Manager.prototype.push = function(key, value) { | |
this.list[key] = value; | |
}; | |
/** | |
* Iterates over all cookies in CookieManager.list and create/saves them. JSON.stringifies the cookie value. | |
*/ | |
Cookie.Manager.prototype.save = function() { | |
for (var cookie in this.list) { | |
if (this.list.hasOwnProperty(cookie)) { | |
try { | |
Cookie.create(cookie, JSON.stringify(this.list[cookie])); | |
} catch(e) {} | |
} | |
} | |
}; | |
window.Cookie = Cookie; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment