1/** 2 * Cache busting for Advanced Ads groups. 3 * 4 * The ad slots are rendered as empty placeholders so the surrounding page can 5 * stay in Kinsta's full page cache. This script asks Advanced Ads for a freshly 6 * picked ad per visitor and drops it into the placeholders. 7 * 8 * Each placeholder is requested on its own. The plugin's batched "deferedAds" 9 * parameter is rejected ("The method is not allowed to render.") on Advanced 10 * Ads 2.x, so single requests are the only supported route. Only one ad slot is 11 * visible at a time (desktop or mobile), so this stays cheap. 12 */ 13(function () { 14 'use strict'; 15 16 var SELECTOR = '.niu-ads-group[data-niu-ads-group]'; 17 18 /** 19 * Run a callback once the document is ready. 20 * 21 * Advanced Ads exposes advanced_ads_ready(); fall back to a listener when 22 * the plugin's inline script is not present. 23 * 24 * @param {Function} callback Callback to run. 25 */ 26 function onReady(callback) { 27 if (typeof window.advanced_ads_ready === 'function') { 28 window.advanced_ads_ready(callback, 'interactive'); 29 return; 30 } 31 32 if (document.readyState !== 'loading') { 33 callback(); 34 return; 35 } 36 37 document.addEventListener('DOMContentLoaded', callback, {once: true}); 38 } 39 40 /** 41 * Insert the returned ad markup and run any scripts it contains. 42 * 43 * Ads can carry inline <script> tags (tracking, AdSense). Nodes inserted 44 * through innerHTML never execute, so they are rebuilt to run. 45 * 46 * @param {HTMLElement} placeholder Element to fill. 47 * @param {string} markup Ad markup. 48 */ 49 function fill(placeholder, markup) { 50 if (!placeholder || !markup) { 51 return; 52 } 53 54 placeholder.innerHTML = markup; 55 56 var scripts = placeholder.querySelectorAll('script'); 57 58 Array.prototype.forEach.call(scripts, function (original) { 59 var replacement = document.createElement('script'); 60 61 Array.prototype.forEach.call(original.attributes, function (attribute) { 62 replacement.setAttribute(attribute.name, attribute.value); 63 }); 64 65 if (original.src) { 66 replacement.async = false; 67 } else { 68 replacement.textContent = original.textContent; 69 } 70 71 original.parentNode.replaceChild(replacement, original); 72 }); 73 } 74 75 /** 76 * Request one ad and fill its placeholder. 77 * 78 * @param {HTMLElement} placeholder Element to fill. 79 */ 80 function loadAd(placeholder) { 81 var groupId = placeholder.getAttribute('data-niu-ads-group'); 82 83 if (!groupId) { 84 return; 85 } 86 87 var body = new FormData(); 88 body.append('action', 'advads_ad_select'); 89 body.append('ad_method', 'group'); 90 body.append('ad_id', groupId); 91 92 if (placeholder.id) { 93 body.append('elementId', placeholder.id); 94 } 95 96 fetch(window.niuAdsPar.ajaxUrl, { 97 method: 'POST', 98 credentials: 'same-origin', 99 body: body 100 }) 101 .then(function (response) { 102 if (!response.ok) { 103 throw new Error('Ad request failed with status ' + response.status); 104 } 105 106 return response.json(); 107 }) 108 .then(function (data) { 109 if (!data || data.status !== 'success') { 110 return; 111 } 112 113 fill(placeholder, data.item); 114 }) 115 .catch(function () { 116 // Leave the placeholder empty; the layout already reserves the space. 117 }); 118 } 119 120 /** 121 * Request the ads for every placeholder on the page. 122 */ 123 function loadAds() { 124 if (typeof window.niuAdsPar === 'undefined' || !window.niuAdsPar.ajaxUrl) { 125 return; 126 } 127 128 var placeholders = document.querySelectorAll(SELECTOR); 129 130 Array.prototype.forEach.call(placeholders, loadAd); 131 } 132 133 onReady(loadAds); 134})();
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.