`;
}
// This is the NEW lazy-load behavior you pasted, but done safely from JS
function attachLazyGpt(slotId) {
const el = document.getElementById(slotId);
if (!el) return;
// Prevent double init
if (el.dataset.lazyInit === “1”) return;
el.dataset.lazyInit = “1”;
// Wait until required libs exist (inView + GPT + stpd)
let tries = 0;
const boot = () => {
tries++;
if (
typeof window.inView !== “function” ||
!window.googletag ||
!googletag.cmd ||
!window.stpd ||
!stpd.que
) {
if (tries < 80) return setTimeout(boot, 100);
return; // give up silently
}
// Lazy-load once when entering viewport
inView(`#${slotId}`).once(“enter”, (function () {
googletag.cmd.push(function () {
// Extra guard: only define/display once
const node = document.getElementById(slotId);
if (!node || node.dataset.gptInitialized === “1”) return;
node.dataset.gptInitialized = “1”;
// Your new code defines the slot on enter
googletag
.defineSlot(
`/22646657118/${slotId}`,
[[300, 250], [250, 250]]
,
slotId
)
.addService(googletag.pubads());
googletag.display(slotId);
stpd.que.push((function () {
stpd.initializeAdUnit(slotId);
}));
});
}));
};
boot();
}
function injectAds() {
let adIndex = 0;
for (let i = 0; i < allArticleParapgraphs.length; i++) {
if (adIndex >= adSlotIds.length) break;
const block = allArticleParapgraphs[i];
// Optional: inject immediately after image-based rebellt items
if (
INJECT_AFTER_IMAGE_REBELLT_ITEM &&
block.classList.contains(“rebellt-item”) &&
block.querySelector(“img”)
) {
const slotId = adSlotIds[adIndex];
if (!document.getElementById(slotId)) {
block.insertAdjacentHTML(“afterend”, createAdPlaceholder(slotId));
attachLazyGpt(slotId);
adIndex++;
}
continue;
}
// Normal cadence: every N blocks
if ((i + 1) % INJECT_EVERY === 0) {
const slotId = adSlotIds[adIndex];
if (!document.getElementById(slotId)) {
block.insertAdjacentHTML(“afterend”, createAdPlaceholder(slotId));
attachLazyGpt(slotId);
adIndex++;
}
}
}
}
injectAds();
})();