1

For my research, I use JavaScript to download data of the top YouTube comments that are shown to a user, that is why I am using data from the YouTube frontend and not the YouTube API. Apart from comment text, number of likes, etc. I would also like to store the profile pictures of the people leaving comments. The img tag looks like this:

<img id="img" class="style-scope yt-img-shadow" alt="name" height="40" width="40"
src="https://yt3.ggpht.com/ytc/AKedOLSXk4DiR7nlvyuGiByOeaJmg4pH8Yt2FAqoVy73YQ=s48-c-k-c0x00ffffff-no-rj">

The problem that I am experiencing is that the src attribute does not have an extension at the end, so when I am trying to download a .jpg image with the code listed below, the image is just opened in a new tab and the link from the src attribute is saved as a .jpg file, which is not interpreted correctly. I also tried adding an extension like .png or .jpg at the end of the link, but it didn't work.

function downloadImage(imageSrc) {
  const link = document.createElement('a')
  link.href = imageSrc
  link.download = 'image.jpg'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

The solutions that I found so far:

  1. Save Image file from Image URL javascript html

  2. How to view the full background image of a YouTube channel?

Unfortunately, they don't consider the case with no extension in the src attribute when the images should be saved automatically with JavaScript.

luosua
  • 11
  • 2
  • Do you want just to download the image or to do it with a predefined file extension? – testing_22 Oct 12 '21 at 22:22
  • And from where do you want to download it? Directly from the youtube console? Because it's blocked by CORS if you try unspecified sources – testing_22 Oct 12 '21 at 22:33
  • @testing_22 I just want to download an image, it doesn't matter to me if it is in .jpg or .png format. Basically I want to do with JavaScript what I can do manually by right-clicking the image and choosing "Save image as". I am using a Chrome extension called Page Manipulator to inject the JavaScript code into the YouTube frontend. I am also downloading comment data to a .csv file from the same script and didn't have any troubles with that. – luosua Oct 13 '21 at 10:43

0 Answers0