Skip to content

Instantly share code, notes, and snippets.

@cfstras
Last active January 30, 2024 17:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfstras/8824566 to your computer and use it in GitHub Desktop.
Save cfstras/8824566 to your computer and use it in GitHub Desktop.
Lightweight YouTube Oneboxer for Discourse.

Note (2024): This was relevant in 2014, when the YouTube OneBoxer integrated with Discourse was not exactly behaving how I liked it, so I made this one.
It probably doesn't work anymore.

YtLight

Replace those nasty YouTube iframes with static images for ultimate loading time in threads with lots of videos

When clicked, they get replaced by the actual iframe.

before

after

install

It is recommended to backup your database beforehand.

rake plugin:install repo=https://gist.github.com/8824566.git name=ytlight
rake assets:precompile
rake posts:rebake # this will take a long time.
# now restart your services

Note that you can omit the post rebaking, this will cause only new posts to use the light embed.

uninstall

rm -r plugins/ytlight/
rake assets:precompile
rake posts:rebake
# now restart your services

license

Beerware!

#!/bin/env ruby
# encoding: utf-8
# name: lightweight YouTube oneboxer
# version: 0.1
# authors: cfstras
register_javascript <<JS
$(document).on('click', '.ytlight', function() {
// Create an iFrame with autoplay set to true
var iframe = $('<iframe class="ytlight" src="//www.youtube.com/embed/'+ this.id
+ '?autoplay=1&autohide=1&border=0&wmode=opaque&enablejsapi=1&start=' +
$(this).data("time")+ '" />');
// Replace the YouTube thumbnail with YouTube HTML5 Player
$(this).empty().append(iframe);
});
JS
register_css <<CSS
.ytlight {
width: 690px;
height: 387px;
}
iframe.ytlight {
border: none;
}
div.ytlight {
margin-top: 1em;
margin-bottom: 1em;
background-color: #000;
position: relative;
overflow: hidden;
}
.ytlight_thumb,.ytlight_circle {
cursor: hand;
cursor: pointer;
}
.ytlight_thumb {
/* center&cutoff */
width: 100%;
height: auto;
position: absolute;
top: -9999px;
bottom: -9999px;
left: -9999px;
right: -9999px;
margin: auto;
}
.ytlight_circle{
opacity: .8;
height: 77px;
left: 50%;
margin-left: -38px;
margin-top: -38px;
position: absolute;
top: 50%;
width: 77px;
background: url("data:image/png;base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AElFTkSuQmCC") no-repeat;
}
CSS
# Without this, all the Onebox stuff isn't loaded,
# so methods like `matches_regexp` do not work.
# It's weird. I know.
Onebox = Onebox
class Onebox::Engine::YTLightOnebox
include Onebox::Engine
def self.priority
0
end
## https://www.youtube.com/watch?v=Z0UISCEe52Y
## http://youtu.be/afyK1HSFfgw
REGEX = /^https?:\/\/(?:www\.)?(?:youtube\.com\/(?:watch\?(?:feature=[^&]*&)?v=|embed)|youtu\.be\/)([a-zA-Z0-9_\-]{11})(?:[#&\?]t=(([0-9]+[smh]?)+))?$/
matches_regexp REGEX
def id
@url.match(REGEX)[1]
end
def time
# youtube embed api does not seem to like this new url format
time = @url.match(REGEX)[2]
if !time.nil? and !time.empty?
parts = time.match(/(?:([0-9]+)h)?(?:([0-9]+)m)?(?:([0-9]+)s)?/)
.to_a.map {|a| a.to_i}
hrs = parts[1]
mins = parts[2]
secs = parts[3]
hrs*3600 + mins*60 + secs
else
0
end
end
def url
"//www.youtube.com/watch?v=#{id}"
end
def to_html
<<HTML
<div class="ytlight" id="#{id}" data-time="#{time}">
<img src="//i.ytimg.com/vi/#{id}/hqdefault.jpg" class="ytlight_thumb">
<div class="ytlight_circle" />
</div>
HTML
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment