Skip to content

Instantly share code, notes, and snippets.

@crgarridos
Last active May 19, 2017 08:44
Show Gist options
  • Save crgarridos/fa2a5a41c9057498e3763e4c2bdde0e9 to your computer and use it in GitHub Desktop.
Save crgarridos/fa2a5a41c9057498e3763e4c2bdde0e9 to your computer and use it in GitHub Desktop.
Javascript snippet to allow popup a youtube video (check http://cgarrido.me/?p=66)
javascript:(function(){
var id = location.href.match(/^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/);
if(id != null && typeof id == "object" && id.length > 1){
id = id[1];
var time = 0;
try {
time = parseInt(yt.player.utils.VideoTagPool.instance.A[0].getCurrentTime());
yt.player.utils.VideoTagPool.instance.A[0].pause();
}
catch(ex){console.log(ex);}
var popupUrl = "https://www.youtube.com/embed/"+id+"?autoplay=1&start="+time;
console.log({
id: id,
time : time,
url: popupUrl
});
var h = screen.height/3;
var w = screen.width/3;
var left = ((screen.width / 2) - (w / 2));
var top = ((screen.height / 2) - (h / 2));
var newWindow = window.open(popupUrl,document.title,'toolbar=0,location=0,menubar=0,width='+w+',height='+h+',top='+top+',left='+left);
/* Puts focus on the newWindow*/
if (window.focus) {
newWindow.focus();
}
} else {
alert("Impossible to detect a video's Youtube ID")
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment