Javascript question for creating an app How to make script that can search and a
ID: 3920023 • Letter: J
Question
Javascript question for creating an app
How to make script that can search and add users to a friends list, can remove friends from a friends list and message specific friends from that list.
Eg. searches for Bob, @Bob found. Click and add @bob to friends list. @Bob is added to your friends list. In friends list, you click @Bob and message @Bob option and @Bob can message you back. Option that can can remove @Bob and if removed, @Bob is removed from friends list.
makes finding cliek or push on them easier if the person it has like > 25 friends Search for your friends Search for a user if or remove friend button appea Rob @RobText appears if message list of users who use this app except for those who hidden ou click it will as If you want to remove or clear conversation Friends list @Rob @Rob1 @Rob2148 @Robbert @Robbie @Robs themselves from goes to a page that stores a friends list search message remove friend ask if you want to remove with a remove button if Remove button is clickéd @Rob is remoted fre if clicke If you touch oreof the names It will give a profile preview and ask if you want to add as a friend message page friend's list You and Rob's conversation appear here If yes then @Rob is added to your list Want to Send hang out? YES NO If clicked it will place that on conversation does the same Friends list page Or press Enter keyExplanation / Answer
<script>
function sortMethod(a, b) {
var x = a.name.toLowerCase();
var y = b.name.toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
window.fbAsyncInit = function() {
FB.init({ appId: 'xxxxxxxxx',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
function updateButton(response) {
var button = document.getElementById('fb-auth');
if (response.authResponse) { // in case if we are logged in
var userInfo = document.getElementById('user-info');
FB.api('/me', function(response) {
userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name;
button.innerHTML = 'Logout';
});
// get friends
FB.api('/me/invitable_friends', function(response) {
var result_holder = document.getElementById('result_friends');
var friend_data = response.data.sort(sortMethod);
var results = '';
for (var i = 0; i < friend_data.length; i++) {
results += '<div><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture">' + friend_data[i].name + '</div>';
}
// and display them at our holder element
result_holder.innerHTML = '<h2>Result list of your friends:</h2>' + results;
});
button.onclick = function() {
FB.logout(function(response) {
window.location.reload();
});
};
} else { // otherwise - dispay login button
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
window.location.reload();
}
}, {scope:'email'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
XML FILE
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<title>Fetch Facebook Friends List By Javascript</title>
</head>
<body>
<h1>Fetch Facebook Friends List By Javascript</h1>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: '123456789012345',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
function updateButton(response) {
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me', function(info) {
login(response, info);
});
} else {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(info) {
login(response, info);
});
} else {
//user cancelled login or did not grant authorization
}
}, {scope:'email,user_birthday,status_update,publish_stream,user_about_me,manage_friendlists'});
}
}
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
function login(response, info){
if (response.authResponse) {
console.log(response);
var token=response.authResponse.accessToken;
var userid=response.authResponse.userID;
var provider='facebook';
var fb_data = '<div align="center"><table border="1" cellpadding="5"><tr><td>User Id</td><td>Name</td><td>Photo</td></tr>';
$.getJSON('https://graph.facebook.com/'+userid+'/friends?fields=name&access_token='+token, function(result){
if(result.data.length > 0){
for(var i=0;i<result.data.length;i++)
{
var userid = result.data[i].id; // user id
var name= result.data[i].name; // facebook name
var image='https://graph.facebook.com/'+ result.data[i].id + '/picture';
fb_data += '<tr><td>'+userid+'</td><td>'+name+'</td><td><img src="'+image+'"></td></tr>';
}
} else {
fb_data += '<tr><td align="center">No record found</td></tr>';
}
fb_data += '</table></div>';
$('#friends').html(fb_data);
console.log(result);
});
}
}
</script>
<div id="friends"></div>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.