Skip to content

Instantly share code, notes, and snippets.

@slouma2000
Created November 1, 2014 08:27
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 slouma2000/8e7c5d9d317a4c524ae9 to your computer and use it in GitHub Desktop.
Save slouma2000/8e7c5d9d317a4c524ae9 to your computer and use it in GitHub Desktop.
getFriends
<script>
if (typeof jQuery === "undefined") { throw new Error("This application requires jQuery"); }
window.fbAsyncInit = function() {
FB.init({
appId: 'xxxxxxx',
status: true, // check login status
xfbml: true,
version: 'v2.1'
});
FB.Canvas.setSize();
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
getFriends();
} else if (response.status === 'not_authorized') {
window.location.href='index.php';
} else {
window.location.href='index.php';
}
}, true);
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function concatObject(obj) {
str = '';
for (var prop in obj) {
str += prop + " value :" + obj[prop] + "\n";
}
return (str);
}
function getFriends() {
var friendsData = [];
FB.api(
"/me/friends/", {
fields: 'name,id,last_name'
},
function(response) {
if (response && !response.error) {
var friends = response.data.sort(sortByName);
// Iterate through the array of friends object.
console.log(friends.length);
for (var i = 0; i < friends.length; i++) {
var item = {
"id": friends[i].id,
"name": friends[i].name,
"desc": '',
"image": 'http://graph.facebook.com/' + friends[i].id + '/picture'
};
//console.log(item);
friendsData.push(item);
var html = '<div class="checkbox">\
<label>\
<input type="checkbox" name="friends[]" id="friends-' + i + '" value="' + friends[i].id + '" >\
' + friends[i].name + ' \
</label>\
</div>';
$('#friendsHolder').append(html);
}
return friendsData;
}
}
);
}
function sortByName(a, b) {
var fn = function(x) {
return x.name.toLowerCase();
};
var ln = function(x) {
return x.name.toLowerCase();
};
if (ln(a) == ln(b)) {
if (fn(a) == fn(b)) {
return 0;
}
return (fn(a) < fn(b)) ? -1 : 1;
}
return (ln(a) < ln(b)) ? -1 : 1;
}
$(document).ready(function() {
$('#btnInvite').click(function(){
var checkBoxes = $('input[name="friends[]"]');
var list = [];
alert($('input[name="friends[]"]').length);
$('input[name="friends[]"]').each(function(index){
console.log('ID : ' + $(this).val());
list.push($(this).val());
if ($(this).attr('checked')){
list.push($(this).val());
}
})
console.log(list);
//alert($('friends'));
FB.ui({method: 'apprequests',
message: 'Invitation au jeu ADP',
to: list[0]
}, function(response){
console.log(response);
});
})
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment