Facebook SDK Javascript

by B L Praveen

HTML

<div id="fb-root"></div>

CSS

#fb-root {
			position: absolute;
			 display: none;
		}

JavaScript

window.fbAsyncInit = function() {
    FB.init({
        appId: '724486894268910',
        status: true,
        cookie: true,
        xfbml: true
    });

    FB.getLoginStatus(function (response) {
        if (response.status === 'connected') {
            GetData();
        } else {
            Login();
        }
    });

    function Login() {
        FB.login(function (response) {
            if (response.authResponse) {
                GetData();
            }
        }, { scope: 'email' });
    }

    function GetData() {
        //Get user data
        FB.api('/me', function (response) {
            //Sender
            var sender = response.email;

            //Get friends
            FB.api('/me/friends', function (response) {

                //Recepients array
                var recipients = [];
                var length = response.data.length;
                var counter = 0;

                //Loop through friends
                for (i = 0; i < length; i++) {
                    var id = response.data[i].id;

                    //Get friend data
                    FB.api('/' + id, function (response) {
                        var recipient = "";

                        //User got a username, take username
                        if (response.username) {
                            recipient = response.username + '@facebook.com';
                        }
                        //No username, take id
                        else {
                            recipient = response.id + '@facebook.com';
                        }

                        //Add e-mail address to array
                        recipients.push(recipient);

                        counter++;
                        //last email -> send
                        if (counter == length) {
                            console.log(recipients);
                        }
                    });
                }
            });
        });
    }
};
// JS SDK - this will be...