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