Pownce Friends of friends

By Aaron Bassett Last update Oct 30, 2007 — Installed 118 times. Daily Installs: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0
// ==UserScript==
// @name		Pownce Friends of friends
// @author		Aaron Bassett 
// @description		Displays some random friends of friends
// @include		*pownce.com/*
// ==/UserScript==

POWNCE_FOF={
	
	yourFriends:[],

	init:function() {
		var onProfile = false;
		var uls = document.getElementsByTagName("ul");
		for(var k=0; k < uls.length; k++) {
			if(uls[k].className == "fans") onProfile = true;
		}

		if(!onProfile) return;

		var pUrl = window.location.toString();
		var parts = pUrl.split("pownce.com/");
		var parts = parts[1].split("/");

		var yourUsername = parts[0];

		GM_xmlhttpRequest({
			method: 'GET',
			url: 'http://api.pownce.com/1.0/users/'+yourUsername+'/friends.json?limit=100',
			onload: function(responseDetails) {
				if(responseDetails.status == 200) {
					var yourFriends = eval('(' + responseDetails.responseText + ')');
					yourFriends = POWNCE_FOF.shuffle(yourFriends.users);
					for(var i=0; i < yourFriends.length; i++) {
						POWNCE_FOF.yourFriends[i] = yourFriends[i].user[0].username
					}
					POWNCE_FOF.yourFriends[POWNCE_FOF.yourFriends.length] = yourUsername;
					for(var j=0; j < 5; j++) {
						POWNCE_FOF.getFoF(yourFriends[j].user[0].username);
					}
				}
			}
		});
	},

	getFoF:function(user) {
		GM_xmlhttpRequest({
			method: 'GET',
			url: 'http://api.pownce.com/1.0/users/'+user+'/friends.json?limit=10',
			onload: function(responseDetails) {
				if(responseDetails.status == 200) {
					var fof = eval('(' + responseDetails.responseText + ')');
					fof = POWNCE_FOF.shuffle(fof.users);
					if(!document.getElementById('POWNCE_FOF')) {
						var uls = document.getElementsByTagName("ul");
						for(var i=0; i < uls.length; i++) {
							if(uls[i].className == "fans") {
								var h3 = document.createElement("h3");
								var txt = document.createTextNode("Friends of Friends");
								h3.appendChild(txt);
								uls[i].parentNode.insertBefore(h3,uls[i].nextSibling);
								
								var newUl = document.createElement("ul");
								newUl.id = "POWNCE_FOF";
								newUl.className = "friends";
								h3.parentNode.insertBefore(newUl,h3.nextSibling);
							}
						}
					}
					var li, abbr, a, img;
					var x = (fof.length < 10) ? fof.length : 10;
					for(var j=0; j < x; j++) {
						if(!POWNCE_FOF.in_array(fof[j].user[0].username, POWNCE_FOF.yourFriends)) {
							li = document.createElement("li");
							li.className = "vcard";
							abbr = document.createElement("abbr");
							abbr.className = "fn";
							if(fof[j].user[10] && fof[j].user[10].age)    abbr.title = fof[j].user[10].age + "yr old ";
							if(fof[j].user[9] && fof[j].user[9].gender)   abbr.title += fof[j].user[9].gender + " ";
							if(fof[j].user[7] && fof[j].user[7].location) abbr.title +=  "from " + fof[j].user[7].location;
							a  = document.createElement("a");
							a.href = fof[j].user[1].permalink;
							a.className = "url";
							a.rel = "acquaintance";
							img = document.createElement("img");
							img.src = fof[j].user[5].profile_photo_urls[1].small_photo_url;
							img.className = "user logo";
							a.appendChild(img);
							txt = document.createTextNode(fof[j].user[3].short_name);
							a.appendChild(txt);
							abbr.appendChild(a);
							li.appendChild(abbr);
							document.getElementById('POWNCE_FOF').appendChild(li);
							POWNCE_FOF.yourFriends[POWNCE_FOF.yourFriends.length] = fof[j].user[0].username;
						}
					}
				}
			}
		});
	},
	
	shuffle:function(o){ //v1.0
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
	},
	
	in_array:function(needle, haystack) {
		var i = haystack.length;
		if (i > 0) {
			do {
				if (haystack[i] === needle) {
					return true;
				}
			} while (i--);
		}
		return false;
	}
}

POWNCE_FOF.init();