Replace an embed or iframe youtube video by an tum

2019-04-13 17:36发布

问题:

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&amp;hl=pt_BR&amp;hd=1&amp;color1=0x5d1719&amp;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&amp;hl=pt_BR&amp;hd=1&amp;color1=0x5d1719&amp;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...).

回答1:

This is what I came up with. It has only been tested in Google Chrome.

function changeobj() {
    objs = document.getElementsByTagName('object');
    for (i in objs) {
        for (o in objs[i].children) {
            if (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;
                    imgurl = 'http://img.youtube.com/vi/'+vidid+'/default.jpg';
                    link = document.createElement('a');
                    link.setAttribute('href',linkurl);
                    link.setAttribute('target','_blank');
                    img = document.createElement('img');
                    img.src = imgurl;
                    link.appendChild(img);
                    objs[i].innerHTML = '';
                    objs[i].appendChild(link);
                }
            }
        }
    }

function changeiframe() {
    objs = document.getElementsByClassName('youtube-player');
    for (i in objs) {
        console.log(objs[i]);
        vidid = objs[i].src.split('/')[4].split('?')[0];
        linkurl = 'http://www.youtube.com/watch?v='+vidid;
        imgurl = 'http://img.youtube.com/vi/'+vidid+'/default.jpg';
        link = document.createElement('a');
        link.setAttribute('href',linkurl);
        link.setAttribute('target','_blank');
        img = document.createElement('img');
        img.src = imgurl;
        link.appendChild(img);
        objs[i].outerHTML = link.outerHTML;
        }
    }

window.onload = function () {
    changeobj();
    changeiframe();
    }

Yes, I know. There is a bug in it. That's why it won't finish. I'm working on it.

EDIT:

I fixed the original bugs:

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;
                    imgurl = 'http://img.youtube.com/vi/'+vidid+'/default.jpg';
                    link = document.createElement('a');
                    link.setAttribute('href',linkurl);
                    link.setAttribute('target','_blank');
                    img = document.createElement('img');
                    img.src = imgurl;
                    link.appendChild(img);
                    objs[i].innerHTML = '';
                    objs[i].appendChild(link);
                }
            }
        }
    }

function changeiframe() {
    objs = document.getElementsByClassName('youtube-player');
    for (i in objs) {
            if (objs[i].src) {
            vidid = objs[i].src.split('/')[4].split('?')[0];
            linkurl = 'http://www.youtube.com/watch?v='+vidid;
            imgurl = 'http://img.youtube.com/vi/'+vidid+'/default.jpg';
            link = document.createElement('a');
            link.setAttribute('href',linkurl);
            link.setAttribute('target','_blank');
            img = document.createElement('img');
            img.src = imgurl;
            link.appendChild(img);
            objs[i].outerHTML = link.outerHTML;
            }
        }
    }

window.onload = function () {
    changeobj();
    changeiframe();
    }

There is another bug that won't load any 4th movie, but I'm sure this should give you enough to make your own version.

EDIT: @evel - If you want to put a play button, you can put something like this into your CSS:

.YOUTUBE_VID {
background-image: play.png;
background-repeat: none;
background-postion: center;
}

And than add it the class to the link:

link.className += " .YOUTUBE_VID";

EDIT: Instead of using the for loop, I'm looping the function. This seems to be working:

function changeiframe() {
    objs = document.getElementsByClassName('youtube-player');
        if (!objs[0]){return false;};
        if (objs[0]) {
        vidid = objs[0].src.split('/')[4].split('?')[0];
        linkurl = 'http://www.youtube.com/watch?v='+vidid;
        imgurl = 'http://img.youtube.com/vi/'+vidid+'/default.jpg';
        link = document.createElement('a');
        link.setAttribute('href',linkurl);
        link.setAttribute('target','_blank');
        img = document.createElement('img');
        img.src = imgurl;
        link.appendChild(img);
        objs[0].outerHTML = link.outerHTML;
        }
        changeiframe();
    }