Bilibili-Evolved/min/download-package.min.js
2021-10-26 13:22:15 +08:00

1 line
1.4 KiB
JavaScript

(()=>(e,t)=>{function n(e,t,n){if(t in e){Object.defineProperty(e,t,{value:n,enumerable:true,configurable:true,writable:true})}else{e[t]=n}return e}class a{constructor(){n(this,"entries",[])}download(e,t){const n=document.createElement("a");const i=URL.createObjectURL(t);if(a.lastPackageUrl){URL.revokeObjectURL(a.lastPackageUrl)}a.lastPackageUrl=i;n.setAttribute("href",i);n.setAttribute("download",escapeFilename(e));document.body.appendChild(n);n.click();n.remove()}add(e,t,n={}){if(t===null||t===undefined){return}this.entries.push({name:escapeFilename(e),data:t,options:n})}async blob(){if(this.entries.length===0){return null}if(this.entries.length===1){const e=this.entries[0].data;return typeof e==="string"?new Blob([e]):e}const e=new JSZip;this.entries.forEach((({name:t,data:n})=>{e.file(t,n)}));return await e.generateAsync({type:"blob"})}async emit(t){if(this.entries.length===0){return}if(!t||this.entries.length===1){t=this.entries[0].name}if(e.downloadPackageEmitMode==="分别下载"&&this.entries.length>1){await Promise.all(this.entries.map((e=>a.single(e.name,e.data,e.options))));return}console.log("[DownloadPackage]",{filename:t,files:this.entries.map((e=>e.name))});const n=await this.blob();if(!n){return}return this.download(t,n)}static async single(e,t,n={}){const i=new a;i.add(e,t,n);return await i.emit()}}n(a,"lastPackageUrl","");return{export:{DownloadPackage:a}}})();