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-deferred-images-start",s="single-file-load-deferred-images-end",o="single-file-load-deferred-images-keep-zoom-level-start",n="single-file-load-deferred-images-keep-zoom-level-end",a="single-file-block-cookies-start",i="single-file-block-cookies-end",r="single-file-dispatch-scroll-event-start",l="single-file-dispatch-scroll-event-end",d="single-file-block-storage-start",c="single-file-block-storage-end",m="single-file-load-image",u="single-file-image-loaded",g=(e,t,s)=>globalThis.addEventListener(e,t,s),p=e=>{try{globalThis.dispatchEvent(e)}catch(e){}},h=globalThis.CustomEvent,f=globalThis.document,E=globalThis.Document,T=globalThis.JSON;let b;b=window._singleFile_fontFaces?window._singleFile_fontFaces:window._singleFile_fontFaces=new Map,f instanceof E&&(g("single-file-new-font-face",(e=>{const t=e.detail,s=Object.assign({},t);delete s.src,b.set(T.stringify(s),t)})),g("single-file-delete-font",(e=>{const t=e.detail,s=Object.assign({},t);delete s.src,b.delete(T.stringify(s))})),g("single-file-clear-fonts",(()=>b=new Map)));const y="[\\x20\\t\\r\\n\\f]",I=new RegExp("\\\\([\\da-f]{1,6}"+y+"?|("+y+")|.)","ig");const w="single-file-",A="_singleFile_waitForUserScript",v="__frameTree__::",S=w+"on-before-capture",R=w+"on-after-capture",N=w+"request-get-adopted-stylesheets",_=w+"response-get-adopted-stylesheets",P=w+"unregister-request-get-adopted-stylesheets",C=w+"user-script-init",M="data-"+w+"removed-content",O="data-"+w+"hidden-content",D="data-"+w+"kept-content",F="data-"+w+"hidden-frame",L="data-"+w+"preserved-space-element",x="data-"+w+"shadow-root-element",U="data-"+w+"win-id",q="data-"+w+"image",k="data-"+w+"poster",H="data-"+w+"video",B="data-"+w+"canvas",V="data-"+w+"movable-style",W="data-"+w+"input-value",z="data-"+w+"lazy-loaded-src",Y="data-"+w+"stylesheet",j="data-"+w+"disabled-noscript",G="data-"+w+"invalid-element",K="data-"+w+"async-script",X="*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)",Z=["NOSCRIPT","DISABLED-NOSCRIPT","META","LINK","STYLE","TITLE","TEMPLATE","SOURCE","OBJECT","SCRIPT","HEAD","BODY"],J=/^'(.*?)'$/,$=/^"(.*?)"$/,Q={regular:"400",normal:"400",bold:"700",bolder:"700",lighter:"100"},ee="single-file-ui-element",te="data:,",se=(e,t,s)=>globalThis.addEventListener(e,t,s),oe=globalThis.JSON;function ne(e,t,s){e.querySelectorAll("noscript:not(["+j+"])").forEach((e=>{e.setAttribute(j,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(X).forEach((e=>e.hidden=!0)),e.querySelectorAll("svg foreignObject").forEach((e=>{const t=e.querySelectorAll("html > head > "+X+", html > body > "+X);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").forEach((t=>{const s=e.createElement("template");s.setAttribute(G,""),s.content.appendChild(t.cloneNode(!0)),o.set(t,s),t.replaceWith(s)})),n=ae(t,e,e.documentElement,s),s.moveStylesInHead&&e.querySelectorAll("body style, body ~ style").forEach((e=>{const s=pe(t,e);s&&ce(e,s)&&(e.setAttribute(V,""),n.markedElements.push(e))}))):n={canvases:[],images:[],posters:[],videos:[],usedFonts:[],shadowRoots:[],markedElements:[]},{canvases:n.canvases,fonts:Array.from(b.values()),stylesheets:ue(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,scrollPosition:{x:t.scrollX,y:t.scrollY},adoptedStyleSheets:ie(e.adoptedStyleSheets)}}function ae(e,t,s,o,n={usedFonts:new Map,canvases:[],images:[],posters:[],videos:[],shadowRoots:[],markedElements:[]},a){if(s.childNodes){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=pe(e,s),s instanceof e.HTMLElement&&o.removeHiddenElements&&(r=(a||s.closest("html > head"))&&Z.includes(s.tagName.toUpperCase())||s.closest("details"),r||(i=a||ce(s,l),i&&(s.setAttribute(O,""),n.markedElements.push(s)))),!i)){if(o.compressHTML&&l){const e=l.getPropertyValue("white-space");e&&e.startsWith("pre")&&(s.setAttribute(L,""),n.markedElements.push(s))}o.removeUnusedFonts&&(re(l,o,n.usedFonts),re(pe(e,s,":first-letter"),o,n.usedFonts),re(pe(e,s,":before"),o,n.usedFonts),re(pe(e,s,":after"),o,n.usedFonts))}!function(e,t,s,o,n,a,i){const r=s.tagName&&s.tagName.toUpperCase();if("CANVAS"==r)try{n.canvases.push({dataURI:s.toDataURL("image/png",""),backgroundColor:i.getPropertyValue("background-color")}),s.setAttribute(B,n.canvases.length-1),n.markedElements.push(s)}catch(e){}if("IMG"==r){const t={currentSrc:a?te:o.loadDeferredImages&&s.getAttribute(z)||s.currentSrc};if(n.images.push(t),s.setAttribute(q,n.images.length-1),n.markedElements.push(s),s.removeAttribute(z),i=i||pe(e,s)){t.size=function(e,t,s){let o=t.naturalWidth,n=t.naturalHeight;if(!o&&!n){const a=null==t.getAttribute("style");if(s=s||pe(e,t)){let e,i,r,l,d,c,m,u,g=!1;if("content-box"==s.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")}e=ge("padding-left",s),i=ge("padding-right",s),r=ge("padding-top",s),l=ge("padding-bottom",s),g?(d=ge("border-left-width",s),c=ge("border-right-width",s),m=ge("border-top-width",s),u=ge("border-bottom-width",s)):d=c=m=u=0,o=Math.max(0,t.clientWidth-e-i-d-c),n=Math.max(0,t.clientHeight-r-l-m-u),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"==r){const o=s.currentSrc;if(o&&!o.startsWith("blob:")&&!o.startsWith("data:")){const t=pe(e,s.parentNode);n.videos.push({positionParent:t&&t.getPropertyValue("position"),src:o,size:{pxWidth:s.clientWidth,pxHeight:s.clientHeight},currentTime:s.currentTime}),s.setAttribute(H,n.videos.length-1)}if(!s.getAttribute("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(k,n.posters.length-1),n.markedElements.push(s)}catch(e){}}}"IFRAME"==r&&a&&o.removeHiddenElements&&(s.setAttribute(F,""),n.markedElements.push(s));"INPUT"==r&&("password"!=s.type&&(s.setAttribute(W,s.value),n.markedElements.push(s)),"radio"!=s.type&&"checkbox"!=s.type||(s.setAttribute(W,s.checked),n.markedElements.push(s)));"TEXTAREA"==r&&(s.setAttribute(W,s.value),n.markedElements.push(s));"SELECT"==r&&s.querySelectorAll("option").forEach((e=>{e.selected&&(e.setAttribute(W,""),n.markedElements.push(e))}));"SCRIPT"==r&&(s.async&&""!=s.getAttribute("async")&&"async"!=s.getAttribute("async")&&(s.setAttribute(K,""),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)&&le(s);if(d&&!s.classList.contains(ee)){const a={};s.setAttribute(x,n.shadowRoots.length),n.markedElements.push(s),n.shadowRoots.push(a);try{if(d.adoptedStyleSheets)if(d.adoptedStyleSheets.length)a.adoptedStyleSheets=ie(d.adoptedStyleSheets);else if(void 0===d.adoptedStyleSheets.length){const e=e=>a.adoptedStyleSheets=e.detail.adoptedStyleSheets;s.addEventListener(_,e),s.dispatchEvent(new CustomEvent(N,{bubbles:!0})),s.removeEventListener(_,e)}}catch(e){}ae(e,t,d,o,n,i),a.content=d.innerHTML,a.mode=d.mode;try{d.adoptedStyleSheets&&void 0===d.adoptedStyleSheets.length&&s.dispatchEvent(new CustomEvent(P,{bubbles:!0}))}catch(e){}}ae(e,t,s,o,n,i),!o.autoSaveExternalSave&&o.removeHiddenElements&&a&&(r||""==s.getAttribute(D)?s.parentElement&&(s.parentElement.setAttribute(D,""),n.markedElements.push(s.parentElement)):i&&(s.setAttribute(M,""),n.markedElements.push(s)))}))}return n}function ie(e){return e?Array.from(e).map((e=>Array.from(e.cssRules).map((e=>e.cssText)).join("\n"))):[]}function re(e,t,s){if(e){const o=e.getPropertyValue("font-style")||"normal";e.getPropertyValue("font-family").split(",").forEach((n=>{if(n=de(n),!t.loadedFonts||t.loadedFonts.find((e=>de(e.family)==n&&e.style==o))){const t=(a=e.getPropertyValue("font-weight"),Q[a.toLowerCase().trim()]||a),i=e.getPropertyValue("font-variant")||"normal",r=[n,t,o,i];s.set(oe.stringify(r),[n,t,o,i])}var a}))}}function le(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 de(e=""){return function(e){e=e.match(J)?e.replace(J,"$1"):e.replace($,"$1");return e.trim()}((t=e.trim(),t.replace(I,((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 ce(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 me(e,t,s){if(e.querySelectorAll("["+j+"]").forEach((e=>{e.textContent=e.getAttribute(j),e.removeAttribute(j)})),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=[M,F,O,L,q,k,H,B,W,x,Y,K];t=e.querySelectorAll(s.map((e=>"["+e+"]")).join(","))}t.forEach((e=>{e.removeAttribute(M),e.removeAttribute(O),e.removeAttribute(D),e.removeAttribute(F),e.removeAttribute(L),e.removeAttribute(q),e.removeAttribute(k),e.removeAttribute(H),e.removeAttribute(B),e.removeAttribute(W),e.removeAttribute(x),e.removeAttribute(Y),e.removeAttribute(K),e.removeAttribute(V)})),s&&s.forEach(((e,t)=>e.replaceWith(t)))}function ue(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(Y,o),t[o]=Array.from(s.sheet.cssRules).map((e=>e.cssText)).join("\n"))}catch(e){}})),t}}function ge(e,t){if(t.getPropertyValue(e).endsWith("px"))return parseFloat(t.getPropertyValue(e))}function pe(e,t,s){try{return e.getComputedStyle(t,s)}catch(e){}}const he={LAZY_SRC_ATTRIBUTE_NAME:z,SINGLE_FILE_UI_ELEMENT_CLASS:ee},fe=10,Ee="attributes",Te=globalThis.browser,be=globalThis.document,ye=globalThis.MutationObserver,Ie=(e,t,s)=>globalThis.addEventListener(e,t,s),we=(e,t,s)=>globalThis.removeEventListener(e,t,s),Ae=new Map;let ve;async function Se(e){if(be.documentElement){Ae.clear();const s=be.body?Math.max(be.body.scrollHeight,be.documentElement.scrollHeight):be.documentElement.scrollHeight,n=be.body?Math.max(be.body.scrollWidth,be.documentElement.scrollWidth):be.documentElement.scrollWidth;if(s>globalThis.innerHeight||n>globalThis.innerWidth){const i=Math.max(s-1.5*globalThis.innerHeight,0),l=Math.max(n-1.5*globalThis.innerWidth,0);if(globalThis.scrollY<i||globalThis.scrollX<l)return function(e){return ve=0,new Promise((async s=>{let n;const i=new Set,l=new ye((async t=>{if((t=t.filter((e=>e.type==Ee))).length){t.filter((e=>{if("src"==e.attributeName&&(e.target.setAttribute(he.LAZY_SRC_ATTRIBUTE_NAME,e.target.src),e.target.addEventListener("load",g)),"src"==e.attributeName||"srcset"==e.attributeName||e.target.tagName&&"SOURCE"==e.target.tagName.toUpperCase())return!e.target.classList||!e.target.classList.contains(he.SINGLE_FILE_UI_ELEMENT_CLASS)})).length&&(n=!0,await Ne(l,e,T),i.size||await Re(l,e,T))}}));async function c(t){await Pe("idleTimeout",(async()=>{n?ve<fe&&(ve++,Me("idleTimeout"),await c(Math.max(500,t/2))):(Me("loadTimeout"),Me("maxTimeout"),_e(l,e,T))}),t,e.loadDeferredImagesNativeTimeout)}function g(e){const t=e.target;t.removeAttribute(he.LAZY_SRC_ATTRIBUTE_NAME),t.removeEventListener("load",g)}async function f(t){n=!0,await Ne(l,e,T),await Re(l,e,T),t.detail&&i.add(t.detail)}async function E(t){await Ne(l,e,T),await Re(l,e,T),i.delete(t.detail),i.size||await Re(l,e,T)}function T(e){l.disconnect(),we(m,f),we(u,E),s(e)}await c(2*e.loadDeferredImagesMaxIdleTime),await Ne(l,e,T),l.observe(be,{subtree:!0,childList:!0,attributes:!0}),Ie(m,f),Ie(u,E),function(e){e.loadDeferredImagesBlockCookies&&p(new h(a)),e.loadDeferredImagesBlockStorage&&p(new h(d)),e.loadDeferredImagesDispatchScrollEvent&&p(new h(r)),e.loadDeferredImagesKeepZoomLevel?p(new h(o)):p(new h(t))}(e)}))}(e)}}}async function Re(e,t,s){await Pe("loadTimeout",(()=>_e(e,t,s)),t.loadDeferredImagesMaxIdleTime,t.loadDeferredImagesNativeTimeout)}async function Ne(e,t,s){await Pe("maxTimeout",(async()=>{await Me("loadTimeout"),await _e(e,t,s)}),10*t.loadDeferredImagesMaxIdleTime,t.loadDeferredImagesNativeTimeout)}async function _e(e,t,o){await Me("idleTimeout"),function(e){e.loadDeferredImagesBlockCookies&&p(new h(i)),e.loadDeferredImagesBlockStorage&&p(new h(c)),e.loadDeferredImagesDispatchScrollEvent&&p(new h(l)),e.loadDeferredImagesKeepZoomLevel?p(new h(n)):p(new h(s))}(t),await Pe("endTimeout",(async()=>{await Me("maxTimeout"),o()}),t.loadDeferredImagesMaxIdleTime/2,t.loadDeferredImagesNativeTimeout),e.disconnect()}async function Pe(e,t,s,o){if(Te&&Te.runtime&&Te.runtime.sendMessage&&!o){if(!Ae.get(e)||!Ae.get(e).pending){const o={callback:t,pending:!0};Ae.set(e,o);try{await Te.runtime.sendMessage({method:"singlefile.lazyTimeout.setTimeout",type:e,delay:s})}catch(o){Ce(e,t,s)}o.pending=!1}}else Ce(e,t,s)}function Ce(e,t,s){const o=Ae.get(e);o&&globalThis.clearTimeout(o),Ae.set(e,t),globalThis.setTimeout(t,s)}async function Me(e){if(Te&&Te.runtime&&Te.runtime.sendMessage)try{await Te.runtime.sendMessage({method:"singlefile.lazyTimeout.clearTimeout",type:e})}catch(t){Oe(e)}else Oe(e)}function Oe(e){const t=Ae.get(e);Ae.delete(e),t&&globalThis.clearTimeout(t)}Te&&Te.runtime&&Te.runtime.onMessage&&Te.runtime.onMessage.addListener&&Te.runtime.onMessage.addListener((e=>{if("singlefile.lazyTimeout.onTimeout"==e.method){const t=Ae.get(e.type);if(t){Ae.delete(e.type);try{t.callback()}catch(t){Oe(e.type)}}}}));const De={ON_BEFORE_CAPTURE_EVENT_NAME:S,ON_AFTER_CAPTURE_EVENT_NAME:R,WIN_ID_ATTRIBUTE_NAME:U,WAIT_FOR_USERSCRIPT_PROPERTY_NAME:A,preProcessDoc:ne,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:me,getShadowRoot:le},Fe='iframe, frame, object[type="text/html"][data]',Le="*",xe="singlefile.frameTree.initRequest",Ue="singlefile.frameTree.ackInitRequest",qe="singlefile.frameTree.cleanupRequest",ke="singlefile.frameTree.initResponse",He="*",Be=5e3,Ve=".",We=globalThis.window==globalThis.top,ze=globalThis.browser,Ye=globalThis.top,je=globalThis.MessageChannel,Ge=globalThis.document,Ke=globalThis.JSON;let Xe,Ze=globalThis.sessions;var Je,$e,Qe;function et(){return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("")}async function tt(e){const t=e.sessionId,s=globalThis[De.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];delete globalThis._singleFile_cleaningUp,We||(Xe=globalThis.frameId=e.windowId),nt(Ge,e.options,Xe,t),We||(e.options.userScriptEnabled&&s&&await s(De.ON_BEFORE_CAPTURE_EVENT_NAME),lt({frames:[ct(Ge,globalThis,Xe,e.options,e.scrolling)],sessionId:t,requestedFrameId:Ge.documentElement.dataset.requestedFrameId&&Xe}),e.options.userScriptEnabled&&s&&await s(De.ON_AFTER_CAPTURE_EVENT_NAME),delete Ge.documentElement.dataset.requestedFrameId)}function st(e){if(!globalThis._singleFile_cleaningUp){globalThis._singleFile_cleaningUp=!0;const t=e.sessionId;rt(mt(Ge),e.windowId,t)}}function ot(e){e.frames.forEach((t=>at("responseTimeouts",e.sessionId,t.windowId)));const t=Ze.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.url=e.url,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,s.scrollPosition=e.scrollPosition,s.scrolling=e.scrolling,s.adoptedStyleSheets=e.adoptedStyleSheets)}));t.frames.filter((e=>!e.processed)).length||(t.frames=t.frames.sort(((e,t)=>t.windowId.split(Ve).length-e.windowId.split(Ve).length)),t.resolve&&(t.requestedFrameId&&t.frames.forEach((e=>{e.windowId==t.requestedFrameId&&(e.requestedFrame=!0)})),t.resolve(t.frames)))}}function nt(e,t,s,o){const n=mt(e);!function(e,t,s,o,n){const a=[];let i;Ze.get(n)?i=Ze.get(n).requestTimeouts:(i={},Ze.set(n,{requestTimeouts:i}));t.forEach(((e,t)=>{const s=o+Ve+t;e.setAttribute(De.WIN_ID_ATTRIBUTE_NAME,s),a.push({windowId:s})})),lt({frames:a,sessionId:n,requestedFrameId:e.documentElement.dataset.requestedFrameId&&o}),t.forEach(((e,t)=>{const a=o+Ve+t;try{dt(e.contentWindow,{method:xe,windowId:a,sessionId:n,options:s,scrolling:e.scrolling})}catch(e){}i[a]=globalThis.setTimeout((()=>lt({frames:[{windowId:a,processed:!0}],sessionId:n})),Be)})),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+Ve+t;let r;try{r=e.contentDocument}catch(e){}if(r)try{const t=e.contentWindow;t.stop(),at("requestTimeouts",n,i),nt(r,s,i,n),a.push(ct(r,t,i,s,e.scrolling))}catch(e){a.push({windowId:i,processed:!0})}})),lt({frames:a,sessionId:n,requestedFrameId:e.documentElement.dataset.requestedFrameId&&o}),delete e.documentElement.dataset.requestedFrameId}(e,n,t,s,o)}function at(e,t,s){const o=Ze.get(t);if(o&&o[e]){const t=o[e][s];t&&(globalThis.clearTimeout(t),delete o[e][s])}}function it(e,t){const s=Ze.get(e);s&&s.responseTimeouts&&(s.responseTimeouts[t]=globalThis.setTimeout((()=>lt({frames:[{windowId:t,processed:!0}],sessionId:e})),1e4))}function rt(e,t,s){e.forEach(((e,o)=>{const n=t+Ve+o;e.removeAttribute(De.WIN_ID_ATTRIBUTE_NAME);try{dt(e.contentWindow,{method:qe,windowId:n,sessionId:s})}catch(e){}})),e.forEach(((e,o)=>{const n=t+Ve+o;let a;try{a=e.contentDocument}catch(e){}if(a)try{rt(mt(a),n,s)}catch(e){}}))}function lt(e){e.method=ke;try{Ye.singlefile.processors.frameTree.initResponse(e)}catch(t){dt(Ye,e,!0)}}function dt(e,t,s){if(e==Ye&&ze&&ze.runtime&&ze.runtime.sendMessage)ze.runtime.sendMessage(t);else if(s){const s=new je;e.postMessage(v+Ke.stringify({method:t.method,sessionId:t.sessionId}),He,[s.port2]),s.port1.postMessage(t)}else e.postMessage(v+Ke.stringify(t),He)}function ct(e,t,s,o,n){const a=De.preProcessDoc(e,t,o),i=De.serialize(e);De.postProcessDoc(e,a.markedElements,a.invalidElements);return{windowId:s,content:i,baseURI:e.baseURI.split("#")[0],url:e.location.href,title:e.title,canvases:a.canvases,fonts:a.fonts,stylesheets:a.stylesheets,images:a.images,posters:a.posters,videos:a.videos,usedFonts:a.usedFonts,shadowRoots:a.shadowRoots,scrollPosition:a.scrollPosition,scrolling:n,adoptedStyleSheets:a.adoptedStyleSheets,processed:!0}}function mt(e){let t=Array.from(e.querySelectorAll(Fe));return e.querySelectorAll(Le).forEach((e=>{const s=De.getShadowRoot(e);s&&(t=t.concat(...s.querySelectorAll(Fe)))})),t}Ze||(Ze=globalThis.sessions=new Map),We&&(Xe="0",ze&&ze.runtime&&ze.runtime.onMessage&&ze.runtime.onMessage.addListener&&ze.runtime.onMessage.addListener((e=>e.method==ke?(ot(e),Promise.resolve({})):e.method==Ue?(at("requestTimeouts",e.sessionId,e.windowId),it(e.sessionId,e.windowId),Promise.resolve({})):void 0))),Je="message",$e=async e=>{if("string"==typeof e.data&&e.data.startsWith(v)){e.preventDefault(),e.stopPropagation();const t=Ke.parse(e.data.substring(v.length));t.method==xe?(e.source&&dt(e.source,{method:Ue,windowId:t.windowId,sessionId:t.sessionId}),We||(globalThis.stop(),t.options.loadDeferredImages&&Se(t.options),await tt(t))):t.method==Ue?(at("requestTimeouts",t.sessionId,t.windowId),it(t.sessionId,t.windowId)):t.method==qe?st(t):t.method==ke&&Ze.get(t.sessionId)&&(e.ports[0].onmessage=e=>ot(e.data))}},Qe=!0,globalThis.addEventListener(Je,$e,Qe);var ut=Object.freeze({__proto__:null,getAsync:function(e){const t=et();return e=Ke.parse(Ke.stringify(e)),new Promise((s=>{Ze.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{},resolve:e=>{e.sessionId=t,s(e)}}),tt({windowId:Xe,sessionId:t,options:e})}))},getSync:function(e){const t=et();e=Ke.parse(Ke.stringify(e)),Ze.set(t,{frames:[],requestTimeouts:{},responseTimeouts:{}}),function(e){const t=e.sessionId,s=globalThis[De.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];delete globalThis._singleFile_cleaningUp,We||(Xe=globalThis.frameId=e.windowId);nt(Ge,e.options,Xe,t),We||(e.options.userScriptEnabled&&s&&s(De.ON_BEFORE_CAPTURE_EVENT_NAME),lt({frames:[ct(Ge,globalThis,Xe,e.options,e.scrolling)],sessionId:t,requestedFrameId:Ge.documentElement.dataset.requestedFrameId&&Xe}),e.options.userScriptEnabled&&s&&s(De.ON_AFTER_CAPTURE_EVENT_NAME),delete Ge.documentElement.dataset.requestedFrameId)}({windowId:Xe,sessionId:t,options:e});const s=Ze.get(t).frames;return s.sessionId=t,s},cleanup:function(e){Ze.delete(e),st({windowId:Xe,sessionId:e,options:{sessionId:e}})},initResponse:ot,TIMEOUT_INIT_REQUEST_MESSAGE:Be});const gt=["AREA","BASE","BR","COL","COMMAND","EMBED","HR","IMG","INPUT","KEYGEN","LINK","META","PARAM","SOURCE","TRACK","WBR"],pt=1,ht=3,ft=8,Et=[{tagName:"HEAD",accept:e=>!e.childNodes.length||e.childNodes[0].nodeType==pt},{tagName:"BODY",accept:e=>!e.childNodes.length}],Tt=[{tagName:"HTML",accept:e=>!e||e.nodeType!=ft},{tagName:"HEAD",accept:e=>!e||e.nodeType!=ft&&(e.nodeType!=ht||!It(e.textContent))},{tagName:"BODY",accept:e=>!e||e.nodeType!=ft},{tagName:"LI",accept:(e,t)=>!e&&t.parentElement&&("UL"==wt(t.parentElement)||"OL"==wt(t.parentElement))||e&&["LI"].includes(wt(e))},{tagName:"DT",accept:e=>!e||["DT","DD"].includes(wt(e))},{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(wt(e))},{tagName:"DD",accept:e=>!e||["DT","DD"].includes(wt(e))},{tagName:"RT",accept:e=>!e||["RT","RP"].includes(wt(e))},{tagName:"RP",accept:e=>!e||["RT","RP"].includes(wt(e))},{tagName:"OPTGROUP",accept:e=>!e||["OPTGROUP"].includes(wt(e))},{tagName:"OPTION",accept:e=>!e||["OPTION","OPTGROUP"].includes(wt(e))},{tagName:"COLGROUP",accept:e=>!e||e.nodeType!=ft&&(e.nodeType!=ht||!It(e.textContent))},{tagName:"CAPTION",accept:e=>!e||e.nodeType!=ft&&(e.nodeType!=ht||!It(e.textContent))},{tagName:"THEAD",accept:e=>!e||["TBODY","TFOOT"].includes(wt(e))},{tagName:"TBODY",accept:e=>!e||["TBODY","TFOOT"].includes(wt(e))},{tagName:"TFOOT",accept:e=>!e},{tagName:"TR",accept:e=>!e||["TR"].includes(wt(e))},{tagName:"TD",accept:e=>!e||["TD","TH"].includes(wt(e))},{tagName:"TH",accept:e=>!e||["TD","TH"].includes(wt(e))}],bt=["STYLE","SCRIPT","XMP","IFRAME","NOEMBED","NOFRAMES","PLAINTEXT","NOSCRIPT"];function yt(e,t,s){return e.nodeType==ht?function(e){const t=e.parentNode;let s;t&&t.nodeType==pt&&(s=wt(t));return!s||bt.includes(s)?"SCRIPT"==s||"STYLE"==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):e.nodeType==ft?"\x3c!--"+e.textContent+"--\x3e":e.nodeType==pt?function(e,t,s){const o=wt(e),n=t&&Et.find((t=>o==wt(t)&&t.accept(e)));let a="";n&&!e.attributes.length||(a="<"+o.toLowerCase(),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"!=o||e.childNodes.length?Array.from(e.childNodes).forEach((e=>a+=yt(e,t,s||"svg"==o))):a+=e.innerHTML;const i=t&&Tt.find((t=>o==wt(t)&&t.accept(e.nextSibling,e)));(s||!i&&!gt.includes(o))&&(a+="</"+o.toLowerCase()+">");return a}(e,t,s):void 0}function It(e){return Boolean(e.match(/^[ \t\n\f\r]/))}function wt(e){return e.tagName&&e.tagName.toUpperCase()}const At={frameTree:ut},vt={COMMENT_HEADER:"Page saved with SingleFile",COMMENT_HEADER_LEGACY:"Archive processed by SingleFile",ON_BEFORE_CAPTURE_EVENT_NAME:S,ON_AFTER_CAPTURE_EVENT_NAME:R,preProcessDoc:ne,postProcessDoc:me,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+yt(e.documentElement,t)}(e,t),getShadowRoot:le};se(C,(()=>globalThis[A]=async e=>{const t=new CustomEvent(e+"-request",{cancelable:!0}),s=new Promise((t=>se(e+"-response",t)));(e=>{try{globalThis.dispatchEvent(e)}catch(e){}})(t),t.defaultPrevented&&await s})),e.helper=vt,e.processors=At,Object.defineProperty(e,"__esModule",{value:!0})}));