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