Bilibili-Evolved/min/download-video.min.js
2018-11-18 19:13:06 +08:00

1 line
5.3 KiB
JavaScript

(()=>{return(e,t)=>{const o=t.attributes.videoInfo.export.VideoInfo;const s=t.attributes.videoInfo.export.BangumiInfo;const n={aid:undefined,cid:undefined,isBangumi:false,isMovie:false};class i{constructor(e,t,o){this.quality=e;this.internalName=t;this.displayName=o}async downloadInfo(){const e=new r(this);await e.fetchVideoInfo();return e}static get availableFormats(){return new Promise((e,t)=>{const o=`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&otype=json`;const s=new XMLHttpRequest;s.addEventListener("load",()=>{const o=JSON.parse(s.responseText);if(o.code!==0){t("获取清晰度信息失败.")}const n=o.data;const a=n.accept_quality;const r=n.accept_format.split(",");const d=n.accept_description;const l=[];while(a.length>0){const e=new i(a.pop(),r.pop(),d.pop());l.push(e)}e(l)});s.addEventListener("error",()=>t(`获取清晰度信息失败.`));s.withCredentials=true;s.open("GET",o);s.send()})}}class a{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;this.loaded=0;this.totalSize=null;this.workingXhr=null}fetchVideoInfo(){return new Promise((e,t)=>{const o=`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&qn=${this.format.quality}&otype=json`;const s=new XMLHttpRequest;s.addEventListener("load",()=>{const o=JSON.parse(s.responseText.replace(/http:/g,"https:")).data;if(o.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const n=o.durl;this.fragments=n.map(e=>new a(e.length,e.size,e.url,e.backup_url));e(this.fragments)});s.withCredentials=true;s.open("GET",o);s.send()})}cancelDownload(){if(this.workingXhr){this.workingXhr.abort()}}downloadUrl(e){return new Promise((t,o)=>{const s=new XMLHttpRequest;s.open("GET",e);s.responseType="arraybuffer";s.withCredentials=false;s.addEventListener("progress",e=>{this.progress&&this.progress((this.loaded+e.loaded)/this.totalSize)});s.addEventListener("load",()=>{if(s.status===200){t(s.response)}else{o(`请求失败.`)}});s.addEventListener("abort",()=>o("下载已取消."));s.addEventListener("error",()=>o(`下载失败.`));s.send();this.workingXhr=s})}copyUrl(){const e=this.fragments.map(e=>e.url).reduce((e,t)=>e+"\r\n"+t);GM_setClipboard(e,"text")}async download(){const e=[];this.loaded=0;this.totalSize=this.fragments.map(e=>e.size).reduce((e,t)=>e+t);for(const t of this.fragments){const o=await this.downloadUrl(t.url);this.loaded+=t.size;e.push(o)}if(e.length<1){throw new Error("下载失败.")}let t=null;let o=null;const s=e=>(e||this.fragments[0]).url.indexOf(".flv")!==-1?".flv":".mp4";if(e.length===1){const[n]=e;t=new Blob([n],{type:s()===".flv"?"video/x-flv":"video/mp4"});o=document.title.replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili","")+s()}else{const n=new JSZip;const i=document.title.replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili","");e.forEach((e,t)=>{n.file(`${i} - ${t+1}${s(this.fragments[t])}`,e)});t=await n.generateAsync({type:"blob"});o=i+".zip"}const n=URL.createObjectURL(t);const i=$("a#video-complete").attr("href");if(i){URL.revokeObjectURL(i)}this.progress&&this.progress(0);return{url:n,filename:o}}}function d(){(async()=>{let e=(unsafeWindow||window).aid;let t=(unsafeWindow||window).cid;if(e===undefined||t===undefined){const s=document.URL.match(/\/av(\d+)/);const n=document.URL.match(/\/ep(\d+)/);if(s&&s[1]){const n=await new o(s[1]).fetchInfo();e=n.aid;t=n.cid}}return[e,t]})().then(e=>{const[o,s]=e;if(o===undefined||s===undefined){return}n.aid=o;n.cid=s;i.availableFormats.then(e=>{let[o]=e;const s=()=>o.downloadInfo().catch(e=>{$(".download-video-panel").addClass("error");$(".video-error").text(e)});async function n(){if(!o){return}$(".download-video-panel").removeClass("action").addClass("progress");const e=await s();e.progress=(e=>{$(".download-progress-value").text(`${fixed(e*100)}`);$(".download-progress-foreground").css("transform",`scaleX(${e})`)});document.querySelector(".download-progress-cancel>span").onclick=(()=>e.cancelDownload());const t=await e.download().catch(e=>{$(".download-video-panel").addClass("error");$(".video-error").text(e)});if(!t){return}const n=document.getElementById("video-complete");n.setAttribute("href",t.url);n.setAttribute("download",t.filename);n.click();const i=`下载完成. <a class="link" href="${t.url}">再次保存</a>`;Toast.success(i,"下载视频");$(".download-video-panel").removeClass("progress").addClass("quality")}async function i(){if(!o){return}const e=await s();e.copyUrl();Toast.success("已复制链接到剪贴板.","复制链接",3e3);$(".download-video-panel").removeClass("action").addClass("quality")}$(".video-action>#video-action-download").on("click",n);$(".video-action>#video-action-copy").on("click",i);e.forEach(e=>{$(`<li>${e.displayName}</li>`).on("click",()=>{o=e;$(".download-video-panel").removeClass("quality").addClass("action")}).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")})})}return{settingsWidget:{category:"视频与直播",content:t.data.downloadVideoDom.text,success:d}}}})();