2014-07-21 22:39:32 -04:00
|
|
|
# name: lazyYT
|
|
|
|
# about: Uses the lazyYT plugin to lazy load Youtube videos
|
2015-01-27 07:30:07 -05:00
|
|
|
# version: 1.0.1
|
2014-07-21 22:39:32 -04:00
|
|
|
# authors: Arpit Jalan
|
2015-02-06 18:08:57 -05:00
|
|
|
# url: https://github.com/discourse/discourse/tree/master/plugins/lazyYT
|
2014-07-21 22:39:32 -04:00
|
|
|
|
|
|
|
# javascript
|
|
|
|
register_asset "javascripts/lazyYT.js"
|
|
|
|
|
|
|
|
# stylesheet
|
|
|
|
register_asset "stylesheets/lazyYT.css"
|
2014-07-28 02:20:08 -04:00
|
|
|
register_asset "stylesheets/lazyYT_mobile.scss", :mobile
|
2014-07-21 22:39:32 -04:00
|
|
|
|
|
|
|
# freedom patch YouTube Onebox
|
|
|
|
class Onebox::Engine::YoutubeOnebox
|
|
|
|
include Onebox::Engine
|
|
|
|
|
|
|
|
def to_html
|
|
|
|
if video_id
|
2014-08-22 15:21:02 -04:00
|
|
|
# Put in the LazyYT div instead of the iframe
|
|
|
|
"<div class=\"lazyYT\" data-youtube-id=\"#{video_id}\" data-width=\"480\" data-height=\"270\" data-parameters=\"#{embed_params}\"></div>"
|
2014-07-21 22:39:32 -04:00
|
|
|
else
|
2014-08-22 15:21:02 -04:00
|
|
|
super
|
2014-07-21 22:39:32 -04:00
|
|
|
end
|
|
|
|
end
|
2014-07-26 09:11:21 -04:00
|
|
|
|
2014-07-21 22:39:32 -04:00
|
|
|
end
|
2014-08-21 06:54:05 -04:00
|
|
|
|
|
|
|
after_initialize do
|
|
|
|
|
|
|
|
Email::Styles.register_plugin_style do |fragment|
|
|
|
|
# YouTube onebox can't go in emails, so replace them with clickable links
|
|
|
|
fragment.css('.lazyYT').each do |i|
|
|
|
|
begin
|
|
|
|
src = "https://www.youtube.com/embed/#{i['data-youtube-id']}?autoplay=1&#{i['data-parameters']}"
|
|
|
|
src_uri = URI(src)
|
|
|
|
display_src = "https://#{src_uri.host}#{src_uri.path}"
|
|
|
|
i.replace "<p><a href='#{src_uri.to_s}'>#{display_src}</a><p>"
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
# If the URL is weird, remove it
|
|
|
|
i.remove
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|