1(function() { 2 const fallbackTimeout = 5000; 3 let localstorageUsed = false; 4 let blueConicLoaded = false; 5 6 document.addEventListener('bcSegmentsProcessed', handleSegmentProcessed); 7 8 function handleSegmentProcessed(e) { 9 if (localstorageUsed) { 10 hydrateLocalStorageSegments(); 11 } 12 fadeInAllSegmentContainers(); 13 } 14 15 async function retry(func, { validate, retries = 0, maxRetries = 5, delay = 500 }) { 16 const results = await func(); 17 if (validate(results) || retries >= maxRetries) { 18 return results; 19 } 20 await new Promise(resolve => setTimeout(resolve, delay)); 21 return retry(func, { validate, retries: retries + 1, maxRetries, delay }); 22 } 23 24 function validateSegments(segments) { 25 return Array.isArray(segments) && segments.length > 0; 26 } 27 28 function setSegmentData(segments) { 29 localStorage.setItem('bcSegData', JSON.stringify(segments)); 30 } 31 32 function getSegmentData() { 33 const lsSegments = localStorage.getItem('bcSegData'); 34 if (lsSegments) { 35 localstorageUsed = true; 36 return Promise.resolve(JSON.parse(lsSegments)); 37 } else { 38 return hydrateLocalStorageSegments(); 39 } 40 } 41 42 async function hydrateLocalStorageSegments() { 43 const segments = await retry(getBlueconicSegments, { validate: validateSegments, maxRetries: 6 }); 44 if (segments.length > 0) { 45 const cleanedSegmentData = segments.map(segment => ({ id: segment.id })); 46 setSegmentData(cleanedSegmentData); 47 } 48 return segments; 49 } 50 51 function dispatchSegmentsProcessedEvent() { 52 const event = new CustomEvent('bcSegmentsProcessed', { detail: { segmentsProcessed: true } }); 53 document.dispatchEvent(event); 54 } 55 56 async function displayHiddenSegments() { 57 const segmentElements = document.querySelectorAll('[class*="bcdynseg-"], [class*="bcdynsegfallback-"]'); 58 const segments = await getSegmentData(); 59 segmentElements.forEach(elm => updateElementVisibility(elm, segments)); 60 dispatchSegmentsProcessedEvent(); 61 } 62 63 function updateElementVisibility(elm, segments) { 64 const segmentIds = Array.from(elm.classList) 65 .filter(cls => cls.startsWith('bcdynseg-') || cls.startsWith('bcdynsegfallback-')) 66 .map(cls => cls.replace(/^(bcdynseg-|bcdynsegfallback-)/, '')); 67 const shouldBeVisible = segmentIds.some(segmentId => segments.some(seg => seg.id === segmentId)); 68 69 shouldBeVisible ? showElement(elm) : hideElement(elm); 70 } 71 72 function showElement(elm) { 73 elm.classList.remove('bcdynseg-hidden'); 74 elm.classList.add('bcdynseg-visible'); 75 } 76 77 function hideElement(elm) { 78 elm.classList.remove('bcdynseg-visible'); 79 elm.classList.add('bcdynseg-hidden'); 80 } 81 82 function fadeInAllSegmentContainers() { 83 const elements = document.querySelectorAll('.bcseg-container'); 84 elements.forEach(elm => { 85 elm.classList.add('bcseg-container-visible'); 86 }); 87 } 88 89 function getSegmentsFromCookie() { 90 const encodedCookieValue = getCookieByName('SESSsegments'); 91 92 if (encodedCookieValue) { 93 try { 94 // Decode the cookie value 95 const decodedCookieValue = decodeURIComponent(encodedCookieValue); 96 97 // Parse the JSON string 98 const segments = JSON.parse(decodedCookieValue); 99 100 if (segments.length) { 101 return segments; 102 } 103 104 return null; 105 } catch (error) { 106 return null; 107 } 108 } else { 109 return null; 110 } 111 } 112 113 function getBlueconicSegments() { 114 const segments = getSegmentsFromCookie(); 115 116 if (segments) { 117 return segments; 118 } 119 120 return window?.blueConicClient?.getSegments() ?? []; 121 } 122 123 function init() { 124 blueConicLoaded = true; 125 displayHiddenSegments(); 126 } 127 128 function useBlueConicToFetchSegments() { 129 if (window.blueConicClient) { 130 setTimeout(init, 250); 131 } else { 132 window.addEventListener('onBlueConicLoaded', init); 133 } 134 135 setTimeout(() => { 136 if (!blueConicLoaded) { 137 dispatchSegmentsProcessedEvent(); 138 } 139 }, fallbackTimeout); 140 } 141 142 function initialSegmentCookieCheck() { 143 const segments = getSegmentsFromCookie(); 144 145 if (segments) { 146 setSegmentData(segments); 147 displayHiddenSegments(); 148 } else { 149 useBlueConicToFetchSegments(); 150 } 151 } 152 153 function getCookieByName(name) { 154 const cookies = document.cookie.split(';'); 155 const cookie = cookies.find(cookie => cookie.trim().startsWith(`${name}=`)); 156 return cookie ? cookie.split('=')[1] : null; 157 } 158 159 // Start the process 160 // Attempt to get segments from cookie 161 // If cookie is not found, use BlueConic SDK to fetch segments 162 initialSegmentCookieCheck(); 163 164})();
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.