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