1import {UrlUtils} from 'sites/url_utils'; 2 3export class CookieUtils { 4 // Public methods 5 static setTrackingCookie(permanentCookieExpiryInMonths) { 6 this.setCookieIfCookieAllowed('ctc', CookieUtils.generateUuid(), permanentCookieExpiryInMonths) 7 } 8 9 static setTrackingSessionCookie() { 10 this.checkCookieAllowed(() => { 11 const cookieValue = Cookies.get('ctc_session') ? CookieUtils.trackingSessionCookie() : CookieUtils.generateUuid(); 12 this.setCookie('ctc_session', cookieValue, CookieUtils.createFutureDateInMinutes(15)); 13 }); 14 } 15 16 static setTrackingRejectedCookie(permanentCookieExpiryInMonths) { 17 this.setCookieIfCookieAllowed('ctc_rejected', '1', permanentCookieExpiryInMonths) 18 } 19 20 static setClinchImpliedConsentFooterCookie(permanentCookieExpiryInMonths) { 21 this.setCookieIfCookieAllowed('clinch_implied_consent_footer_hidden', '1', permanentCookieExpiryInMonths) 22 } 23 24 static setCookieIfCookieAllowed(name, value, expiresInMonths) { 25 this.checkCookieAllowed(() => { 26 if (!Cookies.get(name)) { 27 this.setCookie(name, value, CookieUtils.createFutureDateInMonths(expiresInMonths)) 28 } 29 }) 30 } 31 32 static removeTrackingRejectedCookie() { 33 this.checkCookieAllowed(() => { 34 if (Cookies.get('ctc_rejected')) { 35 this.removeCookie('ctc_rejected'); 36 } 37 }); 38 } 39 40 static setCookie(key, value, expires) { 41 Cookies.set(key, value, { 42 expires: expires, 43 domain: CookieUtils.getHostname(), 44 secure: CookieUtils.isRunningOnHttpsPage() 45 }); 46 } 47 48 static migrateCookie(key, from_domain, permanentCookieExpiryInMonths) { 49 const cookieToMigrateTrackingCode = Cookies.get(key); 50 if (cookieToMigrateTrackingCode) { 51 this.removeCookie(key, from_domain) 52 this.setCookie(key, cookieToMigrateTrackingCode, CookieUtils.createFutureDateInMonths(permanentCookieExpiryInMonths)) 53 } 54 } 55 56 static checkCookieAllowed(block) { 57 if (CookieUtils.isCookieAllowed()) { 58 block() 59 } 60 } 61 62 static removeTrackingCookie() { 63 this.removeCookie('ctc'); 64 } 65 66 static removeSessionCookie() { 67 this.removeCookie('ctc_session'); 68 } 69 70 static removeCookie(name, domain = CookieUtils.getHostname()) { 71 Cookies.remove(name, { 72 domain: domain, 73 secure: CookieUtils.isRunningOnHttpsPage() 74 }); 75 } 76 77 static trackingCookie() { 78 return Cookies.get('ctc'); 79 } 80 81 static trackingSessionCookie() { 82 return Cookies.get('ctc_session'); 83 } 84 85 static trackingRejectedCookie() { 86 return Cookies.get('ctc_rejected'); 87 } 88 89 static clinchImpliedConsentFooterHiddenCookie() { 90 return Cookies.get('clinch_implied_consent_footer_hidden') 91 } 92 93 static isCookieAllowed() { 94 return navigator.cookieEnabled && !UrlUtils.getUrlQueryParams()['_disable_cookies']; 95 } 96 97 static generateCookies(permanentCookieExpiryInMonths) { 98 this.removeTrackingRejectedCookie(); 99 this.setTrackingCookie(permanentCookieExpiryInMonths); 100 this.setTrackingSessionCookie(); 101 } 102 103 static noCookiesAndTrackingActive() { 104 return !Cookies.get('ctc') && !Cookies.get('ctc_rejected') && js_vars.ct.active; 105 } 106 107 static noTrackingRejectedCookie() { 108 return !!Cookies.get('ctc_rejected') 109 } 110 111 // Private methods 112 113 static isRunningOnHttpsPage() { 114 return location.protocol.toLowerCase() === 'https:' 115 } 116 117 static generateUuid() { 118 let n = 78; 119 let result = ''; 120 while (n--) { 121 result += Math.floor(Math.random() * 16).toString(16).toLowerCase(); 122 } 123 return result; 124 } 125 126 static getHostname() { 127 return location.hostname; 128 } 129 130 static createFutureDateInMonths(months) { 131 const date = new Date() 132 date.setMonth(date.getMonth() + months) 133 return date 134 } 135 136 static createFutureDateInMinutes(minutes) { 137 const date = new Date() 138 date.setTime(date.getTime() + minutes * 60000) 139 return date 140 } 141}
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.