Last active
February 28, 2018 09:24
-
-
Save lerni/e1adb37afe62316305f91881a2e78f9a to your computer and use it in GitHub Desktop.
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
// this script loads the YouTube iframe-api, gets all instances and adds enablejsapi & origin-URL to the src-URL | |
// after initiating the API it adds playing-mode & paused-mode to the iframes | |
if ($('[src^="https://www.youtube.com/"]').length) { | |
var tag = document.createElement('script'); | |
tag.src = "https://www.youtube.com/iframe_api"; | |
var firstScriptTag = document.getElementsByTagName('script')[0]; | |
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
} | |
window.BaseUrl = function() { | |
return(window.location.protocol +'//'+ window.location.hostname); | |
} | |
function onYouTubeIframeAPIReady() { | |
$('iframe').filter(function(){ | |
return this.src.indexOf('https://www.youtube.com/') == 0 | |
}).each( function (k, v) { | |
if (!this.id) { this.id='embeddedvideoiframe' + k } | |
id = this.id; | |
url = $('#'+id).attr("src") + '&enablejsapi=1&origin=' + BaseUrl() + '&rel=0'; | |
$('#'+id).attr("src", url); | |
new YT.Player(id, { | |
events: { | |
'onStateChange': onPlayerStateChange, | |
'onReady': function(e) { | |
e.target.mute(); | |
e.target.playVideo(); | |
} | |
} | |
}); | |
}); | |
} | |
var done = false; | |
function onPlayerStateChange(event) { | |
if (event.data == YT.PlayerState.PLAYING && !done) { | |
$(event.target.a).closest("div.media").removeClass("paused-mode"); | |
$(event.target.a).closest("div.media").addClass("playing-mode"); | |
} | |
if (event.data == YT.PlayerState.PAUSED && !done) { | |
$(event.target.a).closest("div.media").removeClass("playing-mode"); | |
$(event.target.a).closest("div.media").addClass("paused-mode"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment