Bilibili-Evolved/min/download-package.min.js
2020-10-19 17:32:48 +08:00

1 line
1.2 KiB
JavaScript

(()=>{return(e,t)=>{class n{constructor(){this.entries=[]}download(e,t){const a=document.createElement("a");const i=URL.createObjectURL(t);if(n.lastPackageUrl){URL.revokeObjectURL(n.lastPackageUrl)}n.lastPackageUrl=i;a.setAttribute("href",i);a.setAttribute("download",escapeFilename(e));document.body.appendChild(a);a.click();a.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=>n.single(e.name,e.data,e.options)));return}console.log("[DownloadPackage]",{filename:t,files:this.entries.map(e=>e.name)});const a=await this.blob();if(!a){return}return this.download(t,a)}static async single(e,t,a={}){const i=new n;i.add(e,t,a);return await i.emit()}}n.lastPackageUrl="";return{export:{DownloadPackage:n}}}})();