PageSourceSearch

https://sakaiku.jp/common/js/js_cookie.js

js sakaiku.jp collected 2026-09-24 20:24:30 UTC 3,123 bytes, 101 lines download raw bytes

1/**
2 * js_cookie.js
3 * 
4 * usage:
5 *   var cookie = new jsCookie('KEY', {
6 *     path   : '/',
7 *     secure : true
8 *   });
9 *   
10 *   cookie.write('value', 1);
11 * //cookie.write('value', ''); # ex.End of session
12 * //cookie.write('value', 'Mon, 14 Apr 2025 17:17:05 GMT'));
13 * 
14 *   var value = cookie.read();
15 * 
16 *   cookie.remove();
17 */
18'use strict';
19
20var jsCookie = function(key, options) {
21    this.vars = {
22        key : null,
23        options : {
24            path     : location.pathname.replace(/\/[^\/]*$/, '/'),
25            domain   : location.hostname,
26            expires  : '',
27            secure   : true,
28            httponly : ''
29        }
30    };
31    this.init(key, options);
32};
33
34jsCookie.prototype.init = function(key, options) {
35    // KEYとして利用できる文字は、英数字と _ - のみ
36    if (key == '' || ! key.match(/^[0-9a-zA-Z|_\-]+$/)) { return; }
37    this.vars.key = key;
38    // optionデフォルトの設定
39    this.vars.options = this.getOptions__(options);
40};
41// 引数があればそれを優先しつつ完全なoptionsを返す
42jsCookie.prototype.getOptions__ = function(options) {
43    if (! options) { options = {}; }
44    var opt = {};
45    for (var key in this.vars.options) {
46        if (options.hasOwnProperty(key)) {
47            opt[key] = options[key];
48        }
49        else if (this.vars.options.hasOwnProperty(key)) {
50            opt[key] = this.vars.options[key];
51        }
52    }
53    // expiresが数字の場合は n日後とする
54    if (typeof(opt.expires) === 'number') {
55        var day = opt.expires;
56        var expires_day = new Date();
57        expires_day.setDate(expires_day.getDate() + day);
58        opt.expires = expires_day.toUTCString();
59    }
60
61    return opt;
62};
63
64jsCookie.prototype.read = function() {
65    if (! this.vars.key) { return null; }
66    var value = this.parse__(this.vars.key);
67    return value;
68};
69jsCookie.prototype.parse__ = function(key) {
70    var cookies = document.cookie ? document.cookie.split('; ') : [];
71    for (var i in cookies) {
72        var parts = cookies[i].split('=');
73        if (parts[0] == encodeURIComponent(key)) {
74            var encoded_str = parts[1];
75            return decodeURIComponent(encoded_str);
76       }
77    }
78    return null;
79};
80
81jsCookie.prototype.write = function(value, expires_day, options) {
82    if (! this.vars.key) { return null; }
83    if (!options) { options = { expires : expires_day }; }
84    else { options.expires = expires_day; }
85    var opt = this.getOptions__(options);
86
87    var cookie_text = [
88        encodeURIComponent(this.vars.key), '=', encodeURIComponent(value),
89        opt.expires   ? '; Expires=' + opt.expires : '',
90        opt.path      ? '; path='    + opt.path : '',
91        opt.domain    ? '; Domain='  + opt.domain : '',
92        opt.secure    ? '; Secure' : '',
93        opt.httponly  ? '; HttpOnly' : ''
94    ].join('');
95    console.log(cookie_text);
96    document.cookie = cookie_text;
97};
98
99jsCookie.prototype.remove = function(options) {
100    this.write('', -1, options);
101};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.