Wednesday, August 12, 2009

How to turn a Facebook Friends List into a CSV

So here's a little something for some people who like to use things that are not nothing but rather are something, a how-to if you will, to translate a Facebook friends list into a CSV:

Facebook, it is designed to let you keep in contact with people.
Yet as most know it is not the end all of human interaction. Despite this
fact, the makers of Facebook have not made it easy to take the contact
information in Facebook and get it out.

However, where there is a will there is a way. Actually there are
many ways. Indeed there are many tools you could use for this purpose,
from the Facebook API to a Greasemonkey script. But when it comes to ad
hoc manipulation of random websites with a minimum of fuss, and perhaps
a little leeway timewise, my tool of choice is Firebug.

And thus here is a function one can put into the Firebug console and
then execute for gathering all the Facebook contact info from a My
Friends page into a CSV file (or actually a new window which can be
saved as a text file and will then become a CSV), which can be easily
imported to your email client or even phone.

NOTE: it's been a little while since I've worked on this code,
and there might be some flaws in it. No guarentee.

function fbcontactexport() {
items = document.getElementsByClassName('UIObjectListing');
datastr = "name, group1, group2, cellphone, landphone";
for(i=0; i < items.length; i++) {
datastr += "\n";
names = items[i].getElementsByClassName('UIObjectListing_Title');
if(names.length > 0) {
datastr += names[0].childNodes[0].nodeValue;
datastr += ",";
subtitle = items[i].getElementsByClassName('UIObjectListing_Subtitle')[0].childNodes;
if(subtitle.length>0) {
datastr += subtitle[0].nodeValue;
datastr += ",";
else {
datastr += ',,';
subtext = items[i].getElementsByClassName('UIObjectListing_Subtext')[0].childNodes;
if(subtext.length > 0) {
datastr += subtext[0].nodeValue;
datastr += ",";
else {
datastr += ',,';
cellNum = items[i].getElementsByClassName('FriendsPage_PhonebookCell');
if(cellNum.length > 0) {
datastr += cellNum[0].childNodes[1].nodeValue;
datastr += ",";
else {
datastr += ',,';
landNum = items[i].getElementsByClassName('FriendsPage_PhonebookLand');
if(landNum.length>0) {
datastr += landNum[0].childNodes[1].nodeValue;
else {
datastr += ',';
nwind =;
return datastr;

One limitation (or at least one big and notable one) though, this only gathers the contacts from a single page in your My Friends
list which, if you have several pages of Friends, means you'll have to
run this script several times. Could it be otherwise? Yes, yes it could.
There are more sophisticated tools as I mentioned (or rather more
appropriate tools as Firebug is amazingly sophisticated for its
primary purpose, that is debugging Javascript). It is likely possible
that a more powerful Firebug script could be written that'll open up
the new pages of friends lists and grab those pages, etc. But this is
a not bad function, and building it just about exceeded my interest in
the subject, so I'll leave it as is for now, or until curiosity strikes
me again like a hurricane.

So take it to your head, take it to your heart and remember Rand

Goodnight Folks!

And God Bless.

No comments: