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