Use Greasemonkey to extract your Facebook Phonebook
by anonymous7/19/2010 UPDATE: There is a BRAND NEW version of the script available on Userscripts here.
10/12/2009 UPDATE: Added fixes from Marcel Chastain
UPDATE: It looks like the version that got uploaded was missing a * in the trigger URL! That might be the issue everyone is having.
UPDATE: Video of the process: fbimport
Facebook’s API + FBConnect is great but it has some severe limitations. Notably, it doesn’t expose all the functionality available on the Facebook site. Tonight in particular, I wanted to be able to copy a dump of my friends’ names and phone numbers off the site to load into a fresh cell phone. Unfortunately, looking at the API this isn’t possible.
Never fear – Greasemonkey provides enough of a hook into Firefox that it would be possible to write a UserScript to accomplish this.
Continuing beyond this point is probably against the Facebook TOS and will probably severely void your warranty.
You have been warned.
The following describes how to use this userscript to extract your Facebook “Phonebook”. It produces of a CSV of your friends’ names and phone numbers. Fair warning – this is a rough prototype and does almost no error handling. Also, since the “Phone” field is a free text field I can’t promise people will have formatted their numbers in any sane fashion. But either way it’s a good start to revering lost numbers.
So here is what you need to do to use the script:
1. Install Greasemonkey – https://addons.mozilla.org/en-US/firefox/addon/748
2. Follow these instructions to install the script – http://userscripts.org/about/installing
Edit: The script is also on Userscripts at http://userscripts.org/scripts/show/43681
3. Navigate over to http://m.facebook.com/friends.php? (You’ll have to login)
4. Answer yes to the prompt and sit back – the script will move through your phonebook and eventually dump you a CSV of the results.
5. Copy/Paste the CSV wherever you want.
6. Un-install the Greasemonkey script.
So that’s it, one less walled garden to worry about. And hopefully one less “I lost my cellphone!” event/group on facebook!
The script:
// ==UserScript==
// @name fb phonebook
// @namespace setfivefb
// @description Scrapes your Facebook friend's phone #s and optionally email addresses. Login to http://m.facebook.com/ and navigate to http://m.facebook.com/friends.php?a&rf6e4c397&refid=5
// @include http://m.facebook.com/*
// ==/UserScript==
var s = document.createElement('script');
s.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
s.type = 'text/javascript';
document.getElementsByTagName('body')[0].appendChild(s);
s.addEventListener('load', function(){
jQuery = unsafeWindow['jQuery'];
jQuery.noConflict();
var queryString = unsafeWindow["window"].location.search;
if( queryString.indexOf("sfScraper") > 0 ){
var localStorage = unsafeWindow['localStorage'];
var scrapeWhat = JSON.parse( localStorage.getItem("sfScrapeWhat") );
if( scrapeWhat.emailsDone ){
extractEmailandPhones();
return false;
}
if( scrapeWhat.phone ){
scrapePhoneContacts();
return false;
}
if( scrapeWhat.email ){
scrapeEmailContacts();
return false;
}
return false;
}
if( jQuery("b:contains('Everyone')").length ){
var localStorage = unsafeWindow['localStorage'];
localStorage.removeItem("sfStoredContacts");
localStorage.removeItem("sfScrapeWhat");
localStorage.removeItem("sfContactLinks");
jQuery("#title").after("
"
+ "What do you want to save?
"
+ "
");
jQuery("#sf-scrapeBtn").click( function(){
var scrapeWhat = { phone: false, email: false, emailsDone: false };
jQuery("#sf-scraperFrm input:checked").each( function(){
if( jQuery(this).val() == "p" ){
scrapeWhat.phone = true;
}else if( jQuery(this).val() == "e" ){
scrapeWhat.email = true;
}
});
localStorage.setItem("sfScrapeWhat", JSON.stringify(scrapeWhat));
if( scrapeWhat.phone ){ scrapePhoneContacts(); }
if( scrapeWhat.email ){ scrapeEmailContacts(); }
return false;
});
}else{
jQuery("#marquee_tabs").after("
You need to switch to the Friends -> 'Everyone' tab for the scrapper to become active.
");
}
}, false);
function extractEmailandPhones(){
var contacts = localStorage.getItem("sfStoredContacts");
var contactLinks = JSON.parse( localStorage.getItem("sfContactLinks") );
if( contacts == null ){
contacts = [ ];
}else{
contacts = JSON.parse( contacts );
}
var name = jQuery(".section_title:first").text();
var email = jQuery("td:contains('Email:')").next("td:first").text();
var phone = jQuery("td:contains('Mobile Number:')").next("td:first").text();
contacts.push( {"n": name, "p": phone, "e": email} );
localStorage.setItem("sfStoredContacts", JSON.stringify(contacts) );
jQuery(".section_title:first").after("
"
+ "Links left: " + contactLinks.length + "
");
if( contactLinks.length ){
unsafeWindow["window"].location = contactLinks.pop();
localStorage.setItem("sfContactLinks", JSON.stringify(contactLinks) );
return false;
}
var textArea = "Name,Phone,Email \n";
jQuery.each(contacts, function(i, val){ textArea += val.n + "," + val.p + "," + val.e + "\n"; });
jQuery("body").append("");
jQuery("#sf-fbContacts").val( textArea );
}
function scrapeEmailContacts(){
var localStorage = unsafeWindow['localStorage'];
var contactLinks = localStorage.getItem("sfContactLinks");
if( contactLinks == null ){
contactLinks = [ ];
}else{
contactLinks = JSON.parse( contacts );
}
jQuery("tr[valign='top']").each( function(){
var url = "http://m.facebook.com" + jQuery(this).find("a:first").attr("href") + "&v=info&sfScraper=true";
contactLinks.push( url );
});
jQuery("#title").after("
"
+ "Saved: " + contactLinks.length + "
");
localStorage.setItem("sfContactLinks", JSON.stringify(contactLinks) );
if( jQuery("a:contains('Next')").length ){
var nextLink = jQuery("a:contains('Next')").attr("href") + "&sfScraper=true";
unsafeWindow["window"].location = "http://m.facebook.com" + nextLink;
}else{
var scrapeWhat = JSON.parse( localStorage.getItem("sfScrapeWhat") );
scrapeWhat.emailsDone = true;
localStorage.setItem("sfScrapeWhat", JSON.stringify(scrapeWhat) );
unsafeWindow["window"].location = contactLinks.pop();
localStorage.setItem("sfContactLinks", JSON.stringify(contactLinks) );
}
return false;
}
function scrapePhoneContacts(){
var localStorage = unsafeWindow['localStorage'];
var contacts = localStorage.getItem("sfStoredContacts");
if( contacts == null ){
contacts = [ ];
}else{
contacts = JSON.parse( contacts );
}
jQuery("tr[valign='top']").each( function(){
var name = jQuery(this).find("a:first").text();
if( jQuery(this).find("a:contains('Call')").length ){
var number = jQuery(this).find("a:contains('Call')")
.attr("href").replace("tel:", "");
contacts.push( {"n": name, "p": number, "e": ""} );
}
});
jQuery("#title").after("
"
+ "Saved: " + contacts.length + "
");
localStorage.setItem("sfStoredContacts", JSON.stringify(contacts) );
if( jQuery("a:contains('Next')").length ){
var nextLink = jQuery("a:contains('Next')").attr("href") + "&sfScraper=true";
unsafeWindow["window"].location = "http://m.facebook.com" + nextLink;
}else{
var textArea = "Name,Phone \n";
jQuery.each(contacts, function(i, val){ textArea += val.n + "," + val.p + "\n"; });
jQuery("body").append("");
jQuery("#sf-fbContacts").val( textArea );
}
return false;
}
Tags: facebook, fbconnect, greasemonkey, javascript, walled garden

March 6th, 2009 at 5:20 am
[...] Facebook’s API doesn’t let you do this, though, but you can do this with the help of a Greasemonkey script. We don’t recommend it, especially since it probably violates Facebook’s Terms of Service. If you’re planning to try it out anyway, you’ll need to install Greasemonkey first. The script is a bit of a rough hack, and the full instructions on how to use it are over here) [...]
March 6th, 2009 at 6:20 am
[...] Facebook’s API doesn’t let you do this, though, but you can do this with the help of a Greasemonkey script. We don’t recommend it, especially since it probably violates Facebook’s Terms of Service. If you’re planning to try it out anyway, you’ll need to install Greasemonkey first. The script is a bit of a rough hack, and the full instructions on how to use it are over here. [...]
March 6th, 2009 at 6:51 am
it says something like this when i try to install script in firefox:
[Exception... "Component returned failure code: 0x80520003 (NS_ERROR_FILE_EXECUTION_FAILED) [nsIProcess.run]” nsresult: “0×80520003 (NS_ERROR_FILE_EXECUTION_FAILED)” location: “JS frame :: chrome://greasemonkey/content/utils.js :: launchApplicationWithDoc :: line 188″ data: no]
March 6th, 2009 at 9:34 am
same problem here… anyone can help?
March 6th, 2009 at 11:14 am
It might be due to a Greasemonkey version discrepancy. Check to make sure you are running the latest release – 0.8.20090123.1
March 6th, 2009 at 11:56 am
i downloaded that version from mashable this morning… can i do something wrong?
March 6th, 2009 at 12:13 pm
even when i try other greasemonkey scripts, it gives me an option to install automatically… this one doesnt :( i really need to make this work :(
March 6th, 2009 at 1:41 pm
[...] Facebook’s API doesn’t let you do this, though, but you can do this with the help of a Greasemonkey script. We don’t recommend it, especially since it probably violates Facebook’s Terms of Service. If you’re planning to try it out anyway, you’ll need to install Greasemonkey first. The script is a bit of a rough hack, and the full instructions on how to use it are over here. [...]
March 6th, 2009 at 4:39 pm
I don’t get how this is supposed to work. It installed ok but how do you run it? Just navigating to the phonebook page doesn’t seem to be triggering any action :-(
March 6th, 2009 at 7:14 pm
[...] Facebook’s API doesn’t let you do this, though, but you can do this with the help of a Greasemonkey script. We don’t recommend it, especially since it probably violates Facebook’s Terms of Service. If you’re planning to try it out anyway, you’ll need to install Greasemonkey first. The script is a bit of a rough hack, and the full instructions on how to use it are over here. [...]
March 6th, 2009 at 8:33 pm
Hey guys give this a shot http://userscripts.org/scripts/review/43681
March 7th, 2009 at 11:05 am
[...] Facebook’s API doesn’t let you do this, though, but you can do this with the help of a Greasemonkey script. We don’t recommend it, especially since it probably violates Facebook’s Terms of Service. If you’re planning to try it out anyway, you’ll need to install Greasemonkey first. The script is a bit of a rough hack, and the full instructions on how to use it are over here. [...]
March 8th, 2009 at 5:44 pm
doesnt work… i tried everything…
June 17th, 2009 at 4:10 pm
Could this script be modified to extract all the messenger addresses (Skype, MSN, …) of the friendlist?
August 19th, 2009 at 7:39 am
[...] Import Facebook Contacts into your Google Voice Contacts (Script) Facebook’s API + FBConnect is great but it has some severe limitations. Notably, it [...]
September 25th, 2009 at 3:41 pm
It doesn’t work
October 4th, 2009 at 9:42 am
You should just use sprnch.com it does this already. works on myspace twitter too.
October 11th, 2009 at 4:54 pm
2 changes needed to get this working.
On line 9, change
div[@class="lnav header"]
to
div[@class="nd header"]
On line 10, change
var desc = nodesSnapshot.snapshotItem(0).childNodes;
to
var desc = nodesSnapshot.snapshotItem(2).childNodes;
That’s it!
–mC
October 12th, 2009 at 8:55 am
thanks Marcel!
October 15th, 2009 at 11:52 am
I have installed the script and made the changes as noted by Marcel, but when I load http://www.facebook.com/friends/?filter=pfp nothing happens. GM says it’s enabled and there is a check next to the script. I verified that the above URL is in GM under manage scripts. Any ideas what I missed? This is my first GM script so keep that in mind.
November 24th, 2009 at 8:34 pm
Paul , you are supposed to go to
http://m.facebook.com/friends.php?
and not to
http://www.facebook.com/friends/?filter=pfp
March 7th, 2010 at 6:49 am
I used this script to create this bookmarklet tool that encapsulate all the above work and finally downloads the contacts directly as .CSV file. It does not need a firefox plugin or downloading a script. Check it out http://jamalpro.freehostia.com/fbphone.htm
July 18th, 2010 at 9:51 am
Doesn’t work. Installed everything, but step 4 (‘Answer yes to the prompt and sit back’) doesn’t happen. Actually, it does nothing :((
July 19th, 2010 at 2:06 pm
[...] The original instructions for how to install the script are available here. [...]
July 23rd, 2010 at 10:35 pm
Nothing at all is happening in the new version. It just sits there on page 2. I’ve read the instructions…
August 1st, 2010 at 5:43 pm
Same problem over here, stops on page two
August 24th, 2010 at 10:49 pm
Now after which I’ll stumble across a post like this and I’ll recall that there quite are nevertheless useful pages on the web. ^_^. Thanks.
September 6th, 2010 at 5:59 am
hey,
the script fails when you do phone and email
phone works fine, hoerver the email does!
really hope this gets sorted, as its a great script
November 5th, 2010 at 5:37 am
Same thing here.. it works for the phones only.. when trying for the emails, it stop at page 2…
November 21st, 2010 at 7:49 pm
Even when i try to install the script i have failure. Pleeeease help me
December 23rd, 2010 at 2:48 pm
Not working anymore, I think. There isn’t any tab of “Phonebook” in the mobile version of Facebook with the last greasemonkey script installed from here http://userscripts.org/scripts/show/43681. What can we do :( thanks
June 9th, 2011 at 2:38 am
Your website is great. Content is very beautiful. Thanks
November 7th, 2011 at 6:51 pm
jogos…
[...]{5} Setfive – Talking to the World » Blog Archive » Use Greasemonkey to extract your Facebook Phonebook[...]…
January 23rd, 2012 at 5:45 pm
This isn’t working :( Would love to have a script to easily add friends from Facebook to LinkedIn.