single-file-bootstrap.js 26 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.Document;let d;if(d=window._singleFile_fontFaces?window._singleFile_fontFaces:window._singleFile_fontFaces=new Map,r instanceof l&&n&&n.runtime&&n.runtime.getURL){o("single-file-new-font-face",(e=>{const t=e.detail,s=Object.assign({},t);delete s.src,d.set(JSON.stringify(s),t)})),o("single-file-delete-font",(e=>{const t=e.detail,s=Object.assign({},t);delete s.src,d.delete(JSON.stringify(s))})),o("single-file-clear-fonts",(()=>d=new Map));let e=r.createElement("script");e.src="data:,("+function(){"undefined"==typeof globalThis&&(window.globalThis=window);const e=globalThis.document,t=globalThis.console,s=e=>globalThis.dispatchEvent(e),n=globalThis.CustomEvent,o=globalThis.FileReader,a=globalThis.Blob,i=t&&t.warn&&((...e)=>t.warn(...e))||(()=>{}),r="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 t=globalThis.FontFace;let o;globalThis.FontFace=function(){return o||(i("SingleFile is hooking the FontFace constructor, document.fonts.delete and document.fonts.clear to handle dynamically loaded fonts."),o=!0),m(...arguments).then((e=>s(new n(r,{detail:e})))),new t(...arguments)},globalThis.FontFace.toString=function(){return"function FontFace() { [native code] }"};const a=e.fonts.delete;e.fonts.delete=function(t){return m(t.family).then((e=>s(new n(l,{detail:e})))),a.call(e.fonts,t)},e.fonts.delete.toString=function(){return"function delete() { [native code] }"};const c=e.fonts.clear;e.fonts.clear=function(){return s(new n(d)),c.call(e.fonts)},e.fonts.clear.toString=function(){return"function clear() { [native code] }"}}async function m(e,t,s){const n={};return n["font-family"]=e,n.src=t,s&&Object.keys(s).forEach((e=>{c[e]&&(n[c[e]]=s[e])})),new Promise((e=>{if(n.src instanceof ArrayBuffer){const t=new o;t.readAsDataURL(new a([n.src])),t.addEventListener("load",(()=>{n.src="url("+t.result+")",e(n)}))}else e(n)}))}}.toString()+")()",(r.documentElement||r).appendChild(e),e.remove(),e=r.createElement("script"),e.src=n.runtime.getURL("/lib/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",p="data-single-file-hidden-content",f="data-single-file-kept-content",h="data-single-file-hidden-frame",b="data-single-file-preserved-space-element",E="data-single-file-shadow-root-element",T="data-single-file-image",y="data-single-file-poster",w="data-single-file-canvas",A="data-single-file-import",I="data-single-file-movable-style",N="data-single-file-input-value",v="data-single-file-lazy-loaded-src",S="data-single-file-stylesheet",R="data-single-file-disabled-noscript",F="data-single-file-async-script",C="*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)",_=["NOSCRIPT","DISABLED-NOSCRIPT","META","LINK","STYLE","TITLE","TEMPLATE","SOURCE","OBJECT","SCRIPT","HEAD"],P=/^'(.*?)'$/,x=/^"(.*?)"$/,O={regular:"400",normal:"400",bold:"700",bolder:"700",lighter:"100"},M="single-file-ui-element",L=(e,t,s)=>globalThis.addEventListener(e,t,s);function k(e,t,s){let n;return e.querySelectorAll("noscript:not([data-single-file-disabled-noscript])").forEach((e=>{e.setAttribute(R,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(C).forEach((e=>e.hidden=!0)),e.querySelectorAll("svg foreignObject").forEach((e=>{const t=e.querySelectorAll("html > head > "+C+", html > body > "+C);t.length&&(Array.from(e.childNodes).forEach((e=>e.remove())),t.forEach((t=>e.appendChild(t))))})),t&&e.documentElement?(n=D(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:[],usedFonts:[],shadowRoots:[],imports:[],markedElements:[]},{canvases:n.canvases,fonts:Array.from(d.values()),stylesheets:z(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"))&&_.includes(s.tagName)||s.closest("details"),r||(i=a||V(s,l),i&&(s.setAttribute(p,""),o.markedElements.push(s)))),!i)){if(n.compressHTML&&l){const e=l.getPropertyValue("white-space");e&&e.startsWith("pre")&&(s.setAttribute(b,""),o.markedElements.push(s))}n.removeUnusedFonts&&(q(l,n,o.usedFonts),q(e.getComputedStyle(s,":first-letter"),n,o.usedFonts),q(e.getComputedStyle(s,":before"),n,o.usedFonts),q(e.getComputedStyle(s,":after"),n,o.usedFonts))}!function(e,t,s,n,o,a,i){if("CANVAS"==s.tagName)try{o.canvases.push({dataURI:s.toDataURL("image/png","")}),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(v)||s.currentSrc};if(o.images.push(t),s.setAttribute(T,o.images.length-1),o.markedElements.push(s),s.removeAttribute(v),i=i||e.getComputedStyle(s)){t.size=function(e,t,s){let n=t.naturalWidth,o=t.naturalHeight;if(!n&&!o){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"),n=t.clientWidth;t.style.setProperty("box-sizing","border-box","important"),p=t.clientWidth!=n,e?t.style.setProperty("box-sizing",e,s):t.style.removeProperty("box-sizing")}i=j("padding-left",s),r=j("padding-right",s),l=j("padding-top",s),d=j("padding-bottom",s),p?(c=j("border-left-width",s),m=j("border-right-width",s),u=j("border-top-width",s),g=j("border-bottom-width",s)):c=m=u=g=0,n=Math.max(0,t.clientWidth-i-r-c-m),o=Math.max(0,t.clientHeight-l-d-u-g),a&&t.removeAttribute("style")}return{pxWidth:n,pxHeight:o}}(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(A,o.imports.length-1),o.markedElements.push(s));"INPUT"==s.tagName&&("password"!=s.type&&(s.setAttribute(N,s.value),o.markedElements.push(s)),"radio"!=s.type&&"checkbox"!=s.type||(s.setAttribute(N,s.checked),o.markedElements.push(s)));"TEXTAREA"==s.tagName&&(s.setAttribute(N,s.value),o.markedElements.push(s));"SELECT"==s.tagName&&s.querySelectorAll("option").forEach((e=>{e.selected&&(e.setAttribute(N,""),o.markedElements.push(e))}));"SCRIPT"==s.tagName&&(s.async&&""!=s.getAttribute("async")&&"async"!=s.getAttribute("async")&&(s.setAttribute(F,""),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)&&U(s);if(d&&!s.classList.contains(M)){const a={};s.setAttribute(E,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(f)?s.parentElement&&(s.parentElement.setAttribute(f,""),o.markedElements.push(s.parentElement)):i&&(s.setAttribute(g,""),o.markedElements.push(s)))})),o}function q(e,t,s){if(e){const n=e.getPropertyValue("font-style")||"normal";e.getPropertyValue("font-family").split(",").forEach((o=>{if(o=H(o),!t.loadedFonts||t.loadedFonts.find((e=>H(e.family)==o&&e.style==n))){const t=(a=e.getPropertyValue("font-weight"),O[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 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(P)?e.replace(P,"$1"):e.replace(x,"$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 V(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)}function B(e,t){if(e.querySelectorAll("[data-single-file-disabled-noscript]").forEach((e=>{e.textContent=e.getAttribute(R),e.removeAttribute(R)})),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,p,b,T,y,w,N,E,A,S,F];t=e.querySelectorAll(s.map((e=>"["+e+"]")).join(","))}t.forEach((e=>{e.removeAttribute(g),e.removeAttribute(p),e.removeAttribute(f),e.removeAttribute(h),e.removeAttribute(b),e.removeAttribute(T),e.removeAttribute(y),e.removeAttribute(w),e.removeAttribute(N),e.removeAttribute(E),e.removeAttribute(A),e.removeAttribute(S),e.removeAttribute(F),e.removeAttribute(I)}))}function z(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(S,n),t[n]=Array.from(s.sheet.cssRules).map((e=>e.cssText)).join("\n"))}catch(e){}})),t}}function j(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=v,G=M,Y="attributes",K=globalThis.browser,$=globalThis.document,Q=globalThis.MutationObserver,X=(e,t,s)=>globalThis.addEventListener(e,t,s),Z=(e,t,s)=>globalThis.removeEventListener(e,t,s),ee=new Map;let te;async function se(e){if($.documentElement){ee.clear();const n=Math.max($.documentElement.scrollHeight-1.5*$.documentElement.clientHeight,0),o=Math.max($.documentElement.scrollWidth-1.5*$.documentElement.clientWidth,0);if(globalThis.scrollY<=n&&globalThis.scrollX<=o)return function(e){return te=0,new Promise((async n=>{let o;const r=new Set,l=new Q((async t=>{if((t=t.filter((e=>e.type==Y))).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(G)})).length&&(o=!0,await oe(l,e,g),r.size||await ne(l,e,g))}}));async function d(t){await ie("idleTimeout",(async()=>{o?te<10&&(te++,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(J),t.removeEventListener("load",c)}async function m(t){o=!0,await oe(l,e,g),await ne(l,e,g),t.detail&&r.add(t.detail)}async function u(t){await oe(l,e,g),await ne(l,e,g),r.delete(t.detail),r.size||await ne(l,e,g)}function g(e){l.disconnect(),Z(t,m),Z(s,u),n(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&&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 ne(e,t,s){await ie("loadTimeout",(()=>ae(e,t,s)),t.loadDeferredImagesMaxIdleTime)}async function oe(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&&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 ie("endTimeout",(async()=>{await le("maxTimeout"),s()}),t.loadDeferredImagesMaxIdleTime/2),e.disconnect()}async function ie(e,t,s){if(K&&K.runtime&&K.runtime.sendMessage){if(!ee.get(e)||!ee.get(e).pending){const n={callback:t,pending:!0};ee.set(e,n);try{await K.runtime.sendMessage({method:"singlefile.lazyTimeout.setTimeout",type:e,delay:s})}catch(n){re(e,t,s)}n.pending=!1}}else re(e,t,s)}function re(e,t,s){const n=ee.get(e);n&&globalThis.clearTimeout(n),ee.set(e,t),globalThis.setTimeout(t,s)}async function le(e){if(K&&K.runtime&&K.runtime.sendMessage)try{await K.runtime.sendMessage({method:"singlefile.lazyTimeout.clearTimeout",type:e})}catch(t){de(e)}else de(e)}function de(e){const t=ee.get(e);ee.delete(e),t&&globalThis.clearTimeout(t)}K&&K.runtime&&K.runtime.onMessage&&K.runtime.onMessage.addListener&&K.runtime.onMessage.addListener((e=>{if("singlefile.lazyTimeout.onTimeout"==e.method){const t=ee.get(e.type);if(t){ee.delete(e.type);try{t.callback()}catch(t){de(e.type)}}}}));const ce={ON_BEFORE_CAPTURE_EVENT_NAME:m,ON_AFTER_CAPTURE_EVENT_NAME:u,WIN_ID_ATTRIBUTE_NAME:"data-single-file-win-id",preProcessDoc:k,serialize:W,postProcessDoc:B,getShadowRoot:U},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",be=5e3,Ee=".",Te=globalThis.window==globalThis.top,ye=globalThis.browser,we=globalThis.top,Ae=globalThis.MessageChannel,Ie=globalThis.document,Ne=new Map;let ve;var Se,Re,Fe;function Ce(){return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("")}async function _e(e){const t=e.sessionId,s=globalThis._singleFile_waitForUserScript;delete globalThis._singleFile_cleaningUp,Te||(ve=globalThis.frameId=e.windowId),Oe(Ie,e.options,ve,t),Te||(e.options.userScriptEnabled&&s&&await s(ce.ON_BEFORE_CAPTURE_EVENT_NAME),De({frames:[Ue(Ie,globalThis,ve,e.options)],sessionId:t,requestedFrameId:Ie.documentElement.dataset.requestedFrameId&&ve}),e.options.userScriptEnabled&&s&&await s(ce.ON_AFTER_CAPTURE_EVENT_NAME),delete Ie.documentElement.dataset.requestedFrameId)}function Pe(e){if(!globalThis._singleFile_cleaningUp){globalThis._singleFile_cleaningUp=!0;const t=e.sessionId;ke(He(Ie),e.windowId,t)}}function xe(e){e.frames.forEach((t=>Me("responseTimeouts",e.sessionId,t.windowId)));const t=Ne.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 Oe(e,t,s,n){const o=He(e);!function(e,t,s,n,o){const a=[];let i;Ne.get(o)?i=Ne.get(o).requestTimeouts:(i={},Ne.set(o,{requestTimeouts:i}));t.forEach(((e,t)=>{const s=n+Ee+t;e.setAttribute(ce.WIN_ID_ATTRIBUTE_NAME,s),a.push({windowId:s})})),De({frames:a,sessionId:o,requestedFrameId:e.documentElement.dataset.requestedFrameId&&n}),t.forEach(((e,t)=>{const a=n+Ee+t;try{qe(e.contentWindow,{method:ge,windowId:a,sessionId:o,options:s})}catch(e){}i[a]=globalThis.setTimeout((()=>De({frames:[{windowId:a,processed:!0}],sessionId:o})),be)})),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+Ee+t;let r;try{r=e.contentDocument}catch(e){}if(r)try{const t=e.contentWindow;t.stop(),Me("requestTimeouts",o,i),Oe(r,s,i,o),a.push(Ue(r,t,i,s))}catch(e){a.push({windowId:i,processed:!0})}})),De({frames:a,sessionId:o,requestedFrameId:e.documentElement.dataset.requestedFrameId&&n}),delete e.documentElement.dataset.requestedFrameId}(e,o,t,s,n)}function Me(e,t,s){const n=Ne.get(t);if(n&&n[e]){const t=n[e][s];t&&(globalThis.clearTimeout(t),delete n[e][s])}}function Le(e,t){const s=Ne.get(e);s&&s.responseTimeouts&&(s.responseTimeouts[t]=globalThis.setTimeout((()=>De({frames:[{windowId:t,processed:!0}],sessionId:e})),1e4))}function ke(e,t,s){e.forEach(((e,n)=>{const o=t+Ee+n;e.removeAttribute(ce.WIN_ID_ATTRIBUTE_NAME);try{qe(e.contentWindow,{method:fe,windowId:o,sessionId:s})}catch(e){}})),e.forEach(((e,n)=>{const o=t+Ee+n;let a;try{a=e.contentDocument}catch(e){}if(a)try{ke(He(a),o,s)}catch(e){}}))}function De(e){e.method=he;try{we.singlefile.processors.frameTree.initResponse(e)}catch(t){qe(we,e,!0)}}function qe(e,t,s){if(e==we&&ye&&ye.runtime&&ye.runtime.sendMessage)ye.runtime.sendMessage(t);else if(s){const s=new Ae;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 Ue(e,t,s,n){const o=ce.preProcessDoc(e,t,n),a=ce.serialize(e);ce.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 He(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&&(ve="0",ye&&ye.runtime&&ye.runtime.onMessage&&ye.runtime.onMessage.addListener&&ye.runtime.onMessage.addListener((e=>e.method==he?(xe(e),Promise.resolve({})):e.method==pe?(Me("requestTimeouts",e.sessionId,e.windowId),Le(e.sessionId,e.windowId),Promise.resolve({})):void 0))),Se="message",Re=async e=>{if("string"==typeof e.data&&e.data.startsWith(me)){e.preventDefault(),e.stopPropagation();const t=JSON.parse(e.data.substring(me.length));t.method==ge?(e.source&&qe(e.source,{method:pe,windowId:t.windowId,sessionId:t.sessionId}),Te||(globalThis.stop(),t.options.loadDeferredImages&&se(t.options),await _e(t))):t.method==pe?(Me("requestTimeouts",t.sessionId,t.windowId),Le(t.sessionId,t.windowId)):t.method==fe?Pe(t):t.method==he&&Ne.get(t.sessionId)&&(e.ports[0].onmessage=e=>xe(e.data))}},Fe=!0,globalThis.addEventListener(Se,Re,Fe);var Ve=Object.freeze({__proto__:null,getAsync:function(e){const t=Ce();return e=JSON.parse(JSON.stringify(e)),new Promise((s=>{Ne.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{},resolve:e=>{e.sessionId=t,s(e)}}),_e({windowId:ve,sessionId:t,options:e})}))},getSync:function(e){const t=Ce();e=JSON.parse(JSON.stringify(e)),Ne.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{}}),function(e){const t=e.sessionId,s=globalThis._singleFile_waitForUserScript;delete globalThis._singleFile_cleaningUp,Te||(ve=globalThis.frameId=e.windowId);Oe(Ie,e.options,ve,t),Te||(e.options.userScriptEnabled&&s&&s(ce.ON_BEFORE_CAPTURE_EVENT_NAME),De({frames:[Ue(Ie,globalThis,ve,e.options)],sessionId:t,requestedFrameId:Ie.documentElement.dataset.requestedFrameId&&ve}),e.options.userScriptEnabled&&s&&s(ce.ON_AFTER_CAPTURE_EVENT_NAME),delete Ie.documentElement.dataset.requestedFrameId)}({windowId:ve,sessionId:t,options:e});const s=Ne.get(t).frames;return s.sessionId=t,s},cleanup:function(e){Ne.delete(e),Pe({windowId:ve,sessionId:e,options:{sessionId:e}})},initResponse:xe,TIMEOUT_INIT_REQUEST_MESSAGE:be});const Be=["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}],je=[{tagName:"html",accept:e=>!e||8!=e.nodeType},{tagName:"head",accept:e=>!e||8!=e.nodeType&&(3!=e.nodeType||!Ge(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||!Ge(e.textContent))},{tagName:"caption",accept:e=>!e||8!=e.nodeType&&(3!=e.nodeType||!Ge(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 n=e.tagName.toLowerCase(),o=t&&ze.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+=Je(e,t,s||"svg"==n))):a+=e.innerHTML;const i=t&&je.find((t=>n==t.tagName&&t.accept(e.nextSibling,e)));(s||!i&&!Be.includes(n))&&(a+="</"+n+">");return a}(e,t,s):void 0}function Ge(e){return Boolean(e.match(/^[ \t\n\f\r]/))}const Ye={frameTree:Ve},Ke={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:k,postProcessDoc:B,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+Je(e.documentElement,t)}(e,t),getShadowRoot:U};L("single-file-user-script-init",(()=>globalThis._singleFile_waitForUserScript=async e=>{const t=new CustomEvent(e+"-request",{cancelable:!0}),s=new Promise((t=>L(e+"-response",t)));(e=>{globalThis.dispatchEvent(e)})(t),t.defaultPrevented&&await s})),e.helper=Ke,e.processors=Ye,Object.defineProperty(e,"__esModule",{value:!0})}));