(() => { let requestId = 0; let initialScrollStarted = false; let initialCheckTimer = null; function getTarget(hash) { if (!hash || hash === "#") return null; let id; try { id = decodeURIComponent(hash.slice(1)); } catch { id = hash.slice(1); } return document.getElementById(id); } function waitForImage(img) { img.loading = "eager"; if (img.complete) { return Promise.resolve(); } return new Promise(resolve => { img.addEventListener("load", resolve, { once: true }); img.addEventListener("error", resolve, { once: true }); }); } function alignTarget(target) { requestAnimationFrame(() => { requestAnimationFrame(() => { target.scrollIntoView({ block: "start", behavior: "auto" }); }); }); } async function fixAnchor(hash) { const target = getTarget(hash); if (!target) return false; const currentRequestId = ++requestId; const imagesBeforeTarget = [ ...document.querySelectorAll( ".blog_rich-text img" ) ].filter(img => { return Boolean( img.compareDocumentPosition(target) & Node.DOCUMENT_POSITION_FOLLOWING ); }); let scrollIsFinalized = false; imagesBeforeTarget.forEach(img => { img.loading = "eager"; if (!img.complete) { img.addEventListener("load", () => { if ( scrollIsFinalized && currentRequestId === requestId && window.location.hash === hash ) { alignTarget(target); } }, { once: true }); } }); await Promise.race([ Promise.all( imagesBeforeTarget.map(waitForImage) ), new Promise(resolve => { setTimeout(resolve, 10000); }) ]); if (currentRequestId !== requestId) { return true; } if (document.fonts?.ready) { await document.fonts.ready; } if (currentRequestId !== requestId) { return true; } scrollIsFinalized = true; alignTarget(target); return true; } function tryInitialScroll() { if (initialScrollStarted) { return true; } const hash = window.location.hash; const target = getTarget(hash); /* * Não marca a rotina como iniciada enquanto * o hash ou o elemento ainda não existirem. */ if (!hash || !target) { return false; } initialScrollStarted = true; fixAnchor(hash); return true; } function beginInitialChecks() { if (tryInitialScroll()) return; if (initialCheckTimer !== null) return; let attempts = 0; /* * Aguarda até 5 segundos para o Webflow e o * Finsweet disponibilizarem o hash e a seção. */ initialCheckTimer = window.setInterval(() => { attempts += 1; const started = tryInitialScroll(); if (started || attempts >= 50) { clearInterval(initialCheckTimer); initialCheckTimer = null; } }, 100); } if (document.readyState === "loading") { document.addEventListener( "DOMContentLoaded", beginInitialChecks, { once: true } ); } else { beginInitialChecks(); } window.addEventListener( "load", beginInitialChecks, { once: true } ); window.addEventListener("hashchange", () => { fixAnchor(window.location.hash); }); })();