diff --git a/Assets/Js/adsense-loader.js b/Assets/Js/adsense-loader.js index 5b73da74..150a9dd3 100644 --- a/Assets/Js/adsense-loader.js +++ b/Assets/Js/adsense-loader.js @@ -258,6 +258,15 @@ // DETECCION DE LLENADO // ========================================================================= + /** @type {Map} */ + var pollIntervals = new Map(); + + /** Intervalo de polling rapido en ms */ + var POLL_INTERVAL = 50; + + /** Maximo de intentos de polling (50ms * 60 = 3 segundos max) */ + var MAX_POLL_ATTEMPTS = 60; + /** * Inicia la deteccion de llenado para un slot * @param {Element} slot @@ -269,33 +278,30 @@ return; } - // Configurar timeout - var timeoutId = setTimeout(function() { - debugLog('Timeout de llenado alcanzado'); - markSlotEmpty(slot); - }, CONFIG.fillTimeout); - fillTimeouts.set(slot, timeoutId); + // Estrategia: Polling rapido (50ms) para detectar data-ad-status lo antes posible. + // AdSense establece data-ad-status muy rapido despues de inyectar el iframe, + // pero MutationObserver a veces no lo detecta inmediatamente. + var pollCount = 0; + var pollId = setInterval(function() { + pollCount++; - // Configurar MutationObserver si hay soporte - if (hasMutationObserverSupport()) { - var mutationObserver = new MutationObserver(function(mutations) { - if (checkFillStatus(slot, ins)) { - // Ya procesado en checkFillStatus - } - }); + if (checkFillStatus(slot, ins)) { + // Estado detectado, limpiar polling + clearInterval(pollId); + pollIntervals.delete(slot); + return; + } - mutationObserver.observe(ins, { - attributes: true, - childList: true, - subtree: true, - attributeFilter: ['data-ad-status'] - }); + // Si alcanzamos el maximo de intentos, marcar como vacio + if (pollCount >= MAX_POLL_ATTEMPTS) { + debugLog('Polling timeout alcanzado (' + (pollCount * POLL_INTERVAL) + 'ms)'); + clearInterval(pollId); + pollIntervals.delete(slot); + markSlotEmpty(slot); + } + }, POLL_INTERVAL); - fillObservers.set(slot, mutationObserver); - } else { - // Sin MutationObserver, solo usar timeout - debugLog('Sin soporte MutationObserver, usando solo timeout'); - } + pollIntervals.set(slot, pollId); } /** @@ -353,17 +359,23 @@ } /** - * Limpia observadores y timeouts de un slot + * Limpia observadores, timeouts e intervalos de un slot * @param {Element} slot */ function cleanupSlot(slot) { - // Limpiar timeout + // Limpiar polling interval + if (pollIntervals.has(slot)) { + clearInterval(pollIntervals.get(slot)); + pollIntervals.delete(slot); + } + + // Limpiar timeout (legacy) if (fillTimeouts.has(slot)) { clearTimeout(fillTimeouts.get(slot)); fillTimeouts.delete(slot); } - // Limpiar MutationObserver + // Limpiar MutationObserver (legacy) if (fillObservers.has(slot)) { fillObservers.get(slot).disconnect(); fillObservers.delete(slot); diff --git a/functions.php b/functions.php index 87d970e3..af3c071f 100644 --- a/functions.php +++ b/functions.php @@ -18,7 +18,7 @@ if (!defined('ABSPATH')) { } // Definir constante de versión del tema -define('ROI_VERSION', '1.0.23'); +define('ROI_VERSION', '1.0.24'); // ============================================================================= // 1. CARGAR AUTOLOADER MANUAL