Created
February 6, 2012 12:45
-
-
Save screeley/1751899 to your computer and use it in GitHub Desktop.
Simple replace text url with video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script src="http://scripts.embed.ly/jquery.embedly-2.1.7.min.js"></script> | |
</head> | |
<body> | |
<p> | |
This is a generic piece of text that contains a http://www.youtube.com/watch?v=t3TmFyyB4zI url. | |
</p> | |
<script> | |
// url Regex | |
var urlRe = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi; | |
$('p').each(function(i,e){ | |
// Extract all of the urls out of the text. | |
var urls = $(this).text().match(urlRe); | |
// Call Embedly | |
$.embedly(urls, {}, function(o,d){ | |
// Replace url in the text with html content | |
var h = $(e).text().replace(d.url, o.html); | |
// Set html content | |
$(e).html(h); | |
}) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment