Skip to content

Instantly share code, notes, and snippets.

@mjlescano
Created October 22, 2014 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjlescano/448266c65de550cbe291 to your computer and use it in GitHub Desktop.
Save mjlescano/448266c65de550cbe291 to your computer and use it in GitHub Desktop.
Javascript helpers to share urls programatically, on facebook or twitter
var S = module.exports = {}
/**
* If the page has og:meta tags facebook will parse the page and read them.
* So the options are optionals.
*/
S.onFacebook = function(options, callback){
// New implementation
// https://developers.facebook.com/docs/reference/dialogs/feed/
if( callback ) {
if ( !window.FB ) return
var shareObj = {
method: 'feed',
link: window.location.href,
caption: window.location.href
}
return FB.ui(_.extend(shareObj, options), callback)
}
// Old implementation, lets to choose where to post the item.
// https://developers.facebook.com/docs/reference/plugins/share-links/
var link = (options && options.link) || window.location.href
var url = 'https://www.facebook.com/sharer/sharer.php?u='+
encodeURIComponent(link)
var w = 626, h = 436
var l = (screen.width/2)-(w/2), t = (screen.height/2)-(h/2)
window.open(url, 'facebook share', 'width='+w+',height='+h+',left='+l+',top='+t)
}
S.onTwitter = function(options){
options = options || {}
var shortenedUrl = encodeURIComponent(options.url)
$.ajax({
url: 'https://api-ssl.bitly.com/v3/shorten?access_token=a62d9787c155149efccd2613a77c220a51f32aee&longUrl=' + shortenedUrl,
dataType: 'jsonp',
type: 'GET'
}).done(function(bitly){
shortenedUrl = encodeURIComponent(bitly.data.url)
})
.always(function(bitly){
var url = 'https://twitter.com/intent/tweet'
url += '?url=' + shortenedUrl
url += '&text=' + encodeURIComponent(options.text || I18n.t('javascripts.helpers.share.twitter_default_text'))
url += '&hashtags=' + encodeURIComponent(I18n.t('javascripts.helpers.share.twitter_hashtag'))
url += '&via=' + encodeURIComponent(I18n.t('javascripts.helpers.share.twitter_handler'))
window.open(url, 'shareOnTwitter')
})
var w = 550, h = 420
var l = (screen.width/2)-(w/2), t = (screen.height/2)-(h/2)
window.open('', 'shareOnTwitter', 'width='+w+',height='+h+',left='+l+',top='+t)
}
S.getTwitterCount = function(url){
var ajax = $.ajax({
url: 'http://cdn.api.twitter.com/1/urls/count.json?url='+url,
dataType : 'jsonp',
type: 'GET'
})
return ajax.pipe(function(data){
return data.count || 0
})
}
S.getFacebookCount = function(url){
var ajax = $.ajax({
url: 'http://graph.facebook.com/'+url,
type: 'GET'
})
return ajax.pipe(function(data){
return data.shares || 0
})
}
S.getCount = function(url){
url = url || window.location.href
var when = $.when(S.getTwitterCount(url), S.getFacebookCount(url))
return when.pipe(function(){
return _.reduce(arguments, function(sum, n) { return sum += n }) || 0
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment