Bilibili-Evolved/min/download-video.min.js
2018-10-25 21:18:32 +08:00

1 line
3.3 KiB
JavaScript

(()=>{return(e,t)=>{const s=(unsafeWindow||window).aid;const o=(unsafeWindow||window).cid;if(s===undefined||o===undefined){console.error(`unable to get aid or cid. aid=${s}, cid=${o}`);return}class n{constructor(e,t,s){this.quality=e;this.internalName=t;this.displayName=s}async download(){const e=new a(this);await e.fetchVideoInfo();e.progress=(e=>{$(".download-progress-value").text(`${fixed(e*100)}`);$(".download-progress-foreground").css("transform",`scaleX(${e})`)});return e.download()}static get availableFormats(){return new Promise((e,t)=>{const r=`https://api.bilibili.com/x/player/playurl?avid=${s}&cid=${o}&otype=json`;const a=new XMLHttpRequest;a.addEventListener("load",()=>{const t=JSON.parse(a.responseText).data;const s=t.accept_quality;const o=t.accept_format.split(",");const r=t.accept_description;const i=[];while(s.length>0){const e=new n(s.pop(),o.pop(),r.pop());i.push(e)}e(i)});a.addEventListener("error",()=>t(`获取清晰度信息失败.`));a.withCredentials=true;a.open("GET",r);a.send()})}}class r{constructor(e,t,s,o){this.length=e;this.size=t;this.url=s;this.backupUrls=o}}class a{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null}fetchVideoInfo(){return new Promise((e,t)=>{const n=`https://api.bilibili.com/x/player/playurl?avid=${s}&cid=${o}&qn=${this.format.quality}&otype=json`;const a=new XMLHttpRequest;a.addEventListener("load",()=>{const s=JSON.parse(a.responseText.replace("http:","https:")).data;if(s.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const o=s.durl;this.fragments=o.map(e=>new r(e.length,e.size,e.url,e.backup_url));if(this.fragments.length>1){t("暂不支持分段视频的下载.")}e(this.fragments)});a.withCredentials=true;a.open("GET",n);a.send()})}download(){return new Promise((e,t)=>{const[s]=this.fragments;const o=new XMLHttpRequest;o.open("GET",s.url);o.responseType="arraybuffer";o.withCredentials=false;o.addEventListener("progress",e=>{this.progress&&this.progress(e.loaded/s.size)});o.addEventListener("load",()=>{if(o.status===200){const t=new Blob([o.response],{type:"video/x-flv"});const n=URL.createObjectURL(t);const r=document.title.replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili","");const a=s.url.indexOf(".flv")!==-1?".flv":".mp4";const i=$("a#video-complete").attr("href");if(i){URL.revokeObjectURL(i)}$("a#video-complete").attr("href",n).attr("download",r+a);this.progress&&this.progress(0);document.getElementById("video-complete").click();e(n)}else{t(`请求失败.`)}});o.addEventListener("error",()=>{t(`下载失败.`)});o.send()})}}return{settingsWidget:{category:"视频与直播",content:t.data.downloadVideoDom.text,success:()=>{n.availableFormats.then(e=>{e.forEach(e=>{async function t(){$(".download-video-panel").removeClass("quality").addClass("progress");await e.download().catch(e=>{$(".download-video-panel").addClass("error");$(".video-error").text(e)});$(".download-video-panel").removeClass("progress").addClass("quality")}$(`<li>${e.displayName}</li>`).on("click",t).prependTo("ol.video-quality")});t.applyStyle("downloadVideoStyle");$("#download-video").on("click",()=>{$(".download-video-panel").toggleClass("opened")}).parent().removeClass("hidden")});$(".video-error").on("click",()=>{$(".video-error").text("");$(".download-video-panel").removeClass("error").removeClass("progress").addClass("quality")})}}}}})();