Created
August 13, 2020 14:40
-
-
Save robozavri/9b1c36422ef62080d424386101bf73f8 to your computer and use it in GitHub Desktop.
js get youtube video thumbnail
This file contains hidden or 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
var Youtube = (function () { | |
'use strict'; | |
var video, results; | |
var getThumb = function (url, size) { | |
if (url === null) { | |
return ''; | |
} | |
size = (size === null) ? 'big' : size; | |
results = url.match('[\\?&]v=([^&#]*)'); | |
video = (results === null) ? url : results[1]; | |
if (size === 'small') { | |
return 'http://img.youtube.com/vi/' + video + '/2.jpg'; | |
} | |
return 'http://img.youtube.com/vi/' + video + '/0.jpg'; | |
}; | |
return { | |
thumb: getThumb | |
}; | |
}()); | |
//Example of usage: | |
var thumb = Youtube.thumb('http://www.youtube.com/watch?v=F4rBAf1wbq4', 'small'); | |
console.log(thumb); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment