single-file-bootstrap.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).singlefileBootstrap={})}(this,(function(e){"use strict";const t="single-file-load-image",s="single-file-image-loaded",o=globalThis.browser,n=e=>globalThis.dispatchEvent(e),i=globalThis.CustomEvent,r=globalThis.document,a=globalThis.HTMLDocument,l=globalThis.FileReader,d=globalThis.Blob,c=[];if(r instanceof a&&o&&o.runtime&&o.runtime.getURL){m="single-file-new-font-face",u=e=>{const t=e.detail;c.find((e=>JSON.stringify(e)==JSON.stringify(t)))||c.push(e.detail)},globalThis.addEventListener(m,u,g);let e=r.createElement("script");e.textContent="("+function(){const e=globalThis.console,t=e=>globalThis.dispatchEvent(e),s=globalThis.CustomEvent,o=e&&e.warn&&((...t)=>e.warn(...t))||(()=>{}),n="single-file-new-font-face",i={family:"font-family",style:"font-style",weight:"font-weight",stretch:"font-stretch",unicodeRange:"unicode-range",variant:"font-variant",featureSettings:"font-feature-settings"};if(globalThis.FontFace){const e=globalThis.FontFace;let r;globalThis.FontFace=function(){r||(o("SingleFile is hooking the FontFace constructor to get font URLs."),r=!0);const a={};a["font-family"]=arguments[0],a.src=arguments[1];const c=arguments[2];if(c&&Object.keys(c).forEach((e=>{i[e]&&(a[i[e]]=c[e])})),a.src instanceof ArrayBuffer){const e=new l;e.readAsDataURL(new d([a.src])),e.addEventListener("load",(()=>{a.src="url("+e.result+")",t(new s(n,{detail:a}))}))}else t(new s(n,{detail:a}));return new e(...arguments)},globalThis.FontFace.toString=function(){return"function FontFace() { [native code] }"}}}.toString()+")()",(r.documentElement||r).appendChild(e),e.remove(),e=r.createElement("script"),e.src=o.runtime.getURL("/dist/web/hooks/hooks-frames-web.js"),e.async=!1,(r.documentElement||r).appendChild(e),e.remove()}var m,u,g;const f=new RegExp("\\\\([\\da-f]{1,6}[\\x20\\t\\r\\n\\f]?|([\\x20\\t\\r\\n\\f])|.)","ig");const h="single-file-on-before-capture",p="single-file-on-after-capture",E="data-single-file-removed-content",b="data-single-file-hidden-content",w="data-single-file-kept-content",y="data-single-file-hidden-frame",T="data-single-file-preserved-space-element",A="data-single-file-shadow-root-element",I="data-single-file-image",v="data-single-file-poster",S="data-single-file-canvas",R="data-single-file-import",F="data-single-file-input-value",N="data-single-file-lazy-loaded-src",_="data-single-file-stylesheet",C="data-single-file-disabled-noscript",M="data-single-file-async-script",P="*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)",q=["NOSCRIPT","DISABLED-NOSCRIPT","META","LINK","STYLE","TITLE","TEMPLATE","SOURCE","OBJECT","SCRIPT","HEAD"],x=/^'(.*?)'$/,k=/^"(.*?)"$/,L={regular:"400",normal:"400",bold:"700",bolder:"700",lighter:"100"},O="single-file-ui-element",D=(e,t,s)=>globalThis.addEventListener(e,t,s);function U(e,t,s){let o;return e.querySelectorAll("noscript:not([data-single-file-disabled-noscript])").forEach((e=>{e.setAttribute(C,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(P).forEach((e=>e.hidden=!0)),e.querySelectorAll("svg foreignObject").forEach((e=>{const t=e.querySelectorAll("html > head > "+P+", html > body > "+P);t.length&&(Array.from(e.childNodes).forEach((e=>e.remove())),t.forEach((t=>e.appendChild(t))))})),o=t&&e.documentElement?V(t,e,e.documentElement,s):{canvases:[],images:[],posters:[],usedFonts:[],shadowRoots:[],imports:[],markedElements:[]},{canvases:o.canvases,fonts:c,stylesheets:j(e),images:o.images,posters:o.posters,usedFonts:Array.from(o.usedFonts.values()),shadowRoots:o.shadowRoots,imports:o.imports,referrer:e.referrer,markedElements:o.markedElements}}function V(e,t,s,o,n={usedFonts:new Map,canvases:[],images:[],posters:[],shadowRoots:[],imports:[],markedElements:[]},i){return Array.from(s.childNodes).filter((t=>t instanceof e.HTMLElement||t instanceof e.SVGElement)).forEach((s=>{let r,a,l;if(!o.autoSaveExternalSave&&(o.removeHiddenElements||o.removeUnusedFonts||o.compressHTML)&&(l=e.getComputedStyle(s),s instanceof e.HTMLElement&&o.removeHiddenElements&&(a=(i||s.closest("html > head"))&&q.includes(s.tagName)||s.closest("details"),a||(r=i||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)}(s,l),r&&(s.setAttribute(b,""),n.markedElements.push(s)))),!r)){if(o.compressHTML&&l){const e=l.getPropertyValue("white-space");e&&e.startsWith("pre")&&(s.setAttribute(T,""),n.markedElements.push(s))}o.removeUnusedFonts&&(z(l,o,n.usedFonts),z(e.getComputedStyle(s,":first-letter"),o,n.usedFonts),z(e.getComputedStyle(s,":before"),o,n.usedFonts),z(e.getComputedStyle(s,":after"),o,n.usedFonts))}!function(e,t,s,o,n,i,r){if("CANVAS"==s.tagName)try{const t=J(e,s,r);n.canvases.push({dataURI:s.toDataURL("image/png",""),width:t.width,height:t.height}),s.setAttribute(S,n.canvases.length-1),n.markedElements.push(s)}catch(e){}if("IMG"==s.tagName){const t={currentSrc:i?"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==":o.loadDeferredImages&&s.getAttribute(N)||s.currentSrc};if(n.images.push(t),s.setAttribute(I,n.images.length-1),n.markedElements.push(s),s.removeAttribute(N),r=r||e.getComputedStyle(s)){t.size=J(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"==s.tagName&&!s.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"==s.tagName&&i&&o.removeHiddenElements&&(s.setAttribute(y,""),n.markedElements.push(s));"LINK"==s.tagName&&s.import&&s.import.documentElement&&(n.imports.push({content:K(s.import)}),s.setAttribute(R,n.imports.length-1),n.markedElements.push(s));"INPUT"==s.tagName&&("password"!=s.type&&(s.setAttribute(F,s.value),n.markedElements.push(s)),"radio"!=s.type&&"checkbox"!=s.type||(s.setAttribute(F,s.checked),n.markedElements.push(s)));"TEXTAREA"==s.tagName&&(s.setAttribute(F,s.value),n.markedElements.push(s));"SELECT"==s.tagName&&s.querySelectorAll("option").forEach((e=>{e.selected&&(e.setAttribute(F,""),n.markedElements.push(e))}));"SCRIPT"==s.tagName&&(s.async&&""!=s.getAttribute("async")&&"async"!=s.getAttribute("async")&&(s.setAttribute(M,""),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)&&B(s);if(d&&!s.classList.contains(O)){const i={};s.setAttribute(A,n.shadowRoots.length),n.markedElements.push(s),n.shadowRoots.push(i),V(e,t,d,o,n,r),i.content=d.innerHTML,i.delegatesFocus=d.delegatesFocus,i.mode=d.mode,d.adoptedStyleSheets&&d.adoptedStyleSheets.length&&(i.adoptedStyleSheets=Array.from(d.adoptedStyleSheets).map((e=>Array.from(e.cssRules).map((e=>e.cssText)).join("\n"))))}V(e,t,s,o,n,r),!o.autoSaveExternalSave&&o.removeHiddenElements&&i&&(a||""==s.getAttribute(w)?s.parentElement&&(s.parentElement.setAttribute(w,""),n.markedElements.push(s.parentElement)):r&&(s.setAttribute(E,""),n.markedElements.push(s)))})),n}function z(e,t,s){if(e){const o=e.getPropertyValue("font-style")||"normal";e.getPropertyValue("font-family").split(",").forEach((n=>{if(n=H(n),!t.loadedFonts||t.loadedFonts.find((e=>H(e.family)==n&&e.style==o))){const t=(i=e.getPropertyValue("font-weight"),L[i.toLowerCase().trim()]||i),r=e.getPropertyValue("font-variant")||"normal",a=[n,t,o,r];s.set(JSON.stringify(a),[n,t,o,r])}var i}))}}function B(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 H(e=""){return function(e){e=e.match(x)?e.replace(x,"$1"):e.replace(k,"$1");return e.trim()}((t=e.trim(),t.replace(f,((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 W(e,t){if(e.querySelectorAll("[data-single-file-disabled-noscript]").forEach((e=>{e.textContent=e.getAttribute(C),e.removeAttribute(C)})),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=[E,y,b,T,I,v,S,F,A,R,_,M];t=e.querySelectorAll(s.map((e=>"["+e+"]")).join(","))}t.forEach((e=>{e.removeAttribute(E),e.removeAttribute(b),e.removeAttribute(w),e.removeAttribute(y),e.removeAttribute(T),e.removeAttribute(I),e.removeAttribute(v),e.removeAttribute(S),e.removeAttribute(F),e.removeAttribute(A),e.removeAttribute(R),e.removeAttribute(_),e.removeAttribute(M)}))}function j(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(_,o),t[o]=Array.from(s.sheet.cssRules).map((e=>e.cssText)).join("\n"))}catch(e){}})),t}}function J(e,t,s){let o=t.naturalWidth,n=t.naturalHeight;if(!o&&!n){let i,r,a,l,d,c,m,u,g=!1;if("content-box"==(s=s||e.getComputedStyle(t)).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")}i=G("padding-left",s),r=G("padding-right",s),a=G("padding-top",s),l=G("padding-bottom",s),g?(d=G("border-left-width",s),c=G("border-right-width",s),m=G("border-top-width",s),u=G("border-bottom-width",s)):d=c=m=u=0,o=Math.max(0,t.clientWidth-i-r-d-c),n=Math.max(0,t.clientHeight-a-l-m-u)}return{pxWidth:o,pxHeight:n}}function G(e,t){if(t.getPropertyValue(e).endsWith("px"))return parseFloat(t.getPropertyValue(e))}function K(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}const Y=N,$=O,Q="attributes",X=globalThis.browser,Z=globalThis.document,ee=globalThis.MutationObserver,te=(e,t,s)=>globalThis.addEventListener(e,t,s),se=(e,t,s)=>globalThis.removeEventListener(e,t,s),oe=new Map;async function ne(e){if(Z.documentElement){oe.clear();const o=Math.max(Z.documentElement.scrollHeight-1.5*Z.documentElement.clientHeight,0),r=Math.max(Z.documentElement.scrollWidth-1.5*Z.documentElement.clientWidth,0);if(globalThis.scrollY<=o&&globalThis.scrollX<=r)return function(e){return new Promise((async o=>{let r;const a=new Set,l=new ee((async t=>{if((t=t.filter((e=>e.type==Q))).length){t.filter((e=>{if("src"==e.attributeName&&(e.target.setAttribute(Y,e.target.src),e.target.addEventListener("load",c)),"src"==e.attributeName||"srcset"==e.attributeName||"SOURCE"==e.target.tagName)return!e.target.classList||!e.target.classList.contains($)})).length&&(r=!0,await re(l,e,g),a.size||await ie(l,e,g))}}));async function d(t){await le("idleTimeout",(async()=>{r?(de("idleTimeout"),await d(Math.max(500,t/2))):(de("loadTimeout"),de("maxTimeout"),ae(l,e,g))}),t)}function c(e){const t=e.target;t.removeAttribute(Y),t.removeEventListener("load",c)}async function m(t){r=!0,await re(l,e,g),await ie(l,e,g),t.detail&&a.add(t.detail)}async function u(t){await re(l,e,g),await ie(l,e,g),a.delete(t.detail),a.size||await ie(l,e,g)}function g(e){l.disconnect(),se(t,m),se(s,u),o(e)}await d(2*e.loadDeferredImagesMaxIdleTime),await re(l,e,g),l.observe(Z,{subtree:!0,childList:!0,attributes:!0}),te(t,m),te(s,u),function(e){e.loadDeferredImagesBlockCookies&&n(new i("single-file-block-cookies-start")),e.loadDeferredImagesBlockStorage&&n(new i("single-file-block-storage-start")),e.loadDeferredImagesKeepZoomLevel?n(new i("single-file-load-deferred-images-keep-zoom-level-start")):n(new i("single-file-load-deferred-images-start"))}(e)}))}(e)}}async function ie(e,t,s){await le("loadTimeout",(()=>ae(e,t,s)),t.loadDeferredImagesMaxIdleTime)}async function re(e,t,s){await le("maxTimeout",(async()=>{await de("loadTimeout"),await ae(e,t,s)}),10*t.loadDeferredImagesMaxIdleTime)}async function ae(e,t,s){await de("idleTimeout"),function(e){e.loadDeferredImagesBlockCookies&&n(new i("single-file-block-cookies-end")),e.loadDeferredImagesBlockStorage&&n(new i("single-file-block-storage-end")),e.loadDeferredImagesKeepZoomLevel?n(new i("single-file-load-deferred-images-keep-zoom-level-end")):n(new i("single-file-load-deferred-images-end"))}(t),await le("endTimeout",(async()=>{await de("maxTimeout"),s()}),t.loadDeferredImagesMaxIdleTime/2),e.disconnect()}async function le(e,t,s){if(X&&X.runtime&&X.runtime.sendMessage){if(!oe.get(e)||!oe.get(e).pending){const o={callback:t,pending:!0};oe.set(e,o),await X.runtime.sendMessage({method:"singlefile.lazyTimeout.setTimeout",type:e,delay:s}),o.pending=!1}}else{const o=oe.get(e);o&&globalThis.clearTimeout(o),oe.set(e,t),globalThis.setTimeout(t,s)}}async function de(e){if(X&&X.runtime&&X.runtime.sendMessage)await X.runtime.sendMessage({method:"singlefile.lazyTimeout.clearTimeout",type:e});else{const t=oe.get(e);oe.delete(e),t&&globalThis.clearTimeout(t)}}X&&X.runtime&&X.runtime.onMessage&&X.runtime.onMessage.addListener&&X.runtime.onMessage.addListener((e=>{if("singlefile.lazyTimeout.onTimeout"==e.method){const t=oe.get(e.type);t&&(oe.delete(e.type),t.callback())}}));const ce={ON_BEFORE_CAPTURE_EVENT_NAME:h,ON_AFTER_CAPTURE_EVENT_NAME:p,WIN_ID_ATTRIBUTE_NAME:"data-single-file-win-id",preProcessDoc:U,serialize:K,postProcessDoc:W,getShadowRoot:B},me="__frameTree__::",ue='iframe, frame, object[type="text/html"][data]',ge="singlefile.frameTree.initRequest",fe="singlefile.frameTree.ackInitRequest",he="singlefile.frameTree.cleanupRequest",pe="singlefile.frameTree.initResponse",Ee=".",be=globalThis.window==globalThis.top,we=globalThis.browser,ye=globalThis.top,Te=globalThis.MessageChannel,Ae=globalThis.document,Ie=new Map;let ve;function Se(){return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("")}async function Re(e){const t=e.sessionId,s=globalThis._singleFile_waitForUserScript;be||(ve=globalThis.frameId=e.windowId),_e(Ae,e.options,ve,t),be||(e.options.userScriptEnabled&&s&&await s(ce.ON_BEFORE_CAPTURE_EVENT_NAME),qe({frames:[ke(Ae,globalThis,ve,e.options)],sessionId:t,requestedFrameId:Ae.documentElement.dataset.requestedFrameId&&ve}),e.options.userScriptEnabled&&s&&await s(ce.ON_AFTER_CAPTURE_EVENT_NAME),delete Ae.documentElement.dataset.requestedFrameId)}function Fe(e){const t=e.sessionId;Pe(Le(Ae),e.windowId,t)}function Ne(e){e.frames.forEach((t=>Ce("responseTimeouts",e.sessionId,t.windowId)));const t=Ie.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.usedFonts=e.usedFonts,s.shadowRoots=e.shadowRoots,s.imports=e.imports,s.processed=e.processed)}));t.frames.filter((e=>!e.processed)).length||(t.frames=t.frames.sort(((e,t)=>t.windowId.split(Ee).length-e.windowId.split(Ee).length)),t.resolve&&(t.requestedFrameId&&t.frames.forEach((e=>{e.windowId==t.requestedFrameId&&(e.requestedFrame=!0)})),t.resolve(t.frames)))}}function _e(e,t,s,o){const n=Le(e);!function(e,t,s,o,n){const i=[];let r;Ie.get(n)?r=Ie.get(n).requestTimeouts:(r={},Ie.set(n,{requestTimeouts:r}));t.forEach(((e,t)=>{const s=o+Ee+t;e.setAttribute(ce.WIN_ID_ATTRIBUTE_NAME,s),i.push({windowId:s})})),qe({frames:i,sessionId:n,requestedFrameId:e.documentElement.dataset.requestedFrameId&&o}),t.forEach(((e,t)=>{const i=o+Ee+t;try{xe(e.contentWindow,{method:ge,windowId:i,sessionId:n,options:s})}catch(e){}r[i]=globalThis.setTimeout((()=>qe({frames:[{windowId:i,processed:!0}],sessionId:n})),750)})),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+Ee+t;let a;try{a=e.contentDocument}catch(e){}if(a)try{const t=e.contentWindow;t.stop(),Ce("requestTimeouts",n,r),_e(a,s,r,n),i.push(ke(a,t,r,s))}catch(e){i.push({windowId:r,processed:!0})}})),qe({frames:i,sessionId:n,requestedFrameId:e.documentElement.dataset.requestedFrameId&&o}),delete e.documentElement.dataset.requestedFrameId}(e,n,t,s,o)}function Ce(e,t,s){const o=Ie.get(t);if(o&&o[e]){const t=o[e][s];t&&(globalThis.clearTimeout(t),delete o[e][s])}}function Me(e,t){const s=Ie.get(e);s&&s.responseTimeouts&&(s.responseTimeouts[t]=globalThis.setTimeout((()=>qe({frames:[{windowId:t,processed:!0}],sessionId:e})),1e4))}function Pe(e,t,s){e.forEach(((e,o)=>{const n=t+Ee+o;e.removeAttribute(ce.WIN_ID_ATTRIBUTE_NAME);try{xe(e.contentWindow,{method:he,windowId:n,sessionId:s})}catch(e){}})),e.forEach(((e,o)=>{const n=t+Ee+o;let i;try{i=e.contentDocument}catch(e){}if(i)try{Pe(Le(i),n,s)}catch(e){}}))}function qe(e){e.method=pe;try{ye.singlefile.processors.frameTree.initResponse(e)}catch(t){xe(ye,e,!0)}}function xe(e,t,s){if(e==ye&&we&&we.runtime&&we.runtime.sendMessage)we.runtime.sendMessage(t);else if(s){const s=new Te;e.postMessage(me+JSON.stringify({method:t.method,sessionId:t.sessionId}),"*",[s.port2]),s.port1.postMessage(t)}else e.postMessage(me+JSON.stringify(t),"*")}function ke(e,t,s,o){const n=ce.preProcessDoc(e,t,o),i=ce.serialize(e);ce.postProcessDoc(e,n.markedElements);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,usedFonts:n.usedFonts,shadowRoots:n.shadowRoots,imports:n.imports,processed:!0}}function Le(e){let t=Array.from(e.querySelectorAll(ue));return e.querySelectorAll("*").forEach((e=>{const s=ce.getShadowRoot(e);s&&(t=t.concat(...s.querySelectorAll(ue)))})),t}be&&(ve="0",we&&we.runtime&&we.runtime.onMessage&&we.runtime.onMessage.addListener&&we.runtime.onMessage.addListener((e=>e.method==pe?(Ne(e),Promise.resolve({})):e.method==fe?(Ce("requestTimeouts",e.sessionId,e.windowId),Me(e.sessionId,e.windowId),Promise.resolve({})):void 0))),((e,t,s)=>{globalThis.addEventListener(e,t,s)})("message",(async e=>{if("string"==typeof e.data&&e.data.startsWith(me)){e.preventDefault(),e.stopPropagation();const t=JSON.parse(e.data.substring(me.length));if(t.method==ge)e.source&&xe(e.source,{method:fe,windowId:t.windowId,sessionId:t.sessionId}),be||(globalThis.stop(),t.options.loadDeferredImages&&ne(t.options),await Re(t));else if(t.method==fe)Ce("requestTimeouts",t.sessionId,t.windowId),Me(t.sessionId,t.windowId);else if(t.method==he)Fe(t);else if(t.method==pe&&Ie.get(t.sessionId)){e.ports[0].onmessage=e=>Ne(e.data)}}}),!0);const Oe={frameTree:Object.freeze({__proto__:null,getAsync:function(e){const t=Se();return e=JSON.parse(JSON.stringify(e)),new Promise((s=>{Ie.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{},resolve:e=>{e.sessionId=t,s(e)}}),Re({windowId:ve,sessionId:t,options:e})}))},getSync:function(e){const t=Se();e=JSON.parse(JSON.stringify(e)),Ie.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{}}),function(e){const t=e.sessionId,s=globalThis._singleFile_waitForUserScript;be||(ve=globalThis.frameId=e.windowId);_e(Ae,e.options,ve,t),be||(e.options.userScriptEnabled&&s&&s(ce.ON_BEFORE_CAPTURE_EVENT_NAME),qe({frames:[ke(Ae,globalThis,ve,e.options)],sessionId:t,requestedFrameId:Ae.documentElement.dataset.requestedFrameId&&ve}),e.options.userScriptEnabled&&s&&s(ce.ON_AFTER_CAPTURE_EVENT_NAME),delete Ae.documentElement.dataset.requestedFrameId)}({windowId:ve,sessionId:t,options:e});const s=Ie.get(t).frames;return s.sessionId=t,s},cleanup:function(e){Ie.delete(e),Fe({windowId:ve,sessionId:e,options:{sessionId:e}})},initResponse:Ne,TIMEOUT_INIT_REQUEST_MESSAGE:750})},De={COMMENT_HEADER:"Page saved with SingleFile",COMMENT_HEADER_LEGACY:"Archive processed by SingleFile",ON_BEFORE_CAPTURE_EVENT_NAME:h,ON_AFTER_CAPTURE_EVENT_NAME:p,preProcessDoc:U,postProcessDoc:W,serialize:K,getShadowRoot:B};D("single-file-user-script-init",(()=>globalThis._singleFile_waitForUserScript=async e=>{const t=new CustomEvent(e+"-request",{cancelable:!0}),s=new Promise((t=>D(e+"-response",t)));(e=>{globalThis.dispatchEvent(e)})(t),t.defaultPrevented&&await s})),e.helper=De,e.processors=Oe,Object.defineProperty(e,"__esModule",{value:!0})}));