2023-03-29 23:54:25 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "onebox"
|
|
|
|
|
|
|
|
class Onebox::Engine::YoutubeOnebox
|
|
|
|
include Onebox::Engine
|
|
|
|
alias_method :default_onebox_to_html, :to_html
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
if SiteSetting.lazy_videos_enabled && SiteSetting.lazy_youtube_enabled && video_id &&
|
|
|
|
!params["list"]
|
|
|
|
result = parse_embed_response
|
|
|
|
result ||= get_opengraph.data
|
|
|
|
|
|
|
|
thumbnail_url = "https://img.youtube.com/vi/#{video_id}/maxresdefault.jpg"
|
|
|
|
|
|
|
|
begin
|
|
|
|
Onebox::Helpers.fetch_response(thumbnail_url)
|
|
|
|
rescue StandardError
|
|
|
|
thumbnail_url = result[:image]
|
|
|
|
end
|
|
|
|
|
|
|
|
escaped_title = ERB::Util.html_escape(video_title)
|
2023-07-03 21:39:37 +08:00
|
|
|
escaped_start_time = ERB::Util.html_escape(params["t"])
|
|
|
|
t_param = "&t=#{escaped_start_time}" if escaped_start_time.present?
|
2023-03-29 23:54:25 +08:00
|
|
|
|
|
|
|
<<~HTML
|
|
|
|
<div class="youtube-onebox lazy-video-container"
|
|
|
|
data-video-id="#{video_id}"
|
|
|
|
data-video-title="#{escaped_title}"
|
2023-04-28 21:46:27 +08:00
|
|
|
data-video-start-time="#{escaped_start_time}"
|
2023-03-29 23:54:25 +08:00
|
|
|
data-provider-name="youtube">
|
2023-07-03 21:39:37 +08:00
|
|
|
<a href="https://www.youtube.com/watch?v=#{video_id}#{t_param}" target="_blank">
|
2023-03-29 23:54:25 +08:00
|
|
|
<img class="youtube-thumbnail"
|
|
|
|
src="#{thumbnail_url}"
|
|
|
|
title="#{escaped_title}">
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
HTML
|
|
|
|
else
|
|
|
|
default_onebox_to_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|