From f2af56f4b233db865e6a509d813d47111f6a7876 Mon Sep 17 00:00:00 2001 From: archi Date: Sun, 17 Oct 2010 20:18:24 +0200 Subject: [PATCH] Youtubelink support :) Tested with Firefox, Chrome and Mobile Safari (IE8 is broken anyway) --- app/helpers/status_messages_helper.rb | 17 +++++++++-------- public/javascripts/view.js | 6 ++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 398b265fd..6b163ed7d 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -19,19 +19,20 @@ module StatusMessagesHelper # next line is important due to XSS! (h is rail's make_html_safe-function) message = h(message).html_safe message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2') - message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch.*v=([A-Za-z0-9_]+)[^ ]*/, '\1youtube.com::\2') + message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)/, '\1youtube.com::\2') message.gsub!(/(http|ftp):\/\/([^ ]+)/, '\2') - youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/) - youtube.to_a.each do |videoid| - if videoid.match('::').nil? - message.gsub!('youtube.com::'+videoid, 'Youtube: ' + youtube_title(videoid) + '') - end + + while youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/) + videoid = youtube[1] + message.gsub!('youtube.com::'+videoid, 'Youtube: ' + youtube_title(videoid) + '') end return message end def youtube_title(id) - #TODO Check if id is cached, and return cached value + unless @@youtube_title_cache[id] == 'no-title' + return @@youtube_title_cache[id] + end ret = 'Unknown Video Title' #TODO add translation http = Net::HTTP.new('gdata.youtube.com', 80) @@ -42,7 +43,7 @@ module StatusMessagesHelper ret = title.to_s[7..-9] end - #TODO Cache the value of ret for id + @@youtube_title_cache[id] = ret; return ret end diff --git a/public/javascripts/view.js b/public/javascripts/view.js index 0df04a66e..6c7783371 100644 --- a/public/javascripts/view.js +++ b/public/javascripts/view.js @@ -72,6 +72,8 @@ $.fn.clearForm = function() { }); }; +var video_active_container = null; + function openVideo(type, videoid, link) { var container = document.createElement('div'); if(type == 'youtube.com') { @@ -79,6 +81,10 @@ function openVideo(type, videoid, link) { } else { container.innerHTML = 'Invalid videotype '+type+' (ID: '+videoid+')'; } + if(video_active_container != null) { + video_active_container.parentNode.removeChild(video_active_container); + } + video_active_container = container; $(container).hide(); link.parentNode.insertBefore(container, this.nextSibling); $(container).slideDown('fast', function() { });