Skip to content

Instantly share code, notes, and snippets.

@tompng
Created September 14, 2012 01:32
Show Gist options
  • Save tompng/3719273 to your computer and use it in GitHub Desktop.
Save tompng/3719273 to your computer and use it in GitHub Desktop.
youtube gen playlist
var sc=document.createElement("script");
sc.src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js";
document.body.appendChild(sc);
document.body.innerHTML="<div id='video'></div>";
url=location.href;
var http=new XMLHttpRequest();
http.open("GET",url,true);
setTimeout(function(){
http.send()},3000);
http.onreadystatechange=function(){
if(http.readyState!=4)return;
if(http.status!=200)error();
else urlload(http.responseText);
}
function error(){
alert("error");
}
function urlload(html){
var list=html.match(/<iframe src="(http:\/\/www.youtube.com\/embed\/[a-zA-Z0-9]+)"/g);
for(var i=0;i<list.length;i++){
list[i]=list[i].substring(13+29,list[i].length-1);
}
console.log(list);
start(list);
}
var index=0;
function onytplayerStateChange(newState) {
console.log(newState)
if (newState == 0) {
document.getElementById("player").loadVideoById(video_list[++index]);
}
}
function onYouTubePlayerReady(playerId) {
console.log("onready");
document.getElementById(playerId).addEventListener("onStateChange","onytplayerStateChange");
}
function start(video){
video_list=video;
swfobject.embedSWF("http://www.youtube.com/v/"+video_list[0]+"?enablejsapi=1&autoplay=1&playerapiid=player",
"video", "854", "480", "8", null, null, { allowScriptAccess: "always" }, { id: "player" });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment