我需要一个可以放在标题上的javascript来识别youtube embed或iframe播放器,并用链接到youtube上vídeo的tumbnail替换它。
脚本应该识别像这样的代码:
<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;
}
还有一个bug仍然存在,changeiframe函数只能替换奇数(第1,第3,第5 ......)。
答案 0 :(得分:1)
这就是我想出的。它仅在谷歌浏览器中进行了测试。
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();
}