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:
Unfortunately, they don't consider the case with no extension in the src
attribute when the images should be saved automatically with JavaScript.