1/** 2 * Fresh-content hydration for the header news ticker and the homepage 3 * "most read" block. 4 * 5 * Both are server-rendered into the (Cloudflare / Super Page Cache) edge-cached 6 * HTML, so a page cached for the whole HTML TTL would otherwise show stale 7 * headlines until the object expires. This swaps in a fresh fragment on load 8 * and on a slow interval / tab focus, and re-runs the fragment's inline scripts 9 * so widget bindings (jNewsbar ticker, "show more headlines") keep working. 10 * External <script src> tags are left alone: the libraries are already loaded. 11 */ 12(function () { 13 'use strict'; 14 15 var cfg = window.afNewsTicker || {}; 16 var interval = parseInt(cfg.interval, 10) || 120000; 17 18 function runInlineScripts(container) { 19 var scripts = container.querySelectorAll('script'); 20 for (var i = 0; i < scripts.length; i++) { 21 var old = scripts[i]; 22 if (old.src) continue; 23 var fresh = document.createElement('script'); 24 fresh.textContent = old.textContent; 25 old.parentNode.replaceChild(fresh, old); 26 } 27 } 28 29 function hydrate(selector, url) { 30 var container = document.querySelector(selector); 31 if (!container || !url || container.getAttribute('data-loading') === '1') return; 32 container.setAttribute('data-loading', '1'); 33 34 var full = url + (url.indexOf('?') === -1 ? '?' : '&') + '_=' + Date.now(); 35 fetch(full, { headers: { Accept: 'application/json' }, credentials: 'same-origin' }) 36 .then(function (response) { return response.ok ? response.json() : null; }) 37 .then(function (data) { 38 container.removeAttribute('data-loading'); 39 if (!data || typeof data.html !== 'string' || data.html === '') return; 40 if (container.getAttribute('data-fresh-at') === String(data.generated_at)) return; 41 container.innerHTML = data.html; 42 container.setAttribute('data-fresh-at', String(data.generated_at)); 43 runInlineScripts(container); 44 }) 45 .catch(function () { container.removeAttribute('data-loading'); }); 46 } 47 48 function hydrateAll() { 49 hydrate('#af-news-ticker', cfg.restUrl || '/wp-json/anarchist/v1/news-ticker'); 50 hydrate('#af-most-read', cfg.mostReadUrl || '/wp-json/anarchist/v1/most-read'); 51 } 52 53 function start() { 54 hydrateAll(); 55 if (interval > 0) setInterval(hydrateAll, interval); 56 document.addEventListener('visibilitychange', function () { 57 if (!document.hidden) hydrateAll(); 58 }); 59 } 60 61 if (document.readyState === 'loading') { 62 document.addEventListener('DOMContentLoaded', start); 63 } else { 64 start(); 65 } 66})();
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.