aboutsummaryrefslogtreecommitdiffstats
path: root/cookie.js
blob: 4825180daf9be4ca93197501871fa7bfe18fe708 (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
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
112
113
114
115
116
117
118
/**
 * ==VimperatorPlugin==
 * @name            cookie.js
 * @description     display cookie in ':pageinfo'.
 * @description-ja  :pageinfo で cookie を表示する
 * @author          janus_wel <janus_wel@fb3.so-net.ne.jp>
 * @version         0.10
 * @minversion      2.0pre
 * ==/VimperatorPlugin==
 *
 * LICENSE
 *   New BSD License
 *
 * USAGE
 *   set 'c' to option 'pageinfo' and type ':pageinfo'
 *
 * EXAMPLE
 *   set pageinfo=gfmc
 *
 * HISTORY
 *   2008/11/06 ver. 0.10   - initial written.
 *
 */

( function () {

// class definition
// cookie manager
function CookieManager() {
    this.initialize.apply(this, arguments);
}
CookieManager.prototype = {
    initialize: function (uri) {
        const Cc = Components.classes;
        const Ci = Components.interfaces;

        const MOZILLA = '@mozilla.org/';
        const IO_SERVICE = MOZILLA + 'network/io-service;1';
        const COOKIE_SERVICE = MOZILLA + 'cookieService;1';

        this.ioService = Cc[IO_SERVICE].getService(Ci.nsIIOService);
        this.cookieService = Cc[COOKIE_SERVICE].getService(Ci.nsICookieService);
        if(!this.ioService || !this.cookieService) {
            throw new Error('error on CookieManager initialize.');
        }

        this.readCookie(uri);
    },

    readCookie: function (uri) {
        if(uri) {
            this.uri = uri;
            this.uriObject = this.ioService.newURI(uri, null, null);
            this.cookie = this._deserializeCookie(this._getCookieString());
        }
    },

    _getCookieString: function () {
        return this.uriObject
            ? this.cookieService.getCookieString(this.uriObject, null)
            : null;
    },

    _setCookieString: function (cookieString) {
        if(this.uriObject && cookieString) {
            this.cookieService.setCookieString(this.uriObject, null, cookieString, null);
        }
    },

    _deserializeCookie: function (cookieString) {
        let cookies = cookieString.split(/; */);
        let cookie = {};
        let key, val;
        for (let i=0, max=cookies.length ; i<max ; ++i) {
            [key, val] = cookies[i].split('=');
            cookie[key] = val;
        }
        return cookie;
    },

    getCookie: function (key) {
        return this.cookie[key] ? this.cookie[key] : null;
    },

    properties: function () {
        liberator.log('debug', 0);
        liberator.log(this.cookie, 0);
        return [k for ([k, v] in Iterator(this.cookie))];
    },

    setCookie: function (obj) {
        this.cookie[obj.key] = obj.value;
        let string = [
            obj.key + '=' + obj.value,
            'domain=' + obj.domain,
            'expires=' + new Date(new Date().getTime() + obj.expires),
        ].join('; ');
        this._setCookieString(string);
    },
};

liberator.modules.buffer.addPageInfoSection(
    'c',
    'Cookie',
    function (verbose) {
        if(verbose) {
            let p;
            let c = new CookieManager(liberator.modules.buffer.URL);
            for ([, p] in Iterator(c.properties())) {
                yield [p, c.getCookie(p)];
            }
        }
    }
);

})();

// vim: set sw=4 ts=4 et;