Skip to content

Instantly share code, notes, and snippets.

@versluis
Last active September 3, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save versluis/a88ce195add12ff8f61905c42b4d80b4 to your computer and use it in GitHub Desktop.
Save versluis/a88ce195add12ff8f61905c42b4d80b4 to your computer and use it in GitHub Desktop.
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