mirror of
https://github.com/iv-org/invidious.git
synced 2025-05-02 14:36:23 -04:00
Add download widget
This commit is contained in:
parent
15efac520e
commit
fb535ad6bb
3 changed files with 96 additions and 0 deletions
|
@ -50,3 +50,55 @@ function hide_youtube_replies(target) {
|
|||
target.innerHTML = "Show replies";
|
||||
target.setAttribute("onclick", "show_youtube_replies(this)");
|
||||
}
|
||||
|
||||
function download_video(title) {
|
||||
var children = document.getElementById("download_widget").children;
|
||||
var progress = document.getElementById("download-progress");
|
||||
var url = "";
|
||||
|
||||
document.getElementById("progress-container").style.display = "";
|
||||
|
||||
for (i = 0; i < children.length; i++) {
|
||||
if (children[i].selected) {
|
||||
url = children[i].getAttribute("data-url");
|
||||
}
|
||||
}
|
||||
|
||||
url = "/videoplayback" + url.split("/videoplayback")[1];
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", url);
|
||||
xhr.responseType = "arraybuffer";
|
||||
|
||||
xhr.onprogress = function(event) {
|
||||
if (event.lengthComputable) {
|
||||
progress.style.width = "" + (event.loaded / event.total)*100 + "%";
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = function(event) {
|
||||
if (event.currentTarget.status != 200) {
|
||||
console.log("Downloading " + title + " failed.")
|
||||
return;
|
||||
}
|
||||
|
||||
var data = new Blob([xhr.response], {'type' : 'video/mp4'});
|
||||
var videoFile = window.URL.createObjectURL(data);
|
||||
|
||||
var link = document.createElement('a');
|
||||
link.href = videoFile;
|
||||
link.setAttribute('download', title);
|
||||
document.body.appendChild(link);
|
||||
|
||||
window.requestAnimationFrame(function() {
|
||||
var event = new MouseEvent('click');
|
||||
link.dispatchEvent(event);
|
||||
document.body.removeChild(link);
|
||||
});
|
||||
|
||||
document.getElementById("progress-container").style.display = "none";
|
||||
progress.style.width = "0%";
|
||||
};
|
||||
|
||||
xhr.send(null);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue