leftypol/js/youtube.js

94 lines
3.1 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(){
// Adds Options panel item
if (typeof localStorage.youtube_embed_proxy === 'undefined') {
if (location.hostname.includes(".onion")){
localStorage.youtube_embed_proxy = 'tuberyps2pn6dor6h47brof3w2asmauahhk4ei42krugybzzzo55klad.onion';
} else {
localStorage.youtube_embed_proxy = 'incogtube.com'; //default value
}
}
if (window.Options && Options.get_tab('general')) {
Options.extend_tab("general", "<fieldset id='media-proxy-fs'><legend>"+_("Media Proxy (requires refresh)")+"</legend>"
+ ('<label id="youtube-embed-proxy-url">' + _('YouTube embed proxy url&nbsp;&nbsp;')+'<input type="text" size=30></label>')
+ '</fieldset>');
$('#youtube-embed-proxy-url>input').val(localStorage.youtube_embed_proxy);
$('#youtube-embed-proxy-url>input').on('input', function() {
localStorage.youtube_embed_proxy = $('#youtube-embed-proxy-url>input').val();
});
}
const ON = "[Remove]";
2021-01-30 03:30:55 +00:00
const OFF = "[Embed]";
const YOUTUBE = 'www.youtube.com';
const PROXY = localStorage.youtube_embed_proxy;
2021-01-30 03:30:55 +00:00
function addEmbedButton(index, videoNode) {
2021-01-30 03:39:00 +00:00
videoNode = $(videoNode);
2021-10-05 21:14:26 +00:00
var contents = videoNode.contents();
var videoId = videoNode.data('video');
var span = $("<span>[Embed]</span>");
var spanProxy = $("<span>[Proxy]</span>");
2021-10-05 21:14:26 +00:00
2021-10-05 20:26:46 +00:00
var makeEmbedNode = function(embedHost) {
2021-10-05 21:18:36 +00:00
return $('<iframe style="float:left;margin: 10px 20px" type="text/html" '+
'width="360" height="270" src="//' + embedHost + '/embed/' + videoId +
2021-01-30 03:30:55 +00:00
'?autoplay=1&html5=1" allowfullscreen frameborder="0"/>');
2021-10-05 21:14:26 +00:00
}
var defaultEmbed = makeEmbedNode(location.hostname.includes(".onion") ? PROXY : YOUTUBE);
2021-10-05 21:18:36 +00:00
var proxyEmbed = makeEmbedNode(PROXY);
videoNode.click(function(e) {
2021-10-05 21:14:26 +00:00
e.preventDefault();
if (span.text() == ON){
2021-10-05 21:14:26 +00:00
videoNode.append(spanProxy);
videoNode.append(contents);
2021-10-05 20:39:49 +00:00
defaultEmbed.remove();
proxyEmbed.remove();
span.text(OFF);
2021-10-05 21:14:26 +00:00
} else {
contents.detach();
span.text(ON);
2021-10-05 21:14:26 +00:00
spanProxy.remove();
videoNode.append(e.target == spanProxy[0] ? proxyEmbed : defaultEmbed);
}
});
videoNode.append(span);
videoNode.append(spanProxy);
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-10-05 21:14:26 +00:00
$('div.video-container', post).each(addEmbedButton);
});
});