From ab2e7a06006374a3e0d4b686095618e1fdf571a7 Mon Sep 17 00:00:00 2001 From: syeopite Date: Tue, 8 Aug 2023 21:49:06 -0700 Subject: [PATCH 01/15] Add error message when comments are disabled --- assets/css/default.css | 4 ++++ assets/js/comments.js | 4 ++++ assets/js/watch.js | 5 +---- locales/en-US.json | 2 ++ src/invidious/videos.cr | 5 +++++ src/invidious/videos/parser.cr | 16 ++++++++++++++++ src/invidious/views/watch.ecr | 27 ++++++++++++++++++++------- 7 files changed, 52 insertions(+), 11 deletions(-) diff --git a/assets/css/default.css b/assets/css/default.css index 00881253..0dbed610 100644 --- a/assets/css/default.css +++ b/assets/css/default.css @@ -797,3 +797,7 @@ h1, h2, h3, h4, h5, p, #download_widget { width: 100%; } + +#comments-turned-off-on-video-message > p, #comments-disabled-message > p { + text-align: center; +} diff --git a/assets/js/comments.js b/assets/js/comments.js index 35ffa96e..848d36fb 100644 --- a/assets/js/comments.js +++ b/assets/js/comments.js @@ -53,6 +53,10 @@ function show_youtube_replies(event) { } function get_youtube_comments() { + if (!video_data.comments_enabled) { + return; + }; + var comments = document.getElementById('comments'); var fallback = comments.innerHTML; diff --git a/assets/js/watch.js b/assets/js/watch.js index 26ad138f..31c09785 100644 --- a/assets/js/watch.js +++ b/assets/js/watch.js @@ -186,8 +186,5 @@ addEventListener('load', function (e) { get_youtube_comments(); } else if (video_data.params.comments[1] === 'reddit') { get_reddit_comments(); - } else { - var comments = document.getElementById('comments'); - comments.innerHTML = ''; - } + } }); diff --git a/locales/en-US.json b/locales/en-US.json index a9f78165..3f314788 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -213,6 +213,8 @@ "View Reddit comments": "View Reddit comments", "Hide replies": "Hide replies", "Show replies": "Show replies", + "youtube_comments_disabled_text": "Comments are turned off on this video", + "invidious_comments_disabled_text": "Comments are hidden as per configuration", "Incorrect password": "Incorrect password", "Wrong answer": "Wrong answer", "Erroneous CAPTCHA": "Erroneous CAPTCHA", diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index a8f02056..fd3ca677 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -272,6 +272,11 @@ struct Video } end + # Returns true if comments are enabled on the video + def comments? + return info["commentsEnabled"].as_bool + end + # Macros defining getters/setters for various types of data private macro getset_string(name) diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index 77520dbe..eba977f6 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -378,6 +378,20 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any .try &.as_s.split(" ", 2)[0] end + # Comments enabled? + comments_enabled = true + + # When comments are enabled the primary results should contain either the comments-entry-point + # or comment-item-section section + if primary_results + section = primary_results.as_a.find { |s| s.dig?("itemSectionRenderer", "sectionIdentifier") == "comment-item-section" } + + # messageRenderer should say "Comments are turned off." + if section && section.dig?("itemSectionRenderer", "contents", 0, "messageRenderer") + comments_enabled = false + end + end + # Return data if live_now @@ -422,6 +436,8 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any "authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""), "authorVerified" => JSON::Any.new(author_verified || false), "subCountText" => JSON::Any.new(subs_text || "-"), + + "commentsEnabled" => JSON::Any.new(comments_enabled), } return params diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 07474896..f4eb4203 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -63,6 +63,7 @@ we're going to need to do it here in order to allow for translations. "preferences" => preferences, "premiere_timestamp" => video.premiere_timestamp.try &.to_unix, "vr" => video.is_vr, + "comments_enabled" => video.comments?, "projection_type" => video.projection_type, "local_disabled" => CONFIG.disabled?("local"), "support_reddit" => true @@ -289,14 +290,26 @@ we're going to need to do it here in order to allow for translations. <% end %>
- <% if nojs %> - <%= comment_html %> + <% if (params.comments <=> ["", ""]) == 0 %> +
+

<%=translate(locale, "invidious_comments_disabled_text")%>

+
<% else %> - + <% if video.comments? %> + <% if nojs %> + <%= comment_html %> + <% else %> + + <% end %> + <% else %> +
+

<%=translate(locale, "youtube_comments_disabled_text")%>

+
+ <% end %> <% end %>
From 2e590ee67c20e01232ff12354f22c49844420420 Mon Sep 17 00:00:00 2001 From: syeopite Date: Tue, 8 Aug 2023 22:41:47 -0700 Subject: [PATCH 02/15] Add btn to try reddit comments when yt is disabled --- assets/js/comments.js | 23 ++++++++++++++++++++++- assets/js/watch.js | 6 ++++++ locales/en-US.json | 1 + src/invidious/views/watch.ecr | 18 +++++++++++++++++- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/assets/js/comments.js b/assets/js/comments.js index 848d36fb..73be4bfa 100644 --- a/assets/js/comments.js +++ b/assets/js/comments.js @@ -72,7 +72,28 @@ function get_youtube_comments() { url += '&ucid=' + video_data.ucid } - var onNon200 = function (xhr) { comments.innerHTML = fallback; }; + var onNon200 = function (xhr) { + if (!video_data.comments_enabled) { + comments.innerHTML = ` +
+

${video_data.youtube_comments_disabled_text}

+ +

+ + + ${video_data.youtube_comments_disabled_try_reddit} + + +

+
`; + + document.getElementById("try-reddit-comments-link").onclick = swap_comments + } else { + comments.innerHTML = fallback; + } + + }; + if (video_data.params.comments[1] === 'youtube') onNon200 = function (xhr) {}; diff --git a/assets/js/watch.js b/assets/js/watch.js index 31c09785..8eaf0654 100644 --- a/assets/js/watch.js +++ b/assets/js/watch.js @@ -178,6 +178,10 @@ addEventListener('load', function (e) { if (video_data.plid) get_playlist(video_data.plid); + if (!video_data.comments_enabled && video_data.params.comments.includes("youtube")) { + return; + } + if (video_data.params.comments[0] === 'youtube') { get_youtube_comments(); } else if (video_data.params.comments[0] === 'reddit') { @@ -188,3 +192,5 @@ addEventListener('load', function (e) { get_reddit_comments(); } }); + +document.getElementById("try-reddit-comments-link").onclick = swap_comments \ No newline at end of file diff --git a/locales/en-US.json b/locales/en-US.json index 3f314788..82b1b13c 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -214,6 +214,7 @@ "Hide replies": "Hide replies", "Show replies": "Show replies", "youtube_comments_disabled_text": "Comments are turned off on this video", + "youtube_comments_disabled_try_reddit": "Try reddit comments?", "invidious_comments_disabled_text": "Comments are hidden as per configuration", "Incorrect password": "Incorrect password", "Wrong answer": "Wrong answer", diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index f4eb4203..9d4c70fe 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -42,6 +42,12 @@ we're going to need to do it here in order to allow for translations. content: "<%= translate(locale, "Show less") %>" } + +<% + # Disable the try reddit link if javascript is disabled +%> + + <% end %>