Bilibili-Evolved/min/image-resolution.min.js
2021-03-08 23:00:14 +08:00

1 line
1.4 KiB
JavaScript

(()=>(t,e)=>{const n=/@(\d+)[Ww]_(\d+)[Hh]/;const o=t.imageResolutionScale==="auto"?window.devicePixelRatio:parseFloat(t.imageResolutionScale);const i=["#certify-img1","#certify-img2"];const r=(t,e)=>{const n=document.createNodeIterator(t,NodeFilter.SHOW_ELEMENT,null);let o=n.nextNode();while(o){if(o instanceof HTMLElement){e(o)}o=n.nextNode()}};async function a(t){const e=(e,r)=>{const a=e(t);if(a===null){return}if(i.some((e=>t.matches(e)))){return}const s=a.match(n);if(!s){return}let[,c,l]=s;let u=parseInt(t.getAttribute("data-resolution-width")||"0");if(parseInt(c)>=u&&u!==0){return}if(t.getAttribute("width")===null&&t.getAttribute("height")===null){t.setAttribute("width",c)}c=Math.round(o*parseInt(c)).toString();l=Math.round(o*parseInt(l)).toString();t.setAttribute("data-resolution-width",c);r(t,a.replace(n,`@${c}w_${l}h`))};Observer.attributes(t,(()=>{e((t=>t.getAttribute("src")),((t,e)=>t.setAttribute("src",e)));e((t=>t.style.backgroundImage),((t,e)=>t.style.backgroundImage=e))}))}const s=async()=>{r(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"){r(t,(t=>a(t)))}}}}}))};s();e.applyStyleFromText(`\n.favInfo-box .favInfo-cover img {\n width: 100% !important;\n object-position: left !important;\n}\n.bili-avatar-img {\n width: 100% !important;\n}\n`,"image-resolution-fix");return{export:{imageResolution:a}}})();