Merge pull request #3076 from SamantazFox/fix-3062-3063

Fix regressions of #2936
This commit is contained in:
Samantaz Fox 2022-05-04 22:29:10 +02:00 committed by GitHub
commit 059796c60d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 36 deletions

View File

@ -13,20 +13,23 @@
// For dynamically inserted elements // For dynamically inserted elements
document.addEventListener('click', function (e) { document.addEventListener('click', function (e) {
if (!e || !e.target) { return; } 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) { switch (handler_name) {
case 'jump_to_time': case 'jump_to_time':
var time = e.getAttribute('data-jump-time'); e.preventDefault();
var time = t.getAttribute('data-jump-time');
player.currentTime(time); player.currentTime(time);
break; break;
case 'get_youtube_replies': case 'get_youtube_replies':
var load_more = e.getAttribute('data-load-more') !== null; var load_more = t.getAttribute('data-load-more') !== null;
var load_replies = e.getAttribute('data-load-replies') !== null; var load_replies = t.getAttribute('data-load-replies') !== null;
get_youtube_replies(e, load_more, load_replies); get_youtube_replies(t, load_more, load_replies);
break; break;
case 'toggle_parent': case 'toggle_parent':
toggle_parent(e); toggle_parent(t);
break; break;
default: default:
break; break;

View File

@ -143,7 +143,7 @@ def fetch_youtube_comments(id, cursor, format, locale, thin_mode, region, sort_b
node_comment = node["commentRenderer"] node_comment = node["commentRenderer"]
end 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"]? || "" author = node_comment["authorText"]?.try &.["simpleText"]? || ""
json.field "verified", (node_comment["authorCommentBadge"]? != nil) json.field "verified", (node_comment["authorCommentBadge"]? != nil)
@ -560,36 +560,27 @@ def fill_links(html, scheme, host)
return html.to_xml(options: XML::SaveOptions::NO_DECL) return html.to_xml(options: XML::SaveOptions::NO_DECL)
end 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["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", "<br>") } || "" content["runs"]?.try &.as_a.try { |r| content_to_comment_html(r, video_id).try &.to_s.gsub("\n", "<br>") } || ""
end end
def content_to_comment_html(content) 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) text = HTML.escape(run["text"].as_s)
if run["bold"]?
text = "<b>#{text}</b>"
end
if run["italics"]?
text = "<i>#{text}</i>"
end
if run["navigationEndpoint"]? if run["navigationEndpoint"]?
if url = run["navigationEndpoint"]["urlEndpoint"]?.try &.["url"].as_s if url = run["navigationEndpoint"]["urlEndpoint"]?.try &.["url"].as_s
url = URI.parse(url) url = URI.parse(url)
displayed_url = url displayed_url = text
if url.host == "youtu.be" if url.host == "youtu.be"
url = "/watch?v=#{url.request_target.lstrip('/')}" url = "/watch?v=#{url.request_target.lstrip('/')}"
displayed_url = "youtube.com#{url}"
elsif url.host.nil? || url.host.not_nil!.ends_with?("youtube.com") elsif url.host.nil? || url.host.not_nil!.ends_with?("youtube.com")
if url.path == "/redirect" if url.path == "/redirect"
# Sometimes, links can be corrupted (why?) so make sure to fallback # Sometimes, links can be corrupted (why?) so make sure to fallback
# nicely. See https://github.com/iv-org/invidious/issues/2682 # 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 displayed_url = url
else else
url = url.request_target url = url.request_target
@ -599,18 +590,29 @@ def content_to_comment_html(content)
text = %(<a href="#{url}">#{reduce_uri(displayed_url)}</a>) text = %(<a href="#{url}">#{reduce_uri(displayed_url)}</a>)
elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]? elsif watch_endpoint = run["navigationEndpoint"]["watchEndpoint"]?
length_seconds = watch_endpoint["startTimeSeconds"]? start_time = watch_endpoint["startTimeSeconds"]?.try &.as_i
video_id = watch_endpoint["videoId"].as_s link_video_id = watch_endpoint["videoId"].as_s
if length_seconds && length_seconds.as_i >= 0 url = "/watch?v=#{link_video_id}"
text = %(<a href="javascript:void(0)" data-onclick="jump_to_time" data-jump-time="#{length_seconds}">#{text}</a>) 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 = %(<a href="#{url}" data-onclick="jump_to_time" data-jump-time="#{start_time}">#{reduce_uri(text)}</a>)
else else
text = %(<a href="/watch?v=#{video_id}">#{"youtube.com/watch?v=#{video_id}"}</a>) text = %(<a href="#{url}">#{text}</a>)
end end
elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s elsif url = run.dig?("navigationEndpoint", "commandMetadata", "webCommandMetadata", "url").try &.as_s
if text.starts_with?(/\s?@/) if text.starts_with?(/\s?[@#]/)
# Handle "pings" in comments differently # Handle "pings" in comments and hasthags differently
# See: https://github.com/iv-org/invidious/issues/3038 # See:
# - https://github.com/iv-org/invidious/issues/3038
# - https://github.com/iv-org/invidious/issues/3062
text = %(<a href="#{url}">#{text}</a>) text = %(<a href="#{url}">#{text}</a>)
else else
text = %(<a href="#{url}">#{reduce_uri(url)}</a>) text = %(<a href="#{url}">#{reduce_uri(url)}</a>)
@ -618,10 +620,13 @@ def content_to_comment_html(content)
end end
end end
text text = "<b>#{text}</b>" if run["bold"]?
end.join("").delete('\ufeff') text = "<i>#{text}</i>" if run["italics"]?
return comment_html text
end
return html_array.join("").delete('\ufeff')
end end
def produce_comment_continuation(video_id, cursor = "", sort_by = "top") def produce_comment_continuation(video_id, cursor = "", sort_by = "top")

View File

@ -1054,7 +1054,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_
# Description # Description
description_html = video_secondary_renderer.try &.dig?("description", "runs") 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 || "<p></p>") params["descriptionHtml"] = JSON::Any.new(description_html || "<p></p>")

View File

@ -69,7 +69,7 @@ private module Parsers
# TODO change default value to nil and typical encoding type to tuple storing type (watchers, views, etc) # TODO change default value to nil and typical encoding type to tuple storing type (watchers, views, etc)
# and count # and count
view_count = item_contents.dig?("viewCountText", "simpleText").try &.as_s.gsub(/\D+/, "").to_i64? || 0_i64 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 # 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). # be retrieved from "thumbnailOverlays" (e.g when the video is a "shorts" one).