Skip to content

Instantly share code, notes, and snippets.

@vly
Last active December 29, 2015 23:49
Show Gist options
  • Save vly/7745641 to your computer and use it in GitHub Desktop.
Save vly/7745641 to your computer and use it in GitHub Desktop.
cookie func
/**
* JavaScript BitArray - v0.2.0
*
* Licensed under the revised BSD License.
* Copyright 2010-2012 Bram Stein
* All rights reserved.
*/
var BitArray=function(e,t){this.length=e;this.buffer=new ArrayBuffer(Math.ceil(this.length/32)*4);this.wordArray=new Uint32Array(this.buffer);if(t){t=t.slice(/^0x/.exec(t)?2:0);if(t.length*4>this.length){throw"Hex value is too large for this bit array."}else if(t.length*4<this.length){while(t.length*4<this.length){t="0"+t}}for(var n=0;n<t.length/8;n++){var r=t.slice(n*8,n*8+8);this.wordArray[n]=parseInt(r,16)}}};BitArray.prototype.size=function(){return this.length};BitArray.prototype.set=function(e,t){if(arguments.length!==2){throw"Index and value are required arguments."}if(e>this.length-1){throw"Index too large."+e+" "+this.length}var n=Math.floor(e/32);var r=e-n*32;if(t){this.wordArray[n]|=1<<r}else{this.wordArray[n]&=~(1<<r)}return this};BitArray.prototype.toggle=function(e){if(e>this.length-1){throw"Index too large."}var t=Math.floor(e/32);var n=e-t*32;this.wordArray[t]^=1<<n;return this};BitArray.prototype.get=function(e){if(e>this.length-1){throw"Index too large."}var t=Math.floor(e/32);var n=e-t*32;return!!(this.wordArray[t]&1<<n)};BitArray.prototype.reset=function(){this.buffer=new ArrayBuffer(Math.ceil(this.length/32)*4);this.wordArray=new Uint32Array(this.buffer);return this};BitArray.prototype.copy=function(){var e=new BitArray(this.length);for(var t=0;t<this.wordArray.length;t++){e.wordArray[t]=this.wordArray[t]}return e};BitArray.prototype.equals=function(e){if(this.length!==e.length){return false}for(var t=0;t<this.wordArray.length;t++){if(this.wordArray[t]!==e.wordArray[t]){return false}}return true};BitArray.prototype.toJSON=function(){return JSON.stringify(this.toArray())};BitArray.prototype.toBinaryString=function(){return this.toArray().map(function(e){return e?"1":"0"}).reverse().join("")};BitArray.prototype.toHexString=function(){var e=[];for(var t=0;t<this.wordArray.length;t+=1){e.push(("00000000"+(this.wordArray[t]>>>0).toString(16)).slice(-8))}return e.join("")};BitArray.prototype.toString=function(){return this.toArray().map(function(e){return e?"1":"0"}).join("")};BitArray.prototype.toArray=function(){var e=[];for(var t=0;t<this.length;t++){e.push(Boolean(this.get(t)))}return e};BitArray.prototype.count=function(){var e=0;for(var t=0;t<this.wordArray.length;t++){x=this.wordArray[t];x=x-(x>>1&1431655765);x=(x&858993459)+(x>>2&858993459);x=x+(x>>4);x&=252645135;e+=x*16843009>>24}return e};BitArray.prototype.not=function(){for(var e=0;e<this.wordArray.length;e++){this.wordArray[e]=~this.wordArray[e]}return this};BitArray.prototype.or=function(e){if(this.length!==e.length){throw"Arguments must be of the same length."}for(var t=0;t<this.wordArray.length;t++){this.wordArray[t]|=e.wordArray[t]}return this};BitArray.prototype.and=function(e){if(this.length!==e.length){throw"Arguments must be of the same length."}for(var t=0;t<this.wordArray.length;t++){this.wordArray[t]&=e.wordArray[t]}return this};BitArray.prototype.xor=function(e){if(this.length!==e.length){throw"Arguments must be of the same length."}for(var t=0;t<this.wordArray.length;t++){this.wordArray[t]^=e.wordArray[t]}return this};
/**
* UoM cookie
*
* Marketing & ITS web teams, UoM
*/
var Uomcookie = function () {
this.cookiename = 'uom';
this.cookiesize = 2048;
this.cookielife = 365;
this.data = '';
};
Uomcookie.prototype.init = function () {
if (this.getCookie() == null) {
this.clearCookie();
}
this.loadData();
};
Uomcookie.prototype.getCookie = function () {
var c_value = document.cookie;
var c_start = c_value.indexOf(' ' + this.cookiename + '=');
if (c_start == -1) {
c_start = c_value.indexOf(this.cookiename + '=');
}
if (c_start == -1) {
c_value = null;
} else {
c_start = c_value.indexOf('=', c_start) + 1;
var c_end = c_value.indexOf(';', c_start);
if (c_end == -1) {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start, c_end));
}
return c_value;
};
Uomcookie.prototype.setCookie = function (value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? '' : '; expires=' + exdate.toUTCString() + ';domain=.unimelb.edu.au;path=/');
document.cookie = this.cookiename + '=' + c_value;
};
Uomcookie.prototype.update = function (location, value) {
this.data.set(location, value);
this.setCookie(this.data.toHexString(), this.cookielife);
};
Uomcookie.prototype.getData = function () {
return this.data.toString();
};
Uomcookie.prototype.clearCookie = function () {
this.setCookie(this.cookiename, new BitArray(this.cookiesize).toHexString(), this.cookielife);
}
Uomcookie.prototype.loadData = function () {
this.data = new BitArray(this.cookiesize, this.getCookie(this.cookiename));
return this.data;
}
// init
try {
_uom_trk = new Uomcookie();
_uom_trk.init();
} catch(e) {
// generic airbrake account?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment