Skip to content

Instantly share code, notes, and snippets.

@ZempTime
Last active August 29, 2015 14:13
Show Gist options
  • Save ZempTime/859706f2b63d6e1fecba to your computer and use it in GitHub Desktop.
Save ZempTime/859706f2b63d6e1fecba to your computer and use it in GitHub Desktop.
Check for video, audio, picture content
$(document).ready ->
$("#post_content").keyup ->
urls = findUrls($("#post_content").text())
youtube_pattern = ///http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?///
flickr_pattern = ///flickr.com\/photos///
soundcloud_pattern = ///w.soundcloud.com///
check_for_assets(youtube_pattern, urls, $("#post_has_video"))
check_for_assets(flickr_pattern, urls, $("#post_has_picture"))
check_for_assets(soundcloud_pattern, urls, $("#post_has_audio"))
return
findUrls = (text) ->
source = (text or "").toString()
urlArray = []
url = undefined
matchArray = undefined
# Regular expression to find FTP, HTTP(S) and email URLs.
regexToken = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g
# Iterate through any URLs in the text.
while (matchArray = regexToken.exec(source)) isnt null
token = matchArray[0]
urlArray.push token
urlArray
check_for_assets = (source_pattern, urls_array, checkbox) ->
source_present = false
source_present = true if $.inArray(source_pattern, urls_array)
console.log source_present, source_pattern, urls_array
if source_present
check(checkbox)
source_present = false
else
uncheck(checkbox)
return
check = (checkbox) ->
checkbox.prop('checked', true)
uncheck = (checkbox) ->
checkbox.prop('checked', false)
@ZempTime
Copy link
Author

the problem is, they all end up as true...

 true /http:\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)(\w*)(&(amp;)?[\w\?=]*)?/ ["https://www.youtube.com/watch?v=9x5PjEN-0hw"]
posts-36a68be7f3de8186f423318246b44587.js?body=1:33 
true /flickr.com\/photos/ ["https://www.youtube.com/watch?v=9x5PjEN-0hw"]
posts-36a68be7f3de8186f423318246b44587.js?body=1:33 
true /w.soundcloud.com/ ["https://www.youtube.com/watch?v=9x5PjEN-0hw"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment