| 1 | (()=>{function e(e,t){let o=e.get("Content-Disposition");if(o){let e=o.match(/filename\*?=['"]?(?:UTF-8'')?([^'";]+)/i);if(e)return decodeURIComponent(e[1])}return t.split("/").pop().split("?")[0]||"download"}htmx.registerExtension("download",{htmx_before_request:(t,{ctx:o})=>{if("download"!==o.swap)return;let n=o.fetch;o.fetch=async(t,r)=>{let a=await n(t,r),l=+a.headers.get("Content-Length")||null;htmx.trigger(o.sourceElement,"htmx:download:start",{total:l});let d=a.body.getReader(),s=[],i=0;for(;;){let{done:e,value:t}=await d.read();if(e)break;s.push(t),i+=t.length,htmx.trigger(o.sourceElement,"htmx:download:progress",{loaded:i,total:l,percent:l?Math.round(i/l*100):null})}return o.download={blob:new Blob(s,{type:a.headers.get("Content-Type")||"application/octet-stream"}),filename:e(a.headers,t)},new Response("",{status:a.status,headers:a.headers})}},htmx_before_swap:(e,{ctx:t})=>{if(!t.download)return;let{blob:o,filename:n}=t.download,r=URL.createObjectURL(o),a=document.createElement("a");return a.href=r,a.download=n,a.click(),URL.revokeObjectURL(r),htmx.trigger(t.sourceElement,"htmx:download:complete",{filename:n,size:o.size}),!1}})})(); |