Bilibili-Evolved/min/image-resolution.min.js
2021-08-21 16:39:12 +08:00

1 line
1.6 KiB
JavaScript

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