From 277be45485688396fd0732e6a67c8231612f21dd Mon Sep 17 00:00:00 2001 From: ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:51:14 -0400 Subject: [PATCH] Use javascript to replace reply links, simplify community.js --- assets/js/comments.js | 10 ++-- assets/js/community.js | 63 ++++------------------ src/invidious/frontend/comments_youtube.cr | 11 +--- src/invidious/routes/api/v1/channels.cr | 10 +++- 4 files changed, 29 insertions(+), 65 deletions(-) diff --git a/assets/js/comments.js b/assets/js/comments.js index 35ffa96e..a26f1f7b 100644 --- a/assets/js/comments.js +++ b/assets/js/comments.js @@ -10,6 +10,10 @@ String.prototype.supplant = function (o) { }); }; +function updateReplyLinkHtml(contentHtml) { + return contentHtml.replace(/target="_blank" href="\/comment_viewer\?[^"]*"/g, 'href="javascript:void(0)"'); +}; + function toggle_comments(event) { var target = event.target; var body = target.parentNode.parentNode.parentNode.children[1]; @@ -93,7 +97,7 @@ function get_youtube_comments() {
{contentHtml}
\
' commentInnerHtml = commentInnerHtml.supplant({ - contentHtml: response.contentHtml, + contentHtml: updateReplyLinkHtml(response.contentHtml), redditComments: video_data.reddit_comments_text, commentsText: video_data.comments_text.supplant({ // toLocaleString correctly splits number with local thousands separator. e.g.: @@ -142,7 +146,7 @@ function get_youtube_replies(target, load_more, load_replies) { if (load_more) { body = body.parentNode.parentNode; body.removeChild(body.lastElementChild); - body.insertAdjacentHTML('beforeend', response.contentHtml); + body.insertAdjacentHTML('beforeend', updateReplyLinkHtml(response.contentHtml)); } else { body.removeChild(body.lastElementChild); @@ -157,7 +161,7 @@ function get_youtube_replies(target, load_more, load_replies) { a.textContent = video_data.hide_replies_text; var div = document.createElement('div'); - div.innerHTML = response.contentHtml; + div.innerHTML = updateReplyLinkHtml(response.contentHtml); body.appendChild(p); body.appendChild(div); diff --git a/assets/js/community.js b/assets/js/community.js index 32fe4ebc..4c708090 100644 --- a/assets/js/community.js +++ b/assets/js/community.js @@ -1,37 +1,16 @@ 'use strict'; var community_data = JSON.parse(document.getElementById('community_data').textContent); -function hide_youtube_replies(event) { - var target = event.target; +// first page of community posts are loaded without javascript so we need to update the Load more button +var initialLoadMore = document.querySelector('a[data-onclick="get_youtube_replies"]'); +initialLoadMore.setAttribute('href', 'javascript:void(0);'); +initialLoadMore.removeAttribute('target'); - var sub_text = target.getAttribute('data-inner-text'); - var inner_text = target.getAttribute('data-sub-text'); +function updateReplyLinkHtml(contentHtml) { + return contentHtml.replace(/target="_blank" href="\/comment_viewer\?[^"]*"/g, 'href="javascript:void(0)"'); +}; - var body = target.parentNode.parentNode.children[1]; - body.style.display = 'none'; - - target.innerHTML = sub_text; - target.onclick = show_youtube_replies; - target.setAttribute('data-inner-text', inner_text); - target.setAttribute('data-sub-text', sub_text); -} - -function show_youtube_replies(event) { - var target = event.target; - - var sub_text = target.getAttribute('data-inner-text'); - var inner_text = target.getAttribute('data-sub-text'); - - var body = target.parentNode.parentNode.children[1]; - body.style.display = ''; - - target.innerHTML = sub_text; - target.onclick = hide_youtube_replies; - target.setAttribute('data-inner-text', inner_text); - target.setAttribute('data-sub-text', sub_text); -} - -function get_youtube_replies(target, load_more) { +function get_youtube_replies(target) { var continuation = target.getAttribute('data-continuation'); var body = target.parentNode.parentNode; @@ -47,29 +26,9 @@ function get_youtube_replies(target, load_more) { helpers.xhr('GET', url, {}, { on200: function (response) { - if (load_more) { - body = body.parentNode.parentNode; - body.removeChild(body.lastElementChild); - body.innerHTML += response.contentHtml; - } else { - body.removeChild(body.lastElementChild); - - var p = document.createElement('p'); - var a = document.createElement('a'); - p.appendChild(a); - - a.href = 'javascript:void(0)'; - a.onclick = hide_youtube_replies; - a.setAttribute('data-sub-text', community_data.hide_replies_text); - a.setAttribute('data-inner-text', community_data.show_replies_text); - a.textContent = community_data.hide_replies_text; - - var div = document.createElement('div'); - div.innerHTML = response.contentHtml; - - body.appendChild(p); - body.appendChild(div); - } + body = body.parentNode.parentNode; + body.removeChild(body.lastElementChild); + body.insertAdjacentHTML('beforeend', updateReplyLinkHtml(response.contentHtml)); }, onNon200: function (xhr) { body.innerHTML = fallback; diff --git a/src/invidious/frontend/comments_youtube.cr b/src/invidious/frontend/comments_youtube.cr index 28688bd5..96a0b089 100644 --- a/src/invidious/frontend/comments_youtube.cr +++ b/src/invidious/frontend/comments_youtube.cr @@ -17,11 +17,7 @@ module Invidious::Frontend::Comments

- - #{replies_count_text}

@@ -205,10 +201,7 @@ module Invidious::Frontend::Comments

- - #{translate(locale, "Load more")}

diff --git a/src/invidious/routes/api/v1/channels.cr b/src/invidious/routes/api/v1/channels.cr index 9bc85859..95172096 100644 --- a/src/invidious/routes/api/v1/channels.cr +++ b/src/invidious/routes/api/v1/channels.cr @@ -386,10 +386,18 @@ module Invidious::Routes::API::V1::Channels format ||= "json" continuation = env.params.query["continuation"]? + ucid = env.params.query["ucid"]? + + if ucid.nil? + response = YoutubeAPI.resolve_url("https://www.youtube.com/post/#{id}") + return error_json(400, "Invalid post ID") if response["error"]? + ucid = response.dig("endpoint", "browseEndpoint", "browseId").as_s + else + ucid = ucid.to_s + end case continuation when nil, "" - ucid = env.params.query["ucid"] comments = Comments.fetch_community_post_comments(ucid, id) else comments = YoutubeAPI.browse(continuation: continuation)