Skip to content

Instantly share code, notes, and snippets.

@DinoscapeProgramming
Last active March 3, 2022 20:47
Show Gist options
  • Save DinoscapeProgramming/3ea5113bca8a54f6cb00d9b816af76b9 to your computer and use it in GitHub Desktop.
Save DinoscapeProgramming/3ea5113bca8a54f6cb00d9b816af76b9 to your computer and use it in GitHub Desktop.
A simple trick to check is a video existing or not
// definition
function videoExists(url) {
var exists;
var video;
try {
video = new URL(url);
} catch {
throw new Error("Invalid URL 😳")
}
var img = new Image();
img.src = "https://img.youtube.com/vi/" + video.searchParams.get('v') + "/mqdefault.jpg";
img.onload = function () {
exists = !this.width === 120 && !this.height === 90;
}
img.onerror = function () {
throw new Error("Hmm... Something went wrong :(");
}
return exists;
}
// usage
videoExists("https://www.youtube.com/watch?v=notExisting") // returns false
videoExists("https://www.youtube.com/watch?v=--AuxdQUdlo") // returns true
// advanced usage
if (videoExists("YOUR VIDEO")) {
console.log("This video exists")
} else {
console.log("I can't found this video :(")
}
// credits
/*
* Credits to YouTube and GitHub
**/
// note
/*
* This code is free to use
**/
@DinoscapeProgramming
Copy link
Author

I hope I can help you with your program or anything else :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment