From 6a02dd88428491a4aad1ec80ed586826316cdf35 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sun, 1 May 2022 17:42:53 +0200 Subject: [PATCH 1/3] Fix broken hashtag links --- src/invidious/comments.cr | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index c6e7fd17..71c16eb4 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -602,9 +602,11 @@ def content_to_comment_html(content) text = %(#{"youtube.com/watch?v=#{video_id}"}) end elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s - if text.starts_with?(/\s?@/) - # Handle "pings" in comments differently - # See: https://github.com/iv-org/invidious/issues/3038 + if text.starts_with?(/\s?[@#]/) + # Handle "pings" in comments and hasthags differently + # See: + # - https://github.com/iv-org/invidious/issues/3038 + # - https://github.com/iv-org/invidious/issues/3062 text = %(#{text}) else text = %(#{reduce_uri(url)}) From e690e166b0df203887715a0ce5c160cdb9f34054 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sun, 1 May 2022 18:48:08 +0200 Subject: [PATCH 2/3] Fix javascript:void(0) instead of youtu.be links --- src/invidious/comments.cr | 27 +++++++++++++++++--------- src/invidious/videos.cr | 2 +- src/invidious/yt_backend/extractors.cr | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index 71c16eb4..8e0d8a96 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -143,7 +143,7 @@ def fetch_youtube_comments(id, cursor, format, locale, thin_mode, region, sort_b node_comment = node["commentRenderer"] end - content_html = node_comment["contentText"]?.try { |t| parse_content(t) } || "" + content_html = node_comment["contentText"]?.try { |t| parse_content(t, id) } || "" author = node_comment["authorText"]?.try &.["simpleText"]? || "" json.field "author", author @@ -554,12 +554,12 @@ def fill_links(html, scheme, host) return html.to_xml(options: XML::SaveOptions::NO_DECL) end -def parse_content(content : JSON::Any) : String +def parse_content(content : JSON::Any, video_id : String? = "") : String content["simpleText"]?.try &.as_s.rchop('\ufeff').try { |b| HTML.escape(b) }.to_s || - content["runs"]?.try &.as_a.try { |r| content_to_comment_html(r).try &.to_s.gsub("\n", "
") } || "" + content["runs"]?.try &.as_a.try { |r| content_to_comment_html(r, video_id).try &.to_s.gsub("\n", "
") } || "" end -def content_to_comment_html(content) +def content_to_comment_html(content, video_id : String? = "") comment_html = content.map do |run| text = HTML.escape(run["text"].as_s) @@ -593,13 +593,22 @@ def content_to_comment_html(content) text = %(#{reduce_uri(displayed_url)}) elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]? - length_seconds = watch_endpoint["startTimeSeconds"]? - video_id = watch_endpoint["videoId"].as_s + start_time = watch_endpoint["startTimeSeconds"]?.try &.as_i + link_video_id = watch_endpoint["videoId"].as_s - if length_seconds && length_seconds.as_i >= 0 - text = %(#{text}) + url = "/watch?v=#{link_video_id}" + url += "&t=#{start_time}" if !start_time.nil? + + # If the current video ID (passed through from the caller function) + # is the same as the video ID in the link, add HTML attributes for + # the JS handler function that bypasses page reload. + # + # See: https://github.com/iv-org/invidious/issues/3063 + if link_video_id == video_id + start_time ||= 0 + text = %(#{reduce_uri(text)}) else - text = %(#{"youtube.com/watch?v=#{video_id}"}) + text = %(#{text}) end elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s if text.starts_with?(/\s?[@#]/) diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 27c2b6d1..c007a07b 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -1039,7 +1039,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_ # Description description_html = video_secondary_renderer.try &.dig?("description", "runs") - .try &.as_a.try { |t| content_to_comment_html(t) } + .try &.as_a.try { |t| content_to_comment_html(t, video_id) } params["descriptionHtml"] = JSON::Any.new(description_html || "

") diff --git a/src/invidious/yt_backend/extractors.cr b/src/invidious/yt_backend/extractors.cr index ce39bc28..f6229a9b 100644 --- a/src/invidious/yt_backend/extractors.cr +++ b/src/invidious/yt_backend/extractors.cr @@ -69,7 +69,7 @@ private module Parsers # TODO change default value to nil and typical encoding type to tuple storing type (watchers, views, etc) # and count view_count = item_contents.dig?("viewCountText", "simpleText").try &.as_s.gsub(/\D+/, "").to_i64? || 0_i64 - description_html = item_contents["descriptionSnippet"]?.try { |t| parse_content(t) } || "" + description_html = item_contents["descriptionSnippet"]?.try { |t| parse_content(t, video_id) } || "" # The length information generally exist in "lengthText". However, the info can sometimes # be retrieved from "thumbnailOverlays" (e.g when the video is a "shorts" one). From b0342b744956809851399a3a5fa735a7b7f4f5ae Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sun, 1 May 2022 19:08:11 +0200 Subject: [PATCH 3/3] Other minor fixes --- assets/js/handlers.js | 17 ++++++++++------- src/invidious/comments.cr | 24 +++++++++--------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/assets/js/handlers.js b/assets/js/handlers.js index 3224e668..f6617b60 100644 --- a/assets/js/handlers.js +++ b/assets/js/handlers.js @@ -13,20 +13,23 @@ // For dynamically inserted elements document.addEventListener('click', function (e) { if (!e || !e.target) { return; } - e = e.target; - var handler_name = e.getAttribute('data-onclick'); + + var t = e.target; + var handler_name = t.getAttribute('data-onclick'); + switch (handler_name) { case 'jump_to_time': - var time = e.getAttribute('data-jump-time'); + e.preventDefault(); + var time = t.getAttribute('data-jump-time'); player.currentTime(time); break; case 'get_youtube_replies': - var load_more = e.getAttribute('data-load-more') !== null; - var load_replies = e.getAttribute('data-load-replies') !== null; - get_youtube_replies(e, load_more, load_replies); + var load_more = t.getAttribute('data-load-more') !== null; + var load_replies = t.getAttribute('data-load-replies') !== null; + get_youtube_replies(t, load_more, load_replies); break; case 'toggle_parent': - toggle_parent(e); + toggle_parent(t); break; default: break; diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index 8e0d8a96..91ea8607 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -560,30 +560,21 @@ def parse_content(content : JSON::Any, video_id : String? = "") : String end def content_to_comment_html(content, video_id : String? = "") - comment_html = content.map do |run| + html_array = content.map do |run| text = HTML.escape(run["text"].as_s) - if run["bold"]? - text = "#{text}" - end - - if run["italics"]? - text = "#{text}" - end - if run["navigationEndpoint"]? if url = run["navigationEndpoint"]["urlEndpoint"]?.try &.["url"].as_s url = URI.parse(url) - displayed_url = url + displayed_url = text if url.host == "youtu.be" url = "/watch?v=#{url.request_target.lstrip('/')}" - displayed_url = "youtube.com#{url}" elsif url.host.nil? || url.host.not_nil!.ends_with?("youtube.com") if url.path == "/redirect" # Sometimes, links can be corrupted (why?) so make sure to fallback # nicely. See https://github.com/iv-org/invidious/issues/2682 - url = HTTP::Params.parse(url.query.not_nil!)["q"]? || "" + url = url.query_params["q"]? || "" displayed_url = url else url = url.request_target @@ -623,10 +614,13 @@ def content_to_comment_html(content, video_id : String? = "") end end - text - end.join("").delete('\ufeff') + text = "#{text}" if run["bold"]? + text = "#{text}" if run["italics"]? - return comment_html + text + end + + return html_array.join("").delete('\ufeff') end def produce_comment_continuation(video_id, cursor = "", sort_by = "top")