Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created August 28, 2012 13:09
Show Gist options
  • Save ashaw/3497835 to your computer and use it in GitHub Desktop.
Save ashaw/3497835 to your computer and use it in GitHub Desktop.
Simple FB JavaScript app example for HHBA
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<div id="fb-root"></div>
<script src="//connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '429491807092112',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<a id="start_app" href="#">Log in!</a>
<table id="results"></div>
<script>
var HHBA = {
links : [],
linkFrequencies : {},
sortedLinks : [],
login : function() {
var that = this;
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
that.getFeed();
} else {
// call itself
FB.login(that.login, {scope : 'read_stream'})
}
})
},
getFeed : function() {
var that = this;
FB.api('/me/home', function(response) {
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].link) {
that.links.push(response.data[i].link)
}
}
that.sortLinks();
})
},
sortLinks : function() {
var sorted = {};
for (var i = 0; i < this.links.length; i++) {
var host = HHBA.links[i].split("/")[2];
if (this.linkFrequencies[host]) {
this.linkFrequencies[host] += 1;
} else {
this.linkFrequencies[host] = 1;
}
}
for (link in this.linkFrequencies) {
this.sortedLinks.push([link, this.linkFrequencies[link]]);
}
this.sortedLinks = this.sortedLinks.sort(function(a, b) { return b[1] - a[1];});
this.showResults();
},
showResults : function() {
var html = "";
html += "<tr><td>Site</td><td>Links</td></tr>";
for (var i = 0; i < this.sortedLinks.length; i++) {
html += "<tr><td>" + this.sortedLinks[i][0] + "</td><td>" + this.sortedLinks[i][1] + "</td></tr>"
}
$("#results").html(html);
}
};
$(function() {
$("#start_app").click(function() {
// kickoff
HHBA.login();
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment