mirror of
https://github.com/iv-org/invidious.git
synced 2025-06-30 02:28:30 +00:00
feat: replace external links in description and comments to confirm before leaving
This commit is contained in:
parent
e8511443f1
commit
ecfb9fed5a
@ -73,4 +73,30 @@ module Invidious::Comments
|
|||||||
|
|
||||||
return html.to_xml(options: XML::SaveOptions::NO_DECL)
|
return html.to_xml(options: XML::SaveOptions::NO_DECL)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def replace_external_links(html)
|
||||||
|
# Check if the document is empty
|
||||||
|
# Prevents edge-case bug with Reddit comments, see issue #3115
|
||||||
|
if html.nil? || html.empty?
|
||||||
|
return html
|
||||||
|
end
|
||||||
|
|
||||||
|
html = XML.parse_html(html)
|
||||||
|
|
||||||
|
html.xpath_nodes(%q(//a)).each do |anchor|
|
||||||
|
url = URI.parse(anchor["href"])
|
||||||
|
|
||||||
|
if !url.host.nil? && !url.host.not_nil!.ends_with?("youtube.com") && !url.host.not_nil!.ends_with?("youtu.be")
|
||||||
|
confirm_leave = "/confirm_leave?link=#{URI.encode_path(url.to_s)}"
|
||||||
|
anchor["href"] = confirm_leave
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
html = html.xpath_node(%q(//body)).not_nil!
|
||||||
|
if node = html.xpath_node(%q(./p))
|
||||||
|
html = node
|
||||||
|
end
|
||||||
|
|
||||||
|
return html.to_xml(options: XML::SaveOptions::NO_DECL)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -303,6 +303,7 @@ module Invidious::Comments
|
|||||||
if format == "html"
|
if format == "html"
|
||||||
response = JSON.parse(response)
|
response = JSON.parse(response)
|
||||||
content_html = Frontend::Comments.template_youtube(response, locale, thin_mode)
|
content_html = Frontend::Comments.template_youtube(response, locale, thin_mode)
|
||||||
|
content_html = Comments.replace_external_links(content_html)
|
||||||
response = JSON.build do |json|
|
response = JSON.build do |json|
|
||||||
json.object do
|
json.object do
|
||||||
json.field "contentHtml", content_html
|
json.field "contentHtml", content_html
|
||||||
|
@ -316,6 +316,9 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
|
|||||||
|
|
||||||
description_html = parse_description(video_secondary_renderer.try &.dig?("attributedDescription"), video_id)
|
description_html = parse_description(video_secondary_renderer.try &.dig?("attributedDescription"), video_id)
|
||||||
|
|
||||||
|
# use comments link_utils to replace external links with the confirmation page
|
||||||
|
description_html = Invidious::Comments.replace_external_links(description_html)
|
||||||
|
|
||||||
# Video metadata
|
# Video metadata
|
||||||
|
|
||||||
metadata = video_secondary_renderer
|
metadata = video_secondary_renderer
|
||||||
|
Loading…
Reference in New Issue
Block a user