function rewriteInAttr(el, attr){ var v = el.getAttribute(attr); if(!v) return; // srcset-Listen if (attr.indexOf('srcset') !== -1) { var out = v.split(',').map(function(part){ var seg = part.trim().split(/\s+/); if(!seg[0]) return part; var url = seg[0]; if (url.indexOf('/images/testimonial/') === -1) return part; url = url.split('#')[0].split('?')[0].replace(/\.(jpe?g|png)$/i, '.webp'); seg[0] = url; return seg.join(' '); }).join(', '); el.setAttribute(attr, out); return; } // einfache URL-Attribute if (v.indexOf('/images/testimonial/') === -1) return; var base = v.split('#')[0].split('?')[0]; var webp = base.replace(/\.(jpe?g|png)$/i, '.webp'); if (webp !== base) el.setAttribute(attr, webp); } function swapScope(scope){ // und (für evtl. vorhandene ) scope.querySelectorAll('img,source').forEach(function(el){ ['src','data-src','data-lazy-src','srcset','data-srcset'].forEach(function(a){ rewriteInAttr(el, a); }); }); } function init(){ swapScope(document); // mehrfach nachfassen (Lazyload/JS-Nachladungen) setTimeout(function(){ swapScope(document); }, 200); setTimeout(function(){ swapScope(document); }, 800); var mo = new MutationObserver(function(muts){ muts.forEach(function(m){ m.addedNodes && m.addedNodes.forEach(function(n){ if(n.nodeType===1) swapScope(n); }); }); }); mo.observe(document.documentElement, {subtree:true, childList:true}); } window.addEventListener('load', function(){ canWebP(function(ok){ if(ok) init(); }); }); })();