| 1 |
/** |
| 2 |
* Optimole Storage Module |
| 3 |
* Handles session storage operations for tracking processed pages |
| 4 |
*/ |
| 5 |
|
| 6 |
import { optmlLogger } from './logger.js'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Storage utilities for managing processed page data |
| 10 |
*/ |
| 11 |
export const optmlStorage = { |
| 12 |
/** |
| 13 |
* Generate storage key for URL and device type combination |
| 14 |
* @param {string} url - Page URL or profile ID |
| 15 |
* @param {number} deviceType - Device type (1=mobile, 2=desktop) |
| 16 |
* @returns {string} Storage key |
| 17 |
*/ |
| 18 |
getKey: function(url, deviceType) { |
| 19 |
return `optml_pp_${url}_${deviceType}`; |
| 20 |
}, |
| 21 |
|
| 22 |
/** |
| 23 |
* Check if a URL/device combination has been processed |
| 24 |
* @param {string} url - Page URL or profile ID |
| 25 |
* @param {number} deviceType - Device type (1=mobile, 2=desktop) |
| 26 |
* @returns {boolean} True if already processed |
| 27 |
*/ |
| 28 |
isProcessed: function(url, deviceType) { |
| 29 |
try { |
| 30 |
const key = this.getKey(url, deviceType); |
| 31 |
const storedValue = sessionStorage.getItem(key); |
| 32 |
|
| 33 |
if (!storedValue) return false; |
| 34 |
|
| 35 |
// Check if the stored timestamp is still valid (within current session) |
| 36 |
const timestamp = parseInt(storedValue, 10); |
| 37 |
const now = Date.now(); |
| 38 |
|
| 39 |
// Consider it valid if it exists in the current session |
| 40 |
return true; |
| 41 |
} catch (e) { |
| 42 |
optmlLogger.error('Error checking sessionStorage:', e); |
| 43 |
return false; |
| 44 |
} |
| 45 |
}, |
| 46 |
|
| 47 |
/** |
| 48 |
* Mark a URL/device combination as processed |
| 49 |
* @param {string} url - Page URL or profile ID |
| 50 |
* @param {number} deviceType - Device type (1=mobile, 2=desktop) |
| 51 |
*/ |
| 52 |
markProcessed: function(url, deviceType) { |
| 53 |
try { |
| 54 |
const key = this.getKey(url, deviceType); |
| 55 |
sessionStorage.setItem(key, Date.now().toString()); |
| 56 |
} catch (e) { |
| 57 |
optmlLogger.error('Error setting sessionStorage:', e); |
| 58 |
} |
| 59 |
} |
| 60 |
}; |
| 61 |
|