single-file-bootstrap.js 25 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",n=globalThis.browser,o=(e,t,s)=>globalThis.addEventListener(e,t,s),a=e=>globalThis.dispatchEvent(e),i=globalThis.CustomEvent,r=globalThis.document,l=globalThis.HTMLDocument;let d;if(d=window._singleFile_fontFaces?window._singleFile_fontFaces:window._singleFile_fontFaces=[],r instanceof l&&n&&n.runtime&&n.runtime.getURL){o("single-file-new-font-face",(e=>{const t=e.detail;d.find((e=>JSON.stringify(e)==JSON.stringify(t)))||d.push(e.detail)})),o("single-file-delete-font",(e=>{const t=e.detail,s=d.findIndex((e=>e["family-name"]==t["family-name"]));-1!=s&&d.splice(s,1)})),o("single-file-clear-fonts",(()=>d=[]));let e=r.createElement("script");e.src="data:,("+function(){"undefined"==typeof globalThis&&(window.globalThis=window);const e=globalThis.console,t=e=>globalThis.dispatchEvent(e),s=globalThis.CustomEvent,n=globalThis.FileReader,o=globalThis.Blob,a=e&&e.warn&&((...t)=>e.warn(...t))||(()=>{}),i="single-file-new-font-face",l="single-file-delete-font",d="single-file-clear-fonts",c={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 n;globalThis.FontFace=function(){return n||(a("SingleFile is hooking the FontFace constructor, document.fonts.delete and document.fonts.clear to handle dynamically loaded fonts."),n=!0),m(...arguments).then((e=>t(new s(i,{detail:e})))),new e(...arguments)},globalThis.FontFace.toString=function(){return"function FontFace() { [native code] }"};const o=r.fonts.delete;r.fonts.delete=function(e){return m(e.family).then((e=>t(new s(l,{detail:e})))),o.call(r.fonts,e)},r.fonts.delete.toString=function(){return"function delete() { [native code] }"};const c=r.fonts.clear;r.fonts.clear=function(){return t(new s(d)),c.call(r.fonts)},r.fonts.clear.toString=function(){return"function clear() { [native code] }"}}async function m(e,t,s){const a={};return a["font-family"]=e,a.src=t,s&&Object.keys(s).forEach((e=>{c[e]&&(a[c[e]]=s[e])})),new Promise((e=>{if(a.src instanceof ArrayBuffer){const t=new n;t.readAsDataURL(new o([a.src])),t.addEventListener("load",(()=>{a.src="url("+t.result+")",e(a)}))}else e(a)}))}}.toString()+")()",(r.documentElement||r).appendChild(e),e.remove(),e=r.createElement("script"),e.src=n.runtime.getURL("/dist/web/hooks/hooks-frames-web.js"),e.async=!1,(r.documentElement||r).appendChild(e),e.remove()}const c=new RegExp("\\\\([\\da-f]{1,6}[\\x20\\t\\r\\n\\f]?|([\\x20\\t\\r\\n\\f])|.)","ig");const m="single-file-on-before-capture",u="single-file-on-after-capture",g="data-single-file-removed-content",f="data-single-file-hidden-content",p="data-single-file-kept-content",h="data-single-file-hidden-frame",E="data-single-file-preserved-space-element",T="data-single-file-shadow-root-element",b="data-single-file-image",y="data-single-file-poster",w="data-single-file-canvas",I="data-single-file-import",A="data-single-file-input-value",N="data-single-file-lazy-loaded-src",v="data-single-file-stylesheet",S="data-single-file-disabled-noscript",R="data-single-file-async-script",F="*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)",C=["NOSCRIPT","DISABLED-NOSCRIPT","META","LINK","STYLE","TITLE","TEMPLATE","SOURCE","OBJECT","SCRIPT","HEAD"],P=/^'(.*?)'$/,_=/^"(.*?)"$/,x={regular:"400",normal:"400",bold:"700",bolder:"700",lighter:"100"},M="single-file-ui-element",O=(e,t,s)=>globalThis.addEventListener(e,t,s);function L(e,t,s){let n;return e.querySelectorAll("noscript:not([data-single-file-disabled-noscript])").forEach((e=>{e.setAttribute(S,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(F).forEach((e=>e.hidden=!0)),e.querySelectorAll("svg foreignObject").forEach((e=>{const t=e.querySelectorAll("html > head > "+F+", html > body > "+F);t.length&&(Array.from(e.childNodes).forEach((e=>e.remove())),t.forEach((t=>e.appendChild(t))))})),n=t&&e.documentElement?D(t,e,e.documentElement,s):{canvases:[],images:[],posters:[],usedFonts:[],shadowRoots:[],imports:[],markedElements:[]},{canvases:n.canvases,fonts:d,stylesheets:V(e),images:n.images,posters:n.posters,usedFonts:Array.from(n.usedFonts.values()),shadowRoots:n.shadowRoots,imports:n.imports,referrer:e.referrer,markedElements:n.markedElements}}function D(e,t,s,n,o={usedFonts:new Map,canvases:[],images:[],posters:[],shadowRoots:[],imports:[],markedElements:[]},a){return Array.from(s.childNodes).filter((t=>t instanceof e.HTMLElement||t instanceof e.SVGElement)).forEach((s=>{let i,r,l;if(!n.autoSaveExternalSave&&(n.removeHiddenElements||n.removeUnusedFonts||n.compressHTML)&&(l=e.getComputedStyle(s),s instanceof e.HTMLElement&&n.removeHiddenElements&&(r=(a||s.closest("html > head"))&&C.includes(s.tagName)||s.closest("details"),r||(i=a||function(e,t){let s=!1;if(t){const n=t.getPropertyValue("display"),o=t.getPropertyValue("opacity"),a=t.getPropertyValue("visibility");if(s="none"==n,!s&&("0"==o||"hidden"==a)&&e.getBoundingClientRect){const t=e.getBoundingClientRect();s=!t.width&&!t.height}}return Boolean(s)}(s,l),i&&(s.setAttribute(f,""),o.markedElements.push(s)))),!i)){if(n.compressHTML&&l){const e=l.getPropertyValue("white-space");e&&e.startsWith("pre")&&(s.setAttribute(E,""),o.markedElements.push(s))}n.removeUnusedFonts&&(k(l,n,o.usedFonts),k(e.getComputedStyle(s,":first-letter"),n,o.usedFonts),k(e.getComputedStyle(s,":before"),n,o.usedFonts),k(e.getComputedStyle(s,":after"),n,o.usedFonts))}!function(e,t,s,n,o,a,i){if("CANVAS"==s.tagName)try{const t=B(e,s,i);o.canvases.push({dataURI:s.toDataURL("image/png",""),width:t.width,height:t.height}),s.setAttribute(w,o.canvases.length-1),o.markedElements.push(s)}catch(e){}if("IMG"==s.tagName){const t={currentSrc:a?"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==":n.loadDeferredImages&&s.getAttribute(N)||s.currentSrc};if(o.images.push(t),s.setAttribute(b,o.images.length-1),o.markedElements.push(s),s.removeAttribute(N),i=i||e.getComputedStyle(s)){t.size=B(e,s,i);const n=i.getPropertyValue("box-shadow"),o=i.getPropertyValue("background-image");n&&"none"!=n||o&&"none"!=o||!(t.size.pxWidth>1||t.size.pxHeight>1)||(t.replaceable=!0,t.backgroundColor=i.getPropertyValue("background-color"),t.objectFit=i.getPropertyValue("object-fit"),t.boxSizing=i.getPropertyValue("box-sizing"),t.objectPosition=i.getPropertyValue("object-position"))}}if("VIDEO"==s.tagName&&!s.poster){const e=t.createElement("canvas"),n=e.getContext("2d");e.width=s.clientWidth,e.height=s.clientHeight;try{n.drawImage(s,0,0,e.width,e.height),o.posters.push(e.toDataURL("image/png","")),s.setAttribute(y,o.posters.length-1),o.markedElements.push(s)}catch(e){}}"IFRAME"==s.tagName&&a&&n.removeHiddenElements&&(s.setAttribute(h,""),o.markedElements.push(s));"LINK"==s.tagName&&s.import&&s.import.documentElement&&(o.imports.push({content:W(s.import)}),s.setAttribute(I,o.imports.length-1),o.markedElements.push(s));"INPUT"==s.tagName&&("password"!=s.type&&(s.setAttribute(A,s.value),o.markedElements.push(s)),"radio"!=s.type&&"checkbox"!=s.type||(s.setAttribute(A,s.checked),o.markedElements.push(s)));"TEXTAREA"==s.tagName&&(s.setAttribute(A,s.value),o.markedElements.push(s));"SELECT"==s.tagName&&s.querySelectorAll("option").forEach((e=>{e.selected&&(e.setAttribute(A,""),o.markedElements.push(e))}));"SCRIPT"==s.tagName&&(s.async&&""!=s.getAttribute("async")&&"async"!=s.getAttribute("async")&&(s.setAttribute(R,""),o.markedElements.push(s)),s.textContent=s.textContent.replace(/<\/script>/gi,"<\\/script>"))}(e,t,s,n,o,i,l);const d=!(s instanceof e.SVGElement)&&q(s);if(d&&!s.classList.contains(M)){const a={};s.setAttribute(T,o.shadowRoots.length),o.markedElements.push(s),o.shadowRoots.push(a),D(e,t,d,n,o,i),a.content=d.innerHTML,a.delegatesFocus=d.delegatesFocus,a.mode=d.mode,d.adoptedStyleSheets&&d.adoptedStyleSheets.length&&(a.adoptedStyleSheets=Array.from(d.adoptedStyleSheets).map((e=>Array.from(e.cssRules).map((e=>e.cssText)).join("\n"))))}D(e,t,s,n,o,i),!n.autoSaveExternalSave&&n.removeHiddenElements&&a&&(r||""==s.getAttribute(p)?s.parentElement&&(s.parentElement.setAttribute(p,""),o.markedElements.push(s.parentElement)):i&&(s.setAttribute(g,""),o.markedElements.push(s)))})),o}function k(e,t,s){if(e){const n=e.getPropertyValue("font-style")||"normal";e.getPropertyValue("font-family").split(",").forEach((o=>{if(o=U(o),!t.loadedFonts||t.loadedFonts.find((e=>U(e.family)==o&&e.style==n))){const t=(a=e.getPropertyValue("font-weight"),x[a.toLowerCase().trim()]||a),i=e.getPropertyValue("font-variant")||"normal",r=[o,t,n,i];s.set(JSON.stringify(r),[o,t,n,i])}var a}))}}function q(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 U(e=""){return function(e){e=e.match(P)?e.replace(P,"$1"):e.replace(_,"$1");return e.trim()}((t=e.trim(),t.replace(c,((e,t,s)=>{const n="0x"+t-65536;return n!=n||s?t:n<0?String.fromCharCode(n+65536):String.fromCharCode(n>>10|55296,1023&n|56320)})))).toLowerCase();var t}function H(e,t){if(e.querySelectorAll("[data-single-file-disabled-noscript]").forEach((e=>{e.textContent=e.getAttribute(S),e.removeAttribute(S)})),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=[g,h,f,E,b,y,w,A,T,I,v,R];t=e.querySelectorAll(s.map((e=>"["+e+"]")).join(","))}t.forEach((e=>{e.removeAttribute(g),e.removeAttribute(f),e.removeAttribute(p),e.removeAttribute(h),e.removeAttribute(E),e.removeAttribute(b),e.removeAttribute(y),e.removeAttribute(w),e.removeAttribute(A),e.removeAttribute(T),e.removeAttribute(I),e.removeAttribute(v),e.removeAttribute(R)}))}function V(e){if(e){const t=[];return e.querySelectorAll("style").forEach(((s,n)=>{try{const o=e.createElement("style");o.textContent=s.textContent,e.body.appendChild(o);const a=o.sheet;o.remove(),a&&a.cssRules.length==s.sheet.cssRules.length||(s.setAttribute(v,n),t[n]=Array.from(s.sheet.cssRules).map((e=>e.cssText)).join("\n"))}catch(e){}})),t}}function B(e,t,s){let n=t.naturalWidth,o=t.naturalHeight;if(!n&&!o){let a,i,r,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"),n=t.clientWidth;t.style.setProperty("box-sizing","border-box","important"),g=t.clientWidth!=n,e?t.style.setProperty("box-sizing",e,s):t.style.removeProperty("box-sizing")}a=z("padding-left",s),i=z("padding-right",s),r=z("padding-top",s),l=z("padding-bottom",s),g?(d=z("border-left-width",s),c=z("border-right-width",s),m=z("border-top-width",s),u=z("border-bottom-width",s)):d=c=m=u=0,n=Math.max(0,t.clientWidth-a-i-d-c),o=Math.max(0,t.clientHeight-r-l-m-u)}return{pxWidth:n,pxHeight:o}}function z(e,t){if(t.getPropertyValue(e).endsWith("px"))return parseFloat(t.getPropertyValue(e))}function W(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 j=N,J=M,G="attributes",Y=globalThis.browser,K=globalThis.document,$=globalThis.MutationObserver,Q=(e,t,s)=>globalThis.addEventListener(e,t,s),X=(e,t,s)=>globalThis.removeEventListener(e,t,s),Z=new Map;async function ee(e){if(K.documentElement){Z.clear();const n=Math.max(K.documentElement.scrollHeight-1.5*K.documentElement.clientHeight,0),o=Math.max(K.documentElement.scrollWidth-1.5*K.documentElement.clientWidth,0);if(globalThis.scrollY<=n&&globalThis.scrollX<=o)return function(e){return new Promise((async n=>{let o;const r=new Set,l=new $((async t=>{if((t=t.filter((e=>e.type==G))).length){t.filter((e=>{if("src"==e.attributeName&&(e.target.setAttribute(j,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(J)})).length&&(o=!0,await se(l,e,g),r.size||await te(l,e,g))}}));async function d(t){await oe("idleTimeout",(async()=>{o?(ie("idleTimeout"),await d(Math.max(500,t/2))):(ie("loadTimeout"),ie("maxTimeout"),ne(l,e,g))}),t)}function c(e){const t=e.target;t.removeAttribute(j),t.removeEventListener("load",c)}async function m(t){o=!0,await se(l,e,g),await te(l,e,g),t.detail&&r.add(t.detail)}async function u(t){await se(l,e,g),await te(l,e,g),r.delete(t.detail),r.size||await te(l,e,g)}function g(e){l.disconnect(),X(t,m),X(s,u),n(e)}await d(2*e.loadDeferredImagesMaxIdleTime),await se(l,e,g),l.observe(K,{subtree:!0,childList:!0,attributes:!0}),Q(t,m),Q(s,u),function(e){e.loadDeferredImagesBlockCookies&&a(new i("single-file-block-cookies-start")),e.loadDeferredImagesBlockStorage&&a(new i("single-file-block-storage-start")),e.loadDeferredImagesKeepZoomLevel?a(new i("single-file-load-deferred-images-keep-zoom-level-start")):a(new i("single-file-load-deferred-images-start"))}(e)}))}(e)}}async function te(e,t,s){await oe("loadTimeout",(()=>ne(e,t,s)),t.loadDeferredImagesMaxIdleTime)}async function se(e,t,s){await oe("maxTimeout",(async()=>{await ie("loadTimeout"),await ne(e,t,s)}),10*t.loadDeferredImagesMaxIdleTime)}async function ne(e,t,s){await ie("idleTimeout"),function(e){e.loadDeferredImagesBlockCookies&&a(new i("single-file-block-cookies-end")),e.loadDeferredImagesBlockStorage&&a(new i("single-file-block-storage-end")),e.loadDeferredImagesKeepZoomLevel?a(new i("single-file-load-deferred-images-keep-zoom-level-end")):a(new i("single-file-load-deferred-images-end"))}(t),await oe("endTimeout",(async()=>{await ie("maxTimeout"),s()}),t.loadDeferredImagesMaxIdleTime/2),e.disconnect()}async function oe(e,t,s){if(Y&&Y.runtime&&Y.runtime.sendMessage){if(!Z.get(e)||!Z.get(e).pending){const n={callback:t,pending:!0};Z.set(e,n);try{await Y.runtime.sendMessage({method:"singlefile.lazyTimeout.setTimeout",type:e,delay:s})}catch(n){ae(e,t,s)}n.pending=!1}}else ae(e,t,s)}function ae(e,t,s){const n=Z.get(e);n&&globalThis.clearTimeout(n),Z.set(e,t),globalThis.setTimeout(t,s)}async function ie(e){if(Y&&Y.runtime&&Y.runtime.sendMessage)try{await Y.runtime.sendMessage({method:"singlefile.lazyTimeout.clearTimeout",type:e})}catch(t){re(e)}else re(e)}function re(e){const t=Z.get(e);Z.delete(e),t&&globalThis.clearTimeout(t)}Y&&Y.runtime&&Y.runtime.onMessage&&Y.runtime.onMessage.addListener&&Y.runtime.onMessage.addListener((e=>{if("singlefile.lazyTimeout.onTimeout"==e.method){const t=Z.get(e.type);if(t){Z.delete(e.type);try{t.callback()}catch(t){re(e.type)}}}}));const le={ON_BEFORE_CAPTURE_EVENT_NAME:m,ON_AFTER_CAPTURE_EVENT_NAME:u,WIN_ID_ATTRIBUTE_NAME:"data-single-file-win-id",preProcessDoc:L,serialize:W,postProcessDoc:H,getShadowRoot:q},de="__frameTree__::",ce='iframe, frame, object[type="text/html"][data]',me="singlefile.frameTree.initRequest",ue="singlefile.frameTree.ackInitRequest",ge="singlefile.frameTree.cleanupRequest",fe="singlefile.frameTree.initResponse",pe=".",he=globalThis.window==globalThis.top,Ee=globalThis.browser,Te=globalThis.top,be=globalThis.MessageChannel,ye=globalThis.document,we=new Map;let Ie;var Ae,Ne,ve;function Se(){return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("")}async function Re(e){const t=e.sessionId,s=globalThis._singleFile_waitForUserScript;he||(Ie=globalThis.frameId=e.windowId),Pe(ye,e.options,Ie,t),he||(e.options.userScriptEnabled&&s&&await s(le.ON_BEFORE_CAPTURE_EVENT_NAME),Oe({frames:[De(ye,globalThis,Ie,e.options)],sessionId:t,requestedFrameId:ye.documentElement.dataset.requestedFrameId&&Ie}),e.options.userScriptEnabled&&s&&await s(le.ON_AFTER_CAPTURE_EVENT_NAME),delete ye.documentElement.dataset.requestedFrameId)}function Fe(e){const t=e.sessionId;Me(ke(ye),e.windowId,t)}function Ce(e){e.frames.forEach((t=>_e("responseTimeouts",e.sessionId,t.windowId)));const t=we.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(pe).length-e.windowId.split(pe).length)),t.resolve&&(t.requestedFrameId&&t.frames.forEach((e=>{e.windowId==t.requestedFrameId&&(e.requestedFrame=!0)})),t.resolve(t.frames)))}}function Pe(e,t,s,n){const o=ke(e);!function(e,t,s,n,o){const a=[];let i;we.get(o)?i=we.get(o).requestTimeouts:(i={},we.set(o,{requestTimeouts:i}));t.forEach(((e,t)=>{const s=n+pe+t;e.setAttribute(le.WIN_ID_ATTRIBUTE_NAME,s),a.push({windowId:s})})),Oe({frames:a,sessionId:o,requestedFrameId:e.documentElement.dataset.requestedFrameId&&n}),t.forEach(((e,t)=>{const a=n+pe+t;try{Le(e.contentWindow,{method:me,windowId:a,sessionId:o,options:s})}catch(e){}i[a]=globalThis.setTimeout((()=>Oe({frames:[{windowId:a,processed:!0}],sessionId:o})),750)})),delete e.documentElement.dataset.requestedFrameId}(e,o,t,s,n),o.length&&function(e,t,s,n,o){const a=[];t.forEach(((e,t)=>{const i=n+pe+t;let r;try{r=e.contentDocument}catch(e){}if(r)try{const t=e.contentWindow;t.stop(),_e("requestTimeouts",o,i),Pe(r,s,i,o),a.push(De(r,t,i,s))}catch(e){a.push({windowId:i,processed:!0})}})),Oe({frames:a,sessionId:o,requestedFrameId:e.documentElement.dataset.requestedFrameId&&n}),delete e.documentElement.dataset.requestedFrameId}(e,o,t,s,n)}function _e(e,t,s){const n=we.get(t);if(n&&n[e]){const t=n[e][s];t&&(globalThis.clearTimeout(t),delete n[e][s])}}function xe(e,t){const s=we.get(e);s&&s.responseTimeouts&&(s.responseTimeouts[t]=globalThis.setTimeout((()=>Oe({frames:[{windowId:t,processed:!0}],sessionId:e})),1e4))}function Me(e,t,s){e.forEach(((e,n)=>{const o=t+pe+n;e.removeAttribute(le.WIN_ID_ATTRIBUTE_NAME);try{Le(e.contentWindow,{method:ge,windowId:o,sessionId:s})}catch(e){}})),e.forEach(((e,n)=>{const o=t+pe+n;let a;try{a=e.contentDocument}catch(e){}if(a)try{Me(ke(a),o,s)}catch(e){}}))}function Oe(e){e.method=fe;try{Te.singlefile.processors.frameTree.initResponse(e)}catch(t){Le(Te,e,!0)}}function Le(e,t,s){if(e==Te&&Ee&&Ee.runtime&&Ee.runtime.sendMessage)Ee.runtime.sendMessage(t);else if(s){const s=new be;e.postMessage(de+JSON.stringify({method:t.method,sessionId:t.sessionId}),"*",[s.port2]),s.port1.postMessage(t)}else e.postMessage(de+JSON.stringify(t),"*")}function De(e,t,s,n){const o=le.preProcessDoc(e,t,n),a=le.serialize(e);le.postProcessDoc(e,o.markedElements);return{windowId:s,content:a,baseURI:e.baseURI.split("#")[0],title:e.title,canvases:o.canvases,fonts:o.fonts,stylesheets:o.stylesheets,images:o.images,posters:o.posters,usedFonts:o.usedFonts,shadowRoots:o.shadowRoots,imports:o.imports,processed:!0}}function ke(e){let t=Array.from(e.querySelectorAll(ce));return e.querySelectorAll("*").forEach((e=>{const s=le.getShadowRoot(e);s&&(t=t.concat(...s.querySelectorAll(ce)))})),t}he&&(Ie="0",Ee&&Ee.runtime&&Ee.runtime.onMessage&&Ee.runtime.onMessage.addListener&&Ee.runtime.onMessage.addListener((e=>e.method==fe?(Ce(e),Promise.resolve({})):e.method==ue?(_e("requestTimeouts",e.sessionId,e.windowId),xe(e.sessionId,e.windowId),Promise.resolve({})):void 0))),Ae="message",Ne=async e=>{if("string"==typeof e.data&&e.data.startsWith(de)){e.preventDefault(),e.stopPropagation();const t=JSON.parse(e.data.substring(de.length));t.method==me?(e.source&&Le(e.source,{method:ue,windowId:t.windowId,sessionId:t.sessionId}),he||(globalThis.stop(),t.options.loadDeferredImages&&ee(t.options),await Re(t))):t.method==ue?(_e("requestTimeouts",t.sessionId,t.windowId),xe(t.sessionId,t.windowId)):t.method==ge?Fe(t):t.method==fe&&we.get(t.sessionId)&&(e.ports[0].onmessage=e=>Ce(e.data))}},ve=!0,globalThis.addEventListener(Ae,Ne,ve);const qe=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],Ue=[{tagName:"head",accept:e=>!e.childNodes.length||1==e.childNodes[0].nodeType},{tagName:"body",accept:e=>!e.childNodes.length}],He=[{tagName:"html",accept:e=>!e||8!=e.nodeType},{tagName:"head",accept:e=>!e||8!=e.nodeType&&(3!=e.nodeType||!ze(e.textContent))},{tagName:"body",accept:e=>!e||8!=e.nodeType},{tagName:"li",accept:(e,t)=>!e&&t.parentElement&&("UL"==t.parentElement.tagName||"OL"==t.parentElement.tagName)||e&&["LI"].includes(e.tagName)},{tagName:"dt",accept:e=>!e||["DT","DD"].includes(e.tagName)},{tagName:"p",accept:e=>e&&["ADDRESS","ARTICLE","ASIDE","BLOCKQUOTE","DETAILS","DIV","DL","FIELDSET","FIGCAPTION","FIGURE","FOOTER","FORM","H1","H2","H3","H4","H5","H6","HEADER","HR","MAIN","NAV","OL","P","PRE","SECTION","TABLE","UL"].includes(e.tagName)},{tagName:"dd",accept:e=>!e||["DT","DD"].includes(e.tagName)},{tagName:"rt",accept:e=>!e||["RT","RP"].includes(e.tagName)},{tagName:"rp",accept:e=>!e||["RT","RP"].includes(e.tagName)},{tagName:"optgroup",accept:e=>!e||["OPTGROUP"].includes(e.tagName)},{tagName:"option",accept:e=>!e||["OPTION","OPTGROUP"].includes(e.tagName)},{tagName:"colgroup",accept:e=>!e||8!=e.nodeType&&(3!=e.nodeType||!ze(e.textContent))},{tagName:"caption",accept:e=>!e||8!=e.nodeType&&(3!=e.nodeType||!ze(e.textContent))},{tagName:"thead",accept:e=>!e||["TBODY","TFOOT"].includes(e.tagName)},{tagName:"tbody",accept:e=>!e||["TBODY","TFOOT"].includes(e.tagName)},{tagName:"tfoot",accept:e=>!e},{tagName:"tr",accept:e=>!e||["TR"].includes(e.tagName)},{tagName:"td",accept:e=>!e||["TD","TH"].includes(e.tagName)},{tagName:"th",accept:e=>!e||["TD","TH"].includes(e.tagName)}],Ve=["style","script","xmp","iframe","noembed","noframes","plaintext","noscript"];function Be(e,t,s){return 3==e.nodeType?function(e){const t=e.parentNode;let s;t&&1==t.nodeType&&(s=t.tagName.toLowerCase());return!s||Ve.includes(s)?"script"==s?e.textContent.replace(/<\//gi,"<\\/").replace(/\/>/gi,"\\/>"):e.textContent:e.textContent.replace(/&/g,"&amp;").replace(/\u00a0/g,"&nbsp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}(e):8==e.nodeType?"\x3c!--"+e.textContent+"--\x3e":1==e.nodeType?function(e,t,s){const n=e.tagName.toLowerCase(),o=t&&Ue.find((t=>n==t.tagName&&t.accept(e)));let a="";o&&!e.attributes.length||(a="<"+n,Array.from(e.attributes).forEach((s=>a+=function(e,t,s){const n=e.name;let o="";if(!n.match(/["'>/=]/)){let a,i=e.value;s&&"class"==n&&(i=Array.from(t.classList).map((e=>e.trim())).join(" ")),i=i.replace(/&/g,"&amp;").replace(/\u00a0/g,"&nbsp;"),i.includes('"')&&(i.includes("'")||!s?i=i.replace(/"/g,"&quot;"):a=!0);const r=!s||!i.match(/^[^ \t\n\f\r'"`=<>]+$/);o+=" ",e.namespace?"http://www.w3.org/XML/1998/namespace"==e.namespaceURI?o+="xml:"+n:"http://www.w3.org/2000/xmlns/"==e.namespaceURI?("xmlns"!==n&&(o+="xmlns:"),o+=n):"http://www.w3.org/1999/xlink"==e.namespaceURI?o+="xlink:"+n:o+=n:o+=n,""!=i&&(o+="=",r&&(o+=a?"'":'"'),o+=i,r&&(o+=a?"'":'"'))}return o}(s,e,t))),a+=">");"TEMPLATE"!=e.tagName||e.childNodes.length?Array.from(e.childNodes).forEach((e=>a+=Be(e,t,s||"svg"==n))):a+=e.innerHTML;const i=t&&He.find((t=>n==t.tagName&&t.accept(e.nextSibling,e)));(s||!i&&!qe.includes(n))&&(a+="</"+n+">");return a}(e,t,s):void 0}function ze(e){return Boolean(e.match(/^[ \t\n\f\r]/))}const We={frameTree:Object.freeze({__proto__:null,getAsync:function(e){const t=Se();return e=JSON.parse(JSON.stringify(e)),new Promise((s=>{we.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{},resolve:e=>{e.sessionId=t,s(e)}}),Re({windowId:Ie,sessionId:t,options:e})}))},getSync:function(e){const t=Se();e=JSON.parse(JSON.stringify(e)),we.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{}}),function(e){const t=e.sessionId,s=globalThis._singleFile_waitForUserScript;he||(Ie=globalThis.frameId=e.windowId);Pe(ye,e.options,Ie,t),he||(e.options.userScriptEnabled&&s&&s(le.ON_BEFORE_CAPTURE_EVENT_NAME),Oe({frames:[De(ye,globalThis,Ie,e.options)],sessionId:t,requestedFrameId:ye.documentElement.dataset.requestedFrameId&&Ie}),e.options.userScriptEnabled&&s&&s(le.ON_AFTER_CAPTURE_EVENT_NAME),delete ye.documentElement.dataset.requestedFrameId)}({windowId:Ie,sessionId:t,options:e});const s=we.get(t).frames;return s.sessionId=t,s},cleanup:function(e){we.delete(e),Fe({windowId:Ie,sessionId:e,options:{sessionId:e}})},initResponse:Ce,TIMEOUT_INIT_REQUEST_MESSAGE:750})},je={COMMENT_HEADER:"Page saved with SingleFile",COMMENT_HEADER_LEGACY:"Archive processed by SingleFile",ON_BEFORE_CAPTURE_EVENT_NAME:m,ON_AFTER_CAPTURE_EVENT_NAME:u,preProcessDoc:L,postProcessDoc:H,serialize:(e,t)=>function(e,t){const s=e.doctype;let n="";return s&&(n="<!DOCTYPE "+s.nodeName,s.publicId?(n+=' PUBLIC "'+s.publicId+'"',s.systemId&&(n+=' "'+s.systemId+'"')):s.systemId&&(n+=' SYSTEM "'+s.systemId+'"'),s.internalSubset&&(n+=" ["+s.internalSubset+"]"),n+="> "),n+Be(e.documentElement,t)}(e,t),getShadowRoot:q};O("single-file-user-script-init",(()=>globalThis._singleFile_waitForUserScript=async e=>{const t=new CustomEvent(e+"-request",{cancelable:!0}),s=new Promise((t=>O(e+"-response",t)));(e=>{globalThis.dispatchEvent(e)})(t),t.defaultPrevented&&await s})),e.helper=je,e.processors=We,Object.defineProperty(e,"__esModule",{value:!0})}));