(function () { const BASE_URL = "https://backoffice.heynowbots.com/news-feed"; const CLASS_NAME = 'heynownews'; const WAIT_TIMEOUT = 10000; const global = window.HeyNowNews = window.HeyNowNews || {}; function ready(fn) { if (document.readyState === 'complete' || document.readyState === 'interactive') { setTimeout(fn, 0); } else { document.addEventListener('DOMContentLoaded', fn); } } function collectAutoContainers() { return Array.from(document.getElementsByClassName(CLASS_NAME)).filter( el => el.getAttribute('data-heynownews-autoload') !== 'false' ); } function waitForContainers() { return new Promise(resolve => { const existing = collectAutoContainers(); if (existing.length) { return resolve(existing); } const observer = new MutationObserver(() => { const found = collectAutoContainers(); if (found.length) { clearTimeout(timeoutId); observer.disconnect(); resolve(found); } }); const timeoutId = setTimeout(() => { observer.disconnect(); resolve(collectAutoContainers()); }, WAIT_TIMEOUT); observer.observe(document.documentElement, { childList: true, subtree: true }); }); } function renderArticles(containers, items) { containers.forEach(container => { container.innerHTML = ''; if (!Array.isArray(items) || !items.length) { const emptyEl = document.createElement('article'); emptyEl.className = 'heynownews-empty'; emptyEl.textContent = 'No hay noticias disponibles.'; container.appendChild(emptyEl); return; } items.forEach(item => { const article = document.createElement('article'); article.className = 'heynownews-article'; const title = document.createElement('h3'); title.textContent = item && item.title ? item.title : 'Sin título'; const content = document.createElement('div'); content.className = 'heynownews-content'; content.innerHTML = (item && item.content) || ''; article.appendChild(title); article.appendChild(content); container.appendChild(article); }); }); } function renderError(containers) { containers.forEach(container => { container.innerHTML = ''; const article = document.createElement('article'); article.className = 'heynownews-error'; article.textContent = 'No pudimos cargar las noticias.'; container.appendChild(article); }); } function bootstrap() { waitForContainers() .then(containers => { if (!containers.length) return null; return fetch(BASE_URL + '/items', { mode: 'cors', cache: 'no-cache' }) .then(resp => { if (!resp.ok) throw new Error('Network'); return resp.json(); }) .then(items => renderArticles(containers, items)) .catch(() => renderError(containers)); }) .catch(() => {}); } global.render = function render(container, items) { if (!container) return; renderArticles([container], Array.isArray(items) ? items : []); }; global.renderError = function renderPreviewError(container) { if (!container) return; renderError([container]); }; document.dispatchEvent(new CustomEvent('HeyNowNewsReady')); ready(bootstrap); })();