(() => { const slots = []; function choose(slot, choices) { const selected = choices[Math.floor(Math.random() * choices.length)]; slot.replaceChildren(selected.content.cloneNode(true)); slot.dataset.adSelected = selected.dataset.adChoice; slot.hidden = false; } document.querySelectorAll('[data-random-article-ad]').forEach(slot => { const choices = Array.from(slot.querySelectorAll('template[data-ad-choice]')); if (!choices.length) return; slots.push({ slot, choices }); choose(slot, choices); }); window.addEventListener('pageshow', event => { if (event.persisted) slots.forEach(({ slot, choices }) => choose(slot, choices)); }); })();