diff --git a/bilibili-evolved.offline.user.js b/bilibili-evolved.offline.user.js index 8887ed5a1..cfe8c428e 100644 --- a/bilibili-evolved.offline.user.js +++ b/bilibili-evolved.offline.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name Bilibili Evolved (Offline) -// @version 259.84 +// @version 260.89 // @description Bilibili Evolved 的离线版, 所有功能都已内置于脚本中. // @author Grant Howard, Coulomb-G // @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G) @@ -881,7 +881,7 @@ offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/m offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-danmaku.min.js"] = (()=>{return(t,e)=>{const{getFriendlyTitle:n}=e.import("title");const{DanmakuInfo:i}=e.import("video-info");const{DanmakuConverter:o}=e.import("danmaku-converter");async function a(t,e){const a=n();const r=new i((unsafeWindow||window).cid);await r.fetchInfo();const l=await(async()=>{if(e===true){let t={title:a};try{await loadLazyPanel(".bilibili-player-video-danmaku-setting");const e=t=>{const e=parseFloat(document.querySelector(t).style.transform.replace(/translateX\(([\d\.]+)/,"$1"));const n={0:0,44:1,94:2,144:3,188:4}[e];return n};t.font=document.querySelector(".bilibili-player-video-danmaku-setting-right-font .bui-select-result").innerText;t.alpha=parseFloat(document.querySelector(".bilibili-player-setting-opacity .bui-bar").style.transform.replace(/scaleX\(([\d\.]+)\)/,"$1"));t.duration=(()=>{const t=[10,8,6,4,2][e(".bilibili-player-setting-speedplus .bui-thumb")];return e=>{switch(e.type){case 4:case 5:return 4;default:return t}}})();t.blockTypes=(()=>{let t=[];const e={".bilibili-player-block-filter-type[ftype=scroll]":[1,2,3],".bilibili-player-block-filter-type[ftype=top]":[5],".bilibili-player-block-filter-type[ftype=bottom]":[4],".bilibili-player-block-filter-type[ftype=color]":["color"]};for(const[n,i]of Object.entries(e)){if(document.querySelector(n).classList.contains("disabled")){t=t.concat(i)}}return t.concat(7,8)})();const n=[1.4,1.2,1,.8,.6][e(".bilibili-player-setting-fontsize .bui-thumb")];t.resolution={x:1920*n,y:1080*n};t.bottomMarginPercent=[.75,.5,.25,0,0][e(".bilibili-player-setting-area .bui-thumb")];if(t.bottomMarginPercent===0&&document.querySelector(".bilibili-player-video-danmaku-setting-left-preventshade input").checked){t.bottomMarginPercent=.15}t.bold=document.querySelector(".bilibili-player-video-danmaku-setting-right-font-bold input").checked}catch(e){t={font:"微软雅黑",alpha:.6,duration:t=>{switch(t.type){case 4:case 5:return 4;default:return 6}},blockTypes:[7,8],resolution:{x:1920,y:1080},bottomMarginPercent:.15,bold:false}}const e=new o(t);const n=e.convertToAssDocument(r.rawXML);return new Blob([n.generateAss()],{type:"text/plain"})}else{return new Blob([r.rawXML],{type:"text/plain"})}})();const c=URL.createObjectURL(l);const s=$("#danmaku-link");const u=s.attr("href");if(u){URL.revokeObjectURL(u)}clearTimeout(t);document.querySelector("#download-danmaku>span").innerHTML="下载弹幕";s.attr("download",`${a}.${e?"ass":"xml"}`).attr("href",c).get(0).click()}return{widget:{content:`\n \n \n 下载弹幕\n \n `,condition:async()=>{let t=await SpinQuery.select(()=>(unsafeWindow||window).cid);return Boolean(t)},success:()=>{const t=document.querySelector("#danmaku-link");$("#download-danmaku").on("click",e=>{if(e.target!==t){const t=setTimeout(()=>document.querySelector("#download-danmaku>span").innerHTML="请稍侯...",200);a(t,e.shiftKey)}})}}}}})(); offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.css"] = `.download-video-panel.opened{transform:scaleY(1) translateX(-50%)}.download-video-panel{position:absolute;top:calc(100% + 2px);transition:.16s ease-out;transform-origin:top;transform:scaleY(0) translateX(-50%);background:#fff;box-shadow:0 3px 20px 2px #0004;z-index:100;left:50%}.video-action>div,ol.video-quality li{cursor:pointer;align-items:center;margin:0;padding:8px 16px;transition:.2s ease-out;text-align:center;white-space:nowrap}.video-action>div{padding:12px 24px}.video-action>div:hover,ol.video-quality li:hover{background:rgba(0,0,0,.16)}.video-action,.video-error,.video-progress,.video-quality{display:none}.download-video-panel.action:not(.error) .video-action,.download-video-panel.error .video-error,.download-video-panel.progress:not(.error) .video-progress,.download-video-panel.quality:not(.error) .video-quality{display:block}.download-progress-text{display:flex}.download-progress-background{background-color:#8884;height:4px;width:100%;margin-top:4px}.download-progress-foreground{background-color:var(--theme-color);transform-origin:left;transition:.2s ease-out;transform:scaleX(0);height:100%;width:100%}.download-progress-cancel{margin-top:8px;display:flex;justify-content:center}.download-progress-cancel>span{background-color:#8884;padding:4px 8px;cursor:pointer}.download-progress-cancel>span:hover{opacity:.9}.download-progress-cancel>span:active{opacity:.8}.video-error,.video-progress{padding:12px}.video-progress{min-width:120px}#video-complete{display:none}`; offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.html"] = `
下载视频
`; -offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.js"] = (()=>{return(e,t)=>{const{getFriendlyTitle:s}=t.import("title");const n={entity:null,aid:undefined,cid:undefined};class o{constructor(){this.menuClasses=["quality","action","progress"];this.currentMenuClass="quality"}get menuPanel(){return document.querySelector(".download-video-panel")}addMenuClass(){this.menuPanel.classList.remove(...this.menuClasses);this.menuPanel.classList.add(this.currentMenuClass);return this.currentMenuClass}resetMenuClass(){[this.currentMenuClass]=this.menuClasses;this.addMenuClass()}nextMenuClass(){const e=this.menuClasses.indexOf(this.currentMenuClass)+1;const t=this.menuClasses[e>=this.menuClasses.length?0:e];this.currentMenuClass=t;this.addMenuClass();return t}addError(){this.menuPanel.classList.add("error")}removeError(){this.menuPanel.classList.remove("error");this.resetMenuClass()}async getUrl(e){if(e){return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&otype=json`}}}class r extends o{async getUrl(e){if(e){return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=&otype=json`}}}class i{constructor(e,t,s){this.quality=e;this.internalName=t;this.displayName=s}async downloadInfo(){const e=new l(this);await e.fetchVideoInfo();return e}static get availableFormats(){return new Promise((e,t)=>{n.entity.getUrl().then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText);if(s.code!==0){t("获取清晰度信息失败.")}const o=s.data||s.result||s;const r=o.accept_quality;const a=o.accept_format.split(",");const l=o.accept_description;const c=[];while(r.length>0){const e=new i(r.pop(),a.pop(),l.pop());c.push(e)}e(c)});n.addEventListener("error",()=>t(`获取清晰度信息失败.`));n.withCredentials=true;n.open("GET",s);n.send()})})}}class a{constructor(e,t,s,n){this.length=e;this.size=t;this.url=s;this.backupUrls=n}}class l{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null;this.totalSize=null;this.workingXhr=null;this.fragmentSplitFactor=6*5}fetchVideoInfo(){return new Promise((e,t)=>{n.entity.getUrl(this.format.quality).then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText.replace(/http:/g,"https:"));const o=s.data||s.result||s;if(o.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const r=o.durl;this.fragments=r.map(e=>new a(e.length,e.size,e.url,e.backup_url));e(this.fragments)});n.withCredentials=true;n.open("GET",s);n.send()})})}updateProgress(){const e=this.progressMap?[...this.progressMap.values()].reduce((e,t)=>e+t,0)/this.totalSize:0;if(e>1||e<0){console.error(`[下载视频] 进度异常: ${e}`,this.progressMap.values())}this.progress&&this.progress(e)}cancelDownload(){if("forEach"in this.workingXhr){this.workingXhr.forEach(e=>e.abort())}else{logError("Cancel Download Failed: forEach in this.workingXhr not found.")}}downloadFragment(e){const t=[];this.workingXhr=[];this.progressMap=new Map;this.updateProgress();const s=Math.round(e.size/this.fragmentSplitFactor);let n=0;const o=e=>[...this.progressMap.keys()].indexOf(e)+1;while(n{const n=new XMLHttpRequest;n.open("GET",e.url);n.responseType="arraybuffer";n.withCredentials=false;n.addEventListener("progress",e=>{console.log(`[下载视频] 视频片段${o(n)}下载进度: ${e.loaded}/${a} bytes loaded, ${i}`);this.progressMap.set(n,e.loaded);this.updateProgress()});n.addEventListener("load",()=>{if((""+n.status)[0]==="2"){t(n.response)}else{s(`请求失败.`)}});n.addEventListener("abort",()=>s("下载已取消."));n.addEventListener("error",()=>{console.error(`[下载视频] 视频片段${o(n)}下载失败: ${i}`);this.progressMap.set(n,0);this.updateProgress();n.open("GET",e.url);n.send()});n.setRequestHeader("Range",i);this.progressMap.set(n,0);n.send();this.workingXhr.push(n)}));n=Math.round(n+s)+1}return Promise.all(t)}copyUrl(){const e=this.fragments.map(e=>e.url).reduce((e,t)=>e+"\r\n"+t);GM_setClipboard(e,"text")}extension(e){return(e||this.fragments[0]).url.indexOf(".flv")!==-1?".flv":".mp4"}makeBlob(e,t=null){return new Blob(Array.isArray(e)?e:[e],{type:this.extension(t)===".flv"?"video/x-flv":"video/mp4"})}cleanUpOldBlobUrl(){const e=document.querySelector("a#video-complete").getAttribute("href");if(e&&!document.querySelector(`.link[href="${e}"]`)){URL.revokeObjectURL(e)}[...document.querySelectorAll(".toast-card-header")].filter(e=>e.innerText.includes("下载视频")).forEach(e=>e.querySelector(".toast-card-dismiss").click())}downloadSingle(e){const[t]=e;const n=this.makeBlob(t);const o=s()+this.extension();return[n,o]}async downloadMultiple(e){const t=new JSZip;const n=s();if(e.length>1){e.forEach((e,s)=>{const o=this.fragments[s];t.file(`${n} - ${s+1}${this.extension(o)}`,this.makeBlob(e,o))})}else{const[s]=e;t.file(`${n}${this.extension()}`,this.makeBlob(s))}const o=await t.generateAsync({type:"blob"});const r=n+".zip";return[o,r]}async download(){const e=[];this.totalSize=this.fragments.map(e=>e.size).reduce((e,t)=>e+t);for(const t of this.fragments){const s=await this.downloadFragment(t);e.push(s)}if(e.length<1){throw new Error("下载失败.")}let t=null;let s=null;if(e.length===1){[t,s]=this.downloadSingle(e)}else{[t,s]=await this.downloadMultiple(e)}this.cleanUpOldBlobUrl();const n=URL.createObjectURL(t);this.progress&&this.progress(0);return{url:n,filename:s}}}async function c(){const e=await SpinQuery.select(()=>(unsafeWindow||window).aid);const t=await SpinQuery.select(()=>(unsafeWindow||window).cid);n.aid=e;n.cid=t;if(document.URL.indexOf("bangumi")!==-1){n.entity=new r}else{n.entity=new o}return Boolean(e&&t)}async function d(){let e=await i.availableFormats;let[s]=e;const o=async()=>{await c();e=await i.availableFormats;const t=$("ol.video-quality");t.html("");e.forEach(e=>{$(`
  • ${e.displayName}
  • `).on("click",()=>{s=e;n.entity.nextMenuClass()}).prependTo(t)})};if(Observer.videoChange){Observer.videoChange(o)}else{Observer.childList("#bofqi",o)}const r=()=>s.downloadInfo().catch(e=>{n.entity.addError();$(".video-error").text(e)});async function a(){if(!s){return}n.entity.nextMenuClass();const e=await r();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=>{n.entity.addError();$(".video-error").text(e)});if(!t){return}const o=document.getElementById("video-complete");o.setAttribute("href",t.url);o.setAttribute("download",t.filename);o.click();const i=`下载完成. 再次保存`;Toast.success(i,"下载视频");n.entity.resetMenuClass()}async function l(){if(!s){return}const e=await r();e.copyUrl();Toast.success("已复制链接到剪贴板.","复制链接",3e3);n.entity.resetMenuClass()}$(".video-action>#video-action-download").on("click",a);$(".video-action>#video-action-copy").on("click",l);t.applyStyle("downloadVideoStyle");const d=document.querySelector(".download-video-panel");const u=()=>$(".download-video-panel").toggleClass("opened");$("#download-video").on("click",e=>{if(!d.contains(e.target)){u()}});$(".video-error").on("click",()=>{$(".video-error").text("");n.entity.removeError()});await SpinQuery.select(()=>document.querySelector(".download-video-panel"));n.entity.addMenuClass()}return{widget:{content:(t.data.downloadVideoDom||t.data.downloadVideoHtml).text,condition:c,success:d}}}})(); +offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.js"] = (()=>{return(e,t)=>{const{getFriendlyTitle:s}=t.import("title");const n={entity:null,aid:undefined,cid:undefined};class o{constructor(){this.menuClasses=["quality","action","progress"];this.currentMenuClass="quality"}get menuPanel(){return document.querySelector(".download-video-panel")}addMenuClass(){this.menuPanel.classList.remove(...this.menuClasses);this.menuPanel.classList.add(this.currentMenuClass);return this.currentMenuClass}resetMenuClass(){[this.currentMenuClass]=this.menuClasses;this.addMenuClass()}nextMenuClass(){const e=this.menuClasses.indexOf(this.currentMenuClass)+1;const t=this.menuClasses[e>=this.menuClasses.length?0:e];this.currentMenuClass=t;this.addMenuClass();return t}addError(){this.menuPanel.classList.add("error")}removeError(){this.menuPanel.classList.remove("error");this.resetMenuClass()}async getUrl(e){if(e){return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&otype=json`}}}class r extends o{async getUrl(e){if(e){return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=&otype=json`}}}class i{constructor(e,t,s){this.quality=e;this.internalName=t;this.displayName=s}async downloadInfo(){const e=new l(this);await e.fetchVideoInfo();return e}static get availableFormats(){return new Promise((e,t)=>{n.entity.getUrl().then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText);if(s.code!==0){t("获取清晰度信息失败.")}const o=s.data||s.result||s;const r=o.accept_quality;const a=o.accept_format.split(",");const l=o.accept_description;const c=[];while(r.length>0){const e=new i(r.pop(),a.pop(),l.pop());c.push(e)}e(c)});n.addEventListener("error",()=>t(`获取清晰度信息失败.`));n.withCredentials=true;n.open("GET",s);n.send()})})}}class a{constructor(e,t,s,n){this.length=e;this.size=t;this.url=s;this.backupUrls=n}}class l{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null;this.totalSize=null;this.workingXhr=null;this.fragmentSplitFactor=6*5}fetchVideoInfo(){return new Promise((e,t)=>{n.entity.getUrl(this.format.quality).then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText.replace(/http:/g,"https:"));const o=s.data||s.result||s;if(o.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const r=o.durl;this.fragments=r.map(e=>new a(e.length,e.size,e.url,e.backup_url));e(this.fragments)});n.withCredentials=true;n.open("GET",s);n.send()})})}updateProgress(){const e=this.progressMap?[...this.progressMap.values()].reduce((e,t)=>e+t,0)/this.totalSize:0;if(e>1||e<0){console.error(`[下载视频] 进度异常: ${e}`,this.progressMap.values())}this.progress&&this.progress(e)}cancelDownload(){if("forEach"in this.workingXhr){this.workingXhr.forEach(e=>e.abort())}else{logError("Cancel Download Failed: forEach in this.workingXhr not found.")}}downloadFragment(e){const t=[];this.workingXhr=[];this.progressMap=new Map;this.updateProgress();const s=Math.round(e.size/this.fragmentSplitFactor);let n=0;const o=e=>[...this.progressMap.keys()].indexOf(e)+1;while(n{const n=new XMLHttpRequest;n.open("GET",e.url);n.responseType="arraybuffer";n.withCredentials=false;n.addEventListener("progress",e=>{console.log(`[下载视频] 视频片段${o(n)}下载进度: ${e.loaded}/${a} bytes loaded, ${i}`);this.progressMap.set(n,e.loaded);this.updateProgress()});n.addEventListener("load",()=>{if((""+n.status)[0]==="2"){t(n.response)}else{s(`请求失败.`)}});n.addEventListener("abort",()=>s("下载已取消."));n.addEventListener("error",()=>{console.error(`[下载视频] 视频片段${o(n)}下载失败: ${i}`);this.progressMap.set(n,0);this.updateProgress();n.open("GET",e.url);n.setRequestHeader("Range",i);n.send()});n.setRequestHeader("Range",i);this.progressMap.set(n,0);n.send();this.workingXhr.push(n)}));n=Math.round(n+s)+1}return Promise.all(t)}copyUrl(){const e=this.fragments.map(e=>e.url).reduce((e,t)=>e+"\r\n"+t);GM_setClipboard(e,"text")}extension(e){return(e||this.fragments[0]).url.indexOf(".flv")!==-1?".flv":".mp4"}makeBlob(e,t=null){return new Blob(Array.isArray(e)?e:[e],{type:this.extension(t)===".flv"?"video/x-flv":"video/mp4"})}cleanUpOldBlobUrl(){const e=document.querySelector("a#video-complete").getAttribute("href");if(e&&!document.querySelector(`.link[href="${e}"]`)){URL.revokeObjectURL(e)}[...document.querySelectorAll(".toast-card-header")].filter(e=>e.innerText.includes("下载视频")).forEach(e=>e.querySelector(".toast-card-dismiss").click())}downloadSingle(e){const[t]=e;const n=this.makeBlob(t);const o=s()+this.extension();return[n,o]}async downloadMultiple(e){const t=new JSZip;const n=s();if(e.length>1){e.forEach((e,s)=>{const o=this.fragments[s];t.file(`${n} - ${s+1}${this.extension(o)}`,this.makeBlob(e,o))})}else{const[s]=e;t.file(`${n}${this.extension()}`,this.makeBlob(s))}const o=await t.generateAsync({type:"blob"});const r=n+".zip";return[o,r]}async download(){const e=[];this.totalSize=this.fragments.map(e=>e.size).reduce((e,t)=>e+t);for(const t of this.fragments){const s=await this.downloadFragment(t);e.push(s)}if(e.length<1){throw new Error("下载失败.")}let t=null;let s=null;if(e.length===1){[t,s]=this.downloadSingle(e)}else{[t,s]=await this.downloadMultiple(e)}this.cleanUpOldBlobUrl();const n=URL.createObjectURL(t);this.progress&&this.progress(0);return{url:n,filename:s}}}async function c(){const e=await SpinQuery.select(()=>(unsafeWindow||window).aid);const t=await SpinQuery.select(()=>(unsafeWindow||window).cid);n.aid=e;n.cid=t;if(document.URL.indexOf("bangumi")!==-1){n.entity=new r}else{n.entity=new o}return Boolean(e&&t)}async function d(){let e=await i.availableFormats;let[s]=e;const o=async()=>{await c();e=await i.availableFormats;const t=$("ol.video-quality");t.html("");e.forEach(e=>{$(`
  • ${e.displayName}
  • `).on("click",()=>{s=e;n.entity.nextMenuClass()}).prependTo(t)})};if(Observer.videoChange){Observer.videoChange(o)}else{Observer.childList("#bofqi",o)}const r=()=>s.downloadInfo().catch(e=>{n.entity.addError();$(".video-error").text(e)});async function a(){if(!s){return}n.entity.nextMenuClass();const e=await r();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=>{n.entity.addError();$(".video-error").text(e)});if(!t){return}const o=document.getElementById("video-complete");o.setAttribute("href",t.url);o.setAttribute("download",t.filename);o.click();const i=`下载完成. 再次保存`;Toast.success(i,"下载视频");n.entity.resetMenuClass()}async function l(){if(!s){return}const e=await r();e.copyUrl();Toast.success("已复制链接到剪贴板.","复制链接",3e3);n.entity.resetMenuClass()}$(".video-action>#video-action-download").on("click",a);$(".video-action>#video-action-copy").on("click",l);t.applyStyle("downloadVideoStyle");const d=document.querySelector(".download-video-panel");const u=()=>$(".download-video-panel").toggleClass("opened");$("#download-video").on("click",e=>{if(!d.contains(e.target)){u()}});$(".video-error").on("click",()=>{$(".video-error").text("");n.entity.removeError()});await SpinQuery.select(()=>document.querySelector(".download-video-panel"));n.entity.addMenuClass()}return{widget:{content:(t.data.downloadVideoDom||t.data.downloadVideoHtml).text,condition:c,success:d}}}})(); offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/expand-danmaku.min.js"] = (()=>{return(e,i)=>{if(typeof isEmbeddedPlayer!=="undefined"&&isEmbeddedPlayer()){return}const r=()=>{SpinQuery.any(()=>$(".bui-collapse-header"),e=>{if(parseInt($(".bui-collapse-body").css("height"))===0&&$(".bui-collapse-arrow-text").text()==="展开"){e.click()}})};if(Observer.videoChange){Observer.videoChange(r)}else{Observer.childList("#bofqi",r)}}})(); offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/expand-description.min.css"] = `.play-up-info .play-up-self,.video-desc .info{height:auto!important}.play-up-info .play-up-self-btn,.video-desc .btn{display:none!important}`; offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/expand-description.min.js"] = (()=>{return(e,p)=>{p.applyStyle("expandDescriptionStyle")}})(); diff --git a/bilibili-evolved.preview-offline.user.js b/bilibili-evolved.preview-offline.user.js index 2914dfe9a..3c00ed6cd 100644 --- a/bilibili-evolved.preview-offline.user.js +++ b/bilibili-evolved.preview-offline.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name Bilibili Evolved (Preview Offline) -// @version 259.84 +// @version 260.89 // @description Bilibili Evolved 的预览离线版, 可以抢先体验新功能, 并且所有功能都已内置于脚本中. // @author Grant Howard, Coulomb-G // @copyright 2019, Grant Howard (https://github.com/the1812) & Coulomb-G (https://github.com/Coulomb-G) @@ -881,7 +881,7 @@ offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/m offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-danmaku.min.js"] = (()=>{return(t,e)=>{const{getFriendlyTitle:n}=e.import("title");const{DanmakuInfo:i}=e.import("video-info");const{DanmakuConverter:o}=e.import("danmaku-converter");async function a(t,e){const a=n();const r=new i((unsafeWindow||window).cid);await r.fetchInfo();const l=await(async()=>{if(e===true){let t={title:a};try{await loadLazyPanel(".bilibili-player-video-danmaku-setting");const e=t=>{const e=parseFloat(document.querySelector(t).style.transform.replace(/translateX\(([\d\.]+)/,"$1"));const n={0:0,44:1,94:2,144:3,188:4}[e];return n};t.font=document.querySelector(".bilibili-player-video-danmaku-setting-right-font .bui-select-result").innerText;t.alpha=parseFloat(document.querySelector(".bilibili-player-setting-opacity .bui-bar").style.transform.replace(/scaleX\(([\d\.]+)\)/,"$1"));t.duration=(()=>{const t=[10,8,6,4,2][e(".bilibili-player-setting-speedplus .bui-thumb")];return e=>{switch(e.type){case 4:case 5:return 4;default:return t}}})();t.blockTypes=(()=>{let t=[];const e={".bilibili-player-block-filter-type[ftype=scroll]":[1,2,3],".bilibili-player-block-filter-type[ftype=top]":[5],".bilibili-player-block-filter-type[ftype=bottom]":[4],".bilibili-player-block-filter-type[ftype=color]":["color"]};for(const[n,i]of Object.entries(e)){if(document.querySelector(n).classList.contains("disabled")){t=t.concat(i)}}return t.concat(7,8)})();const n=[1.4,1.2,1,.8,.6][e(".bilibili-player-setting-fontsize .bui-thumb")];t.resolution={x:1920*n,y:1080*n};t.bottomMarginPercent=[.75,.5,.25,0,0][e(".bilibili-player-setting-area .bui-thumb")];if(t.bottomMarginPercent===0&&document.querySelector(".bilibili-player-video-danmaku-setting-left-preventshade input").checked){t.bottomMarginPercent=.15}t.bold=document.querySelector(".bilibili-player-video-danmaku-setting-right-font-bold input").checked}catch(e){t={font:"微软雅黑",alpha:.6,duration:t=>{switch(t.type){case 4:case 5:return 4;default:return 6}},blockTypes:[7,8],resolution:{x:1920,y:1080},bottomMarginPercent:.15,bold:false}}const e=new o(t);const n=e.convertToAssDocument(r.rawXML);return new Blob([n.generateAss()],{type:"text/plain"})}else{return new Blob([r.rawXML],{type:"text/plain"})}})();const c=URL.createObjectURL(l);const s=$("#danmaku-link");const u=s.attr("href");if(u){URL.revokeObjectURL(u)}clearTimeout(t);document.querySelector("#download-danmaku>span").innerHTML="下载弹幕";s.attr("download",`${a}.${e?"ass":"xml"}`).attr("href",c).get(0).click()}return{widget:{content:`\n \n \n 下载弹幕\n \n `,condition:async()=>{let t=await SpinQuery.select(()=>(unsafeWindow||window).cid);return Boolean(t)},success:()=>{const t=document.querySelector("#danmaku-link");$("#download-danmaku").on("click",e=>{if(e.target!==t){const t=setTimeout(()=>document.querySelector("#download-danmaku>span").innerHTML="请稍侯...",200);a(t,e.shiftKey)}})}}}}})(); offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.css"] = `.download-video-panel.opened{transform:scaleY(1) translateX(-50%)}.download-video-panel{position:absolute;top:calc(100% + 2px);transition:.16s ease-out;transform-origin:top;transform:scaleY(0) translateX(-50%);background:#fff;box-shadow:0 3px 20px 2px #0004;z-index:100;left:50%}.video-action>div,ol.video-quality li{cursor:pointer;align-items:center;margin:0;padding:8px 16px;transition:.2s ease-out;text-align:center;white-space:nowrap}.video-action>div{padding:12px 24px}.video-action>div:hover,ol.video-quality li:hover{background:rgba(0,0,0,.16)}.video-action,.video-error,.video-progress,.video-quality{display:none}.download-video-panel.action:not(.error) .video-action,.download-video-panel.error .video-error,.download-video-panel.progress:not(.error) .video-progress,.download-video-panel.quality:not(.error) .video-quality{display:block}.download-progress-text{display:flex}.download-progress-background{background-color:#8884;height:4px;width:100%;margin-top:4px}.download-progress-foreground{background-color:var(--theme-color);transform-origin:left;transition:.2s ease-out;transform:scaleX(0);height:100%;width:100%}.download-progress-cancel{margin-top:8px;display:flex;justify-content:center}.download-progress-cancel>span{background-color:#8884;padding:4px 8px;cursor:pointer}.download-progress-cancel>span:hover{opacity:.9}.download-progress-cancel>span:active{opacity:.8}.video-error,.video-progress{padding:12px}.video-progress{min-width:120px}#video-complete{display:none}`; offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.html"] = `
    下载视频
    `; -offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.js"] = (()=>{return(e,t)=>{const{getFriendlyTitle:s}=t.import("title");const n={entity:null,aid:undefined,cid:undefined};class o{constructor(){this.menuClasses=["quality","action","progress"];this.currentMenuClass="quality"}get menuPanel(){return document.querySelector(".download-video-panel")}addMenuClass(){this.menuPanel.classList.remove(...this.menuClasses);this.menuPanel.classList.add(this.currentMenuClass);return this.currentMenuClass}resetMenuClass(){[this.currentMenuClass]=this.menuClasses;this.addMenuClass()}nextMenuClass(){const e=this.menuClasses.indexOf(this.currentMenuClass)+1;const t=this.menuClasses[e>=this.menuClasses.length?0:e];this.currentMenuClass=t;this.addMenuClass();return t}addError(){this.menuPanel.classList.add("error")}removeError(){this.menuPanel.classList.remove("error");this.resetMenuClass()}async getUrl(e){if(e){return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&otype=json`}}}class r extends o{async getUrl(e){if(e){return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=&otype=json`}}}class i{constructor(e,t,s){this.quality=e;this.internalName=t;this.displayName=s}async downloadInfo(){const e=new l(this);await e.fetchVideoInfo();return e}static get availableFormats(){return new Promise((e,t)=>{n.entity.getUrl().then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText);if(s.code!==0){t("获取清晰度信息失败.")}const o=s.data||s.result||s;const r=o.accept_quality;const a=o.accept_format.split(",");const l=o.accept_description;const c=[];while(r.length>0){const e=new i(r.pop(),a.pop(),l.pop());c.push(e)}e(c)});n.addEventListener("error",()=>t(`获取清晰度信息失败.`));n.withCredentials=true;n.open("GET",s);n.send()})})}}class a{constructor(e,t,s,n){this.length=e;this.size=t;this.url=s;this.backupUrls=n}}class l{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null;this.totalSize=null;this.workingXhr=null;this.fragmentSplitFactor=6*5}fetchVideoInfo(){return new Promise((e,t)=>{n.entity.getUrl(this.format.quality).then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText.replace(/http:/g,"https:"));const o=s.data||s.result||s;if(o.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const r=o.durl;this.fragments=r.map(e=>new a(e.length,e.size,e.url,e.backup_url));e(this.fragments)});n.withCredentials=true;n.open("GET",s);n.send()})})}updateProgress(){const e=this.progressMap?[...this.progressMap.values()].reduce((e,t)=>e+t,0)/this.totalSize:0;if(e>1||e<0){console.error(`[下载视频] 进度异常: ${e}`,this.progressMap.values())}this.progress&&this.progress(e)}cancelDownload(){if("forEach"in this.workingXhr){this.workingXhr.forEach(e=>e.abort())}else{logError("Cancel Download Failed: forEach in this.workingXhr not found.")}}downloadFragment(e){const t=[];this.workingXhr=[];this.progressMap=new Map;this.updateProgress();const s=Math.round(e.size/this.fragmentSplitFactor);let n=0;const o=e=>[...this.progressMap.keys()].indexOf(e)+1;while(n{const n=new XMLHttpRequest;n.open("GET",e.url);n.responseType="arraybuffer";n.withCredentials=false;n.addEventListener("progress",e=>{console.log(`[下载视频] 视频片段${o(n)}下载进度: ${e.loaded}/${a} bytes loaded, ${i}`);this.progressMap.set(n,e.loaded);this.updateProgress()});n.addEventListener("load",()=>{if((""+n.status)[0]==="2"){t(n.response)}else{s(`请求失败.`)}});n.addEventListener("abort",()=>s("下载已取消."));n.addEventListener("error",()=>{console.error(`[下载视频] 视频片段${o(n)}下载失败: ${i}`);this.progressMap.set(n,0);this.updateProgress();n.open("GET",e.url);n.send()});n.setRequestHeader("Range",i);this.progressMap.set(n,0);n.send();this.workingXhr.push(n)}));n=Math.round(n+s)+1}return Promise.all(t)}copyUrl(){const e=this.fragments.map(e=>e.url).reduce((e,t)=>e+"\r\n"+t);GM_setClipboard(e,"text")}extension(e){return(e||this.fragments[0]).url.indexOf(".flv")!==-1?".flv":".mp4"}makeBlob(e,t=null){return new Blob(Array.isArray(e)?e:[e],{type:this.extension(t)===".flv"?"video/x-flv":"video/mp4"})}cleanUpOldBlobUrl(){const e=document.querySelector("a#video-complete").getAttribute("href");if(e&&!document.querySelector(`.link[href="${e}"]`)){URL.revokeObjectURL(e)}[...document.querySelectorAll(".toast-card-header")].filter(e=>e.innerText.includes("下载视频")).forEach(e=>e.querySelector(".toast-card-dismiss").click())}downloadSingle(e){const[t]=e;const n=this.makeBlob(t);const o=s()+this.extension();return[n,o]}async downloadMultiple(e){const t=new JSZip;const n=s();if(e.length>1){e.forEach((e,s)=>{const o=this.fragments[s];t.file(`${n} - ${s+1}${this.extension(o)}`,this.makeBlob(e,o))})}else{const[s]=e;t.file(`${n}${this.extension()}`,this.makeBlob(s))}const o=await t.generateAsync({type:"blob"});const r=n+".zip";return[o,r]}async download(){const e=[];this.totalSize=this.fragments.map(e=>e.size).reduce((e,t)=>e+t);for(const t of this.fragments){const s=await this.downloadFragment(t);e.push(s)}if(e.length<1){throw new Error("下载失败.")}let t=null;let s=null;if(e.length===1){[t,s]=this.downloadSingle(e)}else{[t,s]=await this.downloadMultiple(e)}this.cleanUpOldBlobUrl();const n=URL.createObjectURL(t);this.progress&&this.progress(0);return{url:n,filename:s}}}async function c(){const e=await SpinQuery.select(()=>(unsafeWindow||window).aid);const t=await SpinQuery.select(()=>(unsafeWindow||window).cid);n.aid=e;n.cid=t;if(document.URL.indexOf("bangumi")!==-1){n.entity=new r}else{n.entity=new o}return Boolean(e&&t)}async function d(){let e=await i.availableFormats;let[s]=e;const o=async()=>{await c();e=await i.availableFormats;const t=$("ol.video-quality");t.html("");e.forEach(e=>{$(`
  • ${e.displayName}
  • `).on("click",()=>{s=e;n.entity.nextMenuClass()}).prependTo(t)})};if(Observer.videoChange){Observer.videoChange(o)}else{Observer.childList("#bofqi",o)}const r=()=>s.downloadInfo().catch(e=>{n.entity.addError();$(".video-error").text(e)});async function a(){if(!s){return}n.entity.nextMenuClass();const e=await r();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=>{n.entity.addError();$(".video-error").text(e)});if(!t){return}const o=document.getElementById("video-complete");o.setAttribute("href",t.url);o.setAttribute("download",t.filename);o.click();const i=`下载完成. 再次保存`;Toast.success(i,"下载视频");n.entity.resetMenuClass()}async function l(){if(!s){return}const e=await r();e.copyUrl();Toast.success("已复制链接到剪贴板.","复制链接",3e3);n.entity.resetMenuClass()}$(".video-action>#video-action-download").on("click",a);$(".video-action>#video-action-copy").on("click",l);t.applyStyle("downloadVideoStyle");const d=document.querySelector(".download-video-panel");const u=()=>$(".download-video-panel").toggleClass("opened");$("#download-video").on("click",e=>{if(!d.contains(e.target)){u()}});$(".video-error").on("click",()=>{$(".video-error").text("");n.entity.removeError()});await SpinQuery.select(()=>document.querySelector(".download-video-panel"));n.entity.addMenuClass()}return{widget:{content:(t.data.downloadVideoDom||t.data.downloadVideoHtml).text,condition:c,success:d}}}})(); +offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/download-video.min.js"] = (()=>{return(e,t)=>{const{getFriendlyTitle:s}=t.import("title");const n={entity:null,aid:undefined,cid:undefined};class o{constructor(){this.menuClasses=["quality","action","progress"];this.currentMenuClass="quality"}get menuPanel(){return document.querySelector(".download-video-panel")}addMenuClass(){this.menuPanel.classList.remove(...this.menuClasses);this.menuPanel.classList.add(this.currentMenuClass);return this.currentMenuClass}resetMenuClass(){[this.currentMenuClass]=this.menuClasses;this.addMenuClass()}nextMenuClass(){const e=this.menuClasses.indexOf(this.currentMenuClass)+1;const t=this.menuClasses[e>=this.menuClasses.length?0:e];this.currentMenuClass=t;this.addMenuClass();return t}addError(){this.menuPanel.classList.add("error")}removeError(){this.menuPanel.classList.remove("error");this.resetMenuClass()}async getUrl(e){if(e){return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&otype=json`}}}class r extends o{async getUrl(e){if(e){return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=&otype=json`}}}class i{constructor(e,t,s){this.quality=e;this.internalName=t;this.displayName=s}async downloadInfo(){const e=new l(this);await e.fetchVideoInfo();return e}static get availableFormats(){return new Promise((e,t)=>{n.entity.getUrl().then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText);if(s.code!==0){t("获取清晰度信息失败.")}const o=s.data||s.result||s;const r=o.accept_quality;const a=o.accept_format.split(",");const l=o.accept_description;const c=[];while(r.length>0){const e=new i(r.pop(),a.pop(),l.pop());c.push(e)}e(c)});n.addEventListener("error",()=>t(`获取清晰度信息失败.`));n.withCredentials=true;n.open("GET",s);n.send()})})}}class a{constructor(e,t,s,n){this.length=e;this.size=t;this.url=s;this.backupUrls=n}}class l{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null;this.totalSize=null;this.workingXhr=null;this.fragmentSplitFactor=6*5}fetchVideoInfo(){return new Promise((e,t)=>{n.entity.getUrl(this.format.quality).then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText.replace(/http:/g,"https:"));const o=s.data||s.result||s;if(o.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const r=o.durl;this.fragments=r.map(e=>new a(e.length,e.size,e.url,e.backup_url));e(this.fragments)});n.withCredentials=true;n.open("GET",s);n.send()})})}updateProgress(){const e=this.progressMap?[...this.progressMap.values()].reduce((e,t)=>e+t,0)/this.totalSize:0;if(e>1||e<0){console.error(`[下载视频] 进度异常: ${e}`,this.progressMap.values())}this.progress&&this.progress(e)}cancelDownload(){if("forEach"in this.workingXhr){this.workingXhr.forEach(e=>e.abort())}else{logError("Cancel Download Failed: forEach in this.workingXhr not found.")}}downloadFragment(e){const t=[];this.workingXhr=[];this.progressMap=new Map;this.updateProgress();const s=Math.round(e.size/this.fragmentSplitFactor);let n=0;const o=e=>[...this.progressMap.keys()].indexOf(e)+1;while(n{const n=new XMLHttpRequest;n.open("GET",e.url);n.responseType="arraybuffer";n.withCredentials=false;n.addEventListener("progress",e=>{console.log(`[下载视频] 视频片段${o(n)}下载进度: ${e.loaded}/${a} bytes loaded, ${i}`);this.progressMap.set(n,e.loaded);this.updateProgress()});n.addEventListener("load",()=>{if((""+n.status)[0]==="2"){t(n.response)}else{s(`请求失败.`)}});n.addEventListener("abort",()=>s("下载已取消."));n.addEventListener("error",()=>{console.error(`[下载视频] 视频片段${o(n)}下载失败: ${i}`);this.progressMap.set(n,0);this.updateProgress();n.open("GET",e.url);n.setRequestHeader("Range",i);n.send()});n.setRequestHeader("Range",i);this.progressMap.set(n,0);n.send();this.workingXhr.push(n)}));n=Math.round(n+s)+1}return Promise.all(t)}copyUrl(){const e=this.fragments.map(e=>e.url).reduce((e,t)=>e+"\r\n"+t);GM_setClipboard(e,"text")}extension(e){return(e||this.fragments[0]).url.indexOf(".flv")!==-1?".flv":".mp4"}makeBlob(e,t=null){return new Blob(Array.isArray(e)?e:[e],{type:this.extension(t)===".flv"?"video/x-flv":"video/mp4"})}cleanUpOldBlobUrl(){const e=document.querySelector("a#video-complete").getAttribute("href");if(e&&!document.querySelector(`.link[href="${e}"]`)){URL.revokeObjectURL(e)}[...document.querySelectorAll(".toast-card-header")].filter(e=>e.innerText.includes("下载视频")).forEach(e=>e.querySelector(".toast-card-dismiss").click())}downloadSingle(e){const[t]=e;const n=this.makeBlob(t);const o=s()+this.extension();return[n,o]}async downloadMultiple(e){const t=new JSZip;const n=s();if(e.length>1){e.forEach((e,s)=>{const o=this.fragments[s];t.file(`${n} - ${s+1}${this.extension(o)}`,this.makeBlob(e,o))})}else{const[s]=e;t.file(`${n}${this.extension()}`,this.makeBlob(s))}const o=await t.generateAsync({type:"blob"});const r=n+".zip";return[o,r]}async download(){const e=[];this.totalSize=this.fragments.map(e=>e.size).reduce((e,t)=>e+t);for(const t of this.fragments){const s=await this.downloadFragment(t);e.push(s)}if(e.length<1){throw new Error("下载失败.")}let t=null;let s=null;if(e.length===1){[t,s]=this.downloadSingle(e)}else{[t,s]=await this.downloadMultiple(e)}this.cleanUpOldBlobUrl();const n=URL.createObjectURL(t);this.progress&&this.progress(0);return{url:n,filename:s}}}async function c(){const e=await SpinQuery.select(()=>(unsafeWindow||window).aid);const t=await SpinQuery.select(()=>(unsafeWindow||window).cid);n.aid=e;n.cid=t;if(document.URL.indexOf("bangumi")!==-1){n.entity=new r}else{n.entity=new o}return Boolean(e&&t)}async function d(){let e=await i.availableFormats;let[s]=e;const o=async()=>{await c();e=await i.availableFormats;const t=$("ol.video-quality");t.html("");e.forEach(e=>{$(`
  • ${e.displayName}
  • `).on("click",()=>{s=e;n.entity.nextMenuClass()}).prependTo(t)})};if(Observer.videoChange){Observer.videoChange(o)}else{Observer.childList("#bofqi",o)}const r=()=>s.downloadInfo().catch(e=>{n.entity.addError();$(".video-error").text(e)});async function a(){if(!s){return}n.entity.nextMenuClass();const e=await r();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=>{n.entity.addError();$(".video-error").text(e)});if(!t){return}const o=document.getElementById("video-complete");o.setAttribute("href",t.url);o.setAttribute("download",t.filename);o.click();const i=`下载完成. 再次保存`;Toast.success(i,"下载视频");n.entity.resetMenuClass()}async function l(){if(!s){return}const e=await r();e.copyUrl();Toast.success("已复制链接到剪贴板.","复制链接",3e3);n.entity.resetMenuClass()}$(".video-action>#video-action-download").on("click",a);$(".video-action>#video-action-copy").on("click",l);t.applyStyle("downloadVideoStyle");const d=document.querySelector(".download-video-panel");const u=()=>$(".download-video-panel").toggleClass("opened");$("#download-video").on("click",e=>{if(!d.contains(e.target)){u()}});$(".video-error").on("click",()=>{$(".video-error").text("");n.entity.removeError()});await SpinQuery.select(()=>document.querySelector(".download-video-panel"));n.entity.addMenuClass()}return{widget:{content:(t.data.downloadVideoDom||t.data.downloadVideoHtml).text,condition:c,success:d}}}})(); offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/expand-danmaku.min.js"] = (()=>{return(e,i)=>{if(typeof isEmbeddedPlayer!=="undefined"&&isEmbeddedPlayer()){return}const r=()=>{SpinQuery.any(()=>$(".bui-collapse-header"),e=>{if(parseInt($(".bui-collapse-body").css("height"))===0&&$(".bui-collapse-arrow-text").text()==="展开"){e.click()}})};if(Observer.videoChange){Observer.videoChange(r)}else{Observer.childList("#bofqi",r)}}})(); offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/expand-description.min.css"] = `.play-up-info .play-up-self,.video-desc .info{height:auto!important}.play-up-info .play-up-self-btn,.video-desc .btn{display:none!important}`; offlineData["https://raw.githubusercontent.com/the1812/Bilibili-Evolved/master/min/expand-description.min.js"] = (()=>{return(e,p)=>{p.applyStyle("expandDescriptionStyle")}})(); diff --git a/min/download-video.min.js b/min/download-video.min.js index b59ada3c7..8e1178ae6 100644 --- a/min/download-video.min.js +++ b/min/download-video.min.js @@ -1 +1 @@ -(()=>{return(e,t)=>{const{getFriendlyTitle:s}=t.import("title");const n={entity:null,aid:undefined,cid:undefined};class o{constructor(){this.menuClasses=["quality","action","progress"];this.currentMenuClass="quality"}get menuPanel(){return document.querySelector(".download-video-panel")}addMenuClass(){this.menuPanel.classList.remove(...this.menuClasses);this.menuPanel.classList.add(this.currentMenuClass);return this.currentMenuClass}resetMenuClass(){[this.currentMenuClass]=this.menuClasses;this.addMenuClass()}nextMenuClass(){const e=this.menuClasses.indexOf(this.currentMenuClass)+1;const t=this.menuClasses[e>=this.menuClasses.length?0:e];this.currentMenuClass=t;this.addMenuClass();return t}addError(){this.menuPanel.classList.add("error")}removeError(){this.menuPanel.classList.remove("error");this.resetMenuClass()}async getUrl(e){if(e){return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&otype=json`}}}class r extends o{async getUrl(e){if(e){return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=&otype=json`}}}class i{constructor(e,t,s){this.quality=e;this.internalName=t;this.displayName=s}async downloadInfo(){const e=new l(this);await e.fetchVideoInfo();return e}static get availableFormats(){return new Promise((e,t)=>{n.entity.getUrl().then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText);if(s.code!==0){t("获取清晰度信息失败.")}const o=s.data||s.result||s;const r=o.accept_quality;const a=o.accept_format.split(",");const l=o.accept_description;const c=[];while(r.length>0){const e=new i(r.pop(),a.pop(),l.pop());c.push(e)}e(c)});n.addEventListener("error",()=>t(`获取清晰度信息失败.`));n.withCredentials=true;n.open("GET",s);n.send()})})}}class a{constructor(e,t,s,n){this.length=e;this.size=t;this.url=s;this.backupUrls=n}}class l{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null;this.totalSize=null;this.workingXhr=null;this.fragmentSplitFactor=6*5}fetchVideoInfo(){return new Promise((e,t)=>{n.entity.getUrl(this.format.quality).then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText.replace(/http:/g,"https:"));const o=s.data||s.result||s;if(o.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const r=o.durl;this.fragments=r.map(e=>new a(e.length,e.size,e.url,e.backup_url));e(this.fragments)});n.withCredentials=true;n.open("GET",s);n.send()})})}updateProgress(){const e=this.progressMap?[...this.progressMap.values()].reduce((e,t)=>e+t,0)/this.totalSize:0;if(e>1||e<0){console.error(`[下载视频] 进度异常: ${e}`,this.progressMap.values())}this.progress&&this.progress(e)}cancelDownload(){if("forEach"in this.workingXhr){this.workingXhr.forEach(e=>e.abort())}else{logError("Cancel Download Failed: forEach in this.workingXhr not found.")}}downloadFragment(e){const t=[];this.workingXhr=[];this.progressMap=new Map;this.updateProgress();const s=Math.round(e.size/this.fragmentSplitFactor);let n=0;const o=e=>[...this.progressMap.keys()].indexOf(e)+1;while(n{const n=new XMLHttpRequest;n.open("GET",e.url);n.responseType="arraybuffer";n.withCredentials=false;n.addEventListener("progress",e=>{console.log(`[下载视频] 视频片段${o(n)}下载进度: ${e.loaded}/${a} bytes loaded, ${i}`);this.progressMap.set(n,e.loaded);this.updateProgress()});n.addEventListener("load",()=>{if((""+n.status)[0]==="2"){t(n.response)}else{s(`请求失败.`)}});n.addEventListener("abort",()=>s("下载已取消."));n.addEventListener("error",()=>{console.error(`[下载视频] 视频片段${o(n)}下载失败: ${i}`);this.progressMap.set(n,0);this.updateProgress();n.open("GET",e.url);n.send()});n.setRequestHeader("Range",i);this.progressMap.set(n,0);n.send();this.workingXhr.push(n)}));n=Math.round(n+s)+1}return Promise.all(t)}copyUrl(){const e=this.fragments.map(e=>e.url).reduce((e,t)=>e+"\r\n"+t);GM_setClipboard(e,"text")}extension(e){return(e||this.fragments[0]).url.indexOf(".flv")!==-1?".flv":".mp4"}makeBlob(e,t=null){return new Blob(Array.isArray(e)?e:[e],{type:this.extension(t)===".flv"?"video/x-flv":"video/mp4"})}cleanUpOldBlobUrl(){const e=document.querySelector("a#video-complete").getAttribute("href");if(e&&!document.querySelector(`.link[href="${e}"]`)){URL.revokeObjectURL(e)}[...document.querySelectorAll(".toast-card-header")].filter(e=>e.innerText.includes("下载视频")).forEach(e=>e.querySelector(".toast-card-dismiss").click())}downloadSingle(e){const[t]=e;const n=this.makeBlob(t);const o=s()+this.extension();return[n,o]}async downloadMultiple(e){const t=new JSZip;const n=s();if(e.length>1){e.forEach((e,s)=>{const o=this.fragments[s];t.file(`${n} - ${s+1}${this.extension(o)}`,this.makeBlob(e,o))})}else{const[s]=e;t.file(`${n}${this.extension()}`,this.makeBlob(s))}const o=await t.generateAsync({type:"blob"});const r=n+".zip";return[o,r]}async download(){const e=[];this.totalSize=this.fragments.map(e=>e.size).reduce((e,t)=>e+t);for(const t of this.fragments){const s=await this.downloadFragment(t);e.push(s)}if(e.length<1){throw new Error("下载失败.")}let t=null;let s=null;if(e.length===1){[t,s]=this.downloadSingle(e)}else{[t,s]=await this.downloadMultiple(e)}this.cleanUpOldBlobUrl();const n=URL.createObjectURL(t);this.progress&&this.progress(0);return{url:n,filename:s}}}async function c(){const e=await SpinQuery.select(()=>(unsafeWindow||window).aid);const t=await SpinQuery.select(()=>(unsafeWindow||window).cid);n.aid=e;n.cid=t;if(document.URL.indexOf("bangumi")!==-1){n.entity=new r}else{n.entity=new o}return Boolean(e&&t)}async function d(){let e=await i.availableFormats;let[s]=e;const o=async()=>{await c();e=await i.availableFormats;const t=$("ol.video-quality");t.html("");e.forEach(e=>{$(`
  • ${e.displayName}
  • `).on("click",()=>{s=e;n.entity.nextMenuClass()}).prependTo(t)})};if(Observer.videoChange){Observer.videoChange(o)}else{Observer.childList("#bofqi",o)}const r=()=>s.downloadInfo().catch(e=>{n.entity.addError();$(".video-error").text(e)});async function a(){if(!s){return}n.entity.nextMenuClass();const e=await r();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=>{n.entity.addError();$(".video-error").text(e)});if(!t){return}const o=document.getElementById("video-complete");o.setAttribute("href",t.url);o.setAttribute("download",t.filename);o.click();const i=`下载完成. 再次保存`;Toast.success(i,"下载视频");n.entity.resetMenuClass()}async function l(){if(!s){return}const e=await r();e.copyUrl();Toast.success("已复制链接到剪贴板.","复制链接",3e3);n.entity.resetMenuClass()}$(".video-action>#video-action-download").on("click",a);$(".video-action>#video-action-copy").on("click",l);t.applyStyle("downloadVideoStyle");const d=document.querySelector(".download-video-panel");const u=()=>$(".download-video-panel").toggleClass("opened");$("#download-video").on("click",e=>{if(!d.contains(e.target)){u()}});$(".video-error").on("click",()=>{$(".video-error").text("");n.entity.removeError()});await SpinQuery.select(()=>document.querySelector(".download-video-panel"));n.entity.addMenuClass()}return{widget:{content:(t.data.downloadVideoDom||t.data.downloadVideoHtml).text,condition:c,success:d}}}})(); \ No newline at end of file +(()=>{return(e,t)=>{const{getFriendlyTitle:s}=t.import("title");const n={entity:null,aid:undefined,cid:undefined};class o{constructor(){this.menuClasses=["quality","action","progress"];this.currentMenuClass="quality"}get menuPanel(){return document.querySelector(".download-video-panel")}addMenuClass(){this.menuPanel.classList.remove(...this.menuClasses);this.menuPanel.classList.add(this.currentMenuClass);return this.currentMenuClass}resetMenuClass(){[this.currentMenuClass]=this.menuClasses;this.addMenuClass()}nextMenuClass(){const e=this.menuClasses.indexOf(this.currentMenuClass)+1;const t=this.menuClasses[e>=this.menuClasses.length?0:e];this.currentMenuClass=t;this.addMenuClass();return t}addError(){this.menuPanel.classList.add("error")}removeError(){this.menuPanel.classList.remove("error");this.resetMenuClass()}async getUrl(e){if(e){return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/x/player/playurl?avid=${n.aid}&cid=${n.cid}&otype=json`}}}class r extends o{async getUrl(e){if(e){return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=${e}&otype=json`}else{return`https://api.bilibili.com/pgc/player/web/playurl?avid=${n.aid}&cid=${n.cid}&qn=&otype=json`}}}class i{constructor(e,t,s){this.quality=e;this.internalName=t;this.displayName=s}async downloadInfo(){const e=new l(this);await e.fetchVideoInfo();return e}static get availableFormats(){return new Promise((e,t)=>{n.entity.getUrl().then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText);if(s.code!==0){t("获取清晰度信息失败.")}const o=s.data||s.result||s;const r=o.accept_quality;const a=o.accept_format.split(",");const l=o.accept_description;const c=[];while(r.length>0){const e=new i(r.pop(),a.pop(),l.pop());c.push(e)}e(c)});n.addEventListener("error",()=>t(`获取清晰度信息失败.`));n.withCredentials=true;n.open("GET",s);n.send()})})}}class a{constructor(e,t,s,n){this.length=e;this.size=t;this.url=s;this.backupUrls=n}}class l{constructor(e,t){this.format=e;this.fragments=t||[];this.progress=null;this.totalSize=null;this.workingXhr=null;this.fragmentSplitFactor=6*5}fetchVideoInfo(){return new Promise((e,t)=>{n.entity.getUrl(this.format.quality).then(s=>{const n=new XMLHttpRequest;n.addEventListener("load",()=>{const s=JSON.parse(n.responseText.replace(/http:/g,"https:"));const o=s.data||s.result||s;if(o.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const r=o.durl;this.fragments=r.map(e=>new a(e.length,e.size,e.url,e.backup_url));e(this.fragments)});n.withCredentials=true;n.open("GET",s);n.send()})})}updateProgress(){const e=this.progressMap?[...this.progressMap.values()].reduce((e,t)=>e+t,0)/this.totalSize:0;if(e>1||e<0){console.error(`[下载视频] 进度异常: ${e}`,this.progressMap.values())}this.progress&&this.progress(e)}cancelDownload(){if("forEach"in this.workingXhr){this.workingXhr.forEach(e=>e.abort())}else{logError("Cancel Download Failed: forEach in this.workingXhr not found.")}}downloadFragment(e){const t=[];this.workingXhr=[];this.progressMap=new Map;this.updateProgress();const s=Math.round(e.size/this.fragmentSplitFactor);let n=0;const o=e=>[...this.progressMap.keys()].indexOf(e)+1;while(n{const n=new XMLHttpRequest;n.open("GET",e.url);n.responseType="arraybuffer";n.withCredentials=false;n.addEventListener("progress",e=>{console.log(`[下载视频] 视频片段${o(n)}下载进度: ${e.loaded}/${a} bytes loaded, ${i}`);this.progressMap.set(n,e.loaded);this.updateProgress()});n.addEventListener("load",()=>{if((""+n.status)[0]==="2"){t(n.response)}else{s(`请求失败.`)}});n.addEventListener("abort",()=>s("下载已取消."));n.addEventListener("error",()=>{console.error(`[下载视频] 视频片段${o(n)}下载失败: ${i}`);this.progressMap.set(n,0);this.updateProgress();n.open("GET",e.url);n.setRequestHeader("Range",i);n.send()});n.setRequestHeader("Range",i);this.progressMap.set(n,0);n.send();this.workingXhr.push(n)}));n=Math.round(n+s)+1}return Promise.all(t)}copyUrl(){const e=this.fragments.map(e=>e.url).reduce((e,t)=>e+"\r\n"+t);GM_setClipboard(e,"text")}extension(e){return(e||this.fragments[0]).url.indexOf(".flv")!==-1?".flv":".mp4"}makeBlob(e,t=null){return new Blob(Array.isArray(e)?e:[e],{type:this.extension(t)===".flv"?"video/x-flv":"video/mp4"})}cleanUpOldBlobUrl(){const e=document.querySelector("a#video-complete").getAttribute("href");if(e&&!document.querySelector(`.link[href="${e}"]`)){URL.revokeObjectURL(e)}[...document.querySelectorAll(".toast-card-header")].filter(e=>e.innerText.includes("下载视频")).forEach(e=>e.querySelector(".toast-card-dismiss").click())}downloadSingle(e){const[t]=e;const n=this.makeBlob(t);const o=s()+this.extension();return[n,o]}async downloadMultiple(e){const t=new JSZip;const n=s();if(e.length>1){e.forEach((e,s)=>{const o=this.fragments[s];t.file(`${n} - ${s+1}${this.extension(o)}`,this.makeBlob(e,o))})}else{const[s]=e;t.file(`${n}${this.extension()}`,this.makeBlob(s))}const o=await t.generateAsync({type:"blob"});const r=n+".zip";return[o,r]}async download(){const e=[];this.totalSize=this.fragments.map(e=>e.size).reduce((e,t)=>e+t);for(const t of this.fragments){const s=await this.downloadFragment(t);e.push(s)}if(e.length<1){throw new Error("下载失败.")}let t=null;let s=null;if(e.length===1){[t,s]=this.downloadSingle(e)}else{[t,s]=await this.downloadMultiple(e)}this.cleanUpOldBlobUrl();const n=URL.createObjectURL(t);this.progress&&this.progress(0);return{url:n,filename:s}}}async function c(){const e=await SpinQuery.select(()=>(unsafeWindow||window).aid);const t=await SpinQuery.select(()=>(unsafeWindow||window).cid);n.aid=e;n.cid=t;if(document.URL.indexOf("bangumi")!==-1){n.entity=new r}else{n.entity=new o}return Boolean(e&&t)}async function d(){let e=await i.availableFormats;let[s]=e;const o=async()=>{await c();e=await i.availableFormats;const t=$("ol.video-quality");t.html("");e.forEach(e=>{$(`
  • ${e.displayName}
  • `).on("click",()=>{s=e;n.entity.nextMenuClass()}).prependTo(t)})};if(Observer.videoChange){Observer.videoChange(o)}else{Observer.childList("#bofqi",o)}const r=()=>s.downloadInfo().catch(e=>{n.entity.addError();$(".video-error").text(e)});async function a(){if(!s){return}n.entity.nextMenuClass();const e=await r();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=>{n.entity.addError();$(".video-error").text(e)});if(!t){return}const o=document.getElementById("video-complete");o.setAttribute("href",t.url);o.setAttribute("download",t.filename);o.click();const i=`下载完成. 再次保存`;Toast.success(i,"下载视频");n.entity.resetMenuClass()}async function l(){if(!s){return}const e=await r();e.copyUrl();Toast.success("已复制链接到剪贴板.","复制链接",3e3);n.entity.resetMenuClass()}$(".video-action>#video-action-download").on("click",a);$(".video-action>#video-action-copy").on("click",l);t.applyStyle("downloadVideoStyle");const d=document.querySelector(".download-video-panel");const u=()=>$(".download-video-panel").toggleClass("opened");$("#download-video").on("click",e=>{if(!d.contains(e.target)){u()}});$(".video-error").on("click",()=>{$(".video-error").text("");n.entity.removeError()});await SpinQuery.select(()=>document.querySelector(".download-video-panel"));n.entity.addMenuClass()}return{widget:{content:(t.data.downloadVideoDom||t.data.downloadVideoHtml).text,condition:c,success:d}}}})(); \ No newline at end of file diff --git a/video/download-video/download-video.js b/video/download-video/download-video.js index 27c613ecb..e7dba4bcd 100644 --- a/video/download-video/download-video.js +++ b/video/download-video/download-video.js @@ -238,6 +238,7 @@ class VideoDownloader this.progressMap.set(xhr, 0); this.updateProgress(); xhr.open("GET", fragment.url); + xhr.setRequestHeader("Range", range); xhr.send(); }); xhr.setRequestHeader("Range", range);