Bilibili-Evolved/min/image-resolution.min.js
2019-04-05 18:52:26 +08:00

1 line
1.3 KiB
JavaScript

(()=>{return(t,e)=>{const r=/@(\d+)[Ww]_(\d+)[Hh]/;const n=t.imageResolutionScale==="auto"?window.devicePixelRatio:parseInt(t.imageResolutionScale);const o=["#certify-img1","#certify-img2"];const i=[".jpg",".png",".gif",".webp"];const s=(t,e)=>{const r=document.createTreeWalker(t,NodeFilter.SHOW_ELEMENT,null,false);let n=r.nextNode();while(n){e(n);n=r.nextNode()}};async function a(t){const e=(e,i)=>{const s=e(t);if(s===null){return}if(o.some(e=>t.matches(e))){return}const a=s.match(r);if(!a){return}let[,u,c]=a;let l=parseInt(t.getAttribute("data-resolution-width")||"0");if(parseInt(u)>=l&&l!==0){return}if(t.getAttribute("width")===null&&t.getAttribute("height")===null){t.setAttribute("width",u)}u=(n*parseInt(u)).toString();c=(n*parseInt(c)).toString();t.setAttribute("data-resolution-width",u);i(t,s.replace(r,`@${u}w_${c}h`))};Observer.observe(t,()=>{e(t=>t.getAttribute("src"),(t,e)=>t.setAttribute("src",e));e(t=>t.style.backgroundImage,(t,e)=>t.style.backgroundImage=e)},{attributeFilter:["src","style"],attributes:true})}s(document.body,t=>a(t));Observer.childListSubtree(document.body,t=>{for(const e of t){for(const t of e.addedNodes){if(t instanceof HTMLElement){a(t);if(t.nodeName.toUpperCase()!=="IMG"){s(t,t=>a(t))}}}}});return{export:{imageResolution:a}}}})();