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