Created
September 14, 2017 02:18
-
-
Save tonY1883/a3b85925081688de569b779b4657439b to your computer and use it in GitHub Desktop.
A small trick to check if youtube video exist with its id.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validVideoId(id) { | |
var img = new Image(); | |
img.src = "http://img.youtube.com/vi/" + id + "/mqdefault.jpg"; | |
img.onload = function () { | |
checkThumbnail(this.width); | |
} | |
} | |
function checkThumbnail(width) { | |
//HACK a mq thumbnail has width of 320. | |
//if the video does not exist(therefore thumbnail don't exist), a default thumbnail of 120 width is returned. | |
if (width === 120) { | |
alert("Error: Invalid video id"); | |
} | |
} |
thanks man it works
my approach is this btw :
im using venilla js.
const is_valid_ytd_video = async (id) => {
let response = await axios.request("https://www.youtube.com/embed/" + id)
return response.data.toLowerCase().includes("Video unavailable".toLowerCase());
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you beauty! I've found nothing else that works.