1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-08 23:39:46 +01:00
vichan/js/youtube.js

59 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>
2024-08-05 18:50:08 +02:00
* 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';
*
*/
2024-08-05 18:50:35 +02:00
onReady(function() {
2021-01-29 21:30:55 -06:00
const ON = "[Remove]";
const OFF = "[Embed]";
2024-08-05 18:50:08 +02:00
2021-01-29 21:30:55 -06:00
function addEmbedButton(index, videoNode) {
videoNode = $(videoNode);
let contents = videoNode.contents();
2021-01-29 21:30:55 -06:00
let videoId = videoNode.data('video');
let span = $("<span>[Embed]</span>");
let embedNode = $('<iframe style="float:left;padding: 0 20px 0 0;margin: 0.2em 0.2em 0.8em 0.2em" type="text/html" '+
2021-01-29 21:30:55 -06:00
'width="360" height="270" src="//www.youtube.com/embed/' + videoId +
'?autoplay=1&html5=1" allowfullscreen frameborder="0"/>');
2021-01-29 21:30:55 -06:00
span.click(function() {
if (span.text() == ON){
videoNode.append(contents);
2021-01-29 21:30:55 -06:00
embedNode.remove();
span.text(OFF);
} else {
contents.detach();
2021-01-29 21:30:55 -06:00
videoNode.append(embedNode);
span.text(ON);
}
});
videoNode.append(span);
2021-01-29 21:30:55 -06:00
}
$('div.video-container', document).each(addEmbedButton);
2024-08-05 18:50:08 +02:00
// allow to work with auto-reload.js, etc.
$(document).on('new_post', function(e, post) {
2021-01-29 21:30:55 -06:00
$('div.video-container', post).each(addEmbedButton);
2024-08-05 18:50:08 +02:00
});
});