Youtubelink support :)
Tested with Firefox, Chrome and Mobile Safari (IE8 is broken anyway)
This commit is contained in:
parent
f676e284f7
commit
f2af56f4b2
2 changed files with 15 additions and 8 deletions
|
|
@ -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):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>')
|
||||
youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/)
|
||||
youtube.to_a.each do |videoid|
|
||||
if videoid.match('::').nil?
|
||||
message.gsub!('youtube.com::'+videoid, '<a onclick="openVideo(\'youtube.com\', \'' + videoid + '\', this)" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
|
||||
end
|
||||
|
||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/)
|
||||
videoid = youtube[1]
|
||||
message.gsub!('youtube.com::'+videoid, '<a onclick="openVideo(\'youtube.com\', \'' + videoid + '\', this)" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <i>'+type+'</i> (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() { });
|
||||
|
|
|
|||
Loading…
Reference in a new issue