¿Cambié el content
de una identificación a una clase porque estaba adivinando que tendría más de un área de contenido?
Estoy seguro de que hay una forma mucho más eficiente de hacerlo, pero este es mi intento. Nota Me encantan las expresiones regulares, así que lo que tengo es lo más cerca que puedo, estoy seguro de que alguien puede ayudar a mejorarlo, por lo que no tiene que reemplazar el ?v=
dentro de cada ciclo.
HTML
<div class="content"><p>Here is a cool video. Check it out: http://www.youtube.com/watch?v=oHg5SJYRHA0 and this one http://www.youtube.com/watch?v=fLBPsZVI8Gc&feature=player_embedded</p></div>
<div class="content"><p>Here is a cool video. Check it out: http://www.youtube.com/watch?v=fLBPsZVI8Gc&feature=player_embedded</p></div>
Guión
$(document).ready(function(){
// I added the video size here in case you wanted to modify it more easily
var vidWidth = 425;
var vidHeight = 344;
$('.content:contains("youtube.com/watch")').each(function(){
var that = $(this);
var txt = $(this).html();
// Tthis could be done by creating an object, adding attributes & inserting parameters, but this is quicker
var e1 = '<obj'+'ect width="' + vidWidth + '" height="' + vidHeight + '"><param name="movie" value="http://www.youtube.com/v/';
var e2 = '&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" ' +
'value="always"></param><em'+'bed src="http://www.youtube.com/v/';
var e3 = '&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + vidWidth +
'" ' + 'height="' + vidHeight + '"></embed></object> ';
var vid = txt.match(/((\?v=)(\w[\w|-]*))/g); // end up with ?v=oHg5SJYRHA0
if (vid.length) {
$.each(vid, function(i){
var ytid = this.replace(/\?v=/,'') // end up with oHg5SJYRHA0
that.append(e1 + ytid + e2 + ytid + e3)
})
}
})
})
seré el primero en admitir que no es bonito, pero funciona. También me pega una versión de trabajo de este código en this pastebin
Actualización: He limpiado el código un poco, aquí es cómo se ve ahora (demo):
$(document).ready(function(){
// I added the video size here in case you wanted to modify it more easily
var vidWidth = 425;
var vidHeight = 344;
var obj = '<object width="' + vidWidth + '" height="' + vidHeight + '">' +
'<param name="movie" value="http://www.youtube.com/v/[vid]&hl=en&fs=1">' +
'</param><param name="allowFullScreen" value="true"></param><param ' +
'name="allowscriptaccess" value="always"></param><em' +
'bed src="http://www.youtube.com/v/[vid]&hl=en&fs=1" ' +
'type="application/x-shockwave-flash" allowscriptaccess="always" ' +
'allowfullscreen="true" width="' + vidWidth + '" ' + 'height="' +
vidHeight + '"></embed></object> ';
$('.content:contains("youtube.com/watch")').each(function(){
var that = $(this);
var vid = that.html().match(/(?:v=)([\w\-]+)/g); // end up with v=oHg5SJYRHA0
if (vid.length) {
$.each(vid, function(){
that.append(obj.replace(/\[vid\]/g, this.replace('v=','')));
});
}
});
});
La respuesta a esta pregunta podría ayudar con la parte de expresiones regulares: http://stackoverflow.com/questions/1713222/add -class-to-all-links-that-link-to-a-certain-domain-with-js-jquery – beggs
BTW hay una API de JavaScript de YouTube que es razonablemente fácil de usar y bastante poderosa (listas de reproducción, etc.) https: // developers .google.com/youtube/js_api_reference –