2014-07-22 08:09:32 +05:30
# name: lazyYT
# about: Uses the lazyYT plugin to lazy load Youtube videos
2015-01-27 18:00:07 +05:30
# version: 1.0.1
2014-07-22 08:09:32 +05:30
# authors: Arpit Jalan
2015-02-06 18:08:57 -05:00
# url: https://github.com/discourse/discourse/tree/master/plugins/lazyYT
2014-07-22 08:09:32 +05:30
# javascript
register_asset " javascripts/lazyYT.js "
# stylesheet
register_asset " stylesheets/lazyYT.css "
2014-07-28 11:50:08 +05:30
register_asset " stylesheets/lazyYT_mobile.scss " , :mobile
2014-07-22 08:09:32 +05:30
# freedom patch YouTube Onebox
class Onebox :: Engine :: YoutubeOnebox
include Onebox :: Engine
2015-08-18 14:47:39 +05:30
alias_method :yt_onebox_to_html , :to_html
2014-07-22 08:09:32 +05:30
def to_html
2015-12-04 08:16:00 +05:30
if video_id && ! params [ 'list' ]
2015-05-26 18:03:50 +05:30
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 14:47:39 +05:30
2014-08-22 12:21:02 -07:00
# Put in the LazyYT div instead of the iframe
2016-01-30 12:32:48 +01:00
escaped_title = ERB :: Util . html_escape ( video_title )
" <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 } \" ></div> "
2014-07-22 08:09:32 +05:30
else
2015-08-18 14:47:39 +05:30
yt_onebox_to_html
2014-07-22 08:09:32 +05:30
end
end
2014-07-26 18:41:21 +05:30
2014-07-22 08:09:32 +05:30
end
2014-08-21 16:24:05 +05:30
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