2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-22 00:37:06 -05:00
|
|
|
# name: lazy-yt
|
2014-07-21 21:39:32 -05:00
|
|
|
# about: Uses the lazyYT plugin to lazy load Youtube videos
|
2015-01-27 06:30:07 -06:00
|
|
|
# version: 1.0.1
|
2014-07-21 21:39:32 -05:00
|
|
|
# authors: Arpit Jalan
|
2021-07-19 10:35:47 -05:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/lazy-yt
|
2014-07-21 21:39:32 -05:00
|
|
|
|
2018-05-15 10:25:43 -05:00
|
|
|
hide_plugin if self.respond_to?(:hide_plugin)
|
2022-12-14 08:52:17 -06:00
|
|
|
enabled_site_setting :lazy_yt_enabled
|
2018-05-15 10:25:43 -05:00
|
|
|
|
2021-05-26 04:41:35 -05:00
|
|
|
require "onebox"
|
|
|
|
|
2014-07-21 21:39:32 -05:00
|
|
|
# stylesheet
|
|
|
|
register_asset "stylesheets/lazyYT.css"
|
2014-07-28 01:20:08 -05:00
|
|
|
register_asset "stylesheets/lazyYT_mobile.scss", :mobile
|
2014-07-21 21:39:32 -05:00
|
|
|
|
|
|
|
# freedom patch YouTube Onebox
|
|
|
|
class Onebox::Engine::YoutubeOnebox
|
|
|
|
include Onebox::Engine
|
2015-08-18 04:17:39 -05:00
|
|
|
alias_method :yt_onebox_to_html, :to_html
|
2014-07-21 21:39:32 -05:00
|
|
|
|
|
|
|
def to_html
|
2023-01-25 09:40:57 -06:00
|
|
|
if SiteSetting.lazy_yt_enabled && video_id && !params["list"]
|
2023-01-06 14:42:16 -06:00
|
|
|
size_restricted = [params["width"], params["height"]].any?
|
|
|
|
video_width = (params["width"] && params["width"].to_i <= 695) ? params["width"] : 690 # embed width
|
|
|
|
video_height = (params["height"] && params["height"].to_i <= 500) ? params["height"] : 388 # embed height
|
2020-06-23 15:21:36 -05:00
|
|
|
size_tags = ["width=\"#{video_width}\"", "height=\"#{video_height}\""]
|
2015-08-18 04:17:39 -05:00
|
|
|
|
2021-04-07 11:29:39 -05:00
|
|
|
result = parse_embed_response
|
|
|
|
result ||= get_opengraph.data
|
|
|
|
|
|
|
|
thumbnail_url = result[:image] || "https://img.youtube.com/vi/#{video_id}/hqdefault.jpg"
|
2020-05-15 11:49:27 -05:00
|
|
|
|
2014-08-22 14:21:02 -05:00
|
|
|
# Put in the LazyYT div instead of the iframe
|
2016-01-30 05:32:48 -06:00
|
|
|
escaped_title = ERB::Util.html_escape(video_title)
|
2020-04-29 13:40:21 -05:00
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
<<~HTML
|
2020-05-25 16:24:40 -05:00
|
|
|
<div class="onebox lazyYT lazyYT-container"
|
2020-04-29 13:40:21 -05:00
|
|
|
data-youtube-id="#{video_id}"
|
|
|
|
data-youtube-title="#{escaped_title}"
|
2023-01-06 14:42:16 -06:00
|
|
|
#{size_restricted ? size_tags.map { |t| "data-#{t}" }.join(" ") : ""}
|
2020-04-29 13:40:21 -05:00
|
|
|
data-parameters="#{embed_params}">
|
|
|
|
<a href="https://www.youtube.com/watch?v=#{video_id}" target="_blank">
|
2020-06-23 15:21:36 -05:00
|
|
|
<img class="ytp-thumbnail-image"
|
|
|
|
src="#{thumbnail_url}"
|
2023-01-06 14:42:16 -06:00
|
|
|
#{size_restricted ? size_tags.join(" ") : ""}
|
2020-06-23 15:21:36 -05:00
|
|
|
title="#{escaped_title}">
|
2020-04-29 13:40:21 -05:00
|
|
|
</a>
|
|
|
|
</div>
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 13:50:55 -06:00
|
|
|
HTML
|
2014-07-21 21:39:32 -05:00
|
|
|
else
|
2015-08-18 04:17:39 -05:00
|
|
|
yt_onebox_to_html
|
2014-07-21 21:39:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-21 05:54:05 -05:00
|
|
|
|
|
|
|
after_initialize do
|
2018-05-09 12:39:17 -05:00
|
|
|
on(:reduce_cooked) do |fragment|
|
2023-01-06 14:42:16 -06:00
|
|
|
fragment
|
|
|
|
.css(".lazyYT")
|
|
|
|
.each do |yt|
|
|
|
|
begin
|
|
|
|
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>}
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
# remove any invalid/weird URIs
|
|
|
|
yt.remove
|
|
|
|
end
|
2014-08-21 05:54:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|