PageSourceSearch

https://www.paidsurveyupdate.com/wp-content/plugins/psu-suite-core…PSU_Core_v1_CookieHandler.js?ver=1744013800

js paidsurveyupdate.com collected 2026-09-27 08:34:38 UTC 6,495 bytes, 199 lines download raw bytes

1/**
2 * PSU_Core_v1_CookieHandler
3 * Centralized cookie management for PSU Suite plugins
4 */
5class PSU_Core_v1_CookieHandler {
6    constructor() {
7        // Core state
8        this.configsByKey = new Map();
9        this.cookieDomain = null;
10        this.initialized = false;
11
12        // Load configuration immediately
13        this.loadConfig();
14    }
15
16    /**
17     * Initialize handler with configuration
18     * @private
19     */
20    loadConfig() {
21        // Default configuration if window config not available
22        const defaultConfig = {
23            cookieDomain: '.paidsurveyupdate.com',
24            cookies: {}
25        };
26
27        try {
28            // Get configuration from window
29            if (typeof window?.PSU_Core_v1CookieConfig === 'object' && window.PSU_Core_v1CookieConfig !== null) {
30                this.cookieDomain = window.PSU_Core_v1CookieConfig.cookieDomain || defaultConfig.cookieDomain;
31
32                // Process cookie configurations
33                if (window.PSU_Core_v1CookieConfig.cookies) {
34                    Object.entries(window.PSU_Core_v1CookieConfig.cookies).forEach(([key, config]) => {
35                        this.configsByKey.set(key, {
36                            name: config.name,
37                            domain: config.domain || this.cookieDomain,
38                            path: config.path || '/',
39                            secure: config.secure !== false,
40                            sameSite: config.sameSite || 'Strict'
41                        });
42                    });
43                }
44
45                this.initialized = true;
46                console.log('[PSU_Core_v1_CookieHandler] Initialized successfully');
47            } else {
48                console.warn('[PSU_Core_v1_CookieHandler] Configuration not found, using defaults');
49                this.cookieDomain = defaultConfig.cookieDomain;
50            }
51        } catch (error) {
52            console.error('[PSU_Core_v1_CookieHandler] Configuration error:', error);
53            this.cookieDomain = defaultConfig.cookieDomain;
54        }
55    }
56
57    /**
58     * Get cookie value by registered key
59     * @param {string} key Registered cookie key
60     * @returns {Object|string|null} Parsed cookie value or null
61     */
62    getCookie(key) {
63        const config = this.getConfig(key);
64        if (!config) {
65            console.error('[PSU_Core_v1_CookieHandler] No configuration found for key:', key);
66            return null;
67        }
68
69        const value = this.getRawCookie(config.name);
70        if (!value) return null;
71
72        try {
73            return JSON.parse(decodeURIComponent(value));
74        } catch (error) {
75            return value; // Return raw value if not JSON
76        }
77    }
78
79    /**
80     * Get raw cookie value by name
81     * @private
82     * @param {string} name Cookie name
83     * @returns {string|null} Cookie value or null if not found
84     */
85    getRawCookie(name) {
86        const value = `; ${document.cookie}`;
87        const parts = value.split(`; ${name}=`);
88
89        if (parts.length === 2) {
90            return parts.pop().split(';').shift();
91        }
92        return null;
93    }
94
95    /**
96     * Set cookie value by registered key
97     * @param {string} key Registered cookie key
98     * @param {*} value Cookie value (will be JSON stringified)
99     * @param {Object} options Optional cookie settings
100     * @returns {boolean} Success status
101     */
102    setCookie(key, value, options = {}) {
103        const config = this.getConfig(key);
104        if (!config) {
105            console.error('[PSU_Core_v1_CookieHandler] No configuration found for key:', key);
106            return false;
107        }
108
109        try {
110            const finalValue = typeof value === 'object' ?
111                encodeURIComponent(JSON.stringify(value)) :
112                encodeURIComponent(value);
113
114            // Build cookie string
115            let cookieString = `${config.name}=${finalValue}`;
116
117            // Add expiration if provided
118            if (options.expires) {
119                const d = new Date();
120                d.setTime(d.getTime() + (options.expires * 24 * 60 * 60 * 1000));
121                cookieString += `; expires=${d.toUTCString()}`;
122            }
123
124            // Add standard attributes
125            cookieString += `; path=${config.path}`;
126            cookieString += `; domain=${config.domain}`;
127
128            if (config.secure) {
129                cookieString += '; secure';
130            }
131
132            cookieString += `; samesite=${config.sameSite}`;
133
134            document.cookie = cookieString;
135            return true;
136
137        } catch (error) {
138            console.error('[PSU_Core_v1_CookieHandler] Error setting cookie:', error);
139            return false;
140        }
141    }
142
143    /**
144     * Delete cookie by registered key
145     * @param {string} key Registered cookie key
146     * @returns {boolean} Success status
147     */
148    deleteCookie(key) {
149        const config = this.getConfig(key);
150        if (!config) {
151            console.error('[PSU_Core_v1_CookieHandler] No configuration found for key:', key);
152            return false;
153        }
154
155        try {
156            // Set expiration to past date
157            document.cookie = `${config.name}=; ` +
158                `expires=Thu, 01 Jan 1970 00:00:00 UTC; ` +
159                `path=${config.path}; ` +
160                `domain=${config.domain}; ` +
161                (config.secure ? 'secure; ' : '') +
162                `samesite=${config.sameSite}`;
163            return true;
164
165        } catch (error) {
166            console.error('[PSU_Core_v1_CookieHandler] Error deleting cookie:', error);
167            return false;
168        }
169    }
170
171    /**
172     * Get cookie configuration by key
173     * @private
174     * @param {string} key Configuration key
175     * @returns {Object|null} Cookie configuration or null
176     */
177    getConfig(key) {
178        return this.configsByKey.get(key) || null;
179    }
180
181    /**
182     * Get cookie domain
183     * @returns {string} Cookie domain
184     */
185    getCookieDomain() {
186        return this.cookieDomain;
187    }
188
189    /**
190     * Check if handler is initialized
191     * @returns {boolean} Initialization status
192     */
193    isInitialized() {
194        return this.initialized;
195    }
196}
197
198// Initialize singleton instance
199window.psuCoreCookieHandler = new PSU_Core_v1_CookieHandler();

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.