This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 353
/
cookie.js
85 lines (73 loc) · 2.27 KB
/
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
// Adam Barth's abandoned(?) HTML Cookie API proposal
// https://docs.google.com/Doc?docid=0AZpchfQ5mBrEZGQ0cDh3YzRfMTRmdHFma21kMg&hl=en&pli=1
(function () {
"use strict";
/** @constructor */
function Cookie(name, value) {
this.name = name;
this.value = value;
this.httpOnly = false;
}
function parseCookies(findName) {
var array = document.cookie.split(/;\s+/),
i, str, idx, cookie;
for (i = 0; i < array.length; i += 1) {
str = array[i];
idx = str.indexOf('=');
cookie = new Cookie(str.substring(0, idx), str.substring(idx + 1));
if (cookie.name === findName) {
return cookie;
} else {
array[i] = cookie;
}
}
return arguments.length > 0 ? null : array;
}
function getCookie(name, callback) {
setTimeout(function () {
callback(parseCookies(name));
}, 0);
}
function getAllCookies(callback) {
setTimeout(function () {
callback(parseCookies());
}, 0);
}
function setCookie(cookie, errback) {
setTimeout(function () {
var str = cookie.name + '=' + (cookie.value || ""),
foundCookie, expectDeleted;
if (cookie.expires) {
str += "; expires=" + cookie.expires;
}
if (cookie.domain) {
str += "; domain=" + cookie.domain;
}
if (cookie.secure) {
str += "; secure";
}
str += "; path=" + (cookie.path || "/");
document.cookie = str;
foundCookie = parseCookies(cookie.name);
expectDeleted = cookie.expires && (new Date(cookie.expires) < new Date());
if (!foundCookie && !expectDeleted) {
errback("Cookie not set");
} else if (foundCookie.value !== cookie.value) {
errback("Cookie not set");
}
}, 0);
}
function deleteCookie(name, errback) {
setTimeout(function () {
document.cookie = name + "=; expires=" + (new Date(0).toGMTString()) + "; path=/";
var foundCookie = parseCookies(name);
if (foundCookie) {
errback("Cookie not deleted");
}
}, 0);
}
document.getCookie = document.getCookie || getCookie;
document.getAllCookies = document.getAllCookies || getAllCookies;
document.setCookie = document.setCookie || setCookie;
document.deleteCookie = document.deleteCookie || deleteCookie;
} ());