(function () {
'use strict';
var BUFFER = 1.5;
function processImages() {
var images = document.querySelectorAll(
'img:not([data-xtreme-lazy-checked])'
);
images.forEach(function (img) {
img.setAttribute('data-xtreme-lazy-checked', 'true');
var rect = img.getBoundingClientRect();
if (rect.top <= window.innerHeight * BUFFER) {
return;
}
if (!img.hasAttribute('loading')) {
img.setAttribute('loading', 'lazy');
}
if (!img.hasAttribute('fetchpriority')) {
img.setAttribute('fetchpriority', 'low');
}
});
}
function processIframes() {
document.querySelectorAll(
'iframe:not([data-xtreme-lazy-checked])'
).forEach(function (iframe) {
iframe.setAttribute(
'data-xtreme-lazy-checked',
'true'
);
var rect = iframe.getBoundingClientRect();
if (rect.top <= window.innerHeight * BUFFER) {
return;
}
iframe.setAttribute('loading', 'lazy');
});
}
function processPage() {
processImages();
processIframes();
}
if (document.readyState === 'loading') {
document.addEventListener(
'DOMContentLoaded',
processPage
);
} else {
processPage();
}
var observer = new MutationObserver(function (mutations) {
var changed = false;
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) {
changed = true;
}
});
if (changed) {
processPage();
}
});
if (document.body) {
observer.observe(document.body, {
childList: true,
subtree: true
});
}
})();