leftypol/js/youtube.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

/*
* youtube
* https://github.com/savetheinternet/Tinyboard/blob/master/js/youtube.js
*
* Don't load the YouTube player unless the video image is clicked.
* This increases performance issues when many videos are embedded on the same page.
* Currently only compatiable with YouTube.
*
* Proof of concept.
*
* Released under the MIT license
* Copyright (c) 2013 Michael Save <savetheinternet@tinyboard.org>
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
*
* Usage:
* $config['embedding'] = array();
* $config['embedding'][0] = array(
* '/^https?:\/\/(\w+\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9\-_]{10,11})(&.+)?$/i',
* $config['youtube_js_html']);
* $config['additional_javascript'][] = 'js/jquery.min.js';
* $config['additional_javascript'][] = 'js/youtube.js';
*
*/
2021-06-27 01:40:57 +00:00
$(document).ready(function(){
2021-01-30 03:30:55 +00:00
const ON = "[Remove]";
const OFF = "[Embed]";
2021-01-30 03:30:55 +00:00
function addEmbedButton(index, videoNode) {
2021-01-30 03:39:00 +00:00
videoNode = $(videoNode);
var contents = videoNode.contents();
var videoId = videoNode.data('video');
var span = $("<span>[Embed]</span>");
var embedNode = $('<iframe style="float:left;margin: 10px 20px" type="text/html" '+
'width="360" height="270" src="//www.youtube.com/embed/' + videoId +
2021-01-30 03:30:55 +00:00
'?autoplay=1&html5=1" allowfullscreen frameborder="0"/>');
videoNode.click(function(e) {
e.preventDefault();
if (span.text() == ON){
videoNode.append(contents);
embedNode.remove();
span.text(OFF);
} else{
contents.detach();
videoNode.append(embedNode);
span.text(ON);
}
});
videoNode.append(span);
2021-01-30 03:30:55 +00:00
}
$('div.video-container', document).each(addEmbedButton);
// allow to work with auto-reload.js, etc.
$(document).on('new_post', function(e, post) {
2021-01-30 03:30:55 +00:00
$('div.video-container', post).each(addEmbedButton);
});
});