diff options
| -rw-r--r-- | src/invidious.cr | 33 | ||||
| -rw-r--r-- | src/invidious/helpers/utils.cr | 21 | ||||
| -rw-r--r-- | src/invidious/views/channel.ecr | 10 | ||||
| -rw-r--r-- | src/invidious/views/login.ecr | 4 | ||||
| -rw-r--r-- | src/invidious/views/template.ecr | 8 | ||||
| -rw-r--r-- | src/invidious/views/watch.ecr | 26 |
6 files changed, 72 insertions, 30 deletions
diff --git a/src/invidious.cr b/src/invidious.cr index 18aa049a..f91e4c72 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -131,6 +131,19 @@ before_all do |env| end end end + + current_page = env.request.path + if env.request.query + query = HTTP::Params.parse(env.request.query.not_nil!) + + if query["referer"]? + query["referer"] = get_referer(env, "/") + end + + current_page += "?#{query}" + end + + env.set "current_page", URI.escape(current_page) end get "/" do |env| @@ -436,8 +449,7 @@ end # See https://github.com/rg3/youtube-dl/blob/master/youtube_dl/extractor/youtube.py#L79 post "/login" do |env| - referer = env.params.query["referer"]? - referer ||= get_referer(env, "/feed/subscriptions") + referer = get_referer(env, "/feed/subscriptions") email = env.params.body["email"]? password = env.params.body["password"]? @@ -531,7 +543,7 @@ post "/login" do |env| end if !tfa_code - next env.redirect "/login?tfa=true&type=google" + next env.redirect "/login?tfa=true&type=google&referer=#{URI.escape(referer)}" end tl = challenge_results[1][2] @@ -702,7 +714,7 @@ get "/signout" do |env| end env.request.cookies.add_response_headers(env.response.headers) - env.redirect referer + env.redirect URI.unescape(referer) end get "/preferences" do |env| @@ -1692,7 +1704,7 @@ get "/api/v1/comments/:id" do |env| if format == "json" next {"comments" => [] of String}.to_json else - next {"content_html" => ""}.to_json + next {"contentHtml" => ""}.to_json end end ctoken = ctoken["ctoken"] @@ -1730,7 +1742,7 @@ get "/api/v1/comments/:id" do |env| if format == "json" next {"comments" => [] of String}.to_json else - next {"content_html" => ""}.to_json + next {"contentHtml" => ""}.to_json end end @@ -1830,7 +1842,8 @@ get "/api/v1/comments/:id" do |env| comments = JSON.parse(comments) content_html = template_youtube_comments(comments) - next {"content_html" => content_html}.to_json + next {"contentHtml" => content_html, + "commentCount" => comments["commentCount"]}.to_json end elsif source == "reddit" client = make_client(REDDIT_URL) @@ -1851,9 +1864,9 @@ get "/api/v1/comments/:id" do |env| end env.response.content_type = "application/json" - next {"title" => reddit_thread.title, - "permalink" => reddit_thread.permalink, - "content_html" => content_html}.to_json + next {"title" => reddit_thread.title, + "permalink" => reddit_thread.permalink, + "contentHtml" => content_html}.to_json end end diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index d424fb9c..fe3e4e24 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -163,10 +163,27 @@ def make_host_url(ssl, host) end def get_referer(env, fallback = "/") - referer = env.request.headers["referer"]? + referer = env.params.query["referer"]? + referer ||= env.request.headers["referer"]? referer ||= fallback - referer = URI.parse(referer).full_path + referer = URI.parse(referer) + + # "Unroll" nested referers + loop do + if referer.query + params = HTTP::Params.parse(referer.query.not_nil!) + if params["referer"]? + referer = URI.parse(URI.unescape(params["referer"])) + else + break + end + else + break + end + end + + referer = referer.full_path if referer == env.request.path referer = fallback diff --git a/src/invidious/views/channel.ecr b/src/invidious/views/channel.ecr index ab8c0b2e..8360d30c 100644 --- a/src/invidious/views/channel.ecr +++ b/src/invidious/views/channel.ecr @@ -16,21 +16,25 @@ <p class="h-box"> <% if user %> <% if subscriptions.includes? ucid %> - <a href="/subscription_ajax?action_remove_subscriptions=1&c=<%= ucid %>"> + <a href="/subscription_ajax?action_remove_subscriptions=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>"> <b>Unsubscribe from <%= author %></b> </a> <% else %> - <a href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= ucid %>"> + <a href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= ucid %>&referer=<%= env.get("current_page") %>"> <b>Subscribe to <%= author %></b> </a> <% end %> <% else %> - <a href="/login"> + <a href="/login?referer=<%= env.get("current_page") %>"> <b>Login to subscribe to <%= author %></b> </a> <% end %> </p> +<p class="h-box"> + <a href="https://www.youtube.com/channel/<%= ucid %>">View channel on YouTube</a> +</p> + <% videos.each_slice(4) do |slice| %> <div class="pure-g"> <% slice.each do |video| %> diff --git a/src/invidious/views/login.ecr b/src/invidious/views/login.ecr index 3f19ba53..dc88379f 100644 --- a/src/invidious/views/login.ecr +++ b/src/invidious/views/login.ecr @@ -16,7 +16,7 @@ </div> <hr> <% if account_type == "invidious" %> - <form class="pure-form pure-form-stacked" action="/login?referer=<%= referer %>&type=invidious" method="post"> + <form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.escape(referer) %>&type=invidious" method="post"> <fieldset> <label for="email">User ID:</label> <input required class="pure-input-1" name="email" type="text" placeholder="User ID"> @@ -34,7 +34,7 @@ </fieldset> </form> <% elsif account_type == "google" %> - <form class="pure-form pure-form-stacked" action="/login?referer=<%= referer %>" method="post"> + <form class="pure-form pure-form-stacked" action="/login?referer=<%= URI.escape(referer) %>" method="post"> <fieldset> <label for="email">Email:</label> <input required class="pure-input-1" name="email" type="email" placeholder="Email"> diff --git a/src/invidious/views/template.ecr b/src/invidious/views/template.ecr index 2f565541..871a5f78 100644 --- a/src/invidious/views/template.ecr +++ b/src/invidious/views/template.ecr @@ -34,7 +34,7 @@ <div class="pure-u-1 pure-u-md-8-24 user-field"> <% if env.get? "user" %> <div class="pure-u-1-4"> - <a href="/toggle_theme" class="pure-menu-heading"> + <a href="/toggle_theme?referer=<%= env.get("current_page") %>" class="pure-menu-heading"> <% preferences = env.get("user").as(User).preferences %> <% if preferences.dark_mode %> <i class="icon ion-ios-sunny"></i> @@ -54,15 +54,15 @@ </a> </div> <div class="pure-u-1-4"> - <a href="/preferences" class="pure-menu-heading"> + <a href="/preferences?referer=<%= env.get("current_page") %>" class="pure-menu-heading"> <i class="icon ion-ios-cog"></i> </a> </div> <div class="pure-u-1-4"> - <a href="/signout" class="pure-menu-heading">Sign out</a> + <a href="/signout?referer=<%= env.get("current_page") %>" class="pure-menu-heading">Sign out</a> </div> <% else %> - <a href="/login" class="pure-menu-heading">Login</a> + <a href="/login?referer=<%= env.get("current_page") %>" class="pure-menu-heading">Login</a> <% end %> </div> </div> diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index cd757f5c..ca5e9372 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -72,7 +72,7 @@ function load_comments(target) { xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status == 200) { - body.innerHTML = xhr.response.content_html; + body.innerHTML = xhr.response.contentHtml; } else { body.innerHTML = fallback; } @@ -106,12 +106,12 @@ function get_reddit_comments() { <a target="_blank" href="https://reddit.com{permalink}">View more comments on Reddit</a> </b> </div> - <div>{content_html}</div> + <div>{contentHtml}</div> <hr>`.supplant({ title: xhr.response.title, permalink: xhr.response.permalink, - content_html: xhr.response.content_html + contentHtml: xhr.response.contentHtml }); } else { get_youtube_comments(); @@ -139,12 +139,13 @@ function get_youtube_comments() { <div> <h3> <a href="javascript:void(0)" onclick="toggle_comments(this)">[ - ]</a> - <a target="_blank" href="https://www.youtube.com/watch?v=<%= video.id %>">View more comments on YouTube</a> + View {commentCount} comments </h3> </div> - <div>{content_html}</div> + <div>{contentHtml}</div> <hr>`.supplant({ - content_html: xhr.response.content_html + contentHtml: xhr.response.contentHtml, + commentCount: commaSeparateNumber(xhr.response.commentCount) }); } else { comments = document.getElementById("comments"); @@ -160,6 +161,13 @@ function get_youtube_comments() { }; } +function commaSeparateNumber(val){ + while (/(\d+)(\d{3})/.test(val.toString())){ + val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2'); + } + return val; +} + String.prototype.supplant = function(o) { return this.replace(/{([^{}]*)}/g, function(a, b) { var r = o[b]; @@ -224,20 +232,20 @@ get_youtube_comments(); <% if user %> <% if subscriptions.includes? video.ucid %> <p> - <a href="/subscription_ajax?action_remove_subscriptions=1&c=<%= video.ucid %>"> + <a href="/subscription_ajax?action_remove_subscriptions=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>"> <b>Unsubscribe from <%= video.author %></b> </a> </p> <% else %> <p> - <a href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= video.ucid %>"> + <a href="/subscription_ajax?action_create_subscription_to_channel=1&c=<%= video.ucid %>&referer=<%= env.get("current_page") %>"> <b>Subscribe to <%= video.author %></b> </a> </p> <% end %> <% else %> <p> - <a href="/login"> + <a href="/login?referer=<%= env.get("current_page") %>"> <b>Login to subscribe to <%= video.author %></b> </a> </p> |
