1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
$(document).ready(function(){ $('.youtube-wrap').each(function(index){ var $wrap = $(this); var url = $wrap.data('url'); //console.log(youtube_url2embed(url)); $wrap.html(videoEmbed.convertMedia(url)); }); }); var videoEmbed = { invoke: function(){ $('body').html(function(i, html) { return videoEmbed.convertMedia(html); }); }, convertMedia: function(html){ var width = 560, height = 315; var pattern1 = /(?:http?s?:\/\/)?(?:www\.)?(?:vimeo\.com)\/?(.+)/g; var pattern2 = /(?:http?s?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g; var pattern3 = /([-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[[email protected]:%_\+.~#?&//=]*)?(?:jpg|jpeg|gif|png))/gi; if(pattern1.test(html)){ var replacement = '<iframe width="' + width + '" height="' + height + '" src="//player.vimeo.com/video/$1" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'; var html = html.replace(pattern1, replacement); } if(pattern2.test(html)){ var replacement = '<iframe width="' + width + '" height="' + height + '" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>'; var html = html.replace(pattern2, replacement); } if(pattern3.test(html)){ var replacement = '<a href="$1" target="_blank"><img class="sml" src="$1" /></a><br />'; var html = html.replace(pattern3, replacement); } return html; } } |
Reference: