2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-22 13:37:06 +08:00
|
|
|
# name: lazy-yt
|
2014-07-22 10:39:32 +08:00
|
|
|
# about: Uses the lazyYT plugin to lazy load Youtube videos
|
2015-01-27 20:30:07 +08:00
|
|
|
# version: 1.0.1
|
2014-07-22 10:39:32 +08:00
|
|
|
# authors: Arpit Jalan
|
2019-08-21 17:02:55 +08:00
|
|
|
# url: https://github.com/discourse/discourse/tree/master/plugins/lazy-yt
|
2014-07-22 10:39:32 +08:00
|
|
|
|
2018-05-15 23:25:43 +08:00
|
|
|
hide_plugin if self.respond_to?(:hide_plugin)
|
|
|
|
|
2014-07-22 10:39:32 +08:00
|
|
|
# javascript
|
|
|
|
register_asset "javascripts/lazyYT.js"
|
|
|
|
|
|
|
|
# stylesheet
|
|
|
|
register_asset "stylesheets/lazyYT.css"
|
2014-07-28 14:20:08 +08:00
|
|
|
register_asset "stylesheets/lazyYT_mobile.scss", :mobile
|
2014-07-22 10:39:32 +08:00
|
|
|
|
|
|
|
# freedom patch YouTube Onebox
|
|
|
|
class Onebox::Engine::YoutubeOnebox
|
|
|
|
include Onebox::Engine
|
2015-08-18 17:17:39 +08:00
|
|
|
alias_method :yt_onebox_to_html, :to_html
|
2014-07-22 10:39:32 +08:00
|
|
|
|
|
|
|
def to_html
|
2015-12-04 10:46:00 +08:00
|
|
|
if video_id && !params['list']
|
2015-05-26 20:33:50 +08:00
|
|
|
video_width = (params['width'] && params['width'].to_i <= 695) ? params['width'] : 480 # embed width
|
|
|
|
video_height = (params['height'] && params['height'].to_i <= 500) ? params['height'] : 270 # embed height
|
2015-08-18 17:17:39 +08:00
|
|
|
|
2014-08-23 03:21:02 +08:00
|
|
|
# Put in the LazyYT div instead of the iframe
|
2016-01-30 19:32:48 +08:00
|
|
|
escaped_title = ERB::Util.html_escape(video_title)
|
2020-04-30 02:40:21 +08:00
|
|
|
|
|
|
|
<<~EOF
|
|
|
|
<div class="lazyYT"
|
|
|
|
data-youtube-id="#{video_id}"
|
|
|
|
data-youtube-title="#{escaped_title}"
|
|
|
|
data-width="#{video_width}"
|
|
|
|
data-height="#{video_height}"
|
|
|
|
data-parameters="#{embed_params}">
|
|
|
|
<a href="https://www.youtube.com/watch?v=#{video_id}" target="_blank">
|
|
|
|
<img src="https://img.youtube.com/vi/#{video_id}/hqdefault.jpg" width="#{video_width}" height="#{video_height}" title="#{escaped_title}">
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
EOF
|
2014-07-22 10:39:32 +08:00
|
|
|
else
|
2015-08-18 17:17:39 +08:00
|
|
|
yt_onebox_to_html
|
2014-07-22 10:39:32 +08:00
|
|
|
end
|
|
|
|
end
|
2014-07-26 21:11:21 +08:00
|
|
|
|
2014-07-22 10:39:32 +08:00
|
|
|
end
|
2014-08-21 18:54:05 +08:00
|
|
|
|
|
|
|
after_initialize do
|
|
|
|
|
2018-05-10 01:39:17 +08:00
|
|
|
on(:reduce_cooked) do |fragment|
|
|
|
|
fragment.css(".lazyYT").each do |yt|
|
2014-08-21 18:54:05 +08:00
|
|
|
begin
|
2018-05-10 01:39:17 +08:00
|
|
|
youtube_id = yt["data-youtube-id"]
|
|
|
|
parameters = yt["data-parameters"]
|
|
|
|
uri = URI("https://www.youtube.com/embed/#{youtube_id}?autoplay=1&#{parameters}")
|
|
|
|
yt.replace %{<p><a href="#{uri.to_s}">https://#{uri.host}#{uri.path}</a></p>}
|
2014-08-21 18:54:05 +08:00
|
|
|
rescue URI::InvalidURIError
|
2018-05-10 01:39:17 +08:00
|
|
|
# remove any invalid/weird URIs
|
|
|
|
yt.remove
|
2014-08-21 18:54:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-10 01:39:17 +08:00
|
|
|
|
2014-08-21 18:54:05 +08:00
|
|
|
end
|