Sincronización de Tema e iFrames Adaptativos
Documentación técnica sobre widget.js: detección de modo oscuro mediante la API postMessage y ajuste automático dinámico de altura.
Arquitectura de Sincronización de Tema e iFrames Adaptativos
Para garantizar que los widgets se integren a la perfección en los sitios de los editores sin barras de desplazamiento ni desajustes de color, The Comparator utiliza assets/js/widget.js.
1. Cómo Funciona la Sincronización de Tema
Los sitios web anfitriones suelen alternar entre modos Claro y Oscuro. El widget incrustado escucha los cambios de tema en la ventana principal mediante la API postMessage de HTML5.
[Sitio Anfitrión (Padre)] ── postMessage({ type: 'tc-theme', theme: 'dark' }) ──> [Widget Comparator (iFrame)]
Configuración del Script en el Sitio Anfitrión
Añade el script ligero widget.js en el pie de página de tu sitio:
<script src="https://thecomparator.tech/assets/js/widget.js" async></script>
Este script realiza automáticamente:
- Detección de mutaciones de la clase
darkendocument.documentElementodocument.body. - Comprobación de
localStorage.themeyprefers-color-scheme: dark. - Envío de eventos
tc-themea todos los iFrames de The Comparator activos en la página.
2. Ajuste Automático Dinámico de Altura
Para evitar barras de desplazamiento verticales dentro del iFrame, el widget calcula su altura de contenido dinámicamente y se la comunica a la ventana principal:
// Ejemplo del receptor en widget.js
window.addEventListener('message', function(event) {
if (event.data && event.data.type === 'tc-resize') {
const iframe = document.querySelector(`iframe[src*="${event.data.widgetId}"]`);
if (iframe) {
iframe.style.height = event.data.height + 'px';
}
}
});
3. Comportamiento sin JavaScript
Si el sitio anfitrión tiene JavaScript desactivado:
- El iFrame utiliza la altura predeterminada
height="450"especificada en el HTML. - El tema se basa en el parámetro
themeespecificado en la URL (?theme=lighto?theme=dark).
Theme Syncing & Responsive iFrame Architecture
To ensure widgets blend seamlessly into host publisher websites without scrollbars or color mismatch, The Comparator uses assets/js/widget.js.
1. How Theme Synchronization Works
Host websites often switch between Light and Dark modes. The embedded widget listens for theme changes on the parent window using the HTML5 postMessage API.
[Host Website (Parent)] ── postMessage({ type: 'tc-theme', theme: 'dark' }) ──> [Comparator Widget (iFrame)]
Parent Host Script Setup
Add the lightweight widget.js script to your page footer:
<script src="https://thecomparator.tech/assets/js/widget.js" async></script>
This script automatically:
- Detects
darkclass mutations ondocument.documentElementordocument.body. - Checks
localStorage.themeandprefers-color-scheme: dark. - Dispatches
tc-themeevents to all active Comparator iFrames on the page.
2. Dynamic Height Auto-Resizing
To prevent vertical scrollbars inside the iFrame, the widget calculates its content scroll height dynamically and notifies the parent window:
// Example inside widget.js listener
window.addEventListener('message', function(event) {
if (event.data && event.data.type === 'tc-resize') {
const iframe = document.querySelector(`iframe[src*="${event.data.widgetId}"]`);
if (iframe) {
iframe.style.height = event.data.height + 'px';
}
}
});
3. Fallback Without JavaScript
If the host site disables JavaScript:
- The iFrame falls back to the default
height="450"specified in HTML. - The theme defaults to the
themeparameter specified in the URL (?theme=lightor?theme=dark).