0

Using the method below, I am able to pull in a YouTube video's thumbnail.

Is there a similar method to pulling in a YouTube video's title as well?

(function playYouTube() {
  let YouTubeContainers = document.querySelectorAll(".embed-youtube");
  for (let i = 0; i < YouTubeContainers.length; i++) {
    let container = YouTubeContainers[i];
    let imageSource =
      "https://img.youtube.com/vi/" +
      container.dataset.videoId +
      "/sddefault.jpg";
    
    let image = new Image();
    image.src = imageSource;
    image.addEventListener("load", function () {
      container.appendChild(image);
    });    
  }
})();
<div class="embed-youtube" data-video-id="eX2qFMC8cFo"></div>
Millhorn
  • 2,953
  • 7
  • 39
  • 77
  • You can get the thumbnail because YouTube uses a consistent and easily references path for them. Note that you aren't "getting the image", you're just figuring out where it's hosted and referencing that. In order to get actual data, you'll need an API call. – Niet the Dark Absol Oct 18 '21 at 17:33
  • @AnuragSrivastava - I would need a js solution, not a PHP one. – Millhorn Oct 18 '21 at 17:43
  • So you didn't look at the other answers – Anurag Srivastava Oct 18 '21 at 17:44
  • I did. But I'm not finding a good JS one that will help me without rewriting everything I've already done. I'd have to rewrite my API call. I wasn't interested in doing that. – Millhorn Oct 18 '21 at 18:01
  • Just replicate the algorithm with JS... Programming languages are all just series of instructions. You can do the same things in any languages. – Leonardo Raele Oct 18 '21 at 18:16
  • @LeonardoRaele - That's logical, but it's not how this type of code would work. The YouTube thumbnail is a static web address, so it's easy to locate based on the video id. The video's title doesn't work the same way. – Millhorn Oct 18 '21 at 19:03
  • I don't know who voted to close this question, but the suggested question revolves around the YouTube Data API and my question does not... These are two different questions and I really wish that people would stop closing questions without reading the context of the question. – Millhorn Oct 19 '21 at 14:36
  • 1
    Are you against using the Data API? It exists for the purpose you're looking for. Did you try it even once? [This link](https://developers.google.com/youtube/v3/docs/videos) states clearly that the API returns a property `snippet.title` which seems to be what your question is about. (Also an answer in the suggested duplicate) What is the exact issue you face? What doesn't work for you? FWIW I voted to close this question. – Anurag Srivastava Oct 19 '21 at 15:43

0 Answers0