1/* global advanced_ads_pro_visitor_conditions */ 2/* eslint-disable camelcase */ 3( function () { 4 if ( typeof advanced_ads_pro_visitor_conditions !== 'object' ) { 5 return; 6 } 7 8 /** 9 * The cookie storage object. 10 * 11 * Since we cannot read expiration times of cookies, we use our own `expires` field to save expiration times. 12 * This allows us to update cookies without updating their expiration times, i.e. without prolonging them. 13 * 14 * @param {string} name The cookie name. 15 * @param {number} exdays The number of days until the cookie expires. 16 */ 17 function cookie_storage( name, exdays ) { 18 this.name = name; 19 this.exdays = exdays; 20 this.data = undefined; 21 this.expires = 0; 22 23 const cookie = advads.get_cookie( name ); 24 25 if ( ! cookie ) { 26 this.data = cookie; 27 return; 28 } 29 30 let cookie_obj = null; 31 try { 32 cookie_obj = JSON.parse( cookie ); 33 } catch ( e ) { 34 this.data = cookie; 35 return; 36 } 37 38 if ( typeof cookie_obj !== 'object' ) { 39 this.data = cookie; 40 return; 41 } 42 43 this.data = cookie_obj.data; 44 this.expires = parseInt( cookie_obj.expires, 10 ); 45 } 46 47 /** 48 * Check if the cookie data exists. 49 */ 50 cookie_storage.prototype.exists = function () { 51 return typeof this.data !== 'undefined'; 52 }; 53 54 /** 55 * Save the cookie data. 56 * 57 * @param {string|number} data The data to save in the cookie. 58 */ 59 cookie_storage.prototype.save = function ( data ) { 60 this.data = data; 61 62 const get_unix_time_in_seconds = function () { 63 return Math.round( new Date().getTime() / 1000 ); 64 }; 65 66 let remaining_time = this.expires - get_unix_time_in_seconds(); 67 68 // Check if the cookie is expired. 69 if ( remaining_time <= 0 ) { 70 remaining_time = this.exdays * 24 * 60 * 60; 71 this.expires = get_unix_time_in_seconds() + remaining_time; 72 } 73 74 advads.set_cookie_sec( 75 this.name, 76 JSON.stringify( { 77 expires: this.expires, 78 data: this.data, 79 } ), 80 remaining_time 81 ); 82 }; 83 84 advanced_ads_pro_visitor_conditions.cookie_storage = cookie_storage; 85 86 // set cookie for referrer visitor condition. 87 const refCookie = new cookie_storage( 88 advanced_ads_pro_visitor_conditions.referrer_cookie_name, 89 advanced_ads_pro_visitor_conditions.referrer_exdays 90 ); 91 if ( ! refCookie.exists() && document.referrer !== '' ) { 92 refCookie.save( document.referrer ); 93 } 94 95 // Set cookie with page impressions. 96 const imprCookie = new cookie_storage( 97 advanced_ads_pro_visitor_conditions.page_impr_cookie_name, 98 advanced_ads_pro_visitor_conditions.page_impr_exdays 99 ); 100 if ( ! imprCookie.exists() ) { 101 imprCookie.save( 1 ); 102 } else { 103 imprCookie.save( parseInt( imprCookie.data, 10 ) + 1 || 1 ); 104 } 105} )();
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.