forked from philikon/webcontacts
/
fbimport.html
196 lines (170 loc) · 5.7 KB
/
fbimport.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!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>
<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>