(function(){
'use strict';
if(!('IntersectionObserver' in window)){
console.warn('EmbedPress Lazy Load: IntersectionObserver not supported, loading all iframes immediately');
loadAllIframes();
return;
}
function getObserverConfig(){
const viewportHeight=window.innerHeight;
const margin=Math.min(200, Math.max(50, viewportHeight * 0.25));
return {
rootMargin: margin + 'px 0px',
threshold: 0.01
};}
const observerConfig=getObserverConfig();
function onIntersection(entries, observer){
entries.forEach(function(entry){
if(entry.isIntersecting){
const placeholder=entry.target;
loadIframe(placeholder);
observer.unobserve(placeholder);
}});
}
function loadIframe(placeholder){
const iframeSrc=placeholder.getAttribute('data-ep-lazy-src');
if(!iframeSrc){
console.warn('EmbedPress Lazy Load: No data-ep-lazy-src found on placeholder');
return;
}
const iframe=document.createElement('iframe');
iframe.src=iframeSrc;
const iframeStyle=placeholder.getAttribute('data-ep-iframe-style');
if(iframeStyle){
iframe.setAttribute('style', iframeStyle);
}
Array.from(placeholder.attributes).forEach(function(attr){
if(attr.name.startsWith('data-ep-iframe-')){
const iframeAttr=attr.name.replace('data-ep-iframe-', '');
if(iframeAttr!=='style'){
iframe.setAttribute(iframeAttr, attr.value);
}}
});
if(placeholder.hasAttribute('class')){
const classes=placeholder.getAttribute('class').replace(/ep-lazy-iframe-placeholder/g, '').trim();
if(classes){
iframe.setAttribute('class', classes);
}}
['data-emid', 'data-embedpress-content', 'data-embed-type', 'title'].forEach(function(attr){
if(placeholder.hasAttribute(attr)){
iframe.setAttribute(attr, placeholder.getAttribute(attr));
}});
if(placeholder.parentNode){
placeholder.parentNode.replaceChild(iframe, placeholder);
}}
function loadAllIframes(){
const placeholders=document.querySelectorAll('.ep-lazy-iframe-placeholder');
placeholders.forEach(function(placeholder){
loadIframe(placeholder);
});
}
function initLazyLoad(){
const placeholders=document.querySelectorAll('.ep-lazy-iframe-placeholder');
if(placeholders.length===0){
return;
}
const observer=new IntersectionObserver(onIntersection, observerConfig);
placeholders.forEach(function(placeholder){
observer.observe(placeholder);
});
}
if(document.readyState==='loading'){
setTimeout(initLazyLoad, 1000);
}else{
setTimeout(initLazyLoad, 1000);
}
window.epReinitLazyLoad=initLazyLoad;
})();