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