I need an javascript that can be placed on header that recognizes an youtube embed or iframe player and replaces it by a tumbnail linked to the vídeo on youtube.
The script should identifies an code like this ones:
<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/J4oJWUJpbLY?fs=1&hl=pt_BR&hd=1&color1=0x5d1719&color2=0xcd311b"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/J4oJWUJpbLY?fs=1&hl=pt_BR&hd=1&color1=0x5d1719&color2=0xcd311b" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>
<iframe title="YouTube video player" class="youtube-player" type="text/html" width="560" height="345" src="http://www.youtube.com/embed/J4oJWUJpbLY?hd=1" frameborder="0"></iframe>
And replace by it:
<a href="http://www.youtube.com/watch?v=n1VCUF2xqKk" target="_blank"><img src="http://img.youtube.com/vi/n1VCUF2xqKk/default.jpg" alt="" /></a>
Look that the variable is the video ID.
It is possible?
UPDATE [Code whith big thumbs and play icon)
//FUNC OBJECT
function changeobj() {
objs = document.getElementsByTagName('object');
for (i in objs) {
for (o in objs[i].children) {
if (objs[i].children[o].getAttribute && objs[i].children[o].getAttribute('name') == 'movie') {
vidid = objs[i].children[o].getAttribute('value').split('/')[4].split('?')[0];
linkurl = 'http://www.youtube.com/watch?v='+vidid;
bgurl = 'http://img.youtube.com/vi/'+vidid+'/0.jpg';
imgurl = 'playsh.png';
link = document.createElement('a');
link.setAttribute('href',linkurl);
link.setAttribute('target','_blank');
link.style.backgroundImage = 'url('+bgurl+')';
link.className += "youvid";
img = document.createElement('img');
img.src = imgurl;
link.appendChild(img);
objs[i].innerHTML = '';
objs[i].appendChild(link);
}
}
}
}
//FUNC IFRAME
function changeiframe() {
objs = document.getElementsByTagName('iframe');
for (i in objs) {
if (objs[i].src) {
vidid = objs[i].src.split('/')[4].split('?')[0];
linkurl = 'http://www.youtube.com/watch?v='+vidid;
bgurl = 'http://img.youtube.com/vi/'+vidid+'/0.jpg';
imgurl = 'playsh.png';
link = document.createElement('a');
link.setAttribute('href',linkurl);
link.setAttribute('target','_blank');
link.style.backgroundImage = 'url('+bgurl+')';
link.className += "youvid";
img = document.createElement('img');
img.src = imgurl;
link.appendChild(img);
objs[i].outerHTML = link.outerHTML;
}
}
}
window.onload = function () {
changeobj();
changeiframe();
}
The CSS:
.youvid {
background-repeat: no-repeat;
position: relative;
display: block;
text-align: center;
background-position: center;
}
One bug still remais, the changeiframe function only replaces the odd ones (1st, 3rd, 5th...).