用缩略图替换嵌入或 iframe youtube 视频 +视频链接
我需要一个可以放置在标题上的 JavaScript,它可以识别 YouTube 嵌入或 iframe 播放器,并将其替换为链接到 YouTube 上视频的缩略图。
该脚本应标识如下代码:
<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>
并替换为:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我想出来的。它仅在 Google Chrome 中进行了测试。
是的,我知道。其中有一个错误。这就是为什么它不会完成。我正在努力。
编辑:
我修复了原来的错误:
还有另一个错误不会加载任何第四部电影,但我确信这应该足以让您制作自己的版本。
编辑:
@evel - 如果你想放置一个播放按钮,你可以将这样的内容放入你的CSS中:
然后将其类添加到链接中:
编辑:
我没有使用 for 循环,而是循环该函数。这似乎有效:
This is what I came up with. It has only been tested in Google Chrome.
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:
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:
And than add it the class to the link:
EDIT:
Instead of using the for loop, I'm looping the function. This seems to be working: