Last active
January 15, 2016 12:46
-
-
Save henkman/cedd092a988c2e45def6 to your computer and use it in GitHub Desktop.
youtube embedded fuckery
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
| function Queue() { | |
| var queue = []; | |
| var offset = 0; | |
| this.enqueue = function(item) { | |
| queue.push(item); | |
| }; | |
| this.dequeue = function() { | |
| if (queue.length == 0) return undefined; | |
| var item = queue[offset]; | |
| if (++ offset * 2 >= queue.length){ | |
| queue = queue.slice(offset); | |
| offset = 0; | |
| } | |
| return item; | |
| }; | |
| } | |
| function YoutubeAPI() { | |
| var toload = []; | |
| var loaded = false; | |
| function loadVideo(video) { | |
| var params = { | |
| videoId: video.src, | |
| playerVars: { | |
| showinfo:0, | |
| controls:video.controls, | |
| modestbranding:1 | |
| }, | |
| events: { | |
| onReady: function() { | |
| var fn; | |
| while(fn = video.queue.dequeue()) { | |
| fn(); | |
| } | |
| } | |
| } | |
| }; | |
| if(video.width) params.width = video.width; | |
| if(video.height) params.height = video.height; | |
| video.queue = new Queue(); | |
| video.loaded = new YT.Player(video.id, params); | |
| } | |
| return { | |
| registerVideo:function(video) { | |
| if(toload.indexOf(video) >= 0) return; | |
| loaded ? | |
| loadVideo(video) : | |
| toload.push(video); | |
| return video; | |
| }, | |
| loadVideos:function() { | |
| loaded = true; | |
| for(var i=0; i<toload.length; i++) { | |
| if(toload[i].loaded) continue; | |
| loadVideo(toload[i]); | |
| } | |
| }, | |
| playVideo:function(video) { | |
| typeof video.loaded.playVideo === 'function' ? | |
| video.loaded.playVideo() : | |
| video.queue.enqueue(function() { | |
| video.loaded.playVideo(); | |
| }); | |
| }, | |
| stopVideo:function(video) { | |
| typeof video.loaded.stopVideo === 'function' ? | |
| video.loaded.stopVideo() : | |
| video.queue.enqueue(function() { | |
| video.loaded.stopVideo(); | |
| }); | |
| }, | |
| seekTo:function(video, position) { | |
| typeof video.loaded.seekTo === 'function' ? | |
| video.loaded.seekTo(position) : | |
| video.queue.enqueue(function() { | |
| video.loaded.seekTo(position); | |
| }); | |
| } | |
| } | |
| } | |
| var youtubeAPI = new YoutubeAPI(); | |
| function onYouTubeIframeAPIReady() { | |
| youtubeAPI.loadVideos(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment