single-file-frames.js 23 KB

1
  1. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).singlefile={})}(this,(function(e){"use strict";const t="single-file-load-deferred-images-start",s="single-file-load-deferred-images-end",o="single-file-load-deferred-images-keep-zoom-level-start",n="single-file-load-deferred-images-keep-zoom-level-end",i="single-file-block-cookies-start",r="single-file-block-cookies-end",a="single-file-dispatch-scroll-event-start",l="single-file-dispatch-scroll-event-end",d="single-file-block-storage-start",c="single-file-block-storage-end",u="single-file-load-image",m="single-file-image-loaded",h="_singleFile_fontFaces",g="_singleFile_worklets",p=globalThis.CustomEvent,f=globalThis.document,b=globalThis.Document,E=globalThis.JSON,T=globalThis.MutationObserver;let y,w;function I(){f instanceof b&&(f.addEventListener("single-file-new-font-face",(e=>{const t=e.detail,s=Object.assign({},t);delete s.src,y.set(E.stringify(s),t)})),f.addEventListener("single-file-delete-font",(e=>{const t=e.detail,s=Object.assign({},t);delete s.src,y.delete(E.stringify(s))})),f.addEventListener("single-file-clear-fonts",(()=>y=new Map)),f.addEventListener("single-file-new-worklet",(e=>{const t=e.detail;w.set(t.moduleURL,t)})))}y=window[h]?window[h]:window[h]=new Map,w=window[g]?window[g]:window[g]=new Map,I(),new T(I).observe(f,{childList:!0});const v="[\\x20\\t\\r\\n\\f]",A=new RegExp("\\\\([\\da-f]{1,6}"+v+"?|("+v+")|.)","ig");const S="single-file-",R="__frameTree__::",_=S+"on-before-capture",M=S+"on-after-capture",P=S+"request-get-adopted-stylesheets",N=S+"response-get-adopted-stylesheets",C=S+"unregister-request-get-adopted-stylesheets",F="data-"+S+"removed-content",L="data-"+S+"hidden-content",x="data-"+S+"kept-content",k="data-"+S+"hidden-frame",q="data-"+S+"preserved-space-element",U="data-"+S+"shadow-root-element",D="data-"+S+"win-id",O="data-"+S+"image",V="data-"+S+"poster",W="data-"+S+"video",H="data-"+S+"canvas",z="data-"+S+"movable-style",B="data-"+S+"input-value",j="data-"+S+"input-checked",Y="data-"+S+"lazy-loaded-src",G="data-"+S+"stylesheet",Z="data-"+S+"disabled-noscript",J="data-"+S+"invalid-element",K="data-"+S+"async-script",$="*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)",X=["NOSCRIPT","DISABLED-NOSCRIPT","META","LINK","STYLE","TITLE","TEMPLATE","SOURCE","OBJECT","SCRIPT","HEAD","BODY"],Q=["SCRIPT","NOSCRIPT","META","LINK","TEMPLATE"],ee=/^'(.*?)'$/,te=/^"(.*?)"$/,se={regular:"400",normal:"400",bold:"700",bolder:"700",lighter:"100"},oe="single-file-ui-element",ne="single-file-infobar",ie="data:,",re=globalThis.JSON,ae=globalThis.CustomEvent,le=globalThis.URL;function de(e,t,s,o,n={usedFonts:new Map,canvases:[],images:[],posters:[],videos:[],shadowRoots:[],markedElements:[]},i){if(s.childNodes){Array.from(s.childNodes).filter((t=>t instanceof e.HTMLElement||t instanceof e.SVGElement||t instanceof globalThis.HTMLElement||t instanceof globalThis.SVGElement)).forEach((s=>{let r,a,l;if(!o.autoSaveExternalSave&&(o.removeHiddenElements||o.removeUnusedFonts||o.compressHTML)&&(l=be(e,s),(s instanceof e.HTMLElement||s instanceof globalThis.HTMLElement)&&o.removeHiddenElements&&(a=(i||s.closest("html > head"))&&X.includes(s.tagName.toUpperCase())||s.closest("details"),a||(r=i||ge(s,l),r&&!Q.includes(s.tagName.toUpperCase())&&(s.setAttribute(L,""),n.markedElements.push(s)))),!r)){if(o.compressHTML&&l){const e=l.getPropertyValue("white-space");e&&e.startsWith("pre")&&(s.setAttribute(q,""),n.markedElements.push(s))}o.removeUnusedFonts&&(ue(l,o,n.usedFonts),ue(be(e,s,":first-letter"),o,n.usedFonts),ue(be(e,s,":before"),o,n.usedFonts),ue(be(e,s,":after"),o,n.usedFonts))}!function(e,t,s,o,n,i,r){const a=s.tagName&&s.tagName.toUpperCase();if("CANVAS"==a)try{n.canvases.push({dataURI:s.toDataURL("image/png",""),backgroundColor:r.getPropertyValue("background-color")}),s.setAttribute(H,n.canvases.length-1),n.markedElements.push(s)}catch(e){}if("IMG"==a){const t={currentSrc:i?ie:o.loadDeferredImages&&s.getAttribute(Y)||s.currentSrc};if(n.images.push(t),s.setAttribute(O,n.images.length-1),n.markedElements.push(s),s.removeAttribute(Y),r=r||be(e,s)){t.size=function(e,t,s){let o=t.naturalWidth,n=t.naturalHeight;if(!o&&!n){const i=null==t.getAttribute("style");if(s=s||be(e,t)){let e,r,a,l,d,c,u,m,h=!1;if("content-box"==s.getPropertyValue("box-sizing")){const e=t.style.getPropertyValue("box-sizing"),s=t.style.getPropertyPriority("box-sizing"),o=t.clientWidth;t.style.setProperty("box-sizing","border-box","important"),h=t.clientWidth!=o,e?t.style.setProperty("box-sizing",e,s):t.style.removeProperty("box-sizing")}e=fe("padding-left",s),r=fe("padding-right",s),a=fe("padding-top",s),l=fe("padding-bottom",s),h?(d=fe("border-left-width",s),c=fe("border-right-width",s),u=fe("border-top-width",s),m=fe("border-bottom-width",s)):d=c=u=m=0,o=Math.max(0,t.clientWidth-e-r-d-c),n=Math.max(0,t.clientHeight-a-l-u-m),i&&t.removeAttribute("style")}}return{pxWidth:o,pxHeight:n}}(e,s,r);const o=r.getPropertyValue("box-shadow"),n=r.getPropertyValue("background-image");o&&"none"!=o||n&&"none"!=n||!(t.size.pxWidth>1||t.size.pxHeight>1)||(t.replaceable=!0,t.backgroundColor=r.getPropertyValue("background-color"),t.objectFit=r.getPropertyValue("object-fit"),t.boxSizing=r.getPropertyValue("box-sizing"),t.objectPosition=r.getPropertyValue("object-position"))}}if("VIDEO"==a){const o=s.currentSrc;if(o&&!o.startsWith("blob:")&&!o.startsWith("data:")){const t=be(e,s.parentNode);n.videos.push({positionParent:t&&t.getPropertyValue("position"),src:o,size:{pxWidth:s.clientWidth,pxHeight:s.clientHeight},currentTime:s.currentTime}),s.setAttribute(W,n.videos.length-1)}if(!s.getAttribute("poster")){const e=t.createElement("canvas"),o=e.getContext("2d");e.width=s.clientWidth,e.height=s.clientHeight;try{o.drawImage(s,0,0,e.width,e.height),n.posters.push(e.toDataURL("image/png","")),s.setAttribute(V,n.posters.length-1),n.markedElements.push(s)}catch(e){}}}"IFRAME"==a&&i&&o.removeHiddenElements&&(s.setAttribute(k,""),n.markedElements.push(s));"INPUT"==a&&("password"!=s.type&&(s.setAttribute(B,s.value),n.markedElements.push(s)),"radio"!=s.type&&"checkbox"!=s.type||(s.setAttribute(j,s.checked),n.markedElements.push(s)));"TEXTAREA"==a&&(s.setAttribute(B,s.value),n.markedElements.push(s));"SELECT"==a&&s.querySelectorAll("option").forEach((e=>{e.selected&&(e.setAttribute(B,""),n.markedElements.push(e))}));"SCRIPT"==a&&(s.async&&""!=s.getAttribute("async")&&"async"!=s.getAttribute("async")&&(s.setAttribute(K,""),n.markedElements.push(s)),s.textContent=s.textContent.replace(/<\/script>/gi,"<\\/script>"))}(e,t,s,o,n,r,l);const d=!(s instanceof e.SVGElement||s instanceof globalThis.SVGElement)&&me(s);if(d&&!s.classList.contains(oe)&&s.tagName.toLowerCase()!=ne){const i={};s.setAttribute(U,n.shadowRoots.length),n.markedElements.push(s),n.shadowRoots.push(i);try{if(d.adoptedStyleSheets)if(d.adoptedStyleSheets.length)i.adoptedStyleSheets=ce(d.adoptedStyleSheets);else if(void 0===d.adoptedStyleSheets.length){const e=e=>i.adoptedStyleSheets=e.detail.adoptedStyleSheets;d.addEventListener(N,e),d.dispatchEvent(new ae(P,{bubbles:!0})),i.adoptedStyleSheets||s.dispatchEvent(new ae(P,{bubbles:!0})),d.removeEventListener(N,e)}}catch(e){}de(e,t,d,o,n,r),i.content=d.innerHTML,i.mode=d.mode,i.delegateFocus=d.delegatesFocus,i.clonable=d.clonable,i.serializable=d.serializable;try{d.adoptedStyleSheets&&void 0===d.adoptedStyleSheets.length&&d.dispatchEvent(new ae(C,{bubbles:!0}))}catch(e){}}de(e,t,s,o,n,r),!o.autoSaveExternalSave&&o.removeHiddenElements&&i&&(a||""==s.getAttribute(x)?s.parentElement&&(s.parentElement.setAttribute(x,""),n.markedElements.push(s.parentElement)):r&&(s.setAttribute(F,""),n.markedElements.push(s)))}))}return n}function ce(e){return e?Array.from(e).map((e=>Array.from(e.cssRules).map((e=>e.cssText)).join("\n"))):[]}function ue(e,t,s){if(e){const o=e.getPropertyValue("font-style")||"normal";e.getPropertyValue("font-family").split(",").forEach((n=>{if(n=he(n),!t.loadedFonts||t.loadedFonts.find((e=>he(e.family)==n&&e.style==o))){const t=(i=e.getPropertyValue("font-weight"),se[i.toLowerCase().trim()]||i),r=e.getPropertyValue("font-variant")||"normal",a=[n,t,o,r];s.set(re.stringify(a),[n,t,o,r])}var i}))}}function me(e){const t=globalThis.chrome;if(e.openOrClosedShadowRoot)return e.openOrClosedShadowRoot;if(!(t&&t.dom&&t.dom.openOrClosedShadowRoot))return e.shadowRoot;try{return t.dom.openOrClosedShadowRoot(e)}catch(t){return e.shadowRoot}}function he(e=""){return function(e){e=e.match(ee)?e.replace(ee,"$1"):e.replace(te,"$1");return e.trim()}((t=e.trim(),t.replace(A,((e,t,s)=>{const o="0x"+t-65536;return o!=o||s?t:o<0?String.fromCharCode(o+65536):String.fromCharCode(o>>10|55296,1023&o|56320)})))).toLowerCase();var t}function ge(e,t){let s=!1;if(t){const o=t.getPropertyValue("display"),n=t.getPropertyValue("opacity"),i=t.getPropertyValue("visibility");if(s="none"==o,!s&&("0"==n||"hidden"==i)&&e.getBoundingClientRect){const t=e.getBoundingClientRect();s=!t.width&&!t.height}}return Boolean(s)}function pe(e){if(e){const t=[];return e.querySelectorAll("style").forEach(((s,o)=>{try{if(!s.sheet.disabled){const n=e.createElement("style");n.textContent=s.textContent,e.body.appendChild(n);const i=n.sheet;n.remove();const r=Array.from(i.cssRules).map((e=>e.cssText)).join("\n"),a=Array.from(s.sheet.cssRules).map((e=>e.cssText)).join("\n");i&&r==a||(s.setAttribute(G,o),t[o]=Array.from(s.sheet.cssRules).map((e=>e.cssText)).join("\n"))}}catch(e){}})),t}}function fe(e,t){if(t.getPropertyValue(e).endsWith("px"))return parseFloat(t.getPropertyValue(e))}function be(e,t,s){try{return e.getComputedStyle(t,s)}catch(e){}}const Ee={LAZY_SRC_ATTRIBUTE_NAME:Y,SINGLE_FILE_UI_ELEMENT_CLASS:oe},Te=10,ye="attributes",we=globalThis.browser,Ie=globalThis.document,ve=globalThis.MutationObserver,Ae=new Map;let Se;async function Re(e){if(Ie.documentElement){Ae.clear();const s=Ie.body?Math.max(Ie.body.scrollHeight,Ie.documentElement.scrollHeight):Ie.documentElement.scrollHeight,n=Ie.body?Math.max(Ie.body.scrollWidth,Ie.documentElement.scrollWidth):Ie.documentElement.scrollWidth;if(s>globalThis.innerHeight||n>globalThis.innerWidth){const r=Math.max(s-1.5*globalThis.innerHeight,0),l=Math.max(n-1.5*globalThis.innerWidth,0);if(globalThis.scrollY<r||globalThis.scrollX<l)return function(e){return Se=0,new Promise((async s=>{let n;const r=new Set,l=new ve((async t=>{if((t=t.filter((e=>e.type==ye))).length){t.filter((e=>{if("src"==e.attributeName&&(e.target.setAttribute(Ee.LAZY_SRC_ATTRIBUTE_NAME,e.target.src),e.target.addEventListener("load",h)),"src"==e.attributeName||"srcset"==e.attributeName||e.target.tagName&&"SOURCE"==e.target.tagName.toUpperCase())return!e.target.classList||!e.target.classList.contains(Ee.SINGLE_FILE_UI_ELEMENT_CLASS)})).length&&(n=!0,await Me(l,e,E),r.size||await _e(l,e,E))}}));async function c(t){await Ne("idleTimeout",(async()=>{n?Se<Te&&(Se++,Fe("idleTimeout"),await c(Math.max(500,t/2))):(Fe("loadTimeout"),Fe("maxTimeout"),Pe(l,e,E))}),t,e.loadDeferredImagesNativeTimeout)}function h(e){const t=e.target;t.removeAttribute(Ee.LAZY_SRC_ATTRIBUTE_NAME),t.removeEventListener("load",h)}async function g(t){n=!0,await Me(l,e,E),await _e(l,e,E),t.detail&&r.add(t.detail)}async function b(t){await Me(l,e,E),await _e(l,e,E),r.delete(t.detail),r.size||await _e(l,e,E)}function E(e){l.disconnect(),Ie.removeEventListener(u,g),Ie.removeEventListener(m,b),s(e)}await c(2*e.loadDeferredImagesMaxIdleTime),await Me(l,e,E),l.observe(Ie,{subtree:!0,childList:!0,attributes:!0}),Ie.addEventListener(u,g),Ie.addEventListener(m,b),function(e){e.loadDeferredImagesBlockCookies&&f.dispatchEvent(new p(i)),e.loadDeferredImagesBlockStorage&&f.dispatchEvent(new p(d)),e.loadDeferredImagesDispatchScrollEvent&&f.dispatchEvent(new p(a)),e.loadDeferredImagesKeepZoomLevel?f.dispatchEvent(new p(o)):f.dispatchEvent(new p(t))}(e)}))}(e)}}}async function _e(e,t,s){await Ne("loadTimeout",(()=>Pe(e,t,s)),t.loadDeferredImagesMaxIdleTime,t.loadDeferredImagesNativeTimeout)}async function Me(e,t,s){await Ne("maxTimeout",(async()=>{await Fe("loadTimeout"),await Pe(e,t,s)}),10*t.loadDeferredImagesMaxIdleTime,t.loadDeferredImagesNativeTimeout)}async function Pe(e,t,o){await Fe("idleTimeout"),function(e){e.loadDeferredImagesBlockCookies&&f.dispatchEvent(new p(r)),e.loadDeferredImagesBlockStorage&&f.dispatchEvent(new p(c)),e.loadDeferredImagesDispatchScrollEvent&&f.dispatchEvent(new p(l)),e.loadDeferredImagesKeepZoomLevel?f.dispatchEvent(new p(n)):f.dispatchEvent(new p(s))}(t),await Ne("endTimeout",(async()=>{await Fe("maxTimeout"),o()}),t.loadDeferredImagesMaxIdleTime/2,t.loadDeferredImagesNativeTimeout),e.disconnect()}async function Ne(e,t,s,o){if(we&&we.runtime&&we.runtime.sendMessage&&!o){if(!Ae.get(e)||!Ae.get(e).pending){const o={callback:t,pending:!0};Ae.set(e,o);try{await we.runtime.sendMessage({method:"singlefile.lazyTimeout.setTimeout",type:e,delay:s})}catch(o){Ce(e,t,s)}o.pending=!1}}else Ce(e,t,s)}function Ce(e,t,s){const o=Ae.get(e);o&&globalThis.clearTimeout(o),Ae.set(e,t),globalThis.setTimeout(t,s)}async function Fe(e){if(we&&we.runtime&&we.runtime.sendMessage)try{await we.runtime.sendMessage({method:"singlefile.lazyTimeout.clearTimeout",type:e})}catch(t){Le(e)}else Le(e)}function Le(e){const t=Ae.get(e);Ae.delete(e),t&&globalThis.clearTimeout(t)}we&&we.runtime&&we.runtime.onMessage&&we.runtime.onMessage.addListener&&we.runtime.onMessage.addListener((e=>{if("singlefile.lazyTimeout.onTimeout"==e.method){const t=Ae.get(e.type);if(t){Ae.delete(e.type);try{t.callback()}catch(t){Le(e.type)}}}}));const xe={ON_BEFORE_CAPTURE_EVENT_NAME:_,ON_AFTER_CAPTURE_EVENT_NAME:M,WIN_ID_ATTRIBUTE_NAME:D,WAIT_FOR_USERSCRIPT_PROPERTY_NAME:"_singleFile_waitForUserScript",preProcessDoc:function(e,t,s){e.querySelectorAll("noscript:not(["+Z+"])").forEach((e=>{e.setAttribute(Z,e.textContent),e.textContent=""})),function(e){e.querySelectorAll("meta[http-equiv=refresh]").forEach((e=>{e.removeAttribute("http-equiv"),e.setAttribute("disabled-http-equiv","refresh")}))}(e),e.head&&e.head.querySelectorAll($).forEach((e=>e.hidden=!0)),e.querySelectorAll("svg foreignObject").forEach((e=>{const t=e.querySelectorAll("html > head > "+$+", html > body > "+$);t.length&&(Array.from(e.childNodes).forEach((e=>e.remove())),t.forEach((t=>e.appendChild(t))))}));const o=new Map;let n;return t&&e.documentElement?(e.querySelectorAll("button button, a a").forEach((t=>{const s=e.createElement("template");s.setAttribute(J,""),s.content.appendChild(t.cloneNode(!0)),o.set(t,s),t.replaceWith(s)})),n=de(t,e,e.documentElement,s),s.moveStylesInHead&&e.querySelectorAll("body style, body ~ style").forEach((e=>{const s=be(t,e);s&&ge(e,s)&&(e.setAttribute(z,""),n.markedElements.push(e))}))):n={canvases:[],images:[],posters:[],videos:[],usedFonts:[],shadowRoots:[],markedElements:[]},{canvases:n.canvases,fonts:Array.from(y.values()),worklets:Array.from(w.values()),stylesheets:pe(e),images:n.images,posters:n.posters,videos:n.videos,usedFonts:Array.from(n.usedFonts.values()),shadowRoots:n.shadowRoots,referrer:e.referrer?new le("/",new le(e.referrer).origin).href:"",markedElements:n.markedElements,invalidElements:o,scrollPosition:{x:t.scrollX,y:t.scrollY},adoptedStyleSheets:ce(e.adoptedStyleSheets)}},serialize:function(e){const t=e.doctype;let s="";return t&&(s="<!DOCTYPE "+t.nodeName,t.publicId?(s+=' PUBLIC "'+t.publicId+'"',t.systemId&&(s+=' "'+t.systemId+'"')):t.systemId&&(s+=' SYSTEM "'+t.systemId+'"'),t.internalSubset&&(s+=" ["+t.internalSubset+"]"),s+="> "),s+e.documentElement.outerHTML},postProcessDoc:function(e,t,s){if(e.querySelectorAll("["+Z+"]").forEach((e=>{e.textContent=e.getAttribute(Z),e.removeAttribute(Z)})),e.querySelectorAll("meta[disabled-http-equiv]").forEach((e=>{e.setAttribute("http-equiv",e.getAttribute("disabled-http-equiv")),e.removeAttribute("disabled-http-equiv")})),e.head&&e.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach((e=>e.removeAttribute("hidden"))),!t){const s=[F,k,L,q,O,V,W,H,B,j,U,G,K];t=e.querySelectorAll(s.map((e=>"["+e+"]")).join(","))}t.forEach((e=>{e.removeAttribute(F),e.removeAttribute(L),e.removeAttribute(x),e.removeAttribute(k),e.removeAttribute(q),e.removeAttribute(O),e.removeAttribute(V),e.removeAttribute(W),e.removeAttribute(H),e.removeAttribute(B),e.removeAttribute(j),e.removeAttribute(U),e.removeAttribute(G),e.removeAttribute(K),e.removeAttribute(z)})),s&&s.forEach(((e,t)=>e.replaceWith(t)))},getShadowRoot:me},ke='iframe, frame, object[type="text/html"][data]',qe="*",Ue="singlefile.frameTree.initRequest",De="singlefile.frameTree.ackInitRequest",Oe="singlefile.frameTree.cleanupRequest",Ve="singlefile.frameTree.initResponse",We="*",He=5e3,ze=1e4,Be=".",je=globalThis.window==globalThis.top,Ye=globalThis.browser,Ge=globalThis.top,Ze=globalThis.MessageChannel,Je=globalThis.document,Ke=globalThis.JSON,$e=globalThis.MutationObserver,Xe=globalThis.DOMParser;let Qe,et=globalThis.sessions;function tt(){globalThis.addEventListener("message",(async e=>{if("string"==typeof e.data&&e.data.startsWith(R)){e.preventDefault(),e.stopPropagation();const t=Ke.parse(e.data.substring(R.length));if(t.method==Ue)e.source&&ut(e.source,{method:De,windowId:t.windowId,sessionId:t.sessionId}),je||(globalThis.stop(),t.options.loadDeferredImages&&Re(t.options),await ot(t));else if(t.method==De)at("requestTimeouts",t.sessionId,t.windowId),lt(t.sessionId,t.windowId);else if(t.method==Oe)nt(t);else if(t.method==Ve&&et.get(t.sessionId)){e.ports[0].onmessage=e=>it(e.data)}}}),!0)}function st(){return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("")}async function ot(e){const t=e.sessionId,s=globalThis[xe.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];delete globalThis._singleFile_cleaningUp,je||(Qe=globalThis.frameId=e.windowId),rt(Je,e.options,Qe,t),je||(e.options.userScriptEnabled&&s&&await s(xe.ON_BEFORE_CAPTURE_EVENT_NAME),ct({frames:[mt(Je,globalThis,Qe,e.options,e.scrolling)],sessionId:t,requestedFrameId:Je.documentElement.dataset.requestedFrameId&&Qe}),e.options.userScriptEnabled&&s&&await s(xe.ON_AFTER_CAPTURE_EVENT_NAME),delete Je.documentElement.dataset.requestedFrameId)}function nt(e){if(!globalThis._singleFile_cleaningUp){globalThis._singleFile_cleaningUp=!0;const t=e.sessionId;dt(ht(Je),e.windowId,t)}}function it(e){e.frames.forEach((t=>at("responseTimeouts",e.sessionId,t.windowId)));const t=et.get(e.sessionId);if(t){e.requestedFrameId&&(t.requestedFrameId=e.requestedFrameId),e.frames.forEach((e=>{let s=t.frames.find((t=>e.windowId==t.windowId));s||(s={windowId:e.windowId},t.frames.push(s)),s.processed||(s.content=e.content,s.baseURI=e.baseURI,s.title=e.title,s.url=e.url,s.canvases=e.canvases,s.fonts=e.fonts,s.worklets=e.worklets,s.stylesheets=e.stylesheets,s.images=e.images,s.posters=e.posters,s.videos=e.videos,s.usedFonts=e.usedFonts,s.shadowRoots=e.shadowRoots,s.processed=e.processed,s.scrollPosition=e.scrollPosition,s.scrolling=e.scrolling,s.adoptedStyleSheets=e.adoptedStyleSheets)}));t.frames.filter((e=>!e.processed)).length||(t.frames=t.frames.sort(((e,t)=>t.windowId.split(Be).length-e.windowId.split(Be).length)),t.resolve&&(t.requestedFrameId&&t.frames.forEach((e=>{e.windowId==t.requestedFrameId&&(e.requestedFrame=!0)})),t.resolve(t.frames)))}}function rt(e,t,s,o){const n=ht(e);!function(e,t,s,o,n){const i=[];let r;et.get(n)?r=et.get(n).requestTimeouts:(r={},et.set(n,{requestTimeouts:r}));t.forEach(((e,t)=>{const s=o+Be+t;e.setAttribute(xe.WIN_ID_ATTRIBUTE_NAME,s),i.push({windowId:s})})),ct({frames:i,sessionId:n,requestedFrameId:e.documentElement.dataset.requestedFrameId&&o}),t.forEach(((e,t)=>{const i=o+Be+t;try{ut(e.contentWindow,{method:Ue,windowId:i,sessionId:n,options:s,scrolling:e.scrolling})}catch(e){}r[i]=globalThis.setTimeout((()=>ct({frames:[{windowId:i,processed:!0}],sessionId:n})),He)})),delete e.documentElement.dataset.requestedFrameId}(e,n,t,s,o),n.length&&function(e,t,s,o,n){const i=[];t.forEach(((e,t)=>{const r=o+Be+t;let a,l;try{a=e.contentDocument,l=e.contentWindow,l.stop()}catch(e){}const d=e.getAttribute("srcdoc");if(!a&&d){a=(new Xe).parseFromString(d,"text/html"),l=globalThis}if(a)try{at("requestTimeouts",n,r),rt(a,s,r,n),i.push(mt(a,l,r,s,e.scrolling))}catch(e){i.push({windowId:r,processed:!0})}})),ct({frames:i,sessionId:n,requestedFrameId:e.documentElement.dataset.requestedFrameId&&o}),delete e.documentElement.dataset.requestedFrameId}(e,n,t,s,o)}function at(e,t,s){const o=et.get(t);if(o&&o[e]){const t=o[e][s];t&&(globalThis.clearTimeout(t),delete o[e][s])}}function lt(e,t){const s=et.get(e);s&&s.responseTimeouts&&(s.responseTimeouts[t]=globalThis.setTimeout((()=>ct({frames:[{windowId:t,processed:!0}],sessionId:e})),ze))}function dt(e,t,s){e.forEach(((e,o)=>{const n=t+Be+o;e.removeAttribute(xe.WIN_ID_ATTRIBUTE_NAME);try{ut(e.contentWindow,{method:Oe,windowId:n,sessionId:s})}catch(e){}})),e.forEach(((e,o)=>{const n=t+Be+o;let i;try{i=e.contentDocument}catch(e){}if(i)try{dt(ht(i),n,s)}catch(e){}}))}function ct(e){e.method=Ve;try{Ge.singlefile.processors.frameTree.initResponse(e)}catch(t){ut(Ge,e,!0)}}function ut(e,t,s){if(e==Ge&&Ye&&Ye.runtime&&Ye.runtime.sendMessage)Ye.runtime.sendMessage(t);else if(s){const s=new Ze;e.postMessage(R+Ke.stringify({method:t.method,sessionId:t.sessionId}),We,[s.port2]),s.port1.postMessage(t)}else e.postMessage(R+Ke.stringify(t),We)}function mt(e,t,s,o,n){const i=xe.preProcessDoc(e,t,o),r=xe.serialize(e);xe.postProcessDoc(e,i.markedElements,i.invalidElements);return{windowId:s,content:r,baseURI:e.baseURI.split("#")[0],url:e.documentURI,title:e.title,canvases:i.canvases,fonts:i.fonts,worklets:i.worklets,stylesheets:i.stylesheets,images:i.images,posters:i.posters,videos:i.videos,usedFonts:i.usedFonts,shadowRoots:i.shadowRoots,scrollPosition:i.scrollPosition,scrolling:n,adoptedStyleSheets:i.adoptedStyleSheets,processed:!0}}function ht(e){let t=Array.from(e.querySelectorAll(ke));return e.querySelectorAll(qe).forEach((e=>{const s=xe.getShadowRoot(e);s&&(t=t.concat(...s.querySelectorAll(ke)))})),t}et||(et=globalThis.sessions=new Map),je&&(Qe="0",Ye&&Ye.runtime&&Ye.runtime.onMessage&&Ye.runtime.onMessage.addListener&&Ye.runtime.onMessage.addListener((e=>e.method==Ve?(it(e),Promise.resolve({})):e.method==De?(at("requestTimeouts",e.sessionId,e.windowId),lt(e.sessionId,e.windowId),Promise.resolve({})):void 0))),tt(),new $e(tt).observe(Je,{childList:!0}),e.TIMEOUT_INIT_REQUEST_MESSAGE=He,e.cleanup=function(e){et.delete(e),nt({windowId:Qe,sessionId:e,options:{sessionId:e}})},e.getAsync=function(e){const t=st();return e=Ke.parse(Ke.stringify(e)),new Promise((s=>{et.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{},resolve:e=>{e.sessionId=t,s(e)}}),ot({windowId:Qe,sessionId:t,options:e})}))},e.getSync=function(e){const t=st();e=Ke.parse(Ke.stringify(e)),et.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{}}),function(e){const t=e.sessionId,s=globalThis[xe.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];delete globalThis._singleFile_cleaningUp,je||(Qe=globalThis.frameId=e.windowId);rt(Je,e.options,Qe,t),je||(e.options.userScriptEnabled&&s&&s(xe.ON_BEFORE_CAPTURE_EVENT_NAME),ct({frames:[mt(Je,globalThis,Qe,e.options,e.scrolling)],sessionId:t,requestedFrameId:Je.documentElement.dataset.requestedFrameId&&Qe}),e.options.userScriptEnabled&&s&&s(xe.ON_AFTER_CAPTURE_EVENT_NAME),delete Je.documentElement.dataset.requestedFrameId)}({windowId:Qe,sessionId:t,options:e});const s=et.get(t).frames;return s.sessionId=t,s},e.initResponse=it}));