From fb535ad6bba09359c176b0d0d7c9570cc873e41a Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Wed, 23 Jan 2019 19:05:24 -0600 Subject: [PATCH] Add download widget --- assets/css/default.css | 16 +++++++++++ assets/js/watch.js | 52 +++++++++++++++++++++++++++++++++++ src/invidious/views/watch.ecr | 28 +++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/assets/css/default.css b/assets/css/default.css index 5fac9c2c..c9893190 100644 --- a/assets/css/default.css +++ b/assets/css/default.css @@ -267,3 +267,19 @@ img.thumbnail { margin-right: 2em; height: 0; } + +#progress-container { + width: 100%; + border-radius: 2px; + background: #aaa; + display: none; +} + +#download-progress { + width: 0%; + border-radius: 2px; + height: 10px; + background-color: #0078e7; + margin-top: 0.5em; + margin-bottom: 0.5em; +} diff --git a/assets/js/watch.js b/assets/js/watch.js index 99500686..d8167114 100644 --- a/assets/js/watch.js +++ b/assets/js/watch.js @@ -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); +} \ No newline at end of file diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index db7dcb7b..3f1ff3bc 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -53,6 +53,34 @@

<%= translate(locale, "Watch video on Youtube") %>

+ +
+
+ + +
+ + + + +
+

<%= number_with_separator(video.views) %>

<%= number_with_separator(video.likes) %>

<%= number_with_separator(video.dislikes) %>