-
Notifications
You must be signed in to change notification settings - Fork 28
/
background.html
178 lines (157 loc) · 5.25 KB
/
background.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
<!DOCTYPE html>
<html>
<script type="text/javascript" src="/js/settings.js"></script>
<!-- Web SQL Database to store friends.. -->
<script type="text/javascript" src="/js/database.js"></script>
<!-- For OAuth google authentication. -->
<script type="text/javascript" src="/js/lib/chrome_ex_oauthsimple.js"></script>
<script type="text/javascript" src="/js/lib/chrome_ex_oauth.js"></script>
<!-- For importing to contacts. -->
<script type="text/javascript" src="/js/gmail_exporter.js"></script>
<script type="text/javascript" src="/js/csv_exporter.js"></script>
<script type="text/javascript" src="/js/grabber.js"></script>
<!-- Add additional export/import methods here. -->
<script type="text/javascript" src="/js/lib/jquery-1.5.2.min.js"></script>
<script type="text/javascript">
var db = new FriendDB();
var grabber = new ProfileGrabber();
var facebook_id = -1;
var worker_id = -1;
var error_occured = false;
var friendsMap = {};
var cachedMap = {};
// Start it globally.
db.open();
// Synchronously retrieve the current extension version.
var version = 'NaN';
var currVersion = chrome.app.getDetails().version;
var prevVersion = settings.version;
// Check if the extension has been just updated or installed.
if (currVersion != prevVersion) {
if (typeof prevVersion == 'undefined') {
// onInstall: Create the database tables.
db.createTable();
}
else {
// onUpdate: Do nothing now.
}
settings.version = currVersion;
}
/**
* Open a singleton page, which means, if a page already exists, it just selects it.
* @param url The page which it will navigate to.
*/
function openSingletonPage(url) {
var views = chrome.extension.getViews();
for (var v in views) {
var view = views[v];
if (view.location.href.indexOf(url) == 0) {
view.focus();
return;
}
}
chrome.tabs.create({url: url, selected: true}, function(tab) {
worker_id = tab.id;
});
}
function putFriendCache(friend) {
cachedMap[friend.id] = friend;
}
function clearCache() {
cachedMap = {};
}
/**
* Callback when friend finished exporting.
*/
function onFriendFinishExport(status) {
chrome.tabs.sendRequest(worker_id, {
finishedProcessingFriend: status.finishedProcessingFriend,
friend: status.friend,
success: status.success,
message: status.message
});
}
/**
* Callback when friend finished relayed.
*/
function onFriendRelay(friend) {
var payload = { gotInfoForFriend: friend };
chrome.tabs.sendRequest(worker_id, payload, function (response) {
if (!response.OK) {
error_occured = true;
chrome.tabs.sendRequest(worker_id, { facebookError: 1 });
}
else {
db.persistFriend(friend, function (response) {
//console.debug('persisted!');
});
}
});
}
function startExportFriendData() {
var i = 0;
var errorHappened = false;
// Iterate through each friend by key[value].
$.each(friendsMap, function(key, value) {
// If the item is cached, no need to refetch it, just relay the information.
if (cachedMap[key]) {
console.log('Cache received, no need to grab it again.');
onFriendRelay(cachedMap[key]);
}
else {
// Figure out the proper info page, this makes sure to support both
// profile name pages with (unique names) or just profile id pages
// (numbers). As well, append the ID so we can uniquely identify each page
// request with their ID being fetched..
var href = 'http://m.facebook.com' + value.path;
href.match('\\?') ? href += '&' : href += '?';
href += 'v=info';
// Delay load each friend.
var delay = i * 11000 + Math.random() * 1000;
setTimeout(function() {
var friend = { id: key, name: value.text, photo: value.photo };
// Verify no error happened. This could happen if your not logged into
// the mobile version of Facebook.
// Force refreshing the page is the safest way out of corruption.
if (errorHappened) {
chrome.tabs.sendRequest(worker_id, { facebookError: 1 });
window.location.reload();
}
chrome.tabs.sendRequest(worker_id, {
friendExtractionStarted: key,
name: friend.name
});
grabber.extractInfo(friend, href, function(f) {
if (f.error) {
errorHappened = true;
}
else {
onFriendRelay(f);
}
});
}, delay);
i++;
}
});
}
// Extension request listener.
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.switchToWorkerTab) {
facebook_id = sender.tab.id;
openSingletonPage(chrome.extension.getURL('controller.html'));
}
else if (request.doGmailExport) {
var gmail = new GoogleExport(request.requestedFriends)
gmail.process(onFriendFinishExport);
}
else if (request.doCSVExport) {
var csv = new CSVExporter(request.requestedFriends);
csv.process(onFriendFinishExport);
chrome.tabs.sendRequest(worker_id, { csvExportFinished: csv.getDump() });
}
else if (request.friendsListReceived) {
friendsMap = request.friendsListReceived;
}
});
</script>
</html>