caterium-app/public/core/performance.js
2026-09-07 15:29:20 +03:00

22 lines
1.0 KiB
JavaScript

(()=>{
'use strict';
const critical=img=>img.closest('header,.brand,#sunCloudAuthGate,.sun-auth-gate')||img.id==='sunLoginLogo'||img.classList.contains('sun-live-catalog-logo');
const tune=img=>{
if(!(img instanceof HTMLImageElement)||critical(img))return;
if(!img.hasAttribute('loading'))img.loading='lazy';
if(!img.hasAttribute('decoding'))img.decoding='async';
if(!img.hasAttribute('fetchpriority'))img.setAttribute('fetchpriority','low');
};
const scan=root=>{
if(root instanceof HTMLImageElement)tune(root);
root?.querySelectorAll?.('img').forEach(tune);
};
const start=()=>{
scan(document);
const mo=new MutationObserver(records=>records.forEach(r=>r.addedNodes.forEach(n=>{if(n.nodeType===1)scan(n)})));
mo.observe(document.documentElement,{childList:true,subtree:true});
window.SunPerformance={scanImages:()=>scan(document),disconnect:()=>mo.disconnect()};
};
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',start,{once:true});else start();
})();