-
Notifications
You must be signed in to change notification settings - Fork 3
/
cookies.html
66 lines (66 loc) · 1.77 KB
/
cookies.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<script>
function bindEvent(element, eventName, eventHandler) {
if (element.addEventListener){
element.addEventListener(eventName, eventHandler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, eventHandler);
}
}
function getCookies(){
var squirtSettings = {squirt_reply:true};
var cookieSplit = document.cookie.split(";");
for(var i = 0; i < cookieSplit.length; i++){
var cookie = cookieSplit[i].trim();
var [name,value] = cookie.split("=");
var idx = name.indexOf("squirt_");
if(idx != -1) {
squirtSettings[name.replace(/^squirt_/, '')] = value;
}
}
return squirtSettings;
}
function setCookies(obj){
var cookie_string = '';
for (var k in obj){
if (obj.hasOwnProperty(k)) {
cookie_string += "squirt_"+k+"="+obj[k]+";"
}
}
if (cookie_string.length > 0){
var expiration_date = new Date();
expiration_date.setFullYear(expiration_date.getFullYear() + 1);
cookie_string += " path=/; expires=" + expiration_date.toUTCString();
document.cookie = cookie_string;
}
}
bindEvent(window, 'message', function (e) {
var msg = e.data;
if (typeof msg == "object" && msg.squirt_request){
if (msg.squirt_request == "getCookies"){
window.parent.postMessage(getCookies(), '*');
}
if (msg.squirt_request == "setCookies" && msg.obj){
setCookies(msg.obj);
}
}
});
window.onload = function() {
let cookies = getCookies();
let cookieSpan = document.createElement('div');
let cookieText = '';
for (var k in cookies){
if (cookies.hasOwnProperty(k)) {
cookieText += k +'='+cookies[k]+'<br>';
}
}
cookieSpan.innerHTML = cookieText;
document.body.appendChild(cookieSpan);
}
</script>
</head>
<body></body>
</html>