single-file-hooks-frames.js 9.9 KB
!function(){"use strict";(e=>{const t="single-file-lazy-load",n="single-file-load-image",i="single-file-request-get-adopted-stylesheets",r={family:"font-family",style:"font-style",weight:"font-weight",stretch:"font-stretch",unicodeRange:"unicode-range",variant:"font-variant",featureSettings:"font-feature-settings"},o=e.fetch.bind(e),l=e.CustomEvent,s=e.document,d=e.screen,a=e.Element,c=e.UIEvent,g=e.Event,f=e.FileReader,_=e.Blob,m=e.JSON,u=e.MutationObserver,h=e.URL,p=new Map,y=new Map;let E;function v(){s.addEventListener("single-file-load-deferred-images-start",(()=>w())),s.addEventListener("single-file-load-deferred-images-keep-zoom-level-start",(()=>w(!0))),s.addEventListener("single-file-load-deferred-images-end",(()=>F())),s.addEventListener("single-file-load-deferred-images-keep-zoom-level-end",(()=>F(!0))),s.addEventListener("single-file-load-deferred-images-reset",b),s.addEventListener("single-file-load-deferred-images-keep-zoom-level-reset",(()=>{const e=s.documentElement.style.getPropertyValue("-sf-transform"),t=s.documentElement.style.getPropertyPriority("-sf-transform"),n=s.documentElement.style.getPropertyValue("-sf-transform-origin"),i=s.documentElement.style.getPropertyPriority("-sf-transform-origin"),r=s.documentElement.style.getPropertyValue("-sf-min-height"),o=s.documentElement.style.getPropertyPriority("-sf-min-height");s.documentElement.style.setProperty("transform",e,t),s.documentElement.style.setProperty("transform-origin",n,i),s.documentElement.style.setProperty("min-height",r,o),s.documentElement.style.removeProperty("-sf-transform"),s.documentElement.style.removeProperty("-sf-transform-origin"),s.documentElement.style.removeProperty("-sf-min-height"),b()})),s.addEventListener("single-file-dispatch-scroll-event-start",(()=>{E=!0})),s.addEventListener("single-file-dispatch-scroll-event-end",(()=>{E=!1})),s.addEventListener("single-file-block-cookies-start",(()=>{try{s.__defineGetter__("cookie",(()=>{throw new Error("document.cookie temporary blocked by SingleFile")}))}catch(e){}})),s.addEventListener("single-file-block-cookies-end",(()=>{delete s.cookie})),s.addEventListener("single-file-block-storage-start",(()=>{e._singleFile_localStorage||(e._singleFile_localStorage=e.localStorage,e.__defineGetter__("localStorage",(()=>{throw new Error("localStorage temporary blocked by SingleFile")}))),e._singleFile_indexedDB||(e._singleFile_indexedDB=e.indexedDB,e.__defineGetter__("indexedDB",(()=>{throw new Error("indexedDB temporary blocked by SingleFile")})))})),s.addEventListener("single-file-block-storage-end",(()=>{e._singleFile_localStorage&&(delete e.localStorage,e.localStorage=e._singleFile_localStorage,delete e._singleFile_localStorage),e._singleFile_indexedDB||(delete e.indexedDB,e.indexedDB=e._singleFile_indexedDB,delete e._singleFile_indexedDB)})),s.addEventListener("single-file-request-fetch-supported",(()=>s.dispatchEvent(new l("single-file-response-fetch-supported")))),s.addEventListener("single-file-request-fetch",(async e=>{const{url:t,options:n}=m.parse(e.detail);let i;try{const e=await o(t,n);i={url:t,response:await e.arrayBuffer(),headers:[...e.headers],status:e.status}}catch(e){i={url:t,error:e&&(e.message||e.toString())}}s.dispatchEvent(new l("single-file-response-fetch",{detail:i}))})),s.addEventListener(i,S),s.addEventListener("single-file-bootstrap",(t=>{try{e.bootstrap&&t.detail.data&&e.bootstrap(t.detail.data)}catch(e){}}))}function w(i){const r=s.scrollingElement||s.documentElement,o=r.clientHeight,c=r.clientWidth,f=Math.max(r.scrollHeight-o,o),_=Math.max(r.scrollWidth-c,c);if(s.querySelectorAll("[loading=lazy]").forEach((e=>{e.loading="eager",e.setAttribute(t,"")})),r.__defineGetter__("clientHeight",(()=>f)),r.__defineGetter__("clientWidth",(()=>_)),d.__defineGetter__("height",(()=>f)),d.__defineGetter__("width",(()=>_)),e._singleFile_innerHeight=e.innerHeight,e._singleFile_innerWidth=e.innerWidth,e.__defineGetter__("innerHeight",(()=>f)),e.__defineGetter__("innerWidth",(()=>_)),i||e._singleFile_getBoundingClientRect||(e._singleFile_getBoundingClientRect=a.prototype.getBoundingClientRect,a.prototype.getBoundingClientRect=function(){const t=e._singleFile_getBoundingClientRect.call(this);return this==r&&(t.__defineGetter__("height",(()=>f)),t.__defineGetter__("bottom",(()=>f+t.top)),t.__defineGetter__("width",(()=>_)),t.__defineGetter__("right",(()=>_+t.left))),t}),!e._singleFileImage){const t=e.Image;e._singleFileImage=e.Image,e.__defineGetter__("Image",(function(){return function(){const e=new t(...arguments),i=new t(...arguments);return i.__defineSetter__("src",(t=>{e.src=t,s.dispatchEvent(new l(n,{detail:e.src}))})),i.__defineGetter__("src",(()=>e.src)),i.__defineSetter__("srcset",(t=>{s.dispatchEvent(new l(n)),e.srcset=t})),i.__defineGetter__("srcset",(()=>e.srcset)),i.__defineGetter__("height",(()=>e.height)),i.__defineGetter__("width",(()=>e.width)),i.__defineGetter__("naturalHeight",(()=>e.naturalHeight)),i.__defineGetter__("naturalWidth",(()=>e.naturalWidth)),e.decode&&i.__defineGetter__("decode",(()=>()=>e.decode())),e.onload=e.onloadend=e.onerror=t=>{s.dispatchEvent(new l("single-file-image-loaded",{detail:e.src})),i.dispatchEvent(new g(t.type,t))},i}}))}let m,u;i?(m=o/f,u=c/_):(m=(o+e.scrollY)/f,u=(c+e.scrollX)/_);const h=Math.min(m,u);if(h<1){const e=s.documentElement.style.getPropertyValue("transform"),t=s.documentElement.style.getPropertyPriority("transform"),n=s.documentElement.style.getPropertyValue("transform-origin"),r=s.documentElement.style.getPropertyPriority("transform-origin"),o=s.documentElement.style.getPropertyValue("min-height"),l=s.documentElement.style.getPropertyPriority("min-height");s.documentElement.style.setProperty("transform-origin",(m<1?"50%":"0")+" "+(u<1?"50%":"0")+" 0","important"),s.documentElement.style.setProperty("transform","scale3d("+h+", "+h+", 1)","important"),s.documentElement.style.setProperty("min-height",100/h+"vh","important"),B(),i?(s.documentElement.style.setProperty("-sf-transform",e,t),s.documentElement.style.setProperty("-sf-transform-origin",n,r),s.documentElement.style.setProperty("-sf-min-height",o,l)):(s.documentElement.style.setProperty("transform",e,t),s.documentElement.style.setProperty("transform-origin",n,r),s.documentElement.style.setProperty("min-height",o,l))}if(!i){B();const e=r.getBoundingClientRect();window==window.top&&[...p].forEach((([t,n])=>{const i=n.options&&n.options.root&&n.options.root.getBoundingClientRect,r=i&&n.options.root.getBoundingClientRect(),o=y.get(t);if(o){const l=o.map((t=>{const n=t.getBoundingClientRect();return{target:t,intersectionRatio:1,boundingClientRect:n,intersectionRect:n,isIntersecting:!0,rootBounds:i?r:e,time:0}})).filter((e=>e.boundingClientRect.width&&e.boundingClientRect.height));l.length&&n.callback.call(t,l,t)}}))}}function F(n){s.querySelectorAll("["+t+"]").forEach((e=>{e.loading="lazy",e.removeAttribute(t)})),n||e._singleFile_getBoundingClientRect&&(a.prototype.getBoundingClientRect=e._singleFile_getBoundingClientRect,delete e._singleFile_getBoundingClientRect),e._singleFileImage&&(delete e.Image,e.Image=e._singleFileImage,delete e._singleFileImage),n||B()}function b(){const t=s.scrollingElement||s.documentElement;null!=e._singleFile_innerHeight&&(delete e.innerHeight,e.innerHeight=e._singleFile_innerHeight,delete e._singleFile_innerHeight),null!=e._singleFile_innerWidth&&(delete e.innerWidth,e.innerWidth=e._singleFile_innerWidth,delete e._singleFile_innerWidth),delete t.clientHeight,delete t.clientWidth,delete d.height,delete d.width}if(v(),new u(v).observe(s,{childList:!0}),e.CSS&&e.CSS.paintWorklet&&e.CSS.paintWorklet.addModule){const t=e.CSS.paintWorklet.addModule;e.CSS.paintWorklet.addModule=function(n,i){const r=t.apply(e.CSS.paintWorklet,arguments);return n=new h(n,s.baseURI).href,s.dispatchEvent(new l("single-file-new-worklet",{detail:{moduleURL:n,options:i}})),r}}if(e.FontFace){const t=e.FontFace;e.FontFace=function(){return P(...arguments).then((e=>s.dispatchEvent(new l("single-file-new-font-face",{detail:e})))),new t(...arguments)},e.FontFace.prototype=t.prototype,e.FontFace.toString=function(){return"function FontFace() { [native code] }"};const n=s.fonts.delete;s.fonts.delete=function(e){return P(e.family).then((e=>s.dispatchEvent(new l("single-file-delete-font",{detail:e})))),n.call(s.fonts,e)},s.fonts.delete.toString=function(){return"function delete() { [native code] }"};const i=s.fonts.clear;s.fonts.clear=function(){return s.dispatchEvent(new l("single-file-clear-fonts")),i.call(s.fonts)},s.fonts.clear.toString=function(){return"function clear() { [native code] }"}}if(e.IntersectionObserver){const t=e.IntersectionObserver;e.IntersectionObserver=function(){const e=new t(...arguments),n=t.prototype.observe||e.observe,i=t.prototype.unobserve||e.unobserve,r=arguments[0],o=arguments[1];return n&&(e.observe=function(t){let i=y.get(e);return i||(i=[],y.set(e,i)),i.push(t),n.call(e,t)}),i&&(e.unobserve=function(t){let n=y.get(e);return n&&(n=n.filter((e=>e!=t)),n.length?y.set(e,n):(y.delete(e),p.delete(e))),i.call(e,t)}),p.set(e,{callback:r,options:o}),e},e.IntersectionObserver.prototype=t.prototype,e.IntersectionObserver.toString=function(){return"function IntersectionObserver() { [native code] }"}}function S(e){const t=e.target.shadowRoot;if(e.stopPropagation(),t){t.addEventListener(i,S,{capture:!0}),t.addEventListener("single-file-unregister-request-get-adopted-stylesheets",(()=>t.removeEventListener(i,S)),{once:!0});const e=Array.from(t.adoptedStyleSheets).map((e=>Array.from(e.cssRules).map((e=>e.cssText)).join("\n")));e.length&&t.dispatchEvent(new l("single-file-response-get-adopted-stylesheets",{detail:{adoptedStyleSheets:e}}))}}async function P(e,t,n){const i={};return i["font-family"]=e,i.src=t,n&&Object.keys(n).forEach((e=>{r[e]&&(i[r[e]]=n[e])})),new Promise((e=>{if(i.src instanceof ArrayBuffer){const t=new f;t.readAsDataURL(new _([i.src])),t.addEventListener("load",(()=>{i.src="url("+t.result+")",e(i)}))}else e(i)}))}function B(){try{e.dispatchEvent(new c("resize")),E&&e.dispatchEvent(new c("scroll"))}catch(e){}}})("object"==typeof globalThis?globalThis:window)}();