Last active
January 7, 2016 17:38
-
-
Save asemler/763db85f1fdac7194eaf to your computer and use it in GitHub Desktop.
init-youtube
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
var setupYouTubeAPI = function() { | |
// Load the IFrame Player API code asynchronously. | |
var tag = document.createElement('script'); | |
tag.src = 'https://www.youtube.com/player_api'; | |
// inject script to DOM | |
var firstScriptTag = document.getElementsByTagName('script')[0]; | |
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
// listen for ready event | |
function onPlayerReady() { | |
var data = videoPlayer.player.getVideoData(); | |
videoPlayer.title = data.title; | |
self.updateDOM(); | |
} | |
function onPlayerError() { | |
// TODO- handle error | |
videoPlayer.loadError = true; | |
} | |
// listen for YouTube Player Ready | |
window.onYouTubePlayerAPIReady = () => { | |
videoPlayer.player = new window.YT.Player('ytplayer', { | |
videoId: videoPlayer.videoId, | |
width: '100%', | |
height: '560', | |
events: { | |
'onReady': onPlayerReady, | |
'onError': onPlayerError | |
} | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment