Skip to content

Instantly share code, notes, and snippets.

@embedly
Created April 22, 2010 20:30
Show Gist options
  • Save embedly/375779 to your computer and use it in GitHub Desktop.
Save embedly/375779 to your computer and use it in GitHub Desktop.
//FROM http://ad1987.blogspot.com/2010/04/howto-create-facebook-status-message.html
function stripHTML(source){
var strippedText = source.replace(/<\/?[^>]+(>|$)/g, "");
return strippedText
}
function replaceURLWithHTMLLinks(source) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return source.replace(exp,"<a href='$1' target='_blank'>$1</a>");
}
// Deal With the shares
function add_to_db(message){
d = {};
d['message'] = message;
$.ajax({
type: 'POST',
url: '/ajax',
data: d,
success: function(json){ if (window.console && window.console.firebug) console.log(json.error);},
dataType: 'json'
});
}
//Add the message to to the share list
function add_to_shares(message, embed){
var post = '<li><p>'+replaceURLWithHTMLLinks(message)+'</p>';
if (embed != null)
post += embed;
post += '</li>';
$('#shares').prepend(post);
add_to_db(message);
}
$(document).ready(function() {
//Bind Handler
$('#sharebox .sharebutton').bind('click', function(e){
e.preventDefault();
var text = stripHTML($('#sharebox .sharecontent').val());
//If there is no content in the box just return.
if (!text || !text.replace(/\s/g, ''))
return false
// Find all the urls in the post
var exp = /(\bhttp:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var urls = text.match(exp);
// We only want one link.
if (urls != null && urls.length > 1){
return false
}else if(urls != null){
//Call Embedly
$.embedly(urls[0], {maxWidth: 480, wrapElement: 'div', method : "afterParent"},
function(oembed){
if (oembed != null)
add_to_shares(text, oembed.code);
else
add_to_shares(text)
});
} else {
//No Url just add the text.
add_to_shares(text, null);
}
});
//Deal with the links already on the page.
$('#shares').embedly({maxWidth: 480, wrapElement: 'div', method : "afterParent" });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment