Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

203 lines (175 sloc) 6.027 kB
<!DOCTYPE html>
<html>
<head>
<style>
html, body {
margin: 0;
font-family: helvetica;
height: 100%;
font-size: 12px;
}
#topRow {
padding: 5px 10px;
background-color: #B2CCFF;
height: 25px;
font-size: 12px;
}
#mainContent {
text-align: center;
border: 1px solid black;
width: -moz-calc(100% - 2px);
height: auto;
min-height: -moz-calc(100% - 75px);
}
#progressBar {
width: 200px;
}
footer {
padding: 10px 10px;
font-size: 12px;
background-color: #B2CCFF;
height: 18px;
}
</style>
</head>
<body>
<div id="topRow">
<button id="startButton" onclick="startFBImport();">
Import Facebook contacts
</button>
</div>
<div id="mainContent">
<p>Note: Due to a limitation in the Facebook API, your friend's
email addresses, phone numbers, and physical addresses cannot be
imported.</p>
<p>You need to allow pop-up windows. If the pop-up window doesn't
come up even after you've allowed them, reload the page and click
the <em>Import</em> button again.</p>
<div id="feedback">
<progress id="progressBar"></progress>
<p id="statusMsg">(Not started yet.)</p>
</div>
</div>
<footer>
<a href="addressbook.html">Back to the address book.</a>
</footer>
<div id="fb-root"></div>
<script type="application/javascript;version=1.8" src="contacts.js"></script>
<script type="application/javascript;version=1.8">
var PERMS = ["friends_about_me", "friends_birthday", "friends_website",
"friends_photos", "email"];
var USER_FIELDS = ["uid", "name", "first_name", "middle_name", "last_name",
"email", "birthday", "website", "profile_url"];
var FRIENDS_QUERY = "SELECT " + USER_FIELDS.join(", ") + " FROM user " +
"WHERE uid IN " +
"(SELECT uid2 FROM friend WHERE uid1=me())";
function startFBImport() {
window.fbAsyncInit = function() {
FB.init({
appId : '320554684628287', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
onFBInit();
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
}
function onFBInit() {
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
onFBLoggedIn();
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: PERMS.join(",")});
}
function onFBLoggedIn() {
document.getElementById("startButton").disabled = false;
var friends_query = FB.Data.query(FRIENDS_QUERY);
FB.Data.waitOn([friends_query], function () {
console.log("Mai fwiends", friends_query.value);
importFBContacts(friends_query.value);
});
}
function importFBContacts(friends) {
var startTime = Date.now();
var total = friends.length;
var counter = 0;
var progressBar = document.getElementById("progressBar");
var statusMsg = document.getElementById("statusMsg");
progressBar.setAttribute("max", friends.length);
function next() {
progressBar.setAttribute("value", counter);
counter += 1;
if (friends.length) {
processFriend(friends.shift());
} else {
console.log("Done");
var duration = ((Date.now() - startTime) / 1000).toFixed(0);
statusMsg.textContent = "Successfully imported " + total + " friends. " +
"Took " + duration + " seconds";
document.getElementById("startButton").disabled = false;
}
}
function errorCallback(error) {
console.error(error);
document.getElementById("startButton").disabled = false;
statusMsg.textContent = "An error occurred";
}
function processFriend(friend) {
console.log("Processing", friend);
statusMsg.textContent =
"Importing " + friend.name + " (" + counter + "/" + total + ")";
// Do we already have a contact by that name?
window.navigator.mozContacts.find(["id", "name", "displayName"],
createOrUpdateRecord.bind(this, friend),
errorCallback,
{filter: {displayName: friend.name}});
}
function createOrUpdateRecord(friend, existingRecords) {
var id = "fb-" + friend.uid;
if (existingRecords.length) {
// We already have a record for this friend, so let's update it.
id = existingRecords[0].id;
// TODO for now we just do the same thing for both.
//return;
}
var record = {id: id,
displayName: friend.name,
name: {familyName: friend.last_name,
givenName: friend.first_name,
middleName: friend.middle_name},
birthday: null,
emails: [],
urls: []};
if (friend.birthday) {
var date = new Date(friend.birthday);
if (date != "Invalid Date") {
record.birthday = date;
}
}
if (friend.email) {
record.emails.push({type: "home", value: friend.email, pref: true});
}
if (friend.website) {
record.urls.push({type: "home", value: friend.website, pref: true});
}
if (friend.profile_url) {
record.urls.push({type: "facebook", value: friend.profile_url, pref: false});
}
window.navigator.mozContacts.update(next, errorCallback, record);
}
next();
}
</script>
Jump to Line
Something went wrong with that request. Please try again.