Fix image resolution not rounding numbers

This commit is contained in:
the1812 2019-04-21 11:09:31 +08:00
parent 02fe79586b
commit cf2b1cfedd
6 changed files with 12 additions and 12 deletions

View File

@ -244,7 +244,7 @@
根据屏幕DPI请求更高分辨率的图片, 例如DPI缩放200%则请求2倍的分辨率, 加载时间也会相应变长一些.
适用于2K, 4K等的显示屏, DPI缩放为100%的用户不需要此功能.
![image-resolution](images/compressed/image-resolution.jpg);
![image-resolution](images/compressed/image-resolution.jpg)
### 旧版动态跳转支持
将新版动态的链接换为旧版动态, 同时可在附加功能中在新旧动态间切换.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
(()=>{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=(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 s(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.attributes(t,()=>{e(t=>t.getAttribute("src"),(t,e)=>t.setAttribute("src",e));e(t=>t.style.backgroundImage,(t,e)=>t.style.backgroundImage=e)})}i(document.body,t=>s(t));Observer.childListSubtree(document.body,t=>{for(const e of t){for(const t of e.addedNodes){if(t instanceof HTMLElement){s(t);if(t.nodeName.toUpperCase()!=="IMG"){i(t,t=>s(t))}}}}});return{export:{imageResolution:s}}}})();
(()=>{return(t,e)=>{const r=/@(\d+)[Ww]_(\d+)[Hh]/;const o=t.imageResolutionScale==="auto"?window.devicePixelRatio:parseFloat(t.imageResolutionScale);const n=["#certify-img1","#certify-img2"];const i=(t,e)=>{const r=document.createTreeWalker(t,NodeFilter.SHOW_ELEMENT,null,false);let o=r.nextNode();while(o){e(o);o=r.nextNode()}};async function s(t){const e=(e,i)=>{const s=e(t);if(s===null){return}if(n.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=Math.round(o*parseInt(u)).toString();c=Math.round(o*parseInt(c)).toString();t.setAttribute("data-resolution-width",u);i(t,s.replace(r,`@${u}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)})}i(document.body,t=>s(t));Observer.childListSubtree(document.body,t=>{for(const e of t){for(const t of e.addedNodes){if(t instanceof HTMLElement){s(t);if(t.nodeName.toUpperCase()!=="IMG"){i(t,t=>s(t))}}}}});return{export:{imageResolution:s}}}})();

View File

@ -1,5 +1,5 @@
const regex = /@(\d+)[Ww]_(\d+)[Hh]/;
const dpi = settings.imageResolutionScale === "auto" ? window.devicePixelRatio : parseInt(settings.imageResolutionScale);
const dpi = settings.imageResolutionScale === "auto" ? window.devicePixelRatio : parseFloat(settings.imageResolutionScale);
const excludeSelectors = [
"#certify-img1",
"#certify-img2",
@ -43,8 +43,8 @@ export async function imageResolution(element) {
if (element.getAttribute("width") === null && element.getAttribute("height") === null) {
element.setAttribute("width", width);
}
width = (dpi * parseInt(width)).toString();
height = (dpi * parseInt(height)).toString();
width = Math.round(dpi * parseInt(width)).toString();
height = Math.round(dpi * parseInt(height)).toString();
element.setAttribute("data-resolution-width", width);
setValue(element, value.replace(regex, `@${width}w_${height}h`));
};

View File

@ -1,5 +1,5 @@
const regex = /@(\d+)[Ww]_(\d+)[Hh]/;
const dpi = settings.imageResolutionScale === "auto" ? window.devicePixelRatio : parseInt(settings.imageResolutionScale);
const dpi = settings.imageResolutionScale === "auto" ? window.devicePixelRatio : parseFloat(settings.imageResolutionScale);
const excludeSelectors = [
"#certify-img1",
"#certify-img2",
@ -52,8 +52,8 @@ export async function imageResolution(element: HTMLElement)
{
element.setAttribute("width", width);
}
width = (dpi * parseInt(width)).toString();
height = (dpi * parseInt(height)).toString();
width = Math.round(dpi * parseInt(width)).toString();
height = Math.round(dpi * parseInt(height)).toString();
element.setAttribute("data-resolution-width", width);
setValue(element, value.replace(regex, `@${width}w_${height}h`));
};