youtube.js: iterate over nodes

This commit is contained in:
nonmakina 2021-01-29 21:30:55 -06:00 committed by Zankaria
parent 688fba82de
commit 7280287695

View File

@ -23,40 +23,33 @@
*/ */
onReady(function() { onReady(function() {
let do_embed_yt = function(tag) { const ON = "[Remove]";
$('div.video-container a', tag).click(function() { const OFF = "[Embed]";
let videoID = $(this.parentNode).data('video');
$(this.parentNode).html('<iframe style="float:left;margin: 10px 20px" type="text/html" ' + function addEmbedButton(index, videoNode) {
'width="360" height="270" src="//www.youtube.com/embed/' + videoID + videoNode = $(videoNode);
let videoId = videoNode.data('video');
let span = $("<span>[Embed]</span>");
let embedNode = $('<iframe style="float:left;margin: 10px 20px" type="text/html" '+
'width="360" height="270" src="//www.youtube.com/embed/' + videoId +
'?autoplay=1&html5=1" allowfullscreen frameborder="0"/>'); '?autoplay=1&html5=1" allowfullscreen frameborder="0"/>');
span.click(function() {
const ON = "[Remove]"; if (span.text() == ON){
const OFF = "[Embed]"; embedNode.remove();
span.text(OFF);
let videoNode = $('div.video-container', tag); } else {
let videoId = videoNode.data('video'); videoNode.append(embedNode);
let span = $("<span>[Embed]</span>"); span.text(ON);
let embedNode = $('<iframe style="float:left;margin: 10px 20px" type="text/html" '+ }
'width="360" height="270" src="//www.youtube.com/embed/' + videoId +
'?autoplay=1&html5=1" allowfullscreen frameborder="0"/>')
span.click(function() {
if (span.text() == ON){
embedNode.remove();
span.text(OFF);
} else {
videoNode.append(embedNode);
span.text(ON);
}
});
}); });
videoNode.append(span); videoNode.append(span);
}; }
do_embed_yt(document);
$('div.video-container', document).each(addEmbedButton);
// allow to work with auto-reload.js, etc. // allow to work with auto-reload.js, etc.
$(document).on('new_post', function(e, post) { $(document).on('new_post', function(e, post) {
do_embed_yt(post); $('div.video-container', post).each(addEmbedButton);
}); });
}); });