forked from rhysbrettbowen/Agency-Adserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_cookie.js
111 lines (102 loc) · 3.2 KB
/
parse_cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var FDCookie = function(obj){
this.fpt = [0];
for(i in obj){
this[i] = obj[i];
}
};
FDCookie.prototype.getValue = function(key){
return this[key];
};
FDCookie.prototype.getString = function(){
var obj = {};
for(i in this){
if(typeof(i)!='function' && i != 'name')
obj[i] = this[i];
}
return JSON.stringify(obj);
};
FDCookie.prototype.copy = function(obj){
for(i in obj){
this[i] = obj[i];
}
return this;
};
FDCookie.prototype.getAndSet = function(key,fn){
this[key] = fn.call(this,this[key]);
};
var genParse = function(sep){
return function(cook){
var c = new Cookie(cook);
if(cook=="")
return c;
var temp = cook.split(new RegExp(sep+" *"));
var tlen = temp.length;
for(var i = 0; i < tlen; i++){
split = temp[i].split("=");
if(split.length>1){
c[split.shift()]=split.join("=");
}else{
c[split[0]]=null;
}
}
return c;
};
};
var genStringify = function(sep){
return function(obj){
var str = '';
for(i in obj){
if(obj.hasOwnProperty(i)&&typeof(obj[i])!='function'){
str += i+"="+obj[i]+sep;
}
}
};
};
var getCookie = function(code,cookie){
var ret = cookie.split(";");
for(n = ret.length-1;n>=0;n--){
if(ret[n].match(new RegExp("^FD"+code+"\\s?="))){
console.log(ret[n]);
console.log(ret[n].substring(ret[n].indexOf("{")));
return JSON.parse(ret[n].substring(ret[n].indexOf("{")));
}
}
};
module.exports.parse = genParse(";");
module.exports.stringify = genStringify(";");
//module.exports.parseFD = genParse("&");
//module.exports.stringifyFD = genStringify("&");
module.exports.add = function(camp,loc,n,creat,date,cook){
var ret = [];
var cookie = new FDCookie(getCookie(camp,cook));
cookie.getAndSet("pctm",function(val){return val?""+(val*1+1):"1";});
cookie.getAndSet("date",function(val){return date;});
cookie.getAndSet("FQ",function(val){return val?(this.fpt[this.fpt.length-1]==loc?""+(val*1+1):"1"):"1";});
cookie.getAndSet("fpt",function(val){
for(n=val.length-1;n>=0;n--){
if(val[n]==loc)
val = val.splice(n,1);
}
val.push(loc);
return val;
});
cookie.getAndSet("FL"+loc,function(val){return val?""+(val*1+1):"1";});
cookie.getAndSet("FM"+creat,function(val){return val?""+(val*1+1):"1";});
cookie.getAndSet("pctc",function(val){return creat;});
ret.push("FD",camp,"=",cookie.getString());
// console.log(ret);
return ret.join("");
};
module.exports.getVal = function(camp,key,cook){
var cookie = new FDCookie(getCookie(camp,cook));
return cookie.getValue(key);
};
module.exports.addClick = function(camp,loc,n,creat,date,cook){
var ret = [];
var cookie = new FDCookie(getCookie(camp,cook));
cookie.getAndSet("pctcrt",function(val){return val?""+(val*1+1):"1";});
cookie.getAndSet("pdc",function(val){return date;});
cookie.getAndSet("pctl",function(val){return loc});
ret.push("FD",camp,"=",cookie.getString());
return ret.join("");
};