Skip to content

Instantly share code, notes, and snippets.

@mfyz
Created March 30, 2014 03:22
Show Gist options
  • Save mfyz/9866970 to your computer and use it in GitHub Desktop.
Save mfyz/9866970 to your computer and use it in GitHub Desktop.
Get social counts via javacript
<!doctype html>
<html>
<head>
<title>Page</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
// Tiny object for getting counts
var socialGetter = (function() {
/* just a utility to do the script injection */
function injectScript(url) {
var script = document.createElement('script');
script.async = true;
script.src = url;
document.body.appendChild(script);
}
return {
getFacebookCount: function(url, callbackName) {
injectScript('https://graph.facebook.com/?id=' + url + '&callback=' + callbackName);
},
getTwitterCount: function(url, callbackName) {
injectScript('http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=' + callbackName);
}
};
})();
// Callbacks to do something with the result
function twitterCallback(result) {
result.count && console.log('The count is: ', result.count);
}
function facebookCallback(result) {
result.shares && console.log('The count is: ', result.shares);
}
// Usage
socialGetter.getFacebookCount('http://davidwalsh.name/twitter-facebook-jsonp', 'facebookCallback');
socialGetter.getTwitterCount('http://davidwalsh.name/twitter-facebook-jsonp', 'twitterCallback');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment