!function(){"use strict";const e=new Map,t=8388608;async function r(e,r,s){for(let a=0;a*t<=s.array.length;a++){const n={method:"singlefile.fetchResponse",requestId:r,headers:s.headers,status:s.status,error:s.error};n.truncated=s.array.length>t,n.truncated?(n.finished=(a+1)*t>s.array.length,n.array=s.array.slice(a*t,(a+1)*t)):n.array=s.array,await browser.tabs.sendMessage(e,n)}return{}}function s(t,r={},a){return new Promise((n,i)=>{const o=new XMLHttpRequest;if(o.withCredentials=!0,o.responseType="arraybuffer",o.onerror=e=>i(new Error(e.detail)),o.onreadystatechange=()=>{o.readyState==XMLHttpRequest.DONE&&(o.status||o.response.byteLength?401!=o.status&&403!=o.status&&404!=o.status||a?n({arrayBuffer:o.response,array:Array.from(new Uint8Array(o.response)),headers:{"content-type":o.getResponseHeader("Content-Type")},status:o.status}):s(t,r,!0).then(n).catch(i):i(new Error("Empty response")))},o.open("GET",t,!0),r.headers)for(const e of Object.entries(r.headers))o.setRequestHeader(e[0],e[1]);if(a){const t=String(Math.random()).substring(2);d=t,f=r.referrer,e.set(d,f),o.setRequestHeader("x-single-file-request-id",t)}var d,f;o.send()})}browser.runtime.onMessage.addListener((e,t)=>{if(e.method&&e.method.startsWith("singlefile.fetch"))return new Promise(a=>{(async function(e,t){if("singlefile.fetch"==e.method)try{const a=await s(e.url,{referrer:e.referrer,headers:e.headers});return r(t.tab.id,e.requestId,a)}catch(s){return r(t.tab.id,e.requestId,{error:s.message,array:[]})}else if("singlefile.fetchFrame"==e.method)return browser.tabs.sendMessage(t.tab.id,e)})(e,t).then(a).catch(e=>a({error:e&&(e.message||e.toString())}))})}),browser.runtime.onMessage.addListener((e,t)=>{if("singlefile.frameTree.initResponse"==e.method||"singlefile.frameTree.ackInitRequest"==e.method)return browser.tabs.sendMessage(t.tab.id,e,{frameId:0}),Promise.resolve({})});const a=new Map;function n(e,t){e.delete(t)}browser.runtime.onMessage.addListener((e,t)=>{if("singlefile.lazyTimeout.setTimeout"==e.method){let r,s=a.get(t.tab.id);if(s)if(r=s.get(t.frameId),r){const t=r.get(e.type);t&&clearTimeout(t)}else r=new Map;const i=setTimeout(async()=>{try{const r=a.get(t.tab.id),s=r.get(t.frameId);r&&s&&n(s,e.type),await browser.tabs.sendMessage(t.tab.id,{method:"singlefile.lazyTimeout.onTimeout",type:e.type})}catch(e){}},e.delay);return s||(s=new Map,r=new Map,s.set(t.frameId,r),a.set(t.tab.id,s)),r.set(e.type,i),Promise.resolve({})}if("singlefile.lazyTimeout.clearTimeout"==e.method){let r=a.get(t.tab.id);if(r){const s=r.get(t.frameId);if(s){const t=s.get(e.type);t&&clearTimeout(t),n(s,e.type)}}return Promise.resolve({})}}),browser.tabs.onRemoved.addListener(e=>a.delete(e))}();
