Blog

Use Greasemonkey to extract your Facebook Phonebook

by anonymous

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:

Facebook Phonebook Exporter

// ==UserScript==
// @name           fb phonebook
// @namespace      setfivefb
// @description    Extract name/phonenumber from fb phonebook. Navigate to http://m.facebook.com/friends.php? and switch the tab to "Phonebook". The script will extract your friends' name/phone and present a CSV at the end.
// @include        http://m.facebook.com/friends.php?*
// ==/UserScript==

// lets make sure we are actually looking at a phonebook page
var nodesSnapshot = document.evaluate('//div[@class="nopad"]/div[@class="nd header"]/small', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
var desc = nodesSnapshot.snapshotItem(2).childNodes;

for(var i=0; i < desc.length; i++){
  // figure out what page we are on
  if(desc[i].nodeName == "B" && desc[i].firstChild.textContent != "Phonebook"){
    return;
 }
}

// get the pagination link
var allLinks = document.getElementsByTagName("a");
var nextLink = "";

nodesSnapshot = document.evaluate('//div[@class="pager"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
var pagerLinks = nodesSnapshot.snapshotItem(0).childNodes;

// get the stuff back from the cache
var arr = GM_getValue("setfivefb");
if(arr == null){
  arr = new Array();
}else{
  arr = new String(arr).split("^");
}

// we are on the first page so reset our name/phone cache
// and present a conformation dialog
if( new String(pagerLinks[0].nodeName) == "#text" ){
  arr = new Array();
  var res = confirm("Do you want to extract your phonebook?");
  if(!res)
   return;
}

for(var i=0; i < pagerLinks.length; i++){

  if(pagerLinks[i].innerHTML == "Next"){
  nextLink = "http://m.facebook.com" + new String(pagerLinks[i].getAttribute("href"));
  break;
}
}

// grab all the names+phones off the page
nodesSnapshot = document.evaluate('//table[@class="results"]//tr', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
var obj;
var ns;
var t;
var hasMatch = false;

for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ )
{
  ns = nodesSnapshot.snapshotItem(i).childNodes[1].childNodes;
  obj = new String();
  hasMatch = false;

  for(var j=0; j  0){

  if(t.indexOf(":") != -1 && ns[j].childNodes[1] && !hasMatch){
  obj += "," + ns[j].childNodes[1].textContent;
  hasMatch = true;
}else{
  obj = t;
}

}
}

if(obj.indexOf(",") == -1)
  obj += ",NaN";

   arr.push(obj);
}

// throw the array into our temporary storage spot and push the page forward
GM_setValue("setfivefb", arr.join("^"));

if(nextLink != "")
  window.location = nextLink;
else{
  // display the results of this mess somewhere
  var str = new Array();
  var tmp;

  for(var i=0; i < arr.length; i++){
  str.push(arr[i]);
}

document.body.innerHTML = str.join("");
}

Tags: , , , ,

21 Responses to “Use Greasemonkey to extract your Facebook Phonebook”

  1. Extract Your Phonebook From Facebook, At Your Own Risk Says:

    [...] 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) [...]

  2. How to Extract Your Phonebook From Facebook, At Your Own Risk | 82123 Says:

    [...] 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. [...]

  3. telperin Says:

    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]

  4. peterc Says:

    same problem here… anyone can help?

  5. anonymous Says:

    It might be due to a Greasemonkey version discrepancy. Check to make sure you are running the latest release – 0.8.20090123.1

  6. peterc Says:

    i downloaded that version from mashable this morning… can i do something wrong?

  7. peterc Says:

    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 :(

  8. Extract Your Phonebook From Facebook, At Your Own Risk | Orange County Adobe Dreamweaver User Group Says:

    [...] 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. [...]

  9. hey_tc Says:

    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 :-(

  10. Extract Your Phonebook From Facebook, At Your Own Risk | KGRAND ONLINE NEWS Says:

    [...] 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. [...]

  11. anonymous Says:

    Hey guys give this a shot http://userscripts.org/scripts/review/43681

  12. Extract Your Phonebook From Facebook, At Your Own Risk | TechWeb Says:

    [...] 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. [...]

  13. mate Says:

    doesnt work… i tried everything…

  14. Me Says:

    Could this script be modified to extract all the messenger addresses (Skype, MSN, …) of the friendlist?

  15. Google Voice Applications & Scripts | Ninja Blogging Says:

    [...] Import Facebook Contacts into your Google Voice Contacts (Script) Facebook’s API + FBConnect is great but it has some severe limitations. Notably, it [...]

  16. Jack Mo Says:

    It doesn’t work

  17. jason Says:

    You should just use sprnch.com it does this already. works on myspace twitter too.

  18. Marcel Chastain Says:

    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

  19. Ashish Datta Says:

    thanks Marcel!

  20. Paul Aerison Says:

    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.

  21. Jacopo Says:

    Paul , you are supposed to go to
    http://m.facebook.com/friends.php?

    and not to
    http://www.facebook.com/friends/?filter=pfp

Leave a Reply