Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pull in the latest video from YouTube
<div class="iframe-container">
<iframe class="latestVideoEmbed" vnum="0" cid="UCMhv8E4GMw6erC-9iiYtNFg" allowfullscreen="" width="800" height="480" frameborder="0"></iframe>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var reqURL = "https://api.rss2json.com/v1/api.json?rss_url=" + encodeURIComponent("https://www.youtube.com/feeds/videos.xml?channel_id=");
function loadVideo(iframe) {
$.getJSON(reqURL + iframe.getAttribute('cid'),
function(data) {
var videoNumber = (iframe.getAttribute('vnum') ? Number(iframe.getAttribute('vnum')) : 0);
console.log(videoNumber);
var link = data.items[videoNumber].link;
id = link.substr(link.indexOf("=") + 1);
iframe.setAttribute("src", "https://youtube.com/embed/" + id + "?controls=0&autoplay=1&rel=0");
}
);
}
var iframes = document.getElementsByClassName('latestVideoEmbed');
for (var i = 0, len = iframes.length; i < len; i++) {
loadVideo(iframes[i]);
}
</script>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment