Skip to content

Instantly share code, notes, and snippets.

@DylanFM
Created June 9, 2011 00:52
Show Gist options
  • Save DylanFM/1015803 to your computer and use it in GitHub Desktop.
Save DylanFM/1015803 to your computer and use it in GitHub Desktop.
On Surftourist.com we've had some people putting in a vimeo or youtube link in their descriptions. We want to support video properly, but this is a quick attempt at addressing the solution for the meantime.
# Search for video urls within an element and embed them if possible
embedVideos = (el) ->
el = $ el
if el.length
el.each ->
section = $ @
matches = section.html().match /http:\/\/(www\.)?(youtube|vimeo)\.com\/[^\s<]*/gmi
if matches?.length
# Vimeo or Youtube?
for match in matches
# Use embedly for youtube because youtube oembed doesn't support jsonp
# Don't use embedly for Vimeo because it's slower than vimeo
$.ajax
url: "http://#{if /youtube/i.test(match) then "api.embed.ly/1/oembed" else "vimeo.com/api/oembed.json"}?url=#{encodeURIComponent(match)}"
dataType: 'jsonp'
data:
format: 'json'
iframe: 1
maxheight: 300
crossDomain: true
success: (data) -> section.html section.html().replace(match, data.html) if data?.html?.length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment