PageSourceSearch

https://cdn.9game.cn/ngm-portal-jws/public/javascripts/common/ucb.js

js 9game.cn collected 2026-10-02 03:38:01 UTC 4,142 bytes, 133 lines download raw bytes

1/**
2 * 封装操作cookie的方法
3 * 
4 * @name ucb.Cookie
5 * @author Ican Cheung <[email protected]>
6 * 
7 */
8var ucb = ucb || {};
9
10ucb.Supports = {
11    /**
12     * @property 是否支持触控
13     * @type {Boolean}
14     */
15    Touch: ('ontouchstart' in window)
16};
17
18ucb.Cookie = ucb.Cookie || {};
19
20(function(cookie) {
21    
22    /**
23     * 验证字符串是否合法的cookie键名
24     * 
25     * @param {string} source 需要验证的key
26     * @return {bool} 是否合法的cookie键名
27     */
28    cookie._isValidKey = function(key) {
29        
30        return (new RegExp("^[^\\x00-\\x20\\x7f\\(\\)<>@,;:\\\\\\\"\\[\\]\\?=\\{\\}\\/\\u0080-\\uffff]+\x24"))
31                .test(key);
32    };
33    
34    /**
35     * 获取cookie的值,不对值进行解码
36     * 
37     * @function
38     * @param {string} key 需要获取Cookie的键名
39     * 
40     * @returns {string|null} 获取的Cookie值,获取不到时返回null
41     */
42    cookie.getRaw = function(key) {
43        if (cookie._isValidKey(key)) {
44            var reg = new RegExp("(^| )" + key + "=([^;]*)(;|\x24)"), result = reg.exec(document.cookie);
45            
46            if (result) {
47                return result[2] || null;
48            }
49        }
50        
51        return null;
52    };
53    
54    /**
55     * 获取cookie的值,用decodeURIComponent进行解码
56     * 
57     * @function
58     * @param {string} key 需要获取Cookie的键名
59     * @description <b>注意:</b>该方法会对cookie值进行decodeURIComponent解码。如果想获得cookie源字符串,请使用getRaw方法。
60     * 
61     * @returns {string|null} cookie的值,获取不到时返回null
62     */
63    cookie.get = function(key) {
64        var value = cookie.getRaw(key);
65        if ('string' == typeof value) {
66            value = decodeURIComponent(value);
67            return value;
68        }
69        return null;
70    };
71    
72    /**
73     * 设置cookie的值,不对值进行编码
74     * 
75     * @function
76     * @param {string} key 需要设置Cookie的键名
77     * @param {string} value 需要设置Cookie的值
78     * @param {Object} [options] 设置Cookie的其他可选参数
79     * @config {string} [path] cookie路径
80     * @config {Date|number}
80 [expires] cookie过期时间,如果类型是数字的话, 单位是毫秒
81     * @config {string} [domain] cookie域名
82     * @config {string} [secure] cookie是否安全传输
83     */
84    cookie.setRaw = function(key, value, options) {
85        if (!cookie._isValidKey(key)) {
86            return;
87        }
88        
89        options = options || {};
90        
91        // 计算cookie过期时间
92        var expires = options.expires;
93        if ('number' == typeof options.expires) {
94            expires = new Date();
95            expires.setTime(expires.getTime() + options.expires);
96        }
97        
98        document.cookie = key + "=" + value + (options.path ? "; path=" + options.path : "")
99                + (expires ? "; expires=" + expires.toGMTString() : "")
100                + (options.domain ? "; domain=" + options.domain : "") + (options.secure ? "; secure" : '');
101    };
102    
103    /**
104     * 设置cookie的值,用encodeURIComponent进行编码
105     * 
106     * @function
107     * @param {string} key 需要设置Cookie的键名
108     * @param {string} value 需要设置Cookie的值
109     * @param {Object} [options] 设置Cookie的其他可选参数
110     * @config {string} [path] cookie路径
111     * @config {Date|number}
111 [expires] cookie过期时间,如果类型是数字的话, 单位是毫秒
112     * @config {string} [domain] cookie域名
113     * @config {string} [secure] cookie是否安全传输
114     */
115    cookie.set = function(key, value, options) {
116        cookie.setRaw(key, encodeURIComponent(value), options);
117    };
118    
119    /**
120     * 删除cookie的值
121     * 
122     * @function
123     * @param {string} key 需要删除Cookie的键名
124     * @param {Object} options 需要删除的cookie对应的 path domain 等值
125     */
126    cookie.remove = function(key, options) {
127        options = options || {};
128        options.expires = new Date(0);
129        cookie.setRaw(key, '', options);
130    };
131    
132})(ucb.Cookie);
133

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.