Skip to content

Instantly share code, notes, and snippets.

@jeffmerrick
Created Oct 14, 2014
Embed
What would you like to do?
Final script
(function(window, $, undefined){
function getInfo() {
var movieInfo = {};
movieInfo['title'] = $('h1.title').html();
movieInfo['year'] = $('span.year').html();
movieInfo['type'] = $('.noreview-col-left h3').html().replace(' Details','');
return movieInfo;
}
function getVideo(searchQuery) {
var queryURL = 'https://www.googleapis.com/youtube/v3/search?q='+searchQuery+'&key=<YOUR API KEY HERE>&part=snippet&maxResults=1&type=video&videoEmbeddable=true';
$.getJSON(queryURL, function(data) {
var videoId = data.items[0].id.videoId;
$('#displaypage-overview').append('<div id="yt-trailer"><iframe width="960" height="540" src="//www.youtube.com/embed/'+videoId+'?rel=0" frameborder="0" allowfullscreen></iframe><br><br><a href="https://www.youtube.com/results?search_query='+searchQuery+'" target="_blank">Wrong trailer?</a></div>');
});
}
$('.actions').append('<br><br><span class="btnWrap mltBtn mltBtn-s60"><a id="watch-trailer" href="javascript:void(0);" style="margin-top:-5px" class="svf-button svfb-silver addlk evo-btn svf-button-inq save2add"><span class="inr">Watch Trailer</span></a></span>');
$('#watch-trailer').on('click', function(){
if($('#yt-trailer').length == 0) {
var typeModifier = '';
if(getInfo().type == 'Show') {
var typeModifier = 'season 1';
} else if(getInfo().type == 'Movie') {
var typeModifier = 'movie' + ' ' + getInfo().year;
}
var searchQuery = getInfo().title + ' ' + typeModifier + ' trailer';
getVideo(searchQuery);
} else {
$('#yt-trailer').remove();
}
});
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment