summaryrefslogtreecommitdiff
path: root/webcards/scripts/cookie.js
blob: 2eb5977ab20a12a018c491b967c46e822e175860 (plain)
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
function CookieManager() {
}

CookieManager.prototype = {
    getCookie: function(name){
        let cookies = document.cookie.split(";");
        for(let i in cookies) {
            let cname = cookies[i].trim().split("=")[0];
            if(cname == name){
                return cookies[i].trim().slice(name.length + 1);
            }
        }
        return "";
    },

    setCookie: function(name, value, data={}) {
        let extra = "";

        for(let key in data)
        {
            extra += "; " + key + "=" + data[key];
        }

        document.cookie = name + "=" + value + extra;
    },

    setYearCookie: function(name, value) {
        var date = new Date(Date.now());
        date.setFullYear(date.getFullYear() + 1);
        this.setCookie(name, value, {expires: date.toUTCString()});
    },

    removeCookie: function(name) {
        var date = new Date(0);
        this.setCookie(name, "", {expires: date.toUTCString()});
    }
};

var Cookies = new CookieManager();