用缩略图替换嵌入或 iframe youtube 视频 +视频链接

发布于 2024-10-12 15:33:24 字数 3492 浏览 6 评论 0原文

我需要一个可以放置在标题上的 JavaScript,它可以识别 YouTube 嵌入或 iframe 播放器,并将其替换为链接到 YouTube 上视频的缩略图。

该脚本应标识如下代码:

<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>

并替换为:

<a href="http://www.youtube.com/watch?v=n1VCUF2xqKk" target="_blank"><img src="http://img.youtube.com/vi/n1VCUF2xqKk/default.jpg" alt="" /></a>

查看变量是视频 ID。

有可能吗?

更新 [带有大拇指和播放图标的代码)

//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();
    }

CSS:

.youvid {
    background-repeat: no-repeat;
    position: relative;
    display: block;
    text-align: center;
    background-position: center;
}

仍然存在一个错误,changeiframe 函数仅替换奇怪的错误(第一个、第三个、第五个......)。

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...).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人九事 2024-10-19 15:33:24

这就是我想出来的。它仅在 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();
    }

是的,我知道。其中有一个错误。这就是为什么它不会完成。我正在努力。

编辑:

我修复了原来的错误:

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();
    }

还有另一个错误不会加载任何第四部电影,但我确信这应该足以让您制作自己的版本。

编辑:
@evel - 如果你想放置一个播放按钮,你可以将这样的内容放入你的CSS中:

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

然后将其类添加到链接中:

link.className += " .YOUTUBE_VID";

编辑:
我没有使用 for 循环,而是循环该函数。这似乎有效:

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();
    }

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文