Skip to content

A platform for community discussion. Free, open, simple.

FIX: Display a thumbnail for Youtube videos with the classic onebox (#35715)

Loïc Guitauteba1c8090c81
diff --git a/lib/onebox/engine/youtube_onebox.rb b/lib/onebox/engine/youtube_onebox.rb
index 0344b8a804e..10953c1706b 100644
--- a/lib/onebox/engine/youtube_onebox.rb
+++ b/lib/onebox/engine/youtube_onebox.rb
@@ -46,7 +46,7 @@ module Onebox
return
end
- @parse_embed_response = { image: image, title: title }
+ @parse_embed_response = { image:, title: }
end
def placeholder_html
@@ -63,6 +63,7 @@ module Onebox
def to_html
if video_id
<<-HTML
+ <img class="youtube-thumbnail onebox" style="display: none;" src="#{video_thumbnail_url}">
<iframe
src="#{self.class.embed_url(video_id)}?#{embed_params}"
width="#{WIDTH}"
@@ -192,6 +193,10 @@ module Onebox
rescue StandardError
{}
end
+
+ def video_thumbnail_url
+ "https://img.youtube.com/vi/#{video_id}/maxresdefault.jpg"
+ end
end
end
end
diff --git a/spec/lib/onebox/engine/youtube_onebox_spec.rb b/spec/lib/onebox/engine/youtube_onebox_spec.rb
index 8c2fcd82449..21c07ca4d8c 100644
--- a/spec/lib/onebox/engine/youtube_onebox_spec.rb
+++ b/spec/lib/onebox/engine/youtube_onebox_spec.rb
@@ -203,4 +203,8 @@ RSpec.describe Onebox::Engine::YoutubeOnebox do
preview.to match(/iframe/)
preview.to include("embed/eJemwqO0SDw")
end
+
+ it "generates a thumbnail for videos" do
+ expect(Onebox.preview("https://www.youtube.com/watch?v=21Lk4YiASMo").to_s).to match("<img")
+ end
end