Fix bug in downloadText()

This commit is contained in:
the1812 2018-10-25 17:07:11 +08:00
parent f4d077ab63
commit a32686c5ed
7 changed files with 28 additions and 24 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
// ==UserScript==
// @name Bilibili Evolved (Preview)
// @version 1.5.13
// @version 1.5.14
// @description 增强哔哩哔哩Web端体验.(预览版分支)
// @author Grant Howard, Coulomb-G
// @copyright 2018, Grant Howrad (https://github.com/the1812)
@ -60,7 +60,7 @@
fixFullscreen: false,
downloadVideo: true,
latestVersionLink: "https://github.com/the1812/Bilibili-Evolved/raw/preview/bilibili-evolved.preview.user.js",
currentVersion: "1.5.13"
currentVersion: "1.5.14"
};
function loadSettings()
{
@ -217,7 +217,6 @@
const xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => load && load(xhr.responseText));
xhr.addEventListener("error", () => error && error(xhr.responseText));
xhr.withCredentials = true;
xhr.open("GET", url);
xhr.send();
}

View File

@ -1,6 +1,6 @@
// ==UserScript==
// @name Bilibili Evolved
// @version 1.5.13
// @version 1.5.14
// @description 增强哔哩哔哩Web端体验.
// @author Grant Howard, Coulomb-G
// @copyright 2018, Grant Howrad (https://github.com/the1812)
@ -60,7 +60,7 @@
fixFullscreen: false,
downloadVideo: true,
latestVersionLink: "https://github.com/the1812/Bilibili-Evolved/raw/master/bilibili-evolved.user.js",
currentVersion: "1.5.13"
currentVersion: "1.5.14"
};
function loadSettings()
{
@ -217,7 +217,6 @@
const xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => load && load(xhr.responseText));
xhr.addEventListener("error", () => error && error(xhr.responseText));
xhr.withCredentials = true;
xhr.open("GET", url);
xhr.send();
}

View File

@ -1 +1 @@
(()=>{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")})}}}}})();
(()=>{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 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 a=`https://api.bilibili.com/x/player/playurl?avid=${s}&cid=${o}&otype=json`;const r=new XMLHttpRequest;r.addEventListener("load",()=>{const t=JSON.parse(r.responseText).data;const s=t.accept_quality;const o=t.accept_format.split(",");const a=t.accept_description;const i=[];while(s.length>0){const e=new n(s.pop(),o.pop(),a.pop());i.push(e)}e(i)});r.addEventListener("error",()=>t(`获取清晰度信息失败.`));r.withCredentials=true;r.open("GET",a);r.send()})}}class a{constructor(e,t,s,o){this.length=e;this.size=t;this.url=s;this.backupUrls=o}}class r{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 r=new XMLHttpRequest;r.addEventListener("load",()=>{const s=JSON.parse(r.responseText).data;if(s.quality!==this.format.quality){t("获取下载链接失败, 请确认当前账号有下载权限后重试.")}const o=s.durl;this.fragments=o.map(e=>new a(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)});r.withCredentials=true;r.open("GET",n);r.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 a=document.title.replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili","");const r=s.url.indexOf(".flv")!==-1?".flv":".mp4";$("a#video-complete").attr("href",n).attr("download",a+r);e(n)}else{t(`请求失败.`)}});o.addEventListener("error",()=>{t(`下载失败.`)});o.send()})}}return{settingsWidget:{after:()=>$("span.settings-category").filter((e,t)=>t.innerHTML==="视频与直播").parent(),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");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")})}}}}})();

View File

@ -1 +1 @@
1.5.13
1.5.14

View File

@ -33,10 +33,10 @@
return new Promise((resolve, reject) =>
{
const url = `https://api.bilibili.com/x/player/playurl?avid=${aid}&cid=${cid}&otype=json`;
downloadText(url, json =>
const xhr = new XMLHttpRequest();
xhr.addEventListener("load", () =>
{
const data = JSON.parse(json).data;
const data = JSON.parse(xhr.responseText).data;
const qualities = data.accept_quality;
const internalNames = data.accept_format.split(",");
const displayNames = data.accept_description;
@ -51,7 +51,11 @@
formats.push(format);
}
resolve(formats);
}, error => reject(`获取清晰度信息失败: ${error}`));
});
xhr.addEventListener("error", () => reject(`获取清晰度信息失败.`));
xhr.withCredentials = true;
xhr.open("GET", url);
xhr.send();
});
}
}
@ -78,9 +82,10 @@
return new Promise((resolve, reject) =>
{
const url = `https://api.bilibili.com/x/player/playurl?avid=${aid}&cid=${cid}&qn=${this.format.quality}&otype=json`;
downloadText(url, json =>
const xhr = new XMLHttpRequest();
xhr.addEventListener("load", () =>
{
const data = JSON.parse(json).data;
const data = JSON.parse(xhr.responseText).data;
if (data.quality !== this.format.quality)
{
reject("获取下载链接失败, 请确认当前账号有下载权限后重试.");
@ -97,6 +102,9 @@
}
resolve(this.fragments);
});
xhr.withCredentials = true;
xhr.open("GET", url);
xhr.send();
});
}
download()