1// Define the cookie names to check 2const cookiesToCheck = ["ola_ica", "ola_type"]; 3const accountCookies = ["ola_login_id", "ola_hash"]; 4 5// Modular function to get a cookie by name 6const getCookie = (name) => { 7 const value = `; ${document.cookie}`; 8 const parts = value.split(`; ${name}=`); 9 if (parts.length === 2) return parts.pop().split(";").shift(); 10 return null; 11}; 12 13function checkAdmiralReady() { 14 const admiralScriptStatus = window.cmg_auth.admiralScriptStatus; 15 const allReady = Object.values(admiralScriptStatus).every(value => value === true); 16 17 if (allReady) { 18 console.log("Both scripts are ready. Starting..."); 19 window.admiral('targeting','ready'); 20 } else { 21 console.log("Not all values are true. Script will not run."); 22 } 23} 24 25const sendOLARequest = (ajaxURL, data) => { 26 // Convert data into URL-encoded parameters 27 const params = new URLSearchParams(data); 28 29 // Return the fetch request promise 30 return fetch(ajaxURL, { 31 method: 'POST', // Use POST for AJAX calls 32 headers: { 33 'Content-Type': 'application/x-www-form-urlencoded', // Required for form-encoded data 34 }, 35 body: params, // Send the data as key=value pairs 36 }) 37 .then((response) => { 38 // Check if the response is OK 39 if (!response.ok) { 40 if( response.status === 401 ) { 41 checkLoginCookies(); 42 } 43 throw new Error(`HTTP error! Status: ${response.status}`); 44 } 45 return response.json(); // Parse and return the JSON response 46 }); 47} 48 49// Main function to check all cookies and call API if any are missing 50const checkAccessCookies = () => { 51 const accountCookiesCount = accountCookies.filter((cookie) => getCookie(cookie)); 52 const missingCookies = cookiesToCheck.filter((cookie) => !getCookie(cookie)); 53 54 if ( accountCookiesCount.length == accountCookies.length) { 55 window.admiral('targeting','set','ola_loggedin', "Y"); 56 window.cmg_auth.isLoggedIn = true; 57 if ( missingCookies.length > 0 ) { 58 const requestData = new URLSearchParams({ 59 action: 'refresh_token', // WordPress AJAX action 60 security: auth_ajax.nonce, 61 missing: missingCookies, 62 }); 63 64 sendOLARequest(auth_ajax.url, requestData) 65 .then((response) => { 66 cookiesToCheck.forEach((cookieName) => { 67 if (response['data']['access'][cookieName]) { 68 window.admiral('targeting', 'set', cookieName, response['data']['access'][cookieName]); 69 } else { 70 console.warn(`API response did not include value for ${cookieName}`); 71 } 72 }); 73 window.cmg_auth.admiralScriptStatus.loginReady = true; 74 checkAdmiralReady(); 75 }) 76 .catch((error) => { 77 console.error('AJAX error:', error); 78 }); 79 } else { 80 cookiesToCheck.forEach((cookieName) => { 81 let accessCookie = getCookie(cookieName); 82 if (accessCookie) { 83 // Set the cookie with the returned value and an expiration 84 window.admiral('targeting', 'set', cookieName, accessCookie); 85 } else { 86 console.warn(`API response did not include value for ${cookieName}`); 87 } 88 }); 89 window.cmg_auth.admiralScriptStatus.loginReady = true; 90 checkAdmiralReady(); 91 } 92 } else { 93 window.admiral('targeting','set','ola_loggedin', "N"); 94 window.cmg_auth.isLoggedIn = false; 95 window.cmg_auth.admiralScriptStatus.loginReady = true; 96 checkAdmiralReady(); 97 } 98}; 99checkAccessCookies(); 100 101const checkLoginCookies = () => { 102 const accountCookiesCount = accountCookies.filter((cookie) => getCookie(cookie)); 103 104 if ( accountCookiesCount.length == accountCookies.length) { 105 106 document.querySelectorAll('.loggedout-container').forEach(function (element) { 107 element.style.display = 'none'; 108 }); 109 110 document.querySelectorAll('.loggedin-container').forEach(function (element) { 111 element.style.display = ''; 112 }); 113 } else { 114 115 document.querySelectorAll('.loggedout-container').forEach(function (element) { 116 element.style.display = ''; 117 }); 118 119 document.querySelectorAll('.loggedin-container').forEach(function (element) { 120 element.style.display = 'none'; 121 }); 122 } 123}; 124 125document.addEventListener('DOMContentLoaded', function () { 126 checkLoginCookies(); 127});
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.