diff --git a/assets/js/comments.js b/assets/js/comments.js index 35ffa96e..fc0d93d7 100644 --- a/assets/js/comments.js +++ b/assets/js/comments.js @@ -10,6 +10,14 @@ String.prototype.supplant = function (o) { }); }; +function updateReplyLinks() { + document.querySelectorAll("a[href^='/comment_viewer']").forEach(function (replyLink) { + replyLink.setAttribute("href", "javascript:void(0)"); + replyLink.removeAttribute("target"); + }); +} +updateReplyLinks(); + function toggle_comments(event) { var target = event.target; var body = target.parentNode.parentNode.parentNode.children[1]; @@ -104,6 +112,7 @@ function get_youtube_comments() { }) }); comments.innerHTML = commentInnerHtml; + updateReplyLinks(); comments.children[0].children[0].children[0].onclick = toggle_comments; if (video_data.support_reddit) { comments.children[0].children[1].children[0].onclick = swap_comments; @@ -143,6 +152,7 @@ function get_youtube_replies(target, load_more, load_replies) { body = body.parentNode.parentNode; body.removeChild(body.lastElementChild); body.insertAdjacentHTML('beforeend', response.contentHtml); + updateReplyLinks(); } else { body.removeChild(body.lastElementChild); @@ -161,6 +171,7 @@ function get_youtube_replies(target, load_more, load_replies) { body.appendChild(p); body.appendChild(div); + updateReplyLinks(); } }, onNon200: function (xhr) { diff --git a/assets/js/community.js b/assets/js/community.js index 32fe4ebc..347c5a9a 100644 --- a/assets/js/community.js +++ b/assets/js/community.js @@ -1,37 +1,20 @@ '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'); - - 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 updateReplyLinks() { + document.querySelectorAll("a[href^='/comment_viewer']").forEach(function (replyLink) { + replyLink.setAttribute("href", "javascript:void(0)"); + replyLink.removeAttribute("target"); + }); } +updateReplyLinks(); -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 +30,10 @@ 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', response.contentHtml); + updateReplyLinks(); }, onNon200: function (xhr) { body.innerHTML = fallback; diff --git a/src/invidious/channels/community.cr b/src/invidious/channels/community.cr index 49ffd990..bdfbc27c 100644 --- a/src/invidious/channels/community.cr +++ b/src/invidious/channels/community.cr @@ -279,7 +279,7 @@ def extract_channel_community(items, *, ucid, locale, format, thin_mode, is_sing if format == "html" response = JSON.parse(response) - content_html = IV::Frontend::Comments.template_youtube(response, locale, thin_mode) + content_html = IV::Frontend::Comments.template_youtube(response, locale, thin_mode, ucid, "community") response = JSON.build do |json| json.object do diff --git a/src/invidious/comments/youtube.cr b/src/invidious/comments/youtube.cr index 0716fcde..857c0027 100644 --- a/src/invidious/comments/youtube.cr +++ b/src/invidious/comments/youtube.cr @@ -57,7 +57,7 @@ module Invidious::Comments return initial_data end - def parse_youtube(id, response, format, locale, thin_mode, sort_by = "top", is_post = false) + def parse_youtube(id, response, format, locale, thin_mode, sort_by = "top", type = "video", ucid = nil) contents = nil if on_response_received_endpoints = response["onResponseReceivedEndpoints"]? @@ -115,7 +115,11 @@ module Invidious::Comments json.field "commentCount", comment_count end - if is_post + if !ucid.nil? + json.field "authorId", ucid + end + + if type == "post" json.field "postId", id else json.field "videoId", id @@ -302,7 +306,8 @@ module Invidious::Comments if format == "html" response = JSON.parse(response) - content_html = Frontend::Comments.template_youtube(response, locale, thin_mode) + content_html = Frontend::Comments.template_youtube(response, locale, thin_mode, id, type) + response = JSON.build do |json| json.object do json.field "contentHtml", content_html diff --git a/src/invidious/frontend/comments_youtube.cr b/src/invidious/frontend/comments_youtube.cr index a0e1d783..2d337d9b 100644 --- a/src/invidious/frontend/comments_youtube.cr +++ b/src/invidious/frontend/comments_youtube.cr @@ -1,7 +1,7 @@ module Invidious::Frontend::Comments extend self - def template_youtube(comments, locale, thin_mode, is_replies = false) + def template_youtube(comments, locale, thin_mode, id, type = "video", is_replies = false) String.build do |html| root = comments["comments"].as_a root.each do |child| @@ -13,17 +13,17 @@ module Invidious::Frontend::Comments ) replies_html = <<-END_HTML -