mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
3.1 KiB
JavaScript
1 line
3.1 KiB
JavaScript
(()=>{return(e,t)=>{const o=(unsafeWindow||window).aid;const s=(unsafeWindow||window).cid;if(o===undefined||s===undefined){console.error(`unable to get aid or cid. aid=${o}, cid=${s}`);return}class a{constructor(e,t,o){this.quality=e;this.internalName=t;this.displayName=o}async download(){const e=new r(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 n=`https://api.bilibili.com/x/player/playurl?avid=${o}&cid=${s}&otype=json`;downloadText(n,t=>{const o=JSON.parse(t).data;const s=o.accept_quality;const n=o.accept_format.split(",");const r=o.accept_description;const i=[];while(s.length>0){const e=new a(s.pop(),n.pop(),r.pop());i.push(e)}e(i)},e=>t(`获取清晰度信息失败: ${e}`))})}}class n{constructor(e,t,o,s){this.length=e;this.size=t;this.url=o;this.backupUrls=s}}class r{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null}fetchVideoInfo(){return new Promise((e,t)=>{const a=`https://api.bilibili.com/x/player/playurl?avid=${o}&cid=${s}&qn=${this.format.quality}&otype=json`;downloadText(a,o=>{const s=JSON.parse(o).data;if(s.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const a=s.durl;this.fragments=a.map(e=>new n(e.length,e.size,e.url.replace("http:","https:"),e.backup_url.map(e=>e.replace("http:","https:"))));if(this.fragments.length>1){t("暂不支持分段视频的下载.")}e(this.fragments)})})}download(){return new Promise((e,t)=>{const[o]=this.fragments;const s=new XMLHttpRequest;s.open("GET",o.url);s.responseType="arraybuffer";s.withCredentials=false;s.addEventListener("progress",e=>{this.progress&&this.progress(e.loaded/o.size)});s.addEventListener("load",()=>{if(s.status===200){const t=new Blob([s.response],{type:"video/x-flv"});const a=URL.createObjectURL(t);const n=document.title.replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili","");const r=o.url.indexOf(".flv")!==-1?".flv":".mp4";$("a#video-complete").attr("href",a).attr("download",n+r);e(a)}else{t(`请求失败.`)}});s.addEventListener("error",()=>{t(`下载失败.`)});s.send()})}}return{settingsWidget:{after:()=>$("span.settings-category").filter((e,t)=>t.innerHTML==="视频与直播").parent(),content:t.data.downloadVideoDom.text,success:()=>{a.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");document.getElementById("video-complete").click()}$(`<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")})}}}}})(); |